{* template for handling share address functionality*}
<tr>
<td>
- {$form.address.$blockId.use_shared_address.html}{$form.address.$blockId.use_shared_address.label}{help id="id-sharedAddress" file="CRM/Contact/Form/Contact.hlp"}<br />
+ {$form.address.$blockId.use_shared_address.html}{$form.address.$blockId.use_shared_address.label} {help id="id-sharedAddress" file="CRM/Contact/Form/Contact.hlp"}<br />
{if !empty($sharedAddresses.$blockId.shared_address_display)}
<span class="shared-address-display" id="shared-address-display-name-{$blockId}">
{$sharedAddresses.$blockId.shared_address_display.name}
<div id="shared-address-{$blockId}" class="form-layout-compressed hiddenElement">
{$form.address.$blockId.master_contact_id.label}
{$form.address.$blockId.master_contact_id.html}
+ <div class="shared-address-list"></div>
</div>
</td>
</tr>
{literal}
<script type="text/javascript">
CRM.$(function($) {
+ var blockNo = {/literal}{$blockId}{literal};
- function showHideSharedAddress( blockNo, showSelect ) {
+ function showHideSharedAddress() {
// based on checkbox, show or hide
- if ( $( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).prop('checked') ) {
- if ( showSelect && $( '#shared-address-display-' + blockNo ).length == 0 ) {
- $( '#shared-address-' + blockNo ).show( );
- }
- $( 'table#address_table_' + blockNo ).hide( );
- $( '#shared-address-display-' + blockNo ).show( );
- $( '#shared-address-display-name-' + blockNo ).show( );
- $( '#shared-address-display-cancel-' + blockNo ).hide( );
- $( '.crm-address-custom-set-block-' + blockNo).hide( );
- } else {
- $( '#shared-address-' + blockNo ).hide( );
- $( 'table#address_table_' + blockNo ).show( );
- $( '#shared-address-display-' + blockNo ).hide( );
- $( '#shared-address-display-name-' + blockNo ).hide( );
- $( '#shared-address-display-cancel-' + blockNo ).hide( );
- $( '.crm-address-custom-set-block-' + blockNo).show( );
- }
+ var share = $(this).prop('checked');
+ $('#shared-address-' + blockNo + ', #shared-address-display-' + blockNo + ', #shared-address-display-name-' + blockNo).toggle(!!share);
+ $('table#address_table_' + blockNo +', #shared-address-display-cancel-' + blockNo + ', .crm-address-custom-set-block-' + blockNo).toggle(!share);
}
- var blockNo = {/literal}{$blockId}{literal};
- // call this when form loads
- showHideSharedAddress( blockNo, true );
+ // "Use another contact's address" checkbox
+ $('#address\\[' + blockNo + '\\]\\[use_shared_address\\]').each(showHideSharedAddress).click(showHideSharedAddress);
- // handle check / uncheck of checkbox
- $( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).click( function( ) {
- showHideSharedAddress( blockNo, true );
- });
-
- // start of code to add onchange event for hidden element
- var contactHiddenElement = 'input[name="address[' + blockNo +'][master_contact_id]"]';
+ // When shared contact is selected/unselected
+ $('input[name="address[' + blockNo +'][master_contact_id]"]').change(function() {
+ var $el = $(this),
+ sharedContactId = $el.val(),
+ $contentArea = $('#shared-address-' + blockNo + ' .shared-address-list').html('').off(),
+ $masterElement = $('input[name="address[' + blockNo + '][master_id]"]').val('');
- // observe changes
- $( contactHiddenElement ).change(function( ) {
- var sharedContactId = $( this ).val( );
- if ( !sharedContactId || isNaN( sharedContactId ) ) {
+ if (!sharedContactId || isNaN(sharedContactId)) {
return;
}
- var addressHTML = '';
- var postUrl = {/literal}"{crmURL p='civicrm/ajax/inline' h=0}"{literal};
-
- $.post( postUrl, {
+ $.post(CRM.url('civicrm/ajax/inline'), {
'contact_id': sharedContactId,
'type': 'method',
- 'async': false,
'class_name': 'CRM_Contact_Page_AJAX',
'fn_name': 'getAddressDisplay'
},
- function( response ) {
- if ( response ) {
- var selected = 'checked';
- var addressExists = false;
-
- $.each( response, function( i, val ) {
- if ( i > 1 ) {
+ function(response) {
+ // Avoid race conditions - check that value hasn't been changed by the user while we were waiting for response
+ if (response && $el.val() === sharedContactId) {
+ var selected = ' checked="checked"',
+ addressHTML = '';
+
+ $.each(response, function(i, val) {
+ if (addressHTML) {
selected = '';
} else {
- $( 'input[name="address[' + blockNo + '][master_id]"]' ).val( val.id );
+ $('input[name="address[' + blockNo + '][master_id]"]').val(val.id);
}
-
- addressHTML = addressHTML + '<input type="radio" name="selected_shared_address-'+ blockNo +'" value=' + val.id + ' ' + selected +'>' + val.display_text + '<br/>';
-
- addressExists = true;
+ var name = 'selected_shared_address-'+ blockNo,
+ id = name + '-' + val.id;
+ addressHTML += '<input type="radio" name="' + name + '" id="' + id + '" value="' + val.id + '"' + selected +'><label for="' + id + '">' + val.display_text + '</label><br/>';
});
- if ( addressExists ) {
- $( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( );
- $( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + addressHTML + '</td></tr>');
- $( 'input[name^=selected_shared_address-]' ).click( function( ) {
-
- // get the block id
- var elemId = $(this).attr( 'name' ).split('-');
- $( 'input[name="address[' + elemId[1] + '][master_id]"]' ).val( $(this).val( ) );
- });
- } else {
- var helpText = {/literal}"{ts escape='js'}Selected contact does not have an address. Please edit that contact to add an address, or select a different contact.{/ts}"{literal};
- $( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( );
- $( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + helpText + '</td></tr>');
+ if (!addressHTML) {
+ addressHTML = {/literal}"{ts escape='js'}Selected contact does not have an address. Please edit that contact to add an address, or select a different contact.{/ts}"{literal};
}
+
+ $contentArea.html(addressHTML).on('click', 'input', function() {
+ $masterElement.val($(this).val());
+ });
}
},'json');
});