Merge pull request #16697 from eileenmcnaughton/deprecate
[civicrm-core.git] / templates / CRM / common / additionalBlocks.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
4 | |
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
8 +--------------------------------------------------------------------+
9 *}
10 {literal}
11 <script type="text/javascript" >
12 CRM.$(function($) {
13 {/literal}
14 {if $generateAjaxRequest}
15 {foreach from=$ajaxRequestBlocks key="blockName" item="instances"}
16 {foreach from=$instances key="instance" item="active"}
17 buildAdditionalBlocks( '{$blockName}', '{$className}' );
18 {/foreach}
19 {/foreach}
20 {/if}
21
22 {if $loadShowHideAddressFields}
23 {foreach from=$showHideAddressFields key="blockId" item="fieldName"}
24 processAddressFields( '{$fieldName}', '{$blockId}', 0 );
25 {/foreach}
26 {/if}
27 {literal}
28 });
29
30 function buildAdditionalBlocks( blockName, className ) {
31 var element = blockName + '_Block_';
32
33 //get blockcount of last element of relevant blockName
34 var previousInstance = cj( '[id^="'+ element +'"]:last' ).attr('id').slice( element.length );
35 var currentInstance = parseInt( previousInstance ) + 1;
36
37 //show primary option if block count = 2
38 if ( currentInstance == 2) {
39 cj("#" + blockName + '-Primary').show( );
40 cj("#" + blockName + '-Primary-html').show( );
41 }
42
43 var dataUrl = {/literal}"{crmURL h=0 q='snippet=4'}"{literal} + '&block=' + blockName + '&count=' + currentInstance;
44
45 if ( className == 'CRM_Event_Form_ManageEvent_Location' ) {
46 dataUrl = ( currentInstance <= 2 ) ? dataUrl + '&subPage=Location' : '';
47 }
48
49 {/literal}
50 {if $qfKey}
51 dataUrl += "&qfKey={$qfKey}";
52 {/if}
53 {literal}
54
55 if ( !dataUrl ) {
56 return;
57 }
58
59 var fname = '#' + blockName + '_Block_'+ previousInstance;
60
61 cj('#addMore' + blockName + previousInstance ).hide( );
62 cj.ajax({
63 url : dataUrl,
64 async : false,
65 success : function(html){
66 cj(fname).after(html);
67 cj(fname).nextAll().trigger('crmLoad');
68 }
69 });
70
71 if ( blockName == 'Address' ) {
72 checkLocation('address_' + currentInstance + '_location_type_id', true );
73 /* FIX: for IE, To get the focus after adding new address block on first element */
74 cj('#address_' + currentInstance + '_location_type_id').focus();
75 }
76 }
77
78 //select single for is_bulk & is_primary
79 function singleSelect( object ) {
80 var element = object.split( '_', 3 );
81
82 var block = (element['0'] == 'Address') ? 'Primary' : element['2'].slice('2');
83 var execBlock = '#' + element['0'] + '-' + block + '-html Input[id*="' + element['2'] + '"]';
84
85 //element to check for checkbox
86 var elementChecked = cj( '#' + object ).prop('checked');
87 if ( elementChecked ) {
88 cj( execBlock ).each( function() {
89 if ( cj(this).attr('id') != object ) {
90 cj(this).prop('checked', false );
91 }
92 });
93 } else {
94 cj( '#' + object ).prop('checked', false );
95 }
96
97 //check if non of elements is set Primary / Allowed to Login.
98 if( cj.inArray( element['2'].slice('2'), [ 'Primary', 'Login' ] ) != -1 ) {
99 primary = false;
100 cj( execBlock ).each( function( ) {
101 if ( cj(this).prop('checked' ) ) {
102 primary = true;
103 }
104 });
105 if( ! primary ) {
106 cj('#' + object).prop('checked', true );
107 }
108 }
109 }
110
111 function removeBlock( blockName, blockId ) {
112 var element = cj("#addressBlock > div").size();
113 if ( ( blockName == 'Address' ) && element == 1 ) {
114 return clearFirstBlock(blockName , blockId);
115 }
116
117 if ( cj( "#"+ blockName + "_" + blockId + "_IsPrimary").prop('checked') ) {
118 var primaryBlockId = 1;
119 // consider next block as a primary,
120 // when user delete first block
121 if ( blockId >= 1 ) {
122 var blockIds = getAddressBlock('next');
123 for ( var i = 0; i <= blockIds.length; i++) {
124 var curBlockId = blockIds[i];
125 if ( curBlockId != blockId ) {
126 primaryBlockId = curBlockId;
127 break;
128 }
129 }
130 }
131
132 // finally sets the primary address
133 cj( '#'+ blockName + '_' + primaryBlockId + '_IsPrimary').prop('checked', true);
134 }
135
136 //remove the spacer for address block only.
137 if ( blockName == 'Address' && cj( "#"+ blockName + "_Block_" + blockId ).prev().attr('class') == 'spacer' ){
138 cj( "#"+ blockName + "_Block_" + blockId ).prev().remove();
139 }
140
141 //unset block from html
142 cj( "#"+ blockName + "_Block_" + blockId ).empty().remove();
143
144 //show the link 'add address' to last element of Address Block
145 if ( blockName == 'Address' ) {
146 var lastAddressBlock = cj('div[id^=Address_Block_]').last().attr('id');
147 var lastBlockId = lastAddressBlock.split( '_' );
148 if ( lastBlockId[2] ) {
149 cj( '#addMoreAddress' + lastBlockId[2] ).show();
150 }
151 }
152 }
153
154 function clearFirstBlock( blockName , blockId ) {
155 var element = blockName + '_Block_' + blockId;
156 cj("#" + element +" input, " + "#" + element + " select").each(function () {
157 cj(this).val('');
158 });
159 cj("#addressBlockId:not(.collapsed)").crmAccordionToggle();
160 cj("#addressBlockId .active").removeClass('active');
161 }
162
163 function getAddressBlock( position ) {
164 var addressBlockIds = [];
165 var i = 0;
166 switch ( position ) {
167 case 'last':
168 var lastBlockInfo = cj("#addressBlockId > div").children(':last').attr('id').split( '_', 3);
169 addressBlockIds[i] = lastBlockInfo['2'];
170 break;
171 case 'next':
172 cj("#addressBlockId > div").children().each( function() {
173 if ( cj(this).attr('id') ) {
174 var blockInfo = cj(this).attr('id').split( '_', 3);
175 addressBlockIds[i] = blockInfo['2'];
176 i++;
177 }
178 });
179 break;
180 }
181 return addressBlockIds;
182 }
183 </script>
184 {/literal}