Commit | Line | Data |
---|---|---|
353ea873 | 1 | // https://civicrm.org/licensing |
2b3ddf6e | 2 | /* global CRM:true */ |
b7f93874 | 3 | var CRM = CRM || {}; |
4b513f23 CW |
4 | var cj = CRM.$ = jQuery; |
5 | CRM._ = _; | |
6a488035 TO |
6 | |
7 | /** | |
8 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
9 | * | |
353ea873 CW |
10 | * @param text string for translating |
11 | * @param params object key:value of additional parameters | |
6a488035 | 12 | * |
353ea873 | 13 | * @return string |
6a488035 TO |
14 | */ |
15 | function ts(text, params) { | |
7553cf23 | 16 | "use strict"; |
3f3bba82 TO |
17 | var d = (params && params.domain) ? ('strings::' + params.domain) : null; |
18 | if (d && CRM[d] && CRM[d][text]) { | |
19 | text = CRM[d][text]; | |
20 | } | |
0d75c29c CW |
21 | else if (CRM.strings[text]) { |
22 | text = CRM.strings[text]; | |
3f3bba82 | 23 | } |
2788147f | 24 | if (typeof(params) === 'object') { |
6a488035 | 25 | for (var i in params) { |
32155ad6 | 26 | if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') { |
2788147f | 27 | // sprintf emulation: escape % characters in the replacements to avoid conflicts |
32155ad6 | 28 | text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-')); |
2788147f | 29 | } |
6a488035 TO |
30 | } |
31 | return text.replace(/%-crmescaped-/g, '%'); | |
32 | } | |
33 | return text; | |
34 | } | |
35 | ||
2b3ddf6e CW |
36 | // Legacy code - ignore warnings |
37 | /* jshint ignore:start */ | |
38 | ||
6a488035 TO |
39 | /** |
40 | * This function is called by default at the bottom of template files which have forms that have | |
41 | * conditionally displayed/hidden sections and elements. The PHP is responsible for generating | |
42 | * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to | |
43 | * this function. | |
44 | * | |
353ea873 | 45 | * @deprecated |
6a488035 TO |
46 | * @param showBlocks Array of element Id's to be displayed |
47 | * @param hideBlocks Array of element Id's to be hidden | |
48 | * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...) | |
6a488035 | 49 | */ |
0f5816a6 | 50 | function on_load_init_blocks(showBlocks, hideBlocks, elementType) { |
2b3ddf6e | 51 | if (elementType == null) { |
f54254d8 | 52 | elementType = 'block'; |
0f5816a6 KJ |
53 | } |
54 | ||
f54254d8 TO |
55 | var myElement, i; |
56 | ||
0f5816a6 | 57 | /* This loop is used to display the blocks whose IDs are present within the showBlocks array */ |
f54254d8 TO |
58 | for (i = 0; i < showBlocks.length; i++) { |
59 | myElement = document.getElementById(showBlocks[i]); | |
0f5816a6 | 60 | /* getElementById returns null if element id doesn't exist in the document */ |
2b3ddf6e | 61 | if (myElement != null) { |
0f5816a6 | 62 | myElement.style.display = elementType; |
6a488035 | 63 | } |
0f5816a6 KJ |
64 | else { |
65 | alert('showBlocks array item not in .tpl = ' + showBlocks[i]); | |
66 | } | |
67 | } | |
6a488035 | 68 | |
0f5816a6 | 69 | /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */ |
f54254d8 TO |
70 | for (i = 0; i < hideBlocks.length; i++) { |
71 | myElement = document.getElementById(hideBlocks[i]); | |
0f5816a6 | 72 | /* getElementById returns null if element id doesn't exist in the document */ |
2b3ddf6e | 73 | if (myElement != null) { |
0f5816a6 KJ |
74 | myElement.style.display = 'none'; |
75 | } | |
76 | else { | |
77 | alert('showBlocks array item not in .tpl = ' + hideBlocks[i]); | |
6a488035 | 78 | } |
0f5816a6 | 79 | } |
6a488035 TO |
80 | } |
81 | ||
82 | /** | |
83 | * This function is called when we need to show or hide a related form element (target_element) | |
84 | * based on the value (trigger_value) of another form field (trigger_field). | |
85 | * | |
353ea873 | 86 | * @deprecated |
6a488035 TO |
87 | * @param trigger_field_id HTML id of field whose onchange is the trigger |
88 | * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field | |
89 | * @param target_element_id HTML id of element to be shown or hidden | |
90 | * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row') | |
91 | * @param field_type Type of element radio/select | |
92 | * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match | |
0f5816a6 KJ |
93 | */ |
94 | function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) { | |
f54254d8 | 95 | var target, j; |
0f5816a6 KJ |
96 | |
97 | if (field_type == 'select') { | |
98 | var trigger = trigger_value.split("|"); | |
24cc4545 | 99 | var selectedOptionValue = cj('#' + trigger_field_id).val(); |
0f5816a6 | 100 | |
f54254d8 TO |
101 | target = target_element_id.split("|"); |
102 | for (j = 0; j < target.length; j++) { | |
0f5816a6 KJ |
103 | if (invert) { |
104 | cj('#' + target[j]).show(); | |
105 | } | |
106 | else { | |
107 | cj('#' + target[j]).hide(); | |
108 | } | |
109 | for (var i = 0; i < trigger.length; i++) { | |
110 | if (selectedOptionValue == trigger[i]) { | |
111 | if (invert) { | |
112 | cj('#' + target[j]).hide(); | |
113 | } | |
114 | else { | |
115 | cj('#' + target[j]).show(); | |
116 | } | |
6a488035 | 117 | } |
0f5816a6 KJ |
118 | } |
119 | } | |
6a488035 | 120 | |
0f5816a6 KJ |
121 | } |
122 | else { | |
123 | if (field_type == 'radio') { | |
f54254d8 TO |
124 | target = target_element_id.split("|"); |
125 | for (j = 0; j < target.length; j++) { | |
5fb3af09 | 126 | if (cj('[name="' + trigger_field_id + '"]:first').is(':checked')) { |
0f5816a6 KJ |
127 | if (invert) { |
128 | cj('#' + target[j]).hide(); | |
129 | } | |
130 | else { | |
131 | cj('#' + target[j]).show(); | |
132 | } | |
6a488035 | 133 | } |
0f5816a6 KJ |
134 | else { |
135 | if (invert) { | |
136 | cj('#' + target[j]).show(); | |
137 | } | |
138 | else { | |
139 | cj('#' + target[j]).hide(); | |
140 | } | |
141 | } | |
142 | } | |
6a488035 | 143 | } |
0f5816a6 | 144 | } |
6a488035 TO |
145 | } |
146 | ||
6a488035 TO |
147 | /** |
148 | * Function to change button text and disable one it is clicked | |
353ea873 | 149 | * @deprecated |
6a488035 TO |
150 | * @param obj object - the button clicked |
151 | * @param formID string - the id of the form being submitted | |
152 | * @param string procText - button text after user clicks it | |
353ea873 | 153 | * @return bool |
6a488035 | 154 | */ |
0f5816a6 | 155 | var submitcount = 0; |
c6edd786 | 156 | /* Changes button label on submit, and disables button after submit for newer browsers. |
157 | Puts up alert for older browsers. */ | |
0f5816a6 KJ |
158 | function submitOnce(obj, formId, procText) { |
159 | // if named button clicked, change text | |
160 | if (obj.value != null) { | |
161 | obj.value = procText + " ..."; | |
162 | } | |
47ea29e3 | 163 | cj(obj).closest('form').attr('data-warn-changes', 'false'); |
0f5816a6 KJ |
164 | if (document.getElementById) { // disable submit button for newer browsers |
165 | obj.disabled = true; | |
166 | document.getElementById(formId).submit(); | |
167 | return true; | |
168 | } | |
169 | else { // for older browsers | |
170 | if (submitcount == 0) { | |
171 | submitcount++; | |
172 | return true; | |
173 | } | |
174 | else { | |
175 | alert("Your request is currently being processed ... Please wait."); | |
176 | return false; | |
6a488035 | 177 | } |
0f5816a6 | 178 | } |
6a488035 | 179 | } |
6a488035 | 180 | |
6a488035 TO |
181 | /** |
182 | * Function to show / hide the row in optionFields | |
353ea873 CW |
183 | * @deprecated |
184 | * @param index string, element whose innerHTML is to hide else will show the hidden row. | |
6a488035 | 185 | */ |
0f5816a6 KJ |
186 | function showHideRow(index) { |
187 | if (index) { | |
188 | cj('tr#optionField_' + index).hide(); | |
189 | if (cj('table#optionField tr:hidden:first').length) { | |
190 | cj('div#optionFieldLink').show(); | |
6a488035 | 191 | } |
0f5816a6 KJ |
192 | } |
193 | else { | |
194 | cj('table#optionField tr:hidden:first').show(); | |
195 | if (!cj('table#optionField tr:hidden:last').length) { | |
196 | cj('div#optionFieldLink').hide(); | |
197 | } | |
198 | } | |
199 | return false; | |
6a488035 TO |
200 | } |
201 | ||
2b3ddf6e CW |
202 | /* jshint ignore:end */ |
203 | ||
475e9f44 | 204 | CRM.utils = CRM.utils || {}; |
6a488035 | 205 | CRM.strings = CRM.strings || {}; |
6a488035 | 206 | |
4b513f23 | 207 | (function ($, _, undefined) { |
7553cf23 | 208 | "use strict"; |
0d75c29c | 209 | /* jshint validthis: true */ |
d664f648 | 210 | |
3f586963 CW |
211 | // Theme classes for unattached elements |
212 | $.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container'; | |
213 | ||
47f21f3a CW |
214 | // https://github.com/ivaynberg/select2/pull/2090 |
215 | $.fn.select2.defaults.width = 'resolve'; | |
216 | ||
e20523a8 CW |
217 | // Workaround for https://github.com/ivaynberg/select2/issues/1246 |
218 | $.ui.dialog.prototype._allowInteraction = function(e) { | |
a7c4a5a8 | 219 | return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop, .cke_dialog').length; |
e20523a8 CW |
220 | }; |
221 | ||
9f8862e1 CW |
222 | // Implements jQuery hook.prop |
223 | $.propHooks.disabled = { | |
224 | set: function (el, value, name) { | |
225 | // Sync button enabled status with wrapper css | |
226 | if ($(el).is('span.crm-button > input.crm-form-submit')) { | |
227 | $(el).parent().toggleClass('crm-button-disabled', !!value); | |
228 | } | |
229 | // Sync button enabled status with dialog button | |
230 | if ($(el).is('.ui-dialog input.crm-form-submit')) { | |
9bce560a CW |
231 | $(el).closest('.ui-dialog').find('.ui-dialog-buttonset button[data-identifier='+ $(el).attr('name') +']').prop('disabled', value); |
232 | } | |
233 | if ($(el).is('.crm-form-date-wrapper .crm-hidden-date')) { | |
234 | $(el).siblings().prop('disabled', value); | |
9f8862e1 CW |
235 | } |
236 | } | |
237 | }; | |
238 | ||
475e9f44 CW |
239 | /** |
240 | * Populate a select list, overwriting the existing options except for the placeholder. | |
581c7be2 | 241 | * @param select jquery selector - 1 or more select elements |
475e9f44 | 242 | * @param options array in format returned by api.getoptions |
b7ceb253 CW |
243 | * @param placeholder string|bool - new placeholder or false (default) to keep the old one |
244 | * @param value string|array - will silently update the element with new value without triggering change | |
475e9f44 | 245 | */ |
b7ceb253 | 246 | CRM.utils.setOptions = function(select, options, placeholder, value) { |
581c7be2 | 247 | $(select).each(function() { |
475e9f44 CW |
248 | var |
249 | $elect = $(this), | |
b7ceb253 CW |
250 | val = value || $elect.val() || [], |
251 | opts = placeholder || placeholder === '' ? '' : '[value!=""]'; | |
23e8a31b | 252 | $elect.find('option' + opts).remove(); |
b7ceb253 | 253 | var newOptions = CRM.utils.renderOptions(options, val); |
1d07e7ab | 254 | if (typeof placeholder === 'string') { |
581c7be2 CW |
255 | if ($elect.is('[multiple]')) { |
256 | select.attr('placeholder', placeholder); | |
1d07e7ab CW |
257 | } else { |
258 | newOptions = '<option value="">' + placeholder + '</option>' + newOptions; | |
259 | } | |
260 | } | |
261 | $elect.append(newOptions); | |
b7ceb253 CW |
262 | if (!value) { |
263 | $elect.trigger('crmOptionsUpdated', $.extend({}, options)).trigger('change'); | |
264 | } | |
475e9f44 CW |
265 | }); |
266 | }; | |
267 | ||
b7ceb253 CW |
268 | /** |
269 | * Render an option list | |
8decea37 CW |
270 | * @param options {array} |
271 | * @param val {string} default value | |
272 | * @param escapeHtml {bool} | |
b7ceb253 CW |
273 | * @return string |
274 | */ | |
8decea37 CW |
275 | CRM.utils.renderOptions = function(options, val, escapeHtml) { |
276 | var rendered = '', | |
277 | esc = escapeHtml === false ? _.identity : _.escape; | |
b7ceb253 CW |
278 | if (!$.isArray(val)) { |
279 | val = [val]; | |
280 | } | |
281 | _.each(options, function(option) { | |
282 | if (option.children) { | |
8decea37 | 283 | rendered += '<optgroup label="' + esc(option.value) + '">' + |
ebd7b7da | 284 | CRM.utils.renderOptions(option.children, val) + |
b7ceb253 CW |
285 | '</optgroup>'; |
286 | } else { | |
287 | var selected = ($.inArray('' + option.key, val) > -1) ? 'selected="selected"' : ''; | |
8decea37 | 288 | rendered += '<option value="' + esc(option.key) + '"' + selected + '>' + esc(option.value) + '</option>'; |
b7ceb253 | 289 | } |
475e9f44 | 290 | }); |
b7ceb253 | 291 | return rendered; |
475e9f44 CW |
292 | }; |
293 | ||
1d07e7ab CW |
294 | function chainSelect() { |
295 | var $form = $(this).closest('form'), | |
296 | $target = $('select[data-name="' + $(this).data('target') + '"]', $form), | |
297 | data = $target.data(), | |
298 | val = $(this).val(); | |
299 | $target.prop('disabled', true); | |
300 | if ($target.is('select.crm-chain-select-control')) { | |
301 | $('select[data-name="' + $target.data('target') + '"]', $form).prop('disabled', true).blur(); | |
302 | } | |
303 | if (!(val && val.length)) { | |
304 | CRM.utils.setOptions($target.blur(), [], data.emptyPrompt); | |
305 | } else { | |
306 | $target.addClass('loading'); | |
307 | $.getJSON(CRM.url(data.callback), {_value: val}, function(vals) { | |
308 | $target.prop('disabled', false).removeClass('loading'); | |
309 | CRM.utils.setOptions($target, vals || [], (vals && vals.length ? data.selectPrompt : data.nonePrompt)); | |
310 | }); | |
311 | } | |
312 | } | |
313 | ||
3e201321 | 314 | /** |
315 | * Compare Form Input values against cached initial value. | |
88e9380e CW |
316 | * |
317 | * @return {Boolean} true if changes have been made. | |
3e201321 | 318 | */ |
319 | CRM.utils.initialValueChanged = function(el) { | |
88e9380e | 320 | var isDirty = false; |
18469bf2 | 321 | $(':input:visible, .select2-container:visible+:input.select2-offscreen', el).not('[type=submit], [type=button], .crm-action-menu').each(function () { |
88e9380e | 322 | var initialValue = $(this).data('crm-initial-value'); |
3c0624e2 | 323 | // skip change of value for submit buttons |
bf7a4fbc | 324 | if (initialValue !== undefined && !_.isEqual(initialValue, $(this).val())) { |
88e9380e CW |
325 | isDirty = true; |
326 | } | |
3e201321 | 327 | }); |
328 | return isDirty; | |
8d36b801 | 329 | }; |
b1fc510d CW |
330 | |
331 | /** | |
332 | * This provides defaults for ui.dialog which either need to be calculated or are different from global defaults | |
333 | * | |
334 | * @param settings | |
335 | * @returns {*} | |
336 | */ | |
337 | CRM.utils.adjustDialogDefaults = function(settings) { | |
338 | settings = $.extend({width: '65%', height: '65%', modal: true}, settings || {}); | |
339 | // Support relative height | |
340 | if (typeof settings.height === 'string' && settings.height.indexOf('%') > 0) { | |
341 | settings.height = parseInt($(window).height() * (parseFloat(settings.height)/100), 10); | |
342 | } | |
343 | // Responsive adjustment - increase percent width on small screens | |
344 | if (typeof settings.width === 'string' && settings.width.indexOf('%') > 0) { | |
345 | var screenWidth = $(window).width(), | |
346 | percentage = parseInt(settings.width.replace('%', ''), 10), | |
347 | gap = 100-percentage; | |
348 | if (screenWidth < 701) { | |
349 | settings.width = '100%'; | |
350 | } | |
351 | else if (screenWidth < 1400) { | |
352 | settings.width = '' + parseInt(percentage+gap-((screenWidth - 700)/7*(gap)/100), 10) + '%'; | |
353 | } | |
354 | } | |
355 | return settings; | |
356 | }; | |
357 | ||
ba4fb2b2 | 358 | /** |
353ea873 | 359 | * Wrapper for select2 initialization function; supplies defaults |
a88cf11a | 360 | * @param options object |
ba4fb2b2 | 361 | */ |
a88cf11a CW |
362 | $.fn.crmSelect2 = function(options) { |
363 | return $(this).each(function () { | |
364 | var | |
365 | $el = $(this), | |
a243158e | 366 | settings = {allowClear: !$el.hasClass('required')}; |
a88cf11a CW |
367 | // quickform doesn't support optgroups so here's a hack :( |
368 | $('option[value^=crm_optgroup]', this).each(function () { | |
369 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
370 | $(this).remove(); | |
371 | }); | |
47358d92 | 372 | |
373 | // quickform does not support disabled option, so yet another hack to | |
374 | // add disabled property for option values | |
711da13f | 375 | $('option[value^=crm_disabled_opt]', this).attr('disabled', 'disabled'); |
27a6b676 | 376 | |
a88cf11a CW |
377 | // Defaults for single-selects |
378 | if ($el.is('select:not([multiple])')) { | |
a243158e | 379 | settings.minimumResultsForSearch = 10; |
a88cf11a | 380 | if ($('option:first', this).val() === '') { |
a243158e | 381 | settings.placeholderOption = 'first'; |
a88cf11a | 382 | } |
ba4fb2b2 | 383 | } |
a243158e CW |
384 | $.extend(settings, $el.data('select-params') || {}, options || {}); |
385 | if (settings.ajax) { | |
386 | $el.addClass('crm-ajax-select'); | |
387 | } | |
388 | $el.select2(settings); | |
a88cf11a CW |
389 | }); |
390 | }; | |
391 | ||
392 | /** | |
353ea873 | 393 | * @see CRM_Core_Form::addEntityRef for docs |
a88cf11a CW |
394 | * @param options object |
395 | */ | |
396 | $.fn.crmEntityRef = function(options) { | |
397 | options = options || {}; | |
398 | options.select = options.select || {}; | |
399 | return $(this).each(function() { | |
400 | var | |
4c993609 | 401 | $el = $(this).off('.crmEntity'), |
a88cf11a CW |
402 | entity = options.entity || $el.data('api-entity') || 'contact', |
403 | selectParams = {}; | |
404 | $el.data('api-entity', entity); | |
405 | $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); | |
406 | $el.data('api-params', $.extend({}, $el.data('api-params') || {}, options.api)); | |
a4799f04 | 407 | $el.data('create-links', options.create || $el.data('create-links')); |
b7ceb253 | 408 | $el.addClass('crm-form-entityref crm-' + entity.toLowerCase() + '-ref'); |
ba4fb2b2 | 409 | var settings = { |
b7ceb253 | 410 | // Use select2 ajax helper instead of CRM.api3 because it provides more value |
ba4fb2b2 CW |
411 | ajax: { |
412 | url: CRM.url('civicrm/ajax/rest'), | |
413 | data: function (input, page_num) { | |
b7ceb253 | 414 | var params = getEntityRefApiParams($el); |
ba4fb2b2 CW |
415 | params.input = input; |
416 | params.page_num = page_num; | |
417 | return { | |
418 | entity: $el.data('api-entity'), | |
419 | action: 'getlist', | |
420 | json: JSON.stringify(params) | |
421 | }; | |
422 | }, | |
423 | results: function(data) { | |
424 | return {more: data.more_results, results: data.values || []}; | |
425 | } | |
426 | }, | |
a88cf11a | 427 | minimumInputLength: 1, |
3c0b6a40 | 428 | formatResult: CRM.utils.formatSelect2Result, |
ba4fb2b2 CW |
429 | formatSelection: function(row) { |
430 | return row.label; | |
431 | }, | |
432 | escapeMarkup: function (m) {return m;}, | |
a88cf11a CW |
433 | initSelection: function($el, callback) { |
434 | var | |
435 | multiple = !!$el.data('select-params').multiple, | |
436 | val = $el.val(), | |
437 | stored = $el.data('entity-value') || []; | |
438 | if (val === '') { | |
439 | return; | |
440 | } | |
441 | // If we already have this data, just return it | |
442 | if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { | |
443 | callback(multiple ? stored : stored[0]); | |
444 | } else { | |
78b203e5 | 445 | var params = $.extend({}, $el.data('api-params') || {}, {id: val}); |
a88cf11a | 446 | CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { |
d2b4810b CW |
447 | callback(multiple ? result.values : result.values[0]); |
448 | // Trigger change (store data to avoid an infinite loop of lookups) | |
449 | $el.data('entity-value', result.values).trigger('change'); | |
a88cf11a CW |
450 | }); |
451 | } | |
ba4fb2b2 CW |
452 | } |
453 | }; | |
4c993609 | 454 | // Create new items inline - works for tags |
b7ceb253 | 455 | if ($el.data('create-links') && entity.toLowerCase() === 'tag') { |
4c993609 CW |
456 | selectParams.createSearchChoice = function(term, data) { |
457 | if (!_.findKey(data, {label: term})) { | |
458 | return {id: "0", term: term, label: term + ' (' + ts('new tag') + ')'}; | |
459 | } | |
460 | }; | |
e4f4dc22 | 461 | selectParams.tokenSeparators = [',']; |
4c993609 | 462 | selectParams.createSearchChoicePosition = 'bottom'; |
05b21c58 | 463 | $el.on('select2-selecting.crmEntity', function(e) { |
4c993609 | 464 | if (e.val === "0") { |
a2c28a94 | 465 | // Create a new term |
4c993609 CW |
466 | e.object.label = e.object.term; |
467 | CRM.api3(entity, 'create', $.extend({name: e.object.term}, $el.data('api-params').params || {})) | |
468 | .done(function(created) { | |
469 | var | |
4c993609 CW |
470 | val = $el.select2('val'), |
471 | data = $el.select2('data'), | |
472 | item = {id: created.id, label: e.object.term}; | |
473 | if (val === "0") { | |
e4f4dc22 | 474 | $el.select2('data', item, true); |
4c993609 CW |
475 | } |
476 | else if ($.isArray(val) && $.inArray("0", val) > -1) { | |
477 | _.remove(data, {id: "0"}); | |
478 | data.push(item); | |
e4f4dc22 | 479 | $el.select2('data', data, true); |
4c993609 CW |
480 | } |
481 | }); | |
482 | } | |
483 | }); | |
b7ceb253 CW |
484 | } |
485 | else { | |
a88cf11a CW |
486 | selectParams.formatInputTooShort = function() { |
487 | var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this); | |
cdc8d05f | 488 | txt += renderEntityRefFilters($el) + renderEntityRefCreateLinks($el); |
ba4fb2b2 CW |
489 | return txt; |
490 | }; | |
a88cf11a | 491 | selectParams.formatNoMatches = function() { |
ba4fb2b2 | 492 | var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches; |
cdc8d05f | 493 | txt += renderEntityRefFilters($el) + renderEntityRefCreateLinks($el); |
b7ceb253 | 494 | return txt; |
ba4fb2b2 | 495 | }; |
4c993609 | 496 | $el.on('select2-open.crmEntity', function() { |
ba4fb2b2 | 497 | var $el = $(this); |
b7ceb253 CW |
498 | loadEntityRefFilterOptions($el); |
499 | $('#select2-drop') | |
500 | .off('.crmEntity') | |
501 | .on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
502 | $el.select2('close'); | |
503 | CRM.loadForm($(this).attr('href'), { | |
504 | dialog: {width: 500, height: 'auto'} | |
505 | }).on('crmFormSuccess', function(e, data) { | |
506 | if (data.status === 'success' && data.id) { | |
507 | CRM.status(ts('%1 Created', {1: data.label})); | |
508 | if ($el.select2('container').hasClass('select2-container-multi')) { | |
509 | var selection = $el.select2('data'); | |
510 | selection.push(data); | |
511 | $el.select2('data', selection, true); | |
512 | } else { | |
513 | $el.select2('data', data, true); | |
514 | } | |
c92f6436 | 515 | } |
b7ceb253 CW |
516 | }); |
517 | return false; | |
518 | }) | |
519 | .on('change.crmEntity', 'select.crm-entityref-filter-value', function() { | |
520 | var filter = $el.data('user-filter') || {}; | |
521 | filter.value = $(this).val(); | |
522 | $(this).toggleClass('active', !!filter.value); | |
523 | $el.data('user-filter', filter); | |
524 | if (filter.value) { | |
525 | // Once a filter has been chosen, rerender create links and refocus the search box | |
526 | $el.select2('close'); | |
527 | $el.select2('open'); | |
ba4fb2b2 | 528 | } |
b7ceb253 CW |
529 | }) |
530 | .on('change.crmEntity', 'select.crm-entityref-filter-key', function() { | |
531 | var filter = $el.data('user-filter') || {}; | |
532 | filter.key = $(this).val(); | |
533 | $(this).toggleClass('active', !!filter.key); | |
534 | $el.data('user-filter', filter); | |
535 | loadEntityRefFilterOptions($el); | |
ba4fb2b2 | 536 | }); |
ba4fb2b2 CW |
537 | }); |
538 | } | |
05b21c58 | 539 | $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)); |
a88cf11a | 540 | }); |
ba4fb2b2 CW |
541 | }; |
542 | ||
b7ceb253 CW |
543 | /** |
544 | * Combine api-params with user-filter | |
545 | * @param $el | |
546 | * @returns {*} | |
547 | */ | |
548 | function getEntityRefApiParams($el) { | |
549 | var | |
550 | params = $.extend({params: {}}, $el.data('api-params') || {}), | |
551 | // Prevent original data from being modified - $.extend and _.clone don't cut it, they pass nested objects by reference! | |
552 | combined = _.cloneDeep(params), | |
553 | filter = $.extend({}, $el.data('user-filter') || {}); | |
554 | if (filter.key && filter.value) { | |
555 | // Special case for contact type/sub-type combo | |
bee6039a CW |
556 | if (filter.key === 'contact_type' && (filter.value.indexOf('__') > 0)) { |
557 | combined.params.contact_type = filter.value.split('__')[0]; | |
558 | combined.params.contact_sub_type = filter.value.split('__')[1]; | |
b7ceb253 | 559 | } else { |
cdc8d05f CW |
560 | // Allow json-encoded api filters e.g. {"BETWEEN":[123,456]} |
561 | combined.params[filter.key] = filter.value.charAt(0) === '{' ? $.parseJSON(filter.value) : filter.value; | |
b7ceb253 CW |
562 | } |
563 | } | |
564 | return combined; | |
565 | } | |
566 | ||
9bce560a CW |
567 | function copyAttributes($source, $target, attributes) { |
568 | _.each(attributes, function(name) { | |
569 | if ($source.attr(name)) { | |
570 | $target.attr(name, $source.attr(name)); | |
571 | } | |
572 | }); | |
573 | } | |
574 | ||
575 | $.fn.crmDatepicker = function(options) { | |
576 | return $(this).each(function() { | |
577 | if ($(this).is('.crm-form-date-wrapper .crm-hidden-date')) { | |
578 | // Already initialized | |
579 | return; | |
580 | } | |
581 | var | |
582 | $dataField = $(this).wrap('<span class="crm-form-date-wrapper" />'), | |
583 | settings = $.extend({}, $dataField.data('datepicker') || {}, options || {}), | |
584 | $dateField = $(), | |
585 | $timeField = $(), | |
586 | $clearLink = $(); | |
587 | ||
588 | if (settings.allowClear !== undefined ? settings.allowClear : !$dataField.hasClass('required')) { | |
589 | $clearLink = $('<a class="crm-hover-button crm-clear-link" title="'+ ts('Clear') +'"><span class="icon ui-icon-close"></span></a>') | |
590 | .insertAfter($dataField); | |
591 | } | |
592 | if (settings.time !== false) { | |
593 | $timeField = $('<input>').insertAfter($dataField); | |
594 | copyAttributes($dataField, $timeField, ['class', 'disabled']); | |
595 | $timeField | |
596 | .addClass('crm-form-text crm-form-time') | |
597 | .attr('placeholder', $dataField.attr('time-placeholder') === undefined ? ts('Time') : $dataField.attr('time-placeholder')) | |
598 | .change(updateDataField) | |
599 | .timeEntry({ | |
600 | spinnerImage: '', | |
0d05e8a1 | 601 | show24Hours: settings.time === true || settings.time === undefined ? CRM.config.timeIs24Hr : settings.time == '24' |
9bce560a CW |
602 | }); |
603 | } | |
604 | if (settings.date !== false) { | |
605 | $dateField = $('<input>').insertAfter($dataField); | |
606 | copyAttributes($dataField, $dateField, ['placeholder', 'style', 'class', 'disabled']); | |
607 | $dateField.addClass('crm-form-text crm-form-date'); | |
608 | settings.dateFormat = settings.dateFormat || CRM.config.dateInputFormat; | |
609 | settings.changeMonth = _.includes('m', settings.dateFormat); | |
610 | settings.changeYear = _.includes('y', settings.dateFormat); | |
611 | $dateField.datepicker(settings).change(updateDataField); | |
612 | } | |
613 | function updateInputFields(e, context) { | |
ac5009c1 CW |
614 | var val = $dataField.val(), |
615 | time = null; | |
9bce560a | 616 | if (context !== 'userInput' && context !== 'crmClear') { |
ac5009c1 CW |
617 | if ($dateField.length) { |
618 | $dateField.datepicker('setDate', _.includes(val, '-') ? $.datepicker.parseDate('yy-mm-dd', val) : null); | |
619 | } | |
620 | if ($timeField.length) { | |
621 | if (val.length === 8) { | |
622 | time = val; | |
623 | } else if (val.length === 19) { | |
624 | time = val.split(' ')[1]; | |
9bce560a | 625 | } |
ac5009c1 | 626 | $timeField.timeEntry('setTime', time); |
9bce560a CW |
627 | } |
628 | } | |
ac5009c1 | 629 | $clearLink.css('visibility', val ? 'visible' : 'hidden'); |
9bce560a CW |
630 | } |
631 | function updateDataField(e, context) { | |
632 | if (context !== 'crmClear') { | |
633 | var val = ''; | |
634 | if ($dateField.val()) { | |
635 | val = $.datepicker.formatDate('yy-mm-dd', $dateField.datepicker('getDate')); | |
636 | } | |
637 | if ($timeField.val()) { | |
ac5009c1 | 638 | val += (val ? ' ' : '') + $timeField.timeEntry('getTime').toTimeString().substr(0, 8); |
9bce560a CW |
639 | } |
640 | $dataField.val(val).trigger('change', ['userInput']); | |
641 | } | |
642 | } | |
643 | $dataField.hide().addClass('crm-hidden-date').on('change', updateInputFields); | |
644 | updateInputFields(); | |
645 | }); | |
646 | }; | |
647 | ||
3c0b6a40 | 648 | CRM.utils.formatSelect2Result = function (row) { |
88881f79 | 649 | var markup = '<div class="crm-select2-row">'; |
ff88d165 | 650 | if (row.image !== undefined) { |
88881f79 | 651 | markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>'; |
ff88d165 | 652 | } |
54bee7df | 653 | else if (row.icon_class) { |
88881f79 | 654 | markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>'; |
54bee7df | 655 | } |
3c0b6a40 | 656 | markup += '<div><div class="crm-select2-row-label '+(row.label_class || '')+'">' + row.label + '</div>'; |
88881f79 CW |
657 | markup += '<div class="crm-select2-row-description">'; |
658 | $.each(row.description || [], function(k, text) { | |
659 | markup += '<p>' + text + '</p>'; | |
660 | }); | |
661 | markup += '</div></div></div>'; | |
ff88d165 | 662 | return markup; |
3c0b6a40 | 663 | }; |
a4799f04 | 664 | |
b7ceb253 | 665 | function renderEntityRefCreateLinks($el) { |
a4799f04 CW |
666 | var |
667 | createLinks = $el.data('create-links'), | |
b7ceb253 CW |
668 | params = getEntityRefApiParams($el).params, |
669 | markup = '<div class="crm-entityref-links">'; | |
670 | if (!createLinks || $el.data('api-entity').toLowerCase() !== 'contact') { | |
671 | return ''; | |
672 | } | |
a4799f04 | 673 | if (createLinks === true) { |
b7ceb253 | 674 | createLinks = params.contact_type ? _.where(CRM.config.entityRef.contactCreate, {type: params.contact_type}) : CRM.config.entityRef.contactCreate; |
a4799f04 | 675 | } |
a4799f04 | 676 | _.each(createLinks, function(link) { |
79ae07d9 | 677 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; |
a4799f04 CW |
678 | if (link.type) { |
679 | markup += '<span class="icon ' + link.type + '-profile-icon"></span> '; | |
79ae07d9 CW |
680 | } |
681 | markup += link.label + '</a>'; | |
682 | }); | |
b7ceb253 CW |
683 | markup += '</div>'; |
684 | return markup; | |
685 | } | |
686 | ||
687 | function getEntityRefFilters($el) { | |
688 | var | |
689 | entity = $el.data('api-entity').toLowerCase(), | |
690 | filters = $.extend([], CRM.config.entityRef.filters[entity] || []), | |
691 | filter = $el.data('user-filter') || {}, | |
692 | params = $.extend({params: {}}, $el.data('api-params') || {}).params, | |
693 | result = []; | |
694 | $.each(filters, function() { | |
695 | if (typeof params[this.key] === 'undefined') { | |
696 | result.push(this); | |
697 | } | |
698 | else if (this.key == 'contact_type' && typeof params.contact_sub_type === 'undefined') { | |
699 | this.options = _.remove(this.options, function(option) { | |
bee6039a | 700 | return option.key.indexOf(params.contact_type + '__') === 0; |
b7ceb253 CW |
701 | }); |
702 | result.push(this); | |
703 | } | |
704 | }); | |
705 | return result; | |
706 | } | |
707 | ||
708 | function renderEntityRefFilters($el) { | |
709 | var | |
710 | filters = getEntityRefFilters($el), | |
711 | filter = $el.data('user-filter') || {}, | |
712 | filterSpec = filter.key ? _.find(filters, {key: filter.key}) : null; | |
713 | if (!filters.length) { | |
714 | return ''; | |
715 | } | |
716 | var markup = '<div class="crm-entityref-filters">' + | |
717 | '<select class="crm-entityref-filter-key' + (filter.key ? ' active' : '') + '">' + | |
718 | '<option value="">' + ts('Refine search...') + '</option>' + | |
719 | CRM.utils.renderOptions(filters, filter.key) + | |
720 | '</select> ' + | |
721 | '<select class="crm-entityref-filter-value' + (filter.key ? ' active"' : '"') + (filter.key ? '' : ' style="display:none;"') + '>' + | |
722 | '<option value="">' + ts('- select -') + '</option>'; | |
723 | if (filterSpec && filterSpec.options) { | |
724 | markup += CRM.utils.renderOptions(filterSpec.options, filter.value); | |
725 | } | |
726 | markup += '</select></div>'; | |
79ae07d9 | 727 | return markup; |
a4799f04 | 728 | } |
ff88d165 | 729 | |
b7ceb253 CW |
730 | /** |
731 | * Fetch options for a filter (via ajax if necessary) and populate the appropriate select list | |
732 | * @param $el | |
733 | */ | |
734 | function loadEntityRefFilterOptions($el) { | |
735 | var | |
736 | filters = getEntityRefFilters($el), | |
737 | filter = $el.data('user-filter') || {}, | |
738 | filterSpec = filter.key ? _.find(filters, {key: filter.key}) : null, | |
739 | $valField = $('.crm-entityref-filter-value', '#select2-drop'); | |
740 | if (filterSpec) { | |
741 | $valField.show().val(''); | |
742 | if (filterSpec.options) { | |
743 | CRM.utils.setOptions($valField, filterSpec.options, false, filter.value); | |
744 | } else { | |
745 | $valField.prop('disabled', true); | |
bee6039a | 746 | CRM.api3(filterSpec.entity || $el.data('api-entity'), 'getoptions', {field: filter.key, context: 'search', sequential: 1}) |
b7ceb253 CW |
747 | .done(function(result) { |
748 | var entity = $el.data('api-entity').toLowerCase(), | |
749 | globalFilterSpec = _.find(CRM.config.entityRef.filters[entity], {key: filter.key}) || {}; | |
750 | // Store options globally so we don't have to look them up again | |
751 | globalFilterSpec.options = result.values; | |
752 | $valField.prop('disabled', false); | |
753 | CRM.utils.setOptions($valField, result.values); | |
754 | $valField.val(filter.value || ''); | |
755 | }); | |
756 | } | |
757 | } else { | |
758 | $valField.hide(); | |
759 | } | |
760 | } | |
761 | ||
1136a401 | 762 | //CRM-15598 - Override url validator method to allow relative url's (e.g. /index.htm) |
763 | $.validator.addMethod("url", function(value, element) { | |
764 | if (/^\//.test(value)) { | |
765 | // Relative url: prepend dummy path for validation. | |
766 | value = 'http://domain.tld' + value; | |
767 | } | |
768 | // From jQuery Validation Plugin v1.12.0 | |
769 | return this.optional(element) || /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value); | |
770 | }); | |
771 | ||
3d527838 CW |
772 | /** |
773 | * Wrapper for jQuery validate initialization function; supplies defaults | |
3d527838 CW |
774 | */ |
775 | $.fn.crmValidate = function(params) { | |
776 | return $(this).each(function () { | |
777 | var that = this, | |
778 | settings = $.extend({}, CRM.validate._defaults, CRM.validate.params); | |
779 | $(this).validate(settings); | |
780 | // Call any post-initialization callbacks | |
781 | if (CRM.validate.functions && CRM.validate.functions.length) { | |
782 | $.each(CRM.validate.functions, function(i, func) { | |
783 | func.call(that); | |
784 | }); | |
785 | } | |
786 | }); | |
b7ceb253 | 787 | }; |
3d527838 | 788 | |
f7b92fcd | 789 | // Initialize widgets |
eb90857a CW |
790 | $(document) |
791 | .on('crmLoad', function(e) { | |
792 | $('table.row-highlight', e.target) | |
793 | .off('.rowHighlight') | |
7e13d44e CW |
794 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function (e, data) { |
795 | var filter, $table = $(this).closest('table'); | |
eb90857a | 796 | if ($(this).hasClass('select-rows')) { |
7e13d44e CW |
797 | filter = $(this).prop('checked') ? ':not(:checked)' : ':checked'; |
798 | $('input.select-row' + filter, $table).prop('checked', $(this).prop('checked')).trigger('change', 'master-selected'); | |
eb90857a CW |
799 | } |
800 | else { | |
7e13d44e CW |
801 | $(this).closest('tr').toggleClass('crm-row-selected', $(this).prop('checked')); |
802 | if (data !== 'master-selected') { | |
803 | $('input.select-rows', $table).prop('checked', $(".select-row:not(':checked')", $table).length < 1); | |
804 | } | |
eb90857a | 805 | } |
eb90857a CW |
806 | }) |
807 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
82661158 JP |
808 | if ($("input:radio[name=radio_ts]").size() == 1) { |
809 | $("input:radio[name=radio_ts]").prop("checked", true); | |
810 | } | |
5f34e50b CW |
811 | $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); |
812 | $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); | |
1d07e7ab | 813 | $('select.crm-chain-select-control', e.target).off('.chainSelect').on('change.chainSelect', chainSelect); |
3e201321 | 814 | // Cache Form Input initial values |
88e9380e | 815 | $('form[data-warn-changes] :input', e.target).each(function() { |
329758bb | 816 | $(this).data('crm-initial-value', $(this).val()); |
3e201321 | 817 | }); |
eb90857a | 818 | }) |
eb90857a | 819 | .on('dialogopen', function(e) { |
f292709b CW |
820 | var $el = $(e.target); |
821 | // Modal dialogs should disable scrollbars | |
822 | if ($el.dialog('option', 'modal')) { | |
823 | $el.addClass('modal-dialog'); | |
eb90857a CW |
824 | $('body').css({overflow: 'hidden'}); |
825 | } | |
f292709b | 826 | // Add resize button |
a243158e | 827 | if ($el.parent().hasClass('crm-container') && $el.dialog('option', 'resizable')) { |
77a8d7f9 | 828 | $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 |
829 | $('.crm-dialog-titlebar-resize', $el.parent()).click(function(e) { |
830 | if ($el.data('origSize')) { | |
831 | $el.dialog('option', $el.data('origSize')); | |
832 | $el.data('origSize', null); | |
833 | } else { | |
28d510ab | 834 | var menuHeight = $('#civicrm-menu').outerHeight(); |
f292709b | 835 | $el.data('origSize', { |
28d510ab | 836 | position: {my: 'center', at: 'center center+' + (menuHeight / 2), of: window}, |
f292709b CW |
837 | width: $el.dialog('option', 'width'), |
838 | height: $el.dialog('option', 'height') | |
839 | }); | |
f2f191fe | 840 | $el.dialog('option', {width: '100%', height: ($(window).height() - menuHeight), position: {my: "top", at: "top+"+menuHeight, of: window}}); |
f292709b | 841 | } |
02cd9764 | 842 | $el.trigger('dialogresize'); |
f292709b CW |
843 | e.preventDefault(); |
844 | }); | |
845 | } | |
eb90857a CW |
846 | }) |
847 | .on('dialogclose', function(e) { | |
f292709b | 848 | // Restore scrollbars when closing modal |
5a6148a0 | 849 | if ($('.ui-dialog .modal-dialog:visible').not(e.target).length < 1) { |
eb90857a CW |
850 | $('body').css({overflow: ''}); |
851 | } | |
afc021d8 | 852 | }) |
853 | .on('submit', function(e) { | |
f582fc8f | 854 | // CRM-14353 - disable changes warn when submitting a form |
abb6e044 | 855 | $('[data-warn-changes]').attr('data-warn-changes', 'false'); |
afc021d8 | 856 | }) |
f582fc8f CW |
857 | ; |
858 | ||
859 | // CRM-14353 - Warn of unsaved changes for forms which have opted in | |
860 | window.onbeforeunload = function() { | |
18469bf2 | 861 | if (CRM.utils.initialValueChanged($('form[data-warn-changes=true]:visible'))) { |
f582fc8f CW |
862 | return ts('You have unsaved changes.'); |
863 | } | |
864 | }; | |
148c4e8d CW |
865 | |
866 | /** | |
867 | * Function to make multiselect boxes behave as fields in small screens | |
868 | */ | |
869 | function advmultiselectResize() { | |
870 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
871 | if (amswidth < 700) { | |
872 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
873 | } |
874 | else { | |
148c4e8d CW |
875 | $("form table.advmultiselect td").css('display', 'table-cell'); |
876 | } | |
877 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
878 | if (contactwidth < 600) { | |
879 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 880 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 881 | if (index > 1) { |
2b3ddf6e | 882 | if (index % 2 === 0) { |
148c4e8d CW |
883 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
884 | } | |
885 | var item = $(this); | |
886 | $(this).parent().next().append(item); | |
887 | } | |
888 | }); | |
0f5816a6 KJ |
889 | } |
890 | else { | |
148c4e8d | 891 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 892 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
893 | var nitem = $(this); |
894 | var parent = $(this).parent(); | |
895 | $(this).parent().prev().append(nitem); | |
2b3ddf6e | 896 | if (parent.children().size() === 0) { |
148c4e8d CW |
897 | parent.remove(); |
898 | } | |
899 | }); | |
900 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
901 | } | |
902 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 903 | |
148c4e8d CW |
904 | if (cformwidth < 720) { |
905 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
906 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
907 | if (cformwidth < 480) { | |
908 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
909 | } |
910 | else { | |
148c4e8d CW |
911 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
912 | } | |
0f5816a6 KJ |
913 | } |
914 | else { | |
148c4e8d CW |
915 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
916 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
917 | } | |
918 | } | |
0f5816a6 | 919 | |
148c4e8d | 920 | advmultiselectResize(); |
a2c28a94 | 921 | $(window).resize(advmultiselectResize); |
6a488035 | 922 | |
0f5816a6 | 923 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
924 | $(document) |
925 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
926 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
927 | $(this).addClass('crm-tooltip-active'); |
928 | var topDistance = e.pageY - $(window).scrollTop(); | |
2b3ddf6e | 929 | if (topDistance < 300 || topDistance < $(this).children('.crm-tooltip-wrapper').height()) { |
e24b17b9 CW |
930 | $(this).addClass('crm-tooltip-down'); |
931 | } | |
932 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
933 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
934 | $(this).children().children('.crm-tooltip') | |
935 | .html('<div class="crm-loading-element"></div>') | |
936 | .load(this.href); | |
937 | } | |
938 | }) | |
2c29c2ac RN |
939 | .on('mouseout', 'a.crm-summary-link', function () { |
940 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
941 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
942 | }) | |
2c29c2ac | 943 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
944 | }; |
945 | ||
b0ca6188 | 946 | var helpDisplay, helpPrevious; |
8e3272a1 | 947 | CRM.help = function (title, params, url) { |
55a93b02 | 948 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 949 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
950 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
951 | helpDisplay.close(); | |
b0ca6188 CW |
952 | return; |
953 | } | |
55a93b02 | 954 | helpDisplay.close(); |
b0ca6188 CW |
955 | } |
956 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
957 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
958 | params.type = 'page'; | |
b0ca6188 | 959 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 960 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
961 | { |
962 | data: params, | |
963 | dataType: 'html', | |
e24b17b9 | 964 | success: function (data) { |
6a488035 TO |
965 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
966 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
967 | }, | |
e24b17b9 | 968 | error: function () { |
6a488035 TO |
969 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
970 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
971 | } | |
972 | } | |
973 | ); | |
974 | }; | |
8960d9b9 | 975 | /** |
7442e8f6 | 976 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
8960d9b9 | 977 | */ |
1b2475e1 | 978 | CRM.status = function(options, deferred) { |
9a7ef94f | 979 | // 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 |
980 | if (typeof options === 'string') { |
981 | return CRM.status({start: options, success: options, error: options})[deferred === 'error' ? 'reject' : 'resolve'](); | |
8960d9b9 | 982 | } |
1b2475e1 CW |
983 | var opts = $.extend({ |
984 | start: ts('Saving...'), | |
9a7ef94f | 985 | success: ts('Saved'), |
47737104 CW |
986 | error: function(data) { |
987 | var msg = $.isPlainObject(data) && data.error_message; | |
988 | CRM.alert(msg || ts('Sorry an error occurred and your information was not saved'), ts('Error'), 'error'); | |
1b2475e1 CW |
989 | } |
990 | }, options || {}); | |
991 | 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>') | |
992 | .appendTo('body'); | |
993 | $msg.css('min-width', $msg.width()); | |
994 | function handle(status, data) { | |
995 | var endMsg = typeof(opts[status]) === 'function' ? opts[status](data) : opts[status]; | |
996 | if (endMsg) { | |
997 | $msg.removeClass('status-start').addClass('status-' + status).find('.crm-status-box-msg').html(endMsg); | |
998 | window.setTimeout(function() { | |
f54254d8 TO |
999 | $msg.fadeOut('slow', function() { |
1000 | $msg.remove(); | |
1001 | }); | |
4bad157e CW |
1002 | }, 2000); |
1003 | } else { | |
1b2475e1 | 1004 | $msg.remove(); |
4bad157e | 1005 | } |
1b2475e1 CW |
1006 | } |
1007 | return (deferred || new $.Deferred()) | |
1008 | .done(function(data) { | |
1009 | // If the server returns an error msg call the error handler | |
1010 | var status = $.isPlainObject(data) && (data.is_error || data.status === 'error') ? 'error' : 'success'; | |
1011 | handle(status, data); | |
1012 | }) | |
1013 | .fail(function(data) { | |
1014 | handle('error', data); | |
1015 | }); | |
8960d9b9 | 1016 | }; |
6a488035 | 1017 | |
beab9d1b TO |
1018 | // Convert an Angular promise to a jQuery promise |
1019 | CRM.toJqPromise = function(aPromise) { | |
1020 | var jqDeferred = $.Deferred(); | |
1021 | aPromise.then( | |
1022 | function(data) { jqDeferred.resolve(data); }, | |
1023 | function(data) { jqDeferred.reject(data); } | |
1024 | // should we also handle progress events? | |
1025 | ); | |
1026 | return jqDeferred.promise(); | |
1027 | }; | |
1028 | ||
705c61e9 TO |
1029 | CRM.toAPromise = function($q, jqPromise) { |
1030 | var aDeferred = $q.defer(); | |
1031 | jqPromise.then( | |
1032 | function(data) { aDeferred.resolve(data); }, | |
1033 | function(data) { aDeferred.reject(data); } | |
1034 | // should we also handle progress events? | |
1035 | ); | |
1036 | return aDeferred.promise; | |
1037 | }; | |
1038 | ||
6a488035 | 1039 | /** |
7442e8f6 | 1040 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 1041 | */ |
0f5816a6 | 1042 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
1043 | type = type || 'alert'; |
1044 | title = title || ''; | |
1045 | options = options || {}; | |
1046 | if ($('#crm-notification-container').length) { | |
1047 | var params = { | |
1048 | text: text, | |
1049 | title: title, | |
1050 | type: type | |
1051 | }; | |
1052 | // By default, don't expire errors and messages containing links | |
1053 | var extra = { | |
1054 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
1055 | unique: true | |
1056 | }; | |
1057 | options = $.extend(extra, options); | |
e24b17b9 | 1058 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 1059 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 1060 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
1061 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
1062 | $('.icon.ui-notify-close', this).click(); | |
1063 | } | |
1064 | }); | |
1065 | } | |
1066 | return $('#crm-notification-container').notify('create', params, options); | |
1067 | } | |
1068 | else { | |
1069 | if (title.length) { | |
1070 | text = title + "\n" + text; | |
1071 | } | |
1072 | alert(text); | |
1073 | return null; | |
1074 | } | |
e24b17b9 | 1075 | }; |
6a488035 TO |
1076 | |
1077 | /** | |
1078 | * Close whichever alert contains the given node | |
1079 | * | |
1080 | * @param node | |
1081 | */ | |
0f5816a6 | 1082 | CRM.closeAlertByChild = function (node) { |
6a488035 | 1083 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 1084 | }; |
6a488035 TO |
1085 | |
1086 | /** | |
7442e8f6 | 1087 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 1088 | */ |
5fb83680 | 1089 | CRM.confirm = function (options) { |
27f190b4 | 1090 | var dialog, url, msg, buttons = [], settings = { |
a65e5f52 | 1091 | title: ts('Confirm'), |
7553cf23 | 1092 | message: ts('Are you sure you want to continue?'), |
3f4328da | 1093 | url: null, |
0d5f99d4 | 1094 | width: 'auto', |
a8a8ddac | 1095 | height: 'auto', |
a243158e | 1096 | resizable: false, |
5fb83680 | 1097 | dialogClass: 'crm-container crm-confirm', |
0f5816a6 | 1098 | close: function () { |
5fb83680 | 1099 | $(this).dialog('destroy').remove(); |
0f5816a6 | 1100 | }, |
5fb83680 CW |
1101 | options: { |
1102 | no: ts('Cancel'), | |
1103 | yes: ts('Continue') | |
1104 | } | |
0f5816a6 | 1105 | }; |
a8a8ddac CW |
1106 | if (options && options.url) { |
1107 | settings.resizable = true; | |
1108 | settings.height = '50%'; | |
1109 | } | |
5fb83680 | 1110 | $.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {}); |
a8a8ddac | 1111 | settings = CRM.utils.adjustDialogDefaults(settings); |
5fb83680 | 1112 | if (!settings.buttons && $.isPlainObject(settings.options)) { |
27f190b4 CW |
1113 | $.each(settings.options, function(op, label) { |
1114 | buttons.push({ | |
5fb83680 | 1115 | text: label, |
27f190b4 CW |
1116 | 'data-op': op, |
1117 | icons: {primary: op === 'no' ? 'ui-icon-close' : 'ui-icon-check'}, | |
5fb83680 | 1118 | click: function() { |
27f190b4 | 1119 | var event = $.Event('crmConfirm:' + op); |
5fb83680 CW |
1120 | $(this).trigger(event); |
1121 | if (!event.isDefaultPrevented()) { | |
1122 | dialog.dialog('close'); | |
1123 | } | |
1124 | } | |
1125 | }); | |
1126 | }); | |
27f190b4 CW |
1127 | // Order buttons so that "no" goes on the right-hand side |
1128 | settings.buttons = _.sortBy(buttons, 'data-op').reverse(); | |
2a06342c | 1129 | } |
3f4328da | 1130 | url = settings.url; |
c0b7c815 | 1131 | msg = url ? '' : settings.message; |
5fb83680 CW |
1132 | delete settings.options; |
1133 | delete settings.message; | |
3f4328da | 1134 | delete settings.url; |
c0b7c815 | 1135 | dialog = $('<div class="crm-confirm-dialog"></div>').html(msg || '').dialog(settings); |
5fb83680 CW |
1136 | if ($.isFunction(options)) { |
1137 | dialog.on('crmConfirm:yes', options); | |
7553cf23 | 1138 | } |
3f4328da CW |
1139 | if (url) { |
1140 | CRM.loadPage(url, {target: dialog}); | |
1141 | } | |
c0b7c815 CW |
1142 | else { |
1143 | dialog.trigger('crmLoad'); | |
3f4328da CW |
1144 | } |
1145 | return dialog; | |
e24b17b9 | 1146 | }; |
6a488035 | 1147 | |
ed7225e6 CW |
1148 | /** provides a local copy of ts for a domain */ |
1149 | CRM.ts = function(domain) { | |
1150 | return function(message, options) { | |
1151 | if (domain) { | |
1152 | options = $.extend(options || {}, {domain: domain}); | |
1153 | } | |
f97524d9 TO |
1154 | return ts(message, options); |
1155 | }; | |
f97524d9 TO |
1156 | }; |
1157 | ||
e3d90d6c TO |
1158 | CRM.addStrings = function(domain, strings) { |
1159 | var bucket = (domain == 'civicrm' ? 'strings' : 'strings::' + domain); | |
1160 | CRM[bucket] = CRM[bucket] || {}; | |
1161 | _.extend(CRM[bucket], strings); | |
1162 | }; | |
1163 | ||
6a488035 | 1164 | /** |
7442e8f6 | 1165 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 1166 | */ |
0f5816a6 | 1167 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
1168 | title = title || ''; |
1169 | text = text || ''; | |
1170 | options = options || {}; | |
1171 | ||
1172 | var extra = { | |
1173 | expires: 0 | |
1174 | }; | |
1175 | if ($(this).length) { | |
0d75c29c | 1176 | if (title === '') { |
6a488035 TO |
1177 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); |
1178 | if (label.length) { | |
1179 | label.addClass('crm-error'); | |
1180 | var $label = label.clone(); | |
0d75c29c | 1181 | if (text === '' && $('.crm-marker', $label).length > 0) { |
6a488035 TO |
1182 | text = $('.crm-marker', $label).attr('title'); |
1183 | } | |
1184 | $('.crm-marker', $label).remove(); | |
1185 | title = $label.text(); | |
1186 | } | |
1187 | } | |
47737104 | 1188 | $(this).addClass('crm-error'); |
6a488035 TO |
1189 | } |
1190 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
1191 | if ($(this).length) { | |
1192 | var ele = $(this); | |
0f5816a6 KJ |
1193 | setTimeout(function () { |
1194 | ele.one('change', function () { | |
f54254d8 | 1195 | if (msg && msg.close) msg.close(); |
0f5816a6 KJ |
1196 | ele.removeClass('error'); |
1197 | label.removeClass('crm-error'); | |
1198 | }); | |
1199 | }, 1000); | |
6a488035 TO |
1200 | } |
1201 | return msg; | |
e24b17b9 | 1202 | }; |
6a488035 TO |
1203 | |
1204 | // Display system alerts through js notifications | |
1205 | function messagesFromMarkup() { | |
0f5816a6 | 1206 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 1207 | var text, title = ''; |
6a488035 TO |
1208 | $(this).removeClass('status messages'); |
1209 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
1210 | type = type.replace('crm-', ''); | |
1211 | $('.icon', this).remove(); | |
6a488035 | 1212 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 1213 | text = $('.msg-text', this).html(); |
6a488035 TO |
1214 | title = $('.msg-title', this).html(); |
1215 | } | |
1216 | else { | |
e24b17b9 | 1217 | text = $(this).html(); |
6a488035 TO |
1218 | } |
1219 | var options = $(this).data('options') || {}; | |
1220 | $(this).remove(); | |
1221 | // Duplicates were already removed server-side | |
1222 | options.unique = false; | |
1223 | CRM.alert(text, title, type, options); | |
1224 | }); | |
1225 | // Handle qf form errors | |
1226 | $('form :input.error', this).one('blur', function() { | |
1227 | $('.ui-notify-message.error a.ui-notify-close').click(); | |
1228 | $(this).removeClass('error'); | |
1229 | $(this).next('span.crm-error').remove(); | |
1230 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
1231 | .removeClass('crm-error') | |
1232 | .find('.crm-error').removeClass('crm-error'); | |
1233 | }); | |
1234 | } | |
1235 | ||
e4762285 CW |
1236 | /** |
1237 | * Improve blockUI when used with jQuery dialog | |
1238 | */ | |
1adbbe2d CW |
1239 | var originalBlock = $.fn.block, |
1240 | originalUnblock = $.fn.unblock; | |
1241 | ||
1242 | $.fn.block = function(opts) { | |
1243 | if ($(this).is('.ui-dialog-content')) { | |
1244 | originalBlock.call($(this).parents('.ui-dialog'), opts); | |
1245 | return $(this); | |
1246 | } | |
1247 | return originalBlock.call(this, opts); | |
e4762285 | 1248 | }; |
1adbbe2d CW |
1249 | $.fn.unblock = function(opts) { |
1250 | if ($(this).is('.ui-dialog-content')) { | |
1251 | originalUnblock.call($(this).parents('.ui-dialog'), opts); | |
1252 | return $(this); | |
1253 | } | |
1254 | return originalUnblock.call(this, opts); | |
e4762285 | 1255 | }; |
1adbbe2d | 1256 | |
e4762285 | 1257 | // Preprocess all CRM ajax calls to display messages |
03a7ec8f CW |
1258 | $(document).ajaxSuccess(function(event, xhr, settings) { |
1259 | try { | |
1260 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
1261 | var response = $.parseJSON(xhr.responseText); | |
1262 | if (typeof(response.crmMessages) == 'object') { | |
1263 | $.each(response.crmMessages, function(n, msg) { | |
1264 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
f54254d8 | 1265 | }); |
03a7ec8f | 1266 | } |
bba9b4f0 CW |
1267 | if (response.backtrace) { |
1268 | CRM.console('log', response.backtrace); | |
1269 | } | |
82983331 CW |
1270 | if (typeof response.deprecated === 'string') { |
1271 | CRM.console('warn', response.deprecated); | |
1272 | } | |
03a7ec8f CW |
1273 | } |
1274 | } | |
82983331 | 1275 | // Ignore errors thrown by parseJSON |
03a7ec8f CW |
1276 | catch (e) {} |
1277 | }); | |
1278 | ||
0f5816a6 | 1279 | $(function () { |
fdeb4de2 | 1280 | $.blockUI.defaults.message = null; |
1adbbe2d | 1281 | $.blockUI.defaults.ignoreIfBlocked = true; |
fdeb4de2 | 1282 | |
65b86482 CW |
1283 | if ($('#crm-container').hasClass('crm-public')) { |
1284 | $.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container crm-public'; | |
1285 | } | |
1286 | ||
205bb8ae | 1287 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 1288 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 1289 | |
ef3309b6 | 1290 | if ($('#crm-notification-container').length) { |
6a488035 TO |
1291 | // Initialize notifications |
1292 | $('#crm-notification-container').notify(); | |
1293 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 1294 | } |
ebb9197b | 1295 | |
475e9f44 | 1296 | $('body') |
5fb83680 CW |
1297 | // bind the event for image popup |
1298 | .on('click', 'a.crm-image-popup', function(e) { | |
1299 | CRM.confirm({ | |
1300 | title: ts('Preview'), | |
a243158e | 1301 | resizable: true, |
e4762285 | 1302 | message: '<div class="crm-custom-image-popup"><img style="max-width: 100%" src="' + $(this).attr('href') + '"></div>', |
5fb83680 CW |
1303 | options: null |
1304 | }); | |
1305 | e.preventDefault(); | |
475e9f44 | 1306 | }) |
ebb9197b | 1307 | |
475e9f44 CW |
1308 | .on('click', function (event) { |
1309 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
1310 | if ($(event.target).is('.btn-slide')) { | |
1311 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
1312 | } | |
1313 | }) | |
d664f648 | 1314 | |
4a143c04 CW |
1315 | // Handle clear button for form elements |
1316 | .on('click', 'a.crm-clear-link', function() { | |
9bce560a CW |
1317 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).trigger('change', ['crmClear']); |
1318 | $(this).siblings('input:text').val('').trigger('change', ['crmClear']); | |
4a143c04 CW |
1319 | return false; |
1320 | }) | |
1321 | .on('change', 'input.crm-form-radio:checked', function() { | |
1322 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
843bfb07 | 1323 | }) |
6a488035 | 1324 | |
843bfb07 | 1325 | // Allow normal clicking of links within accordions |
cf021bc5 | 1326 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
843bfb07 | 1327 | e.stopPropagation(); |
cf021bc5 | 1328 | }) |
843bfb07 CW |
1329 | // Handle accordions |
1330 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function (e) { | |
6a488035 | 1331 | if ($(this).parent().hasClass('collapsed')) { |
843bfb07 | 1332 | $(this).next().css('display', 'none').slideDown(200); |
6a488035 TO |
1333 | } |
1334 | else { | |
843bfb07 | 1335 | $(this).next().css('display', 'block').slideUp(200); |
6a488035 TO |
1336 | } |
1337 | $(this).parent().toggleClass('collapsed'); | |
843bfb07 | 1338 | e.preventDefault(); |
6a488035 | 1339 | }); |
843bfb07 CW |
1340 | |
1341 | $().crmtooltip(); | |
1342 | }); | |
1343 | /** | |
1344 | * @deprecated | |
1345 | */ | |
529792df CW |
1346 | $.fn.crmAccordions = function () { |
1347 | CRM.console('warn', 'Warning: $.crmAccordions was called. This function is deprecated and should not be used.'); | |
1348 | }; | |
843bfb07 CW |
1349 | /** |
1350 | * Collapse or expand an accordion | |
1351 | * @param speed | |
1352 | */ | |
0f5816a6 KJ |
1353 | $.fn.crmAccordionToggle = function (speed) { |
1354 | $(this).each(function () { | |
6a488035 TO |
1355 | if ($(this).hasClass('collapsed')) { |
1356 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
1357 | } | |
1358 | else { | |
1359 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
1360 | } | |
1361 | $(this).toggleClass('collapsed'); | |
1362 | }); | |
1363 | }; | |
5ec182d9 CW |
1364 | |
1365 | /** | |
1366 | * Clientside currency formatting | |
e4762285 CW |
1367 | * @param number value |
1368 | * @param [optional] string format - currency representation of the number 1234.56 | |
5ec182d9 CW |
1369 | * @return string |
1370 | */ | |
1371 | var currencyTemplate; | |
1372 | CRM.formatMoney = function(value, format) { | |
1373 | var decimal, separator, sign, i, j, result; | |
1374 | if (value === 'init' && format) { | |
1375 | currencyTemplate = format; | |
1376 | return; | |
1377 | } | |
1378 | format = format || currencyTemplate; | |
1379 | result = /1(.?)234(.?)56/.exec(format); | |
1380 | if (result === null) { | |
1381 | return 'Invalid format passed to CRM.formatMoney'; | |
1382 | } | |
1383 | separator = result[1]; | |
1384 | decimal = result[2]; | |
1385 | sign = (value < 0) ? '-' : ''; | |
1386 | //extracting the absolute value of the integer part of the number and converting to string | |
1387 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
1388 | j = ((j = i.length) > 3) ? j % 3 : 0; |
1389 | 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) : ''); | |
1390 | return format.replace(/1.*234.*56/, result); | |
1391 | }; | |
bba9b4f0 CW |
1392 | |
1393 | CRM.console = function(method, title, msg) { | |
1394 | if (window.console) { | |
1395 | method = $.isFunction(console[method]) ? method : 'log'; | |
1396 | if (msg === undefined) { | |
1397 | return console[method](title); | |
1398 | } else { | |
1399 | return console[method](title, msg); | |
1400 | } | |
1401 | } | |
e4762285 | 1402 | }; |
90efc417 TO |
1403 | |
1404 | // Determine if a user has a given permission. | |
1405 | // @see CRM_Core_Resources::addPermissions | |
1406 | CRM.checkPerm = function(perm) { | |
1407 | return CRM.permissions[perm]; | |
1408 | }; | |
4b513f23 | 1409 | })(jQuery, _); |