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