Commit | Line | Data |
---|---|---|
353ea873 | 1 | // https://civicrm.org/licensing |
6a488035 | 2 | var CRM = CRM || {}; |
4b513f23 CW |
3 | var cj = CRM.$ = jQuery; |
4 | CRM._ = _; | |
6a488035 TO |
5 | |
6 | /** | |
7 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
8 | * | |
353ea873 CW |
9 | * @param text string for translating |
10 | * @param params object key:value of additional parameters | |
6a488035 | 11 | * |
353ea873 | 12 | * @return string |
6a488035 TO |
13 | */ |
14 | function ts(text, params) { | |
7553cf23 | 15 | "use strict"; |
6a488035 | 16 | text = CRM.strings[text] || text; |
2788147f | 17 | if (typeof(params) === 'object') { |
6a488035 | 18 | for (var i in params) { |
32155ad6 | 19 | if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') { |
2788147f | 20 | // sprintf emulation: escape % characters in the replacements to avoid conflicts |
32155ad6 | 21 | text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-')); |
2788147f | 22 | } |
6a488035 TO |
23 | } |
24 | return text.replace(/%-crmescaped-/g, '%'); | |
25 | } | |
26 | return text; | |
27 | } | |
28 | ||
29 | /** | |
30 | * This function is called by default at the bottom of template files which have forms that have | |
31 | * conditionally displayed/hidden sections and elements. The PHP is responsible for generating | |
32 | * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to | |
33 | * this function. | |
34 | * | |
353ea873 | 35 | * @deprecated |
6a488035 TO |
36 | * @param showBlocks Array of element Id's to be displayed |
37 | * @param hideBlocks Array of element Id's to be hidden | |
38 | * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...) | |
6a488035 | 39 | */ |
0f5816a6 KJ |
40 | function on_load_init_blocks(showBlocks, hideBlocks, elementType) { |
41 | if (elementType == null) { | |
42 | var elementType = 'block'; | |
43 | } | |
44 | ||
45 | /* This loop is used to display the blocks whose IDs are present within the showBlocks array */ | |
46 | for (var i = 0; i < showBlocks.length; i++) { | |
47 | var myElement = document.getElementById(showBlocks[i]); | |
48 | /* getElementById returns null if element id doesn't exist in the document */ | |
49 | if (myElement != null) { | |
50 | myElement.style.display = elementType; | |
6a488035 | 51 | } |
0f5816a6 KJ |
52 | else { |
53 | alert('showBlocks array item not in .tpl = ' + showBlocks[i]); | |
54 | } | |
55 | } | |
6a488035 | 56 | |
0f5816a6 KJ |
57 | /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */ |
58 | for (var i = 0; i < hideBlocks.length; i++) { | |
59 | var myElement = document.getElementById(hideBlocks[i]); | |
60 | /* getElementById returns null if element id doesn't exist in the document */ | |
61 | if (myElement != null) { | |
62 | myElement.style.display = 'none'; | |
63 | } | |
64 | else { | |
65 | alert('showBlocks array item not in .tpl = ' + hideBlocks[i]); | |
6a488035 | 66 | } |
0f5816a6 | 67 | } |
6a488035 TO |
68 | } |
69 | ||
70 | /** | |
71 | * This function is called when we need to show or hide a related form element (target_element) | |
72 | * based on the value (trigger_value) of another form field (trigger_field). | |
73 | * | |
353ea873 | 74 | * @deprecated |
6a488035 TO |
75 | * @param trigger_field_id HTML id of field whose onchange is the trigger |
76 | * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field | |
77 | * @param target_element_id HTML id of element to be shown or hidden | |
78 | * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row') | |
79 | * @param field_type Type of element radio/select | |
80 | * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match | |
0f5816a6 KJ |
81 | */ |
82 | function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) { | |
0f5816a6 KJ |
83 | |
84 | if (field_type == 'select') { | |
85 | var trigger = trigger_value.split("|"); | |
24cc4545 | 86 | var selectedOptionValue = cj('#' + trigger_field_id).val(); |
0f5816a6 KJ |
87 | |
88 | var target = target_element_id.split("|"); | |
89 | for (var j = 0; j < target.length; j++) { | |
90 | if (invert) { | |
91 | cj('#' + target[j]).show(); | |
92 | } | |
93 | else { | |
94 | cj('#' + target[j]).hide(); | |
95 | } | |
96 | for (var i = 0; i < trigger.length; i++) { | |
97 | if (selectedOptionValue == trigger[i]) { | |
98 | if (invert) { | |
99 | cj('#' + target[j]).hide(); | |
100 | } | |
101 | else { | |
102 | cj('#' + target[j]).show(); | |
103 | } | |
6a488035 | 104 | } |
0f5816a6 KJ |
105 | } |
106 | } | |
6a488035 | 107 | |
0f5816a6 KJ |
108 | } |
109 | else { | |
110 | if (field_type == 'radio') { | |
111 | var target = target_element_id.split("|"); | |
112 | for (var j = 0; j < target.length; j++) { | |
5fb3af09 | 113 | if (cj('[name="' + trigger_field_id + '"]:first').is(':checked')) { |
0f5816a6 KJ |
114 | if (invert) { |
115 | cj('#' + target[j]).hide(); | |
116 | } | |
117 | else { | |
118 | cj('#' + target[j]).show(); | |
119 | } | |
6a488035 | 120 | } |
0f5816a6 KJ |
121 | else { |
122 | if (invert) { | |
123 | cj('#' + target[j]).show(); | |
124 | } | |
125 | else { | |
126 | cj('#' + target[j]).hide(); | |
127 | } | |
128 | } | |
129 | } | |
6a488035 | 130 | } |
0f5816a6 | 131 | } |
6a488035 TO |
132 | } |
133 | ||
6a488035 TO |
134 | /** |
135 | * Function to change button text and disable one it is clicked | |
353ea873 | 136 | * @deprecated |
6a488035 TO |
137 | * @param obj object - the button clicked |
138 | * @param formID string - the id of the form being submitted | |
139 | * @param string procText - button text after user clicks it | |
353ea873 | 140 | * @return bool |
6a488035 | 141 | */ |
0f5816a6 | 142 | var submitcount = 0; |
c6edd786 | 143 | /* Changes button label on submit, and disables button after submit for newer browsers. |
144 | Puts up alert for older browsers. */ | |
0f5816a6 KJ |
145 | function submitOnce(obj, formId, procText) { |
146 | // if named button clicked, change text | |
147 | if (obj.value != null) { | |
148 | obj.value = procText + " ..."; | |
149 | } | |
47ea29e3 | 150 | cj(obj).closest('form').attr('data-warn-changes', 'false'); |
0f5816a6 KJ |
151 | if (document.getElementById) { // disable submit button for newer browsers |
152 | obj.disabled = true; | |
153 | document.getElementById(formId).submit(); | |
154 | return true; | |
155 | } | |
156 | else { // for older browsers | |
157 | if (submitcount == 0) { | |
158 | submitcount++; | |
159 | return true; | |
160 | } | |
161 | else { | |
162 | alert("Your request is currently being processed ... Please wait."); | |
163 | return false; | |
6a488035 | 164 | } |
0f5816a6 | 165 | } |
6a488035 | 166 | } |
6a488035 | 167 | |
6a488035 TO |
168 | /** |
169 | * Function to show / hide the row in optionFields | |
353ea873 CW |
170 | * @deprecated |
171 | * @param index string, element whose innerHTML is to hide else will show the hidden row. | |
6a488035 | 172 | */ |
0f5816a6 KJ |
173 | function showHideRow(index) { |
174 | if (index) { | |
175 | cj('tr#optionField_' + index).hide(); | |
176 | if (cj('table#optionField tr:hidden:first').length) { | |
177 | cj('div#optionFieldLink').show(); | |
6a488035 | 178 | } |
0f5816a6 KJ |
179 | } |
180 | else { | |
181 | cj('table#optionField tr:hidden:first').show(); | |
182 | if (!cj('table#optionField tr:hidden:last').length) { | |
183 | cj('div#optionFieldLink').hide(); | |
184 | } | |
185 | } | |
186 | return false; | |
6a488035 TO |
187 | } |
188 | ||
475e9f44 | 189 | CRM.utils = CRM.utils || {}; |
6a488035 | 190 | CRM.strings = CRM.strings || {}; |
6a488035 | 191 | |
4b513f23 | 192 | (function ($, _, undefined) { |
7553cf23 | 193 | "use strict"; |
d664f648 | 194 | |
3f586963 CW |
195 | // Theme classes for unattached elements |
196 | $.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container'; | |
197 | ||
47f21f3a CW |
198 | // https://github.com/ivaynberg/select2/pull/2090 |
199 | $.fn.select2.defaults.width = 'resolve'; | |
200 | ||
e20523a8 CW |
201 | // Workaround for https://github.com/ivaynberg/select2/issues/1246 |
202 | $.ui.dialog.prototype._allowInteraction = function(e) { | |
a7c4a5a8 | 203 | return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop, .cke_dialog').length; |
e20523a8 CW |
204 | }; |
205 | ||
475e9f44 CW |
206 | /** |
207 | * Populate a select list, overwriting the existing options except for the placeholder. | |
581c7be2 | 208 | * @param select jquery selector - 1 or more select elements |
475e9f44 | 209 | * @param options array in format returned by api.getoptions |
1d07e7ab | 210 | * @param placeholder string |
475e9f44 | 211 | */ |
581c7be2 CW |
212 | CRM.utils.setOptions = function(select, options, placeholder) { |
213 | $(select).each(function() { | |
475e9f44 CW |
214 | var |
215 | $elect = $(this), | |
23e8a31b | 216 | val = $elect.val() || [], |
1d07e7ab CW |
217 | opts = placeholder || placeholder === '' ? '' : '[value!=""]', |
218 | newOptions = '', | |
219 | theme = function(options) { | |
220 | _.each(options, function(option) { | |
221 | if (option.children) { | |
222 | newOptions += '<optgroup label="' + option.value + '">'; | |
223 | theme(option.children); | |
224 | newOptions += '</optgroup>'; | |
225 | } else { | |
226 | var selected = ($.inArray('' + option.key, val) > -1) ? 'selected="selected"' : ''; | |
227 | newOptions += '<option value="' + option.key + '"' + selected + '>' + option.value + '</option>'; | |
228 | } | |
229 | }); | |
230 | }; | |
46ed212b | 231 | if (!$.isArray(val)) { |
475e9f44 CW |
232 | val = [val]; |
233 | } | |
23e8a31b | 234 | $elect.find('option' + opts).remove(); |
1d07e7ab CW |
235 | theme(options); |
236 | if (typeof placeholder === 'string') { | |
581c7be2 CW |
237 | if ($elect.is('[multiple]')) { |
238 | select.attr('placeholder', placeholder); | |
1d07e7ab CW |
239 | } else { |
240 | newOptions = '<option value="">' + placeholder + '</option>' + newOptions; | |
241 | } | |
242 | } | |
243 | $elect.append(newOptions); | |
e6f4ac36 | 244 | $elect.trigger('crmOptionsUpdated', $.extend({}, options)).trigger('change'); |
475e9f44 CW |
245 | }); |
246 | }; | |
247 | ||
1d07e7ab CW |
248 | function chainSelect() { |
249 | var $form = $(this).closest('form'), | |
250 | $target = $('select[data-name="' + $(this).data('target') + '"]', $form), | |
251 | data = $target.data(), | |
252 | val = $(this).val(); | |
253 | $target.prop('disabled', true); | |
254 | if ($target.is('select.crm-chain-select-control')) { | |
255 | $('select[data-name="' + $target.data('target') + '"]', $form).prop('disabled', true).blur(); | |
256 | } | |
257 | if (!(val && val.length)) { | |
258 | CRM.utils.setOptions($target.blur(), [], data.emptyPrompt); | |
259 | } else { | |
260 | $target.addClass('loading'); | |
261 | $.getJSON(CRM.url(data.callback), {_value: val}, function(vals) { | |
262 | $target.prop('disabled', false).removeClass('loading'); | |
263 | CRM.utils.setOptions($target, vals || [], (vals && vals.length ? data.selectPrompt : data.nonePrompt)); | |
264 | }); | |
265 | } | |
266 | } | |
267 | ||
3e201321 | 268 | /** |
269 | * Compare Form Input values against cached initial value. | |
88e9380e CW |
270 | * |
271 | * @return {Boolean} true if changes have been made. | |
3e201321 | 272 | */ |
273 | CRM.utils.initialValueChanged = function(el) { | |
88e9380e | 274 | var isDirty = false; |
18469bf2 | 275 | $(':input:visible, .select2-container:visible+:input.select2-offscreen', el).not('[type=submit], [type=button], .crm-action-menu').each(function () { |
88e9380e | 276 | var initialValue = $(this).data('crm-initial-value'); |
3c0624e2 | 277 | // skip change of value for submit buttons |
bf7a4fbc | 278 | if (initialValue !== undefined && !_.isEqual(initialValue, $(this).val())) { |
88e9380e CW |
279 | isDirty = true; |
280 | } | |
3e201321 | 281 | }); |
282 | return isDirty; | |
8d36b801 | 283 | }; |
3e201321 | 284 | |
ba4fb2b2 | 285 | /** |
353ea873 | 286 | * Wrapper for select2 initialization function; supplies defaults |
a88cf11a | 287 | * @param options object |
ba4fb2b2 | 288 | */ |
a88cf11a CW |
289 | $.fn.crmSelect2 = function(options) { |
290 | return $(this).each(function () { | |
291 | var | |
292 | $el = $(this), | |
a243158e | 293 | settings = {allowClear: !$el.hasClass('required')}; |
a88cf11a CW |
294 | // quickform doesn't support optgroups so here's a hack :( |
295 | $('option[value^=crm_optgroup]', this).each(function () { | |
296 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
297 | $(this).remove(); | |
298 | }); | |
299 | // Defaults for single-selects | |
300 | if ($el.is('select:not([multiple])')) { | |
a243158e | 301 | settings.minimumResultsForSearch = 10; |
a88cf11a | 302 | if ($('option:first', this).val() === '') { |
a243158e | 303 | settings.placeholderOption = 'first'; |
a88cf11a | 304 | } |
ba4fb2b2 | 305 | } |
a243158e CW |
306 | $.extend(settings, $el.data('select-params') || {}, options || {}); |
307 | if (settings.ajax) { | |
308 | $el.addClass('crm-ajax-select'); | |
309 | } | |
310 | $el.select2(settings); | |
a88cf11a CW |
311 | }); |
312 | }; | |
313 | ||
314 | /** | |
353ea873 | 315 | * @see CRM_Core_Form::addEntityRef for docs |
a88cf11a CW |
316 | * @param options object |
317 | */ | |
318 | $.fn.crmEntityRef = function(options) { | |
319 | options = options || {}; | |
320 | options.select = options.select || {}; | |
321 | return $(this).each(function() { | |
322 | var | |
4c993609 | 323 | $el = $(this).off('.crmEntity'), |
a88cf11a CW |
324 | entity = options.entity || $el.data('api-entity') || 'contact', |
325 | selectParams = {}; | |
326 | $el.data('api-entity', entity); | |
327 | $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); | |
328 | $el.data('api-params', $.extend({}, $el.data('api-params') || {}, options.api)); | |
a4799f04 | 329 | $el.data('create-links', options.create || $el.data('create-links')); |
a243158e | 330 | $el.addClass('crm-form-entityref crm-' + entity + '-ref'); |
ba4fb2b2 CW |
331 | var settings = { |
332 | // Use select2 ajax helper instead of CRM.api because it provides more value | |
333 | ajax: { | |
334 | url: CRM.url('civicrm/ajax/rest'), | |
335 | data: function (input, page_num) { | |
336 | var params = $el.data('api-params') || {}; | |
337 | params.input = input; | |
338 | params.page_num = page_num; | |
339 | return { | |
340 | entity: $el.data('api-entity'), | |
341 | action: 'getlist', | |
342 | json: JSON.stringify(params) | |
343 | }; | |
344 | }, | |
345 | results: function(data) { | |
346 | return {more: data.more_results, results: data.values || []}; | |
347 | } | |
348 | }, | |
a88cf11a | 349 | minimumInputLength: 1, |
3c0b6a40 | 350 | formatResult: CRM.utils.formatSelect2Result, |
ba4fb2b2 CW |
351 | formatSelection: function(row) { |
352 | return row.label; | |
353 | }, | |
354 | escapeMarkup: function (m) {return m;}, | |
a88cf11a CW |
355 | initSelection: function($el, callback) { |
356 | var | |
357 | multiple = !!$el.data('select-params').multiple, | |
358 | val = $el.val(), | |
359 | stored = $el.data('entity-value') || []; | |
360 | if (val === '') { | |
361 | return; | |
362 | } | |
363 | // If we already have this data, just return it | |
364 | if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { | |
365 | callback(multiple ? stored : stored[0]); | |
366 | } else { | |
78b203e5 | 367 | var params = $.extend({}, $el.data('api-params') || {}, {id: val}); |
a88cf11a | 368 | CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { |
d2b4810b CW |
369 | callback(multiple ? result.values : result.values[0]); |
370 | // Trigger change (store data to avoid an infinite loop of lookups) | |
371 | $el.data('entity-value', result.values).trigger('change'); | |
a88cf11a CW |
372 | }); |
373 | } | |
ba4fb2b2 CW |
374 | } |
375 | }; | |
4c993609 | 376 | if ($el.data('create-links') && entity.toLowerCase() === 'contact') { |
a88cf11a CW |
377 | selectParams.formatInputTooShort = function() { |
378 | var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this); | |
d706958c | 379 | if ($el.data('create-links') && CRM.profileCreate && CRM.profileCreate.length) { |
a4799f04 | 380 | txt += ' ' + ts('or') + '<br />' + formatSelect2CreateLinks($el); |
ba4fb2b2 CW |
381 | } |
382 | return txt; | |
383 | }; | |
a88cf11a | 384 | selectParams.formatNoMatches = function() { |
ba4fb2b2 | 385 | var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches; |
4e56e743 | 386 | return txt + (CRM.profileCreate ? ('<br />' + formatSelect2CreateLinks($el)) : ''); |
ba4fb2b2 | 387 | }; |
4c993609 | 388 | $el.on('select2-open.crmEntity', function() { |
ba4fb2b2 CW |
389 | var $el = $(this); |
390 | $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
391 | $el.select2('close'); | |
392 | CRM.loadForm($(this).attr('href'), { | |
393 | dialog: {width: 500, height: 'auto'} | |
394 | }).on('crmFormSuccess', function(e, data) { | |
c92f6436 | 395 | if (data.status === 'success' && data.id) { |
e11b84ce | 396 | CRM.status(ts('%1 Created', {1: data.label})); |
c92f6436 CW |
397 | if ($el.select2('container').hasClass('select2-container-multi')) { |
398 | var selection = $el.select2('data'); | |
399 | selection.push(data); | |
400 | $el.select2('data', selection, true); | |
401 | } else { | |
402 | $el.select2('data', data, true); | |
403 | } | |
ba4fb2b2 CW |
404 | } |
405 | }); | |
406 | return false; | |
407 | }); | |
408 | }); | |
409 | } | |
4c993609 CW |
410 | // Create new items inline - works for tags |
411 | else if ($el.data('create-links')) { | |
412 | selectParams.createSearchChoice = function(term, data) { | |
413 | if (!_.findKey(data, {label: term})) { | |
414 | return {id: "0", term: term, label: term + ' (' + ts('new tag') + ')'}; | |
415 | } | |
416 | }; | |
e4f4dc22 | 417 | selectParams.tokenSeparators = [',']; |
4c993609 CW |
418 | selectParams.createSearchChoicePosition = 'bottom'; |
419 | } | |
420 | $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)) | |
421 | .on('select2-selecting.crmEntity', function(e) { | |
422 | if (e.val === "0") { | |
a2c28a94 | 423 | // Create a new term |
4c993609 CW |
424 | e.object.label = e.object.term; |
425 | CRM.api3(entity, 'create', $.extend({name: e.object.term}, $el.data('api-params').params || {})) | |
426 | .done(function(created) { | |
427 | var | |
4c993609 CW |
428 | val = $el.select2('val'), |
429 | data = $el.select2('data'), | |
430 | item = {id: created.id, label: e.object.term}; | |
431 | if (val === "0") { | |
e4f4dc22 | 432 | $el.select2('data', item, true); |
4c993609 CW |
433 | } |
434 | else if ($.isArray(val) && $.inArray("0", val) > -1) { | |
435 | _.remove(data, {id: "0"}); | |
436 | data.push(item); | |
e4f4dc22 | 437 | $el.select2('data', data, true); |
4c993609 CW |
438 | } |
439 | }); | |
440 | } | |
441 | }); | |
a88cf11a | 442 | }); |
ba4fb2b2 CW |
443 | }; |
444 | ||
3c0b6a40 | 445 | CRM.utils.formatSelect2Result = function (row) { |
88881f79 | 446 | var markup = '<div class="crm-select2-row">'; |
ff88d165 | 447 | if (row.image !== undefined) { |
88881f79 | 448 | markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>'; |
ff88d165 | 449 | } |
54bee7df | 450 | else if (row.icon_class) { |
88881f79 | 451 | markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>'; |
54bee7df | 452 | } |
3c0b6a40 | 453 | markup += '<div><div class="crm-select2-row-label '+(row.label_class || '')+'">' + row.label + '</div>'; |
88881f79 CW |
454 | markup += '<div class="crm-select2-row-description">'; |
455 | $.each(row.description || [], function(k, text) { | |
456 | markup += '<p>' + text + '</p>'; | |
457 | }); | |
458 | markup += '</div></div></div>'; | |
ff88d165 | 459 | return markup; |
3c0b6a40 | 460 | }; |
a4799f04 CW |
461 | |
462 | function formatSelect2CreateLinks($el) { | |
463 | var | |
464 | createLinks = $el.data('create-links'), | |
465 | api = $el.data('api-params') || {}, | |
466 | type = api.params ? api.params.contact_type : null; | |
467 | if (createLinks === true) { | |
4e56e743 | 468 | createLinks = type ? _.where(CRM.profileCreate, {type: type}) : CRM.profileCreate; |
a4799f04 | 469 | } |
79ae07d9 | 470 | var markup = ''; |
a4799f04 | 471 | _.each(createLinks, function(link) { |
79ae07d9 | 472 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; |
a4799f04 CW |
473 | if (link.type) { |
474 | markup += '<span class="icon ' + link.type + '-profile-icon"></span> '; | |
79ae07d9 CW |
475 | } |
476 | markup += link.label + '</a>'; | |
477 | }); | |
478 | return markup; | |
a4799f04 | 479 | } |
ff88d165 | 480 | |
3d527838 CW |
481 | /** |
482 | * Wrapper for jQuery validate initialization function; supplies defaults | |
483 | * @param options object | |
484 | */ | |
485 | $.fn.crmValidate = function(params) { | |
486 | return $(this).each(function () { | |
487 | var that = this, | |
488 | settings = $.extend({}, CRM.validate._defaults, CRM.validate.params); | |
489 | $(this).validate(settings); | |
490 | // Call any post-initialization callbacks | |
491 | if (CRM.validate.functions && CRM.validate.functions.length) { | |
492 | $.each(CRM.validate.functions, function(i, func) { | |
493 | func.call(that); | |
494 | }); | |
495 | } | |
496 | }); | |
497 | } | |
498 | ||
f7b92fcd | 499 | // Initialize widgets |
eb90857a CW |
500 | $(document) |
501 | .on('crmLoad', function(e) { | |
502 | $('table.row-highlight', e.target) | |
503 | .off('.rowHighlight') | |
7e13d44e CW |
504 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function (e, data) { |
505 | var filter, $table = $(this).closest('table'); | |
eb90857a | 506 | if ($(this).hasClass('select-rows')) { |
7e13d44e CW |
507 | filter = $(this).prop('checked') ? ':not(:checked)' : ':checked'; |
508 | $('input.select-row' + filter, $table).prop('checked', $(this).prop('checked')).trigger('change', 'master-selected'); | |
eb90857a CW |
509 | } |
510 | else { | |
7e13d44e CW |
511 | $(this).closest('tr').toggleClass('crm-row-selected', $(this).prop('checked')); |
512 | if (data !== 'master-selected') { | |
513 | $('input.select-rows', $table).prop('checked', $(".select-row:not(':checked')", $table).length < 1); | |
514 | } | |
eb90857a | 515 | } |
eb90857a CW |
516 | }) |
517 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
82661158 JP |
518 | if ($("input:radio[name=radio_ts]").size() == 1) { |
519 | $("input:radio[name=radio_ts]").prop("checked", true); | |
520 | } | |
5f34e50b CW |
521 | $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); |
522 | $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); | |
1d07e7ab | 523 | $('select.crm-chain-select-control', e.target).off('.chainSelect').on('change.chainSelect', chainSelect); |
3e201321 | 524 | // Cache Form Input initial values |
88e9380e | 525 | $('form[data-warn-changes] :input', e.target).each(function() { |
329758bb | 526 | $(this).data('crm-initial-value', $(this).val()); |
3e201321 | 527 | }); |
eb90857a | 528 | }) |
eb90857a | 529 | .on('dialogopen', function(e) { |
f292709b CW |
530 | var $el = $(e.target); |
531 | // Modal dialogs should disable scrollbars | |
532 | if ($el.dialog('option', 'modal')) { | |
533 | $el.addClass('modal-dialog'); | |
eb90857a CW |
534 | $('body').css({overflow: 'hidden'}); |
535 | } | |
f292709b | 536 | // Add resize button |
a243158e | 537 | if ($el.parent().hasClass('crm-container') && $el.dialog('option', 'resizable')) { |
77a8d7f9 | 538 | $el.parent().find('.ui-dialog-titlebar').append($('<button class="crm-dialog-titlebar-resize ui-dialog-titlebar-close" title="'+ts('Toggle fullscreen')+'" style="right:2em;"/>').button({icons: {primary: 'ui-icon-newwin'}, text: false})); |
f292709b CW |
539 | $('.crm-dialog-titlebar-resize', $el.parent()).click(function(e) { |
540 | if ($el.data('origSize')) { | |
541 | $el.dialog('option', $el.data('origSize')); | |
542 | $el.data('origSize', null); | |
543 | } else { | |
28d510ab | 544 | var menuHeight = $('#civicrm-menu').outerHeight(); |
f292709b | 545 | $el.data('origSize', { |
28d510ab | 546 | position: {my: 'center', at: 'center center+' + (menuHeight / 2), of: window}, |
f292709b CW |
547 | width: $el.dialog('option', 'width'), |
548 | height: $el.dialog('option', 'height') | |
549 | }); | |
f2f191fe | 550 | $el.dialog('option', {width: '100%', height: ($(window).height() - menuHeight), position: {my: "top", at: "top+"+menuHeight, of: window}}); |
f292709b CW |
551 | } |
552 | e.preventDefault(); | |
553 | }); | |
554 | } | |
eb90857a CW |
555 | }) |
556 | .on('dialogclose', function(e) { | |
f292709b | 557 | // Restore scrollbars when closing modal |
5a6148a0 | 558 | if ($('.ui-dialog .modal-dialog:visible').not(e.target).length < 1) { |
eb90857a CW |
559 | $('body').css({overflow: ''}); |
560 | } | |
afc021d8 | 561 | }) |
562 | .on('submit', function(e) { | |
f582fc8f | 563 | // CRM-14353 - disable changes warn when submitting a form |
abb6e044 | 564 | $('[data-warn-changes]').attr('data-warn-changes', 'false'); |
afc021d8 | 565 | }) |
f582fc8f CW |
566 | ; |
567 | ||
568 | // CRM-14353 - Warn of unsaved changes for forms which have opted in | |
569 | window.onbeforeunload = function() { | |
18469bf2 | 570 | if (CRM.utils.initialValueChanged($('form[data-warn-changes=true]:visible'))) { |
f582fc8f CW |
571 | return ts('You have unsaved changes.'); |
572 | } | |
573 | }; | |
148c4e8d CW |
574 | |
575 | /** | |
576 | * Function to make multiselect boxes behave as fields in small screens | |
577 | */ | |
578 | function advmultiselectResize() { | |
579 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
580 | if (amswidth < 700) { | |
581 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
582 | } |
583 | else { | |
148c4e8d CW |
584 | $("form table.advmultiselect td").css('display', 'table-cell'); |
585 | } | |
586 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
587 | if (contactwidth < 600) { | |
588 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 589 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 590 | if (index > 1) { |
0f5816a6 | 591 | if (index % 2 == 0) { |
148c4e8d CW |
592 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
593 | } | |
594 | var item = $(this); | |
595 | $(this).parent().next().append(item); | |
596 | } | |
597 | }); | |
0f5816a6 KJ |
598 | } |
599 | else { | |
148c4e8d | 600 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 601 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
602 | var nitem = $(this); |
603 | var parent = $(this).parent(); | |
604 | $(this).parent().prev().append(nitem); | |
0f5816a6 | 605 | if (parent.children().size() == 0) { |
148c4e8d CW |
606 | parent.remove(); |
607 | } | |
608 | }); | |
609 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
610 | } | |
611 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 612 | |
148c4e8d CW |
613 | if (cformwidth < 720) { |
614 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
615 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
616 | if (cformwidth < 480) { | |
617 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
618 | } |
619 | else { | |
148c4e8d CW |
620 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
621 | } | |
0f5816a6 KJ |
622 | } |
623 | else { | |
148c4e8d CW |
624 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
625 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
626 | } | |
627 | } | |
0f5816a6 | 628 | |
148c4e8d | 629 | advmultiselectResize(); |
a2c28a94 | 630 | $(window).resize(advmultiselectResize); |
6a488035 | 631 | |
0f5816a6 | 632 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
633 | $(document) |
634 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
635 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
636 | $(this).addClass('crm-tooltip-active'); |
637 | var topDistance = e.pageY - $(window).scrollTop(); | |
638 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { | |
639 | $(this).addClass('crm-tooltip-down'); | |
640 | } | |
641 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
642 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
643 | $(this).children().children('.crm-tooltip') | |
644 | .html('<div class="crm-loading-element"></div>') | |
645 | .load(this.href); | |
646 | } | |
647 | }) | |
2c29c2ac RN |
648 | .on('mouseout', 'a.crm-summary-link', function () { |
649 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
650 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
651 | }) | |
2c29c2ac | 652 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
653 | }; |
654 | ||
b0ca6188 | 655 | var helpDisplay, helpPrevious; |
8e3272a1 | 656 | CRM.help = function (title, params, url) { |
55a93b02 | 657 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 658 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
659 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
660 | helpDisplay.close(); | |
b0ca6188 CW |
661 | return; |
662 | } | |
55a93b02 | 663 | helpDisplay.close(); |
b0ca6188 CW |
664 | } |
665 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
666 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
667 | params.type = 'page'; | |
b0ca6188 | 668 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 669 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
670 | { |
671 | data: params, | |
672 | dataType: 'html', | |
e24b17b9 | 673 | success: function (data) { |
6a488035 TO |
674 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
675 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
676 | }, | |
e24b17b9 | 677 | error: function () { |
6a488035 TO |
678 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
679 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
680 | } | |
681 | } | |
682 | ); | |
683 | }; | |
8960d9b9 | 684 | /** |
7442e8f6 | 685 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
8960d9b9 | 686 | */ |
1b2475e1 | 687 | CRM.status = function(options, deferred) { |
9a7ef94f | 688 | // For simple usage without async operations you can pass in a string. 2nd param is optional string 'error' if this is not a success msg. |
1b2475e1 CW |
689 | if (typeof options === 'string') { |
690 | return CRM.status({start: options, success: options, error: options})[deferred === 'error' ? 'reject' : 'resolve'](); | |
8960d9b9 | 691 | } |
1b2475e1 CW |
692 | var opts = $.extend({ |
693 | start: ts('Saving...'), | |
9a7ef94f | 694 | success: ts('Saved'), |
1b2475e1 CW |
695 | error: function() { |
696 | CRM.alert(ts('Sorry an error occurred and your information was not saved'), ts('Error')); | |
697 | } | |
698 | }, options || {}); | |
699 | var $msg = $('<div class="crm-status-box-outer status-start"><div class="crm-status-box-inner"><div class="crm-status-box-msg">' + opts.start + '</div></div></div>') | |
700 | .appendTo('body'); | |
701 | $msg.css('min-width', $msg.width()); | |
702 | function handle(status, data) { | |
703 | var endMsg = typeof(opts[status]) === 'function' ? opts[status](data) : opts[status]; | |
704 | if (endMsg) { | |
705 | $msg.removeClass('status-start').addClass('status-' + status).find('.crm-status-box-msg').html(endMsg); | |
706 | window.setTimeout(function() { | |
707 | $msg.fadeOut('slow', function() {$msg.remove()}); | |
4bad157e CW |
708 | }, 2000); |
709 | } else { | |
1b2475e1 | 710 | $msg.remove(); |
4bad157e | 711 | } |
1b2475e1 CW |
712 | } |
713 | return (deferred || new $.Deferred()) | |
714 | .done(function(data) { | |
715 | // If the server returns an error msg call the error handler | |
716 | var status = $.isPlainObject(data) && (data.is_error || data.status === 'error') ? 'error' : 'success'; | |
717 | handle(status, data); | |
718 | }) | |
719 | .fail(function(data) { | |
720 | handle('error', data); | |
721 | }); | |
8960d9b9 | 722 | }; |
6a488035 TO |
723 | |
724 | /** | |
7442e8f6 | 725 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 726 | */ |
0f5816a6 | 727 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
728 | type = type || 'alert'; |
729 | title = title || ''; | |
730 | options = options || {}; | |
731 | if ($('#crm-notification-container').length) { | |
732 | var params = { | |
733 | text: text, | |
734 | title: title, | |
735 | type: type | |
736 | }; | |
737 | // By default, don't expire errors and messages containing links | |
738 | var extra = { | |
739 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
740 | unique: true | |
741 | }; | |
742 | options = $.extend(extra, options); | |
e24b17b9 | 743 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 744 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 745 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
746 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
747 | $('.icon.ui-notify-close', this).click(); | |
748 | } | |
749 | }); | |
750 | } | |
751 | return $('#crm-notification-container').notify('create', params, options); | |
752 | } | |
753 | else { | |
754 | if (title.length) { | |
755 | text = title + "\n" + text; | |
756 | } | |
757 | alert(text); | |
758 | return null; | |
759 | } | |
e24b17b9 | 760 | }; |
6a488035 TO |
761 | |
762 | /** | |
763 | * Close whichever alert contains the given node | |
764 | * | |
765 | * @param node | |
766 | */ | |
0f5816a6 | 767 | CRM.closeAlertByChild = function (node) { |
6a488035 | 768 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 769 | }; |
6a488035 TO |
770 | |
771 | /** | |
7442e8f6 | 772 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 773 | */ |
5fb83680 | 774 | CRM.confirm = function (options) { |
27f190b4 | 775 | var dialog, url, msg, buttons = [], settings = { |
a65e5f52 | 776 | title: ts('Confirm'), |
7553cf23 | 777 | message: ts('Are you sure you want to continue?'), |
3f4328da | 778 | url: null, |
0d5f99d4 | 779 | width: 'auto', |
5fb83680 | 780 | modal: true, |
a243158e | 781 | resizable: false, |
5fb83680 | 782 | dialogClass: 'crm-container crm-confirm', |
0f5816a6 | 783 | close: function () { |
5fb83680 | 784 | $(this).dialog('destroy').remove(); |
0f5816a6 | 785 | }, |
5fb83680 CW |
786 | options: { |
787 | no: ts('Cancel'), | |
788 | yes: ts('Continue') | |
789 | } | |
0f5816a6 | 790 | }; |
5fb83680 CW |
791 | $.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {}); |
792 | if (!settings.buttons && $.isPlainObject(settings.options)) { | |
27f190b4 CW |
793 | $.each(settings.options, function(op, label) { |
794 | buttons.push({ | |
5fb83680 | 795 | text: label, |
27f190b4 CW |
796 | 'data-op': op, |
797 | icons: {primary: op === 'no' ? 'ui-icon-close' : 'ui-icon-check'}, | |
5fb83680 | 798 | click: function() { |
27f190b4 | 799 | var event = $.Event('crmConfirm:' + op); |
5fb83680 CW |
800 | $(this).trigger(event); |
801 | if (!event.isDefaultPrevented()) { | |
802 | dialog.dialog('close'); | |
803 | } | |
804 | } | |
805 | }); | |
806 | }); | |
27f190b4 CW |
807 | // Order buttons so that "no" goes on the right-hand side |
808 | settings.buttons = _.sortBy(buttons, 'data-op').reverse(); | |
2a06342c | 809 | } |
3f4328da CW |
810 | url = settings.url; |
811 | msg = settings.message; | |
5fb83680 CW |
812 | delete settings.options; |
813 | delete settings.message; | |
3f4328da CW |
814 | delete settings.url; |
815 | dialog = $('<div class="crm-confirm-dialog"></div>').dialog(settings); | |
5fb83680 CW |
816 | if ($.isFunction(options)) { |
817 | dialog.on('crmConfirm:yes', options); | |
7553cf23 | 818 | } |
3f4328da CW |
819 | if (url) { |
820 | CRM.loadPage(url, {target: dialog}); | |
821 | } | |
822 | else if (msg && msg.length) { | |
823 | dialog.html(msg).trigger('crmLoad'); | |
824 | } | |
825 | return dialog; | |
e24b17b9 | 826 | }; |
6a488035 | 827 | |
ed7225e6 CW |
828 | /** provides a local copy of ts for a domain */ |
829 | CRM.ts = function(domain) { | |
830 | return function(message, options) { | |
831 | if (domain) { | |
832 | options = $.extend(options || {}, {domain: domain}); | |
833 | } | |
f97524d9 TO |
834 | return ts(message, options); |
835 | }; | |
f97524d9 TO |
836 | }; |
837 | ||
6a488035 | 838 | /** |
7442e8f6 | 839 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 840 | */ |
0f5816a6 | 841 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
842 | title = title || ''; |
843 | text = text || ''; | |
844 | options = options || {}; | |
845 | ||
846 | var extra = { | |
847 | expires: 0 | |
848 | }; | |
849 | if ($(this).length) { | |
850 | if (title == '') { | |
851 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); | |
852 | if (label.length) { | |
853 | label.addClass('crm-error'); | |
854 | var $label = label.clone(); | |
855 | if (text == '' && $('.crm-marker', $label).length > 0) { | |
856 | text = $('.crm-marker', $label).attr('title'); | |
857 | } | |
858 | $('.crm-marker', $label).remove(); | |
859 | title = $label.text(); | |
860 | } | |
861 | } | |
862 | $(this).addClass('error'); | |
863 | } | |
864 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
865 | if ($(this).length) { | |
866 | var ele = $(this); | |
0f5816a6 KJ |
867 | setTimeout(function () { |
868 | ele.one('change', function () { | |
869 | msg && msg.close && msg.close(); | |
870 | ele.removeClass('error'); | |
871 | label.removeClass('crm-error'); | |
872 | }); | |
873 | }, 1000); | |
6a488035 TO |
874 | } |
875 | return msg; | |
e24b17b9 | 876 | }; |
6a488035 TO |
877 | |
878 | // Display system alerts through js notifications | |
879 | function messagesFromMarkup() { | |
0f5816a6 | 880 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 881 | var text, title = ''; |
6a488035 TO |
882 | $(this).removeClass('status messages'); |
883 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
884 | type = type.replace('crm-', ''); | |
885 | $('.icon', this).remove(); | |
6a488035 | 886 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 887 | text = $('.msg-text', this).html(); |
6a488035 TO |
888 | title = $('.msg-title', this).html(); |
889 | } | |
890 | else { | |
e24b17b9 | 891 | text = $(this).html(); |
6a488035 TO |
892 | } |
893 | var options = $(this).data('options') || {}; | |
894 | $(this).remove(); | |
895 | // Duplicates were already removed server-side | |
896 | options.unique = false; | |
897 | CRM.alert(text, title, type, options); | |
898 | }); | |
899 | // Handle qf form errors | |
900 | $('form :input.error', this).one('blur', function() { | |
901 | $('.ui-notify-message.error a.ui-notify-close').click(); | |
902 | $(this).removeClass('error'); | |
903 | $(this).next('span.crm-error').remove(); | |
904 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
905 | .removeClass('crm-error') | |
906 | .find('.crm-error').removeClass('crm-error'); | |
907 | }); | |
908 | } | |
909 | ||
1adbbe2d CW |
910 | var originalBlock = $.fn.block, |
911 | originalUnblock = $.fn.unblock; | |
912 | ||
913 | $.fn.block = function(opts) { | |
914 | if ($(this).is('.ui-dialog-content')) { | |
915 | originalBlock.call($(this).parents('.ui-dialog'), opts); | |
916 | return $(this); | |
917 | } | |
918 | return originalBlock.call(this, opts); | |
919 | } | |
920 | ||
921 | $.fn.unblock = function(opts) { | |
922 | if ($(this).is('.ui-dialog-content')) { | |
923 | originalUnblock.call($(this).parents('.ui-dialog'), opts); | |
924 | return $(this); | |
925 | } | |
926 | return originalUnblock.call(this, opts); | |
927 | } | |
928 | ||
03a7ec8f CW |
929 | // Preprocess all cj ajax calls to display messages |
930 | $(document).ajaxSuccess(function(event, xhr, settings) { | |
931 | try { | |
932 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
933 | var response = $.parseJSON(xhr.responseText); | |
934 | if (typeof(response.crmMessages) == 'object') { | |
935 | $.each(response.crmMessages, function(n, msg) { | |
936 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
937 | }) | |
938 | } | |
bba9b4f0 CW |
939 | if (response.backtrace) { |
940 | CRM.console('log', response.backtrace); | |
941 | } | |
03a7ec8f CW |
942 | } |
943 | } | |
944 | // Suppress errors | |
945 | catch (e) {} | |
946 | }); | |
947 | ||
0f5816a6 | 948 | $(function () { |
fdeb4de2 | 949 | $.blockUI.defaults.message = null; |
1adbbe2d | 950 | $.blockUI.defaults.ignoreIfBlocked = true; |
fdeb4de2 | 951 | |
65b86482 CW |
952 | if ($('#crm-container').hasClass('crm-public')) { |
953 | $.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container crm-public'; | |
954 | } | |
955 | ||
205bb8ae | 956 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 957 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 958 | |
ef3309b6 | 959 | if ($('#crm-notification-container').length) { |
6a488035 TO |
960 | // Initialize notifications |
961 | $('#crm-notification-container').notify(); | |
962 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 963 | } |
ebb9197b | 964 | |
475e9f44 | 965 | $('body') |
5fb83680 CW |
966 | // bind the event for image popup |
967 | .on('click', 'a.crm-image-popup', function(e) { | |
968 | CRM.confirm({ | |
969 | title: ts('Preview'), | |
a243158e | 970 | resizable: true, |
5fb83680 CW |
971 | message: '<div class="crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>', |
972 | options: null | |
973 | }); | |
974 | e.preventDefault(); | |
475e9f44 | 975 | }) |
ebb9197b | 976 | |
475e9f44 CW |
977 | .on('click', function (event) { |
978 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
979 | if ($(event.target).is('.btn-slide')) { | |
980 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
981 | } | |
982 | }) | |
d664f648 | 983 | |
4a143c04 CW |
984 | // Handle clear button for form elements |
985 | .on('click', 'a.crm-clear-link', function() { | |
986 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change(); | |
bcb4280c | 987 | $(this).siblings('input:text').val('').change(); |
4a143c04 CW |
988 | return false; |
989 | }) | |
990 | .on('change', 'input.crm-form-radio:checked', function() { | |
991 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
843bfb07 | 992 | }) |
6a488035 | 993 | |
843bfb07 | 994 | // Allow normal clicking of links within accordions |
cf021bc5 | 995 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
843bfb07 | 996 | e.stopPropagation(); |
cf021bc5 | 997 | }) |
843bfb07 CW |
998 | // Handle accordions |
999 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function (e) { | |
6a488035 | 1000 | if ($(this).parent().hasClass('collapsed')) { |
843bfb07 | 1001 | $(this).next().css('display', 'none').slideDown(200); |
6a488035 TO |
1002 | } |
1003 | else { | |
843bfb07 | 1004 | $(this).next().css('display', 'block').slideUp(200); |
6a488035 TO |
1005 | } |
1006 | $(this).parent().toggleClass('collapsed'); | |
843bfb07 | 1007 | e.preventDefault(); |
6a488035 | 1008 | }); |
843bfb07 CW |
1009 | |
1010 | $().crmtooltip(); | |
1011 | }); | |
1012 | /** | |
1013 | * @deprecated | |
1014 | */ | |
1015 | $.fn.crmAccordions = function () {}; | |
1016 | /** | |
1017 | * Collapse or expand an accordion | |
1018 | * @param speed | |
1019 | */ | |
0f5816a6 KJ |
1020 | $.fn.crmAccordionToggle = function (speed) { |
1021 | $(this).each(function () { | |
6a488035 TO |
1022 | if ($(this).hasClass('collapsed')) { |
1023 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
1024 | } | |
1025 | else { | |
1026 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
1027 | } | |
1028 | $(this).toggleClass('collapsed'); | |
1029 | }); | |
1030 | }; | |
5ec182d9 CW |
1031 | |
1032 | /** | |
1033 | * Clientside currency formatting | |
1034 | * @param value | |
3bdb644f | 1035 | * @param format - currency representation of the number 1234.56 |
5ec182d9 | 1036 | * @return string |
3bdb644f | 1037 | * @see CRM_Core_Resources::addCoreResources |
5ec182d9 CW |
1038 | */ |
1039 | var currencyTemplate; | |
1040 | CRM.formatMoney = function(value, format) { | |
1041 | var decimal, separator, sign, i, j, result; | |
1042 | if (value === 'init' && format) { | |
1043 | currencyTemplate = format; | |
1044 | return; | |
1045 | } | |
1046 | format = format || currencyTemplate; | |
1047 | result = /1(.?)234(.?)56/.exec(format); | |
1048 | if (result === null) { | |
1049 | return 'Invalid format passed to CRM.formatMoney'; | |
1050 | } | |
1051 | separator = result[1]; | |
1052 | decimal = result[2]; | |
1053 | sign = (value < 0) ? '-' : ''; | |
1054 | //extracting the absolute value of the integer part of the number and converting to string | |
1055 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
1056 | j = ((j = i.length) > 3) ? j % 3 : 0; |
1057 | result = sign + (j ? i.substr(0, j) + separator : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + separator) + (2 ? decimal + Math.abs(value - i).toFixed(2).slice(2) : ''); | |
1058 | return format.replace(/1.*234.*56/, result); | |
1059 | }; | |
bba9b4f0 CW |
1060 | |
1061 | CRM.console = function(method, title, msg) { | |
1062 | if (window.console) { | |
1063 | method = $.isFunction(console[method]) ? method : 'log'; | |
1064 | if (msg === undefined) { | |
1065 | return console[method](title); | |
1066 | } else { | |
1067 | return console[method](title, msg); | |
1068 | } | |
1069 | } | |
1070 | } | |
4b513f23 | 1071 | })(jQuery, _); |