Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
232624b1 | 3 | | CiviCRM version 4.4 | |
6a488035 TO |
4 | +--------------------------------------------------------------------+ |
5 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | cj( 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 | if ((typeof(Drupal) != 'undefined') && Drupal.attachBehaviors) { | |
84 | Drupal.attachBehaviors(cj('#' + blockName + '_Block_'+ currentInstance)[0]); | |
85 | } | |
86 | } | |
87 | }); | |
88 | ||
89 | if ( blockName == 'Address' ) { | |
90 | checkLocation('address_' + currentInstance + '_location_type_id', true ); | |
91 | /* FIX: for IE, To get the focus after adding new address block on first element */ | |
92 | cj('#address_' + currentInstance + '_location_type_id').focus(); | |
93 | } | |
94 | } | |
95 | ||
96 | //select single for is_bulk & is_primary | |
97 | function singleSelect( object ) { | |
98 | var element = object.split( '_', 3 ); | |
99 | ||
100 | var block = (element['0'] == 'Address') ? 'Primary' : element['2'].slice('2'); | |
101 | var execBlock = '#' + element['0'] + '-' + block + '-html Input[id*="' + element['2'] + '"]'; | |
102 | ||
103 | //element to check for checkbox | |
8539f25d | 104 | var elementChecked = cj( '#' + object ).prop('checked'); |
6a488035 TO |
105 | if ( elementChecked ) { |
106 | cj( execBlock ).each( function() { | |
107 | if ( cj(this).attr('id') != object ) { | |
8539f25d | 108 | cj(this).prop('checked', false ); |
6a488035 TO |
109 | } |
110 | }); | |
111 | } else { | |
8539f25d | 112 | cj( '#' + object ).prop('checked', false ); |
6a488035 TO |
113 | } |
114 | ||
115 | //check if non of elements is set Primary / Allowed to Login. | |
116 | if( cj.inArray( element['2'].slice('2'), [ 'Primary', 'Login' ] ) != -1 ) { | |
117 | primary = false; | |
118 | cj( execBlock ).each( function( ) { | |
8539f25d | 119 | if ( cj(this).prop('checked' ) ) { |
6a488035 TO |
120 | primary = true; |
121 | } | |
122 | }); | |
123 | if( ! primary ) { | |
8539f25d | 124 | cj('#' + object).prop('checked', true ); |
6a488035 TO |
125 | } |
126 | } | |
127 | } | |
128 | ||
129 | function removeBlock( blockName, blockId ) { | |
130 | var element = cj("#addressBlock > div").size(); | |
131 | if ( ( blockName == 'Address' ) && element == 1 ) { | |
132 | return clearFirstBlock(blockName , blockId); | |
133 | } | |
134 | ||
8539f25d | 135 | if ( cj( "#"+ blockName + "_" + blockId + "_IsPrimary").prop('checked') ) { |
6a488035 TO |
136 | var primaryBlockId = 1; |
137 | // consider next block as a primary, | |
138 | // when user delete first block | |
139 | if ( blockId >= 1 ) { | |
140 | var blockIds = getAddressBlock('next'); | |
141 | for ( var i = 0; i <= blockIds.length; i++) { | |
142 | var curBlockId = blockIds[i]; | |
143 | if ( curBlockId != blockId ) { | |
144 | primaryBlockId = curBlockId; | |
145 | break; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | // finally sets the primary address | |
8539f25d | 151 | cj( '#'+ blockName + '_' + primaryBlockId + '_IsPrimary').prop('checked', true); |
6a488035 TO |
152 | } |
153 | ||
154 | //remove the spacer for address block only. | |
155 | if ( blockName == 'Address' && cj( "#"+ blockName + "_Block_" + blockId ).prev().attr('class') == 'spacer' ){ | |
156 | cj( "#"+ blockName + "_Block_" + blockId ).prev().remove(); | |
157 | } | |
158 | ||
159 | //unset block from html | |
160 | cj( "#"+ blockName + "_Block_" + blockId ).empty().remove(); | |
161 | ||
162 | //show the link 'add address' to last element of Address Block | |
163 | if ( blockName == 'Address' ) { | |
164 | var lastAddressBlock = cj('div[id^=Address_Block_]').last().attr('id'); | |
165 | var lastBlockId = lastAddressBlock.split( '_' ); | |
166 | if ( lastBlockId[2] ) { | |
167 | cj( '#addMoreAddress' + lastBlockId[2] ).show(); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | function clearFirstBlock( blockName , blockId ) { | |
173 | var element = blockName + '_Block_' + blockId; | |
174 | cj("#" + element +" input, " + "#" + element + " select").each(function () { | |
175 | cj(this).val(''); | |
176 | }); | |
177 | cj("#addressBlockId:not(.collapsed)").crmAccordionToggle(); | |
178 | cj("#addressBlockId .active").removeClass('active'); | |
179 | } | |
180 | ||
181 | function getAddressBlock( position ) { | |
182 | var addressBlockIds = new Array(); | |
183 | var i = 0; | |
184 | switch ( position ) { | |
185 | case 'last': | |
186 | var lastBlockInfo = cj("#addressBlockId > div").children(':last').attr('id').split( '_', 3); | |
187 | addressBlockIds[i] = lastBlockInfo['2']; | |
188 | break; | |
189 | case 'next': | |
190 | cj("#addressBlockId > div").children().each( function() { | |
191 | if ( cj(this).attr('id') ) { | |
192 | var blockInfo = cj(this).attr('id').split( '_', 3); | |
193 | addressBlockIds[i] = blockInfo['2']; | |
194 | i++; | |
195 | } | |
196 | }); | |
197 | break; | |
198 | } | |
199 | return addressBlockIds; | |
200 | } | |
201 | </script> | |
202 | {/literal} |