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) { | |
83 | if (target_element_type == null) { | |
84 | var target_element_type = 'block'; | |
85 | } | |
86 | else { | |
87 | if (target_element_type == 'table-row') { | |
88 | var target_element_type = ''; | |
89 | } | |
90 | } | |
91 | ||
92 | if (field_type == 'select') { | |
93 | var trigger = trigger_value.split("|"); | |
24cc4545 | 94 | var selectedOptionValue = cj('#' + trigger_field_id).val(); |
0f5816a6 KJ |
95 | |
96 | var target = target_element_id.split("|"); | |
97 | for (var j = 0; j < target.length; j++) { | |
98 | if (invert) { | |
99 | cj('#' + target[j]).show(); | |
100 | } | |
101 | else { | |
102 | cj('#' + target[j]).hide(); | |
103 | } | |
104 | for (var i = 0; i < trigger.length; i++) { | |
105 | if (selectedOptionValue == trigger[i]) { | |
106 | if (invert) { | |
107 | cj('#' + target[j]).hide(); | |
108 | } | |
109 | else { | |
110 | cj('#' + target[j]).show(); | |
111 | } | |
6a488035 | 112 | } |
0f5816a6 KJ |
113 | } |
114 | } | |
6a488035 | 115 | |
0f5816a6 KJ |
116 | } |
117 | else { | |
118 | if (field_type == 'radio') { | |
119 | var target = target_element_id.split("|"); | |
120 | for (var j = 0; j < target.length; j++) { | |
24cc4545 | 121 | if (cj('[name="' + trigger_field_id + '"]').is(':checked')) { |
0f5816a6 KJ |
122 | if (invert) { |
123 | cj('#' + target[j]).hide(); | |
124 | } | |
125 | else { | |
126 | cj('#' + target[j]).show(); | |
127 | } | |
6a488035 | 128 | } |
0f5816a6 KJ |
129 | else { |
130 | if (invert) { | |
131 | cj('#' + target[j]).show(); | |
132 | } | |
133 | else { | |
134 | cj('#' + target[j]).hide(); | |
135 | } | |
136 | } | |
137 | } | |
6a488035 | 138 | } |
0f5816a6 | 139 | } |
6a488035 TO |
140 | } |
141 | ||
6a488035 TO |
142 | /** |
143 | * Function to change button text and disable one it is clicked | |
353ea873 | 144 | * @deprecated |
6a488035 TO |
145 | * @param obj object - the button clicked |
146 | * @param formID string - the id of the form being submitted | |
147 | * @param string procText - button text after user clicks it | |
353ea873 | 148 | * @return bool |
6a488035 | 149 | */ |
0f5816a6 | 150 | var submitcount = 0; |
c6edd786 | 151 | /* Changes button label on submit, and disables button after submit for newer browsers. |
152 | Puts up alert for older browsers. */ | |
0f5816a6 KJ |
153 | function submitOnce(obj, formId, procText) { |
154 | // if named button clicked, change text | |
155 | if (obj.value != null) { | |
156 | obj.value = procText + " ..."; | |
157 | } | |
158 | if (document.getElementById) { // disable submit button for newer browsers | |
159 | obj.disabled = true; | |
160 | document.getElementById(formId).submit(); | |
161 | return true; | |
162 | } | |
163 | else { // for older browsers | |
164 | if (submitcount == 0) { | |
165 | submitcount++; | |
166 | return true; | |
167 | } | |
168 | else { | |
169 | alert("Your request is currently being processed ... Please wait."); | |
170 | return false; | |
6a488035 | 171 | } |
0f5816a6 | 172 | } |
6a488035 | 173 | } |
6a488035 | 174 | |
6a488035 TO |
175 | /** |
176 | * Function to show / hide the row in optionFields | |
353ea873 CW |
177 | * @deprecated |
178 | * @param index string, element whose innerHTML is to hide else will show the hidden row. | |
6a488035 | 179 | */ |
0f5816a6 KJ |
180 | function showHideRow(index) { |
181 | if (index) { | |
182 | cj('tr#optionField_' + index).hide(); | |
183 | if (cj('table#optionField tr:hidden:first').length) { | |
184 | cj('div#optionFieldLink').show(); | |
6a488035 | 185 | } |
0f5816a6 KJ |
186 | } |
187 | else { | |
188 | cj('table#optionField tr:hidden:first').show(); | |
189 | if (!cj('table#optionField tr:hidden:last').length) { | |
190 | cj('div#optionFieldLink').hide(); | |
191 | } | |
192 | } | |
193 | return false; | |
6a488035 TO |
194 | } |
195 | ||
475e9f44 | 196 | CRM.utils = CRM.utils || {}; |
6a488035 TO |
197 | CRM.strings = CRM.strings || {}; |
198 | CRM.validate = CRM.validate || { | |
199 | params: {}, | |
200 | functions: [] | |
201 | }; | |
202 | ||
4b513f23 | 203 | (function ($, _, undefined) { |
7553cf23 | 204 | "use strict"; |
d664f648 | 205 | |
3f586963 CW |
206 | // Theme classes for unattached elements |
207 | $.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container'; | |
208 | ||
47f21f3a CW |
209 | // https://github.com/ivaynberg/select2/pull/2090 |
210 | $.fn.select2.defaults.width = 'resolve'; | |
211 | ||
e20523a8 CW |
212 | // Workaround for https://github.com/ivaynberg/select2/issues/1246 |
213 | $.ui.dialog.prototype._allowInteraction = function(e) { | |
214 | return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; | |
215 | }; | |
216 | ||
475e9f44 CW |
217 | /** |
218 | * Populate a select list, overwriting the existing options except for the placeholder. | |
219 | * @param $el jquery collection - 1 or more select elements | |
220 | * @param options array in format returned by api.getoptions | |
23e8a31b | 221 | * @param removePlaceholder bool |
475e9f44 | 222 | */ |
23e8a31b | 223 | CRM.utils.setOptions = function($el, options, removePlaceholder) { |
475e9f44 CW |
224 | $el.each(function() { |
225 | var | |
226 | $elect = $(this), | |
23e8a31b CW |
227 | val = $elect.val() || [], |
228 | opts = removePlaceholder ? '' : '[value!=""]'; | |
46ed212b | 229 | if (!$.isArray(val)) { |
475e9f44 CW |
230 | val = [val]; |
231 | } | |
23e8a31b | 232 | $elect.find('option' + opts).remove(); |
e6f4ac36 | 233 | _.each(options, function(option) { |
475e9f44 CW |
234 | var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : ''; |
235 | $elect.append('<option value="' + option.key + '"' + selected + '>' + option.value + '</option>'); | |
236 | }); | |
e6f4ac36 | 237 | $elect.trigger('crmOptionsUpdated', $.extend({}, options)).trigger('change'); |
475e9f44 CW |
238 | }); |
239 | }; | |
240 | ||
3e201321 | 241 | /** |
242 | * Compare Form Input values against cached initial value. | |
243 | * True if changes have been made. | |
244 | * @returns {Boolean} | |
245 | */ | |
246 | CRM.utils.initialValueChanged = function(el) { | |
247 | var isDirty = false | |
248 | $(':input', el).each(function () { | |
249 | if($(this).data('crm-initial-value') !== undefined | |
250 | && $(this).data('crm-initial-value') != $(this).val()){ | |
251 | isDirty = true; | |
252 | } | |
253 | }); | |
254 | return isDirty; | |
255 | } | |
256 | ||
ba4fb2b2 | 257 | /** |
353ea873 | 258 | * Wrapper for select2 initialization function; supplies defaults |
a88cf11a | 259 | * @param options object |
ba4fb2b2 | 260 | */ |
a88cf11a CW |
261 | $.fn.crmSelect2 = function(options) { |
262 | return $(this).each(function () { | |
263 | var | |
264 | $el = $(this), | |
a243158e | 265 | settings = {allowClear: !$el.hasClass('required')}; |
a88cf11a CW |
266 | // quickform doesn't support optgroups so here's a hack :( |
267 | $('option[value^=crm_optgroup]', this).each(function () { | |
268 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
269 | $(this).remove(); | |
270 | }); | |
271 | // Defaults for single-selects | |
272 | if ($el.is('select:not([multiple])')) { | |
a243158e | 273 | settings.minimumResultsForSearch = 10; |
a88cf11a | 274 | if ($('option:first', this).val() === '') { |
a243158e | 275 | settings.placeholderOption = 'first'; |
a88cf11a | 276 | } |
ba4fb2b2 | 277 | } |
a243158e CW |
278 | $.extend(settings, $el.data('select-params') || {}, options || {}); |
279 | if (settings.ajax) { | |
280 | $el.addClass('crm-ajax-select'); | |
281 | } | |
282 | $el.select2(settings); | |
a88cf11a CW |
283 | }); |
284 | }; | |
285 | ||
286 | /** | |
353ea873 | 287 | * @see CRM_Core_Form::addEntityRef for docs |
a88cf11a CW |
288 | * @param options object |
289 | */ | |
290 | $.fn.crmEntityRef = function(options) { | |
291 | options = options || {}; | |
292 | options.select = options.select || {}; | |
293 | return $(this).each(function() { | |
294 | var | |
295 | $el = $(this), | |
296 | entity = options.entity || $el.data('api-entity') || 'contact', | |
297 | selectParams = {}; | |
298 | $el.data('api-entity', entity); | |
299 | $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); | |
300 | $el.data('api-params', $.extend({}, $el.data('api-params') || {}, options.api)); | |
a4799f04 | 301 | $el.data('create-links', options.create || $el.data('create-links')); |
a243158e | 302 | $el.addClass('crm-form-entityref crm-' + entity + '-ref'); |
ba4fb2b2 CW |
303 | var settings = { |
304 | // Use select2 ajax helper instead of CRM.api because it provides more value | |
305 | ajax: { | |
306 | url: CRM.url('civicrm/ajax/rest'), | |
307 | data: function (input, page_num) { | |
308 | var params = $el.data('api-params') || {}; | |
309 | params.input = input; | |
310 | params.page_num = page_num; | |
311 | return { | |
312 | entity: $el.data('api-entity'), | |
313 | action: 'getlist', | |
314 | json: JSON.stringify(params) | |
315 | }; | |
316 | }, | |
317 | results: function(data) { | |
318 | return {more: data.more_results, results: data.values || []}; | |
319 | } | |
320 | }, | |
a88cf11a | 321 | minimumInputLength: 1, |
a4799f04 | 322 | formatResult: formatSelect2Result, |
ba4fb2b2 CW |
323 | formatSelection: function(row) { |
324 | return row.label; | |
325 | }, | |
326 | escapeMarkup: function (m) {return m;}, | |
a88cf11a CW |
327 | initSelection: function($el, callback) { |
328 | var | |
329 | multiple = !!$el.data('select-params').multiple, | |
330 | val = $el.val(), | |
331 | stored = $el.data('entity-value') || []; | |
332 | if (val === '') { | |
333 | return; | |
334 | } | |
335 | // If we already have this data, just return it | |
336 | if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { | |
337 | callback(multiple ? stored : stored[0]); | |
338 | } else { | |
78b203e5 | 339 | var params = $.extend({}, $el.data('api-params') || {}, {id: val}); |
a88cf11a CW |
340 | CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { |
341 | callback(multiple ? result.values : result.values[0]) | |
342 | }); | |
343 | } | |
ba4fb2b2 CW |
344 | } |
345 | }; | |
346 | if ($el.data('create-links')) { | |
a88cf11a CW |
347 | selectParams.formatInputTooShort = function() { |
348 | var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this); | |
a4799f04 CW |
349 | if ($el.data('create-links')) { |
350 | txt += ' ' + ts('or') + '<br />' + formatSelect2CreateLinks($el); | |
ba4fb2b2 CW |
351 | } |
352 | return txt; | |
353 | }; | |
a88cf11a | 354 | selectParams.formatNoMatches = function() { |
ba4fb2b2 | 355 | var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches; |
a4799f04 | 356 | return txt + '<br />' + formatSelect2CreateLinks($el); |
ba4fb2b2 | 357 | }; |
a88cf11a | 358 | $el.off('.createLinks').on('select2-open.createLinks', function() { |
ba4fb2b2 CW |
359 | var $el = $(this); |
360 | $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
361 | $el.select2('close'); | |
362 | CRM.loadForm($(this).attr('href'), { | |
363 | dialog: {width: 500, height: 'auto'} | |
364 | }).on('crmFormSuccess', function(e, data) { | |
c92f6436 | 365 | if (data.status === 'success' && data.id) { |
e11b84ce | 366 | CRM.status(ts('%1 Created', {1: data.label})); |
c92f6436 CW |
367 | if ($el.select2('container').hasClass('select2-container-multi')) { |
368 | var selection = $el.select2('data'); | |
369 | selection.push(data); | |
370 | $el.select2('data', selection, true); | |
371 | } else { | |
372 | $el.select2('data', data, true); | |
373 | } | |
ba4fb2b2 CW |
374 | } |
375 | }); | |
376 | return false; | |
377 | }); | |
378 | }); | |
379 | } | |
a88cf11a CW |
380 | $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)); |
381 | }); | |
ba4fb2b2 CW |
382 | }; |
383 | ||
a4799f04 | 384 | function formatSelect2Result(row) { |
88881f79 | 385 | var markup = '<div class="crm-select2-row">'; |
ff88d165 | 386 | if (row.image !== undefined) { |
88881f79 | 387 | markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>'; |
ff88d165 | 388 | } |
54bee7df | 389 | else if (row.icon_class) { |
88881f79 | 390 | markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>'; |
54bee7df | 391 | } |
88881f79 CW |
392 | markup += '<div><div class="crm-select2-row-label">' + row.label + '</div>'; |
393 | markup += '<div class="crm-select2-row-description">'; | |
394 | $.each(row.description || [], function(k, text) { | |
395 | markup += '<p>' + text + '</p>'; | |
396 | }); | |
397 | markup += '</div></div></div>'; | |
ff88d165 | 398 | return markup; |
a4799f04 CW |
399 | } |
400 | ||
401 | function formatSelect2CreateLinks($el) { | |
402 | var | |
403 | createLinks = $el.data('create-links'), | |
404 | api = $el.data('api-params') || {}, | |
405 | type = api.params ? api.params.contact_type : null; | |
406 | if (createLinks === true) { | |
407 | createLinks = type ? _.where(CRM.profile.contactCreate, {type: type}) : CRM.profile.contactCreate; | |
408 | } | |
79ae07d9 | 409 | var markup = ''; |
a4799f04 | 410 | _.each(createLinks, function(link) { |
79ae07d9 | 411 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; |
a4799f04 CW |
412 | if (link.type) { |
413 | markup += '<span class="icon ' + link.type + '-profile-icon"></span> '; | |
79ae07d9 CW |
414 | } |
415 | markup += link.label + '</a>'; | |
416 | }); | |
417 | return markup; | |
a4799f04 | 418 | } |
ff88d165 | 419 | |
f7b92fcd | 420 | // Initialize widgets |
eb90857a CW |
421 | $(document) |
422 | .on('crmLoad', function(e) { | |
423 | $('table.row-highlight', e.target) | |
424 | .off('.rowHighlight') | |
7e13d44e CW |
425 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function (e, data) { |
426 | var filter, $table = $(this).closest('table'); | |
eb90857a | 427 | if ($(this).hasClass('select-rows')) { |
7e13d44e CW |
428 | filter = $(this).prop('checked') ? ':not(:checked)' : ':checked'; |
429 | $('input.select-row' + filter, $table).prop('checked', $(this).prop('checked')).trigger('change', 'master-selected'); | |
eb90857a CW |
430 | } |
431 | else { | |
7e13d44e CW |
432 | $(this).closest('tr').toggleClass('crm-row-selected', $(this).prop('checked')); |
433 | if (data !== 'master-selected') { | |
434 | $('input.select-rows', $table).prop('checked', $(".select-row:not(':checked')", $table).length < 1); | |
435 | } | |
eb90857a | 436 | } |
eb90857a CW |
437 | }) |
438 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
5f34e50b CW |
439 | $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); |
440 | $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); | |
3e201321 | 441 | // Cache Form Input initial values |
442 | $('form[data-warn-changes] :input', e.target).each( function() { | |
443 | $(this).data('crm-initial-value', $(e.target).val()); | |
444 | }); | |
eb90857a | 445 | }) |
eb90857a | 446 | .on('dialogopen', function(e) { |
f292709b CW |
447 | var $el = $(e.target); |
448 | // Modal dialogs should disable scrollbars | |
449 | if ($el.dialog('option', 'modal')) { | |
450 | $el.addClass('modal-dialog'); | |
eb90857a CW |
451 | $('body').css({overflow: 'hidden'}); |
452 | } | |
f292709b CW |
453 | $el.parent().find('.ui-dialog-titlebar-close').attr('title', ts('Close')); |
454 | // Add resize button | |
a243158e | 455 | if ($el.parent().hasClass('crm-container') && $el.dialog('option', 'resizable')) { |
77a8d7f9 | 456 | $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 |
457 | $('.crm-dialog-titlebar-resize', $el.parent()).click(function(e) { |
458 | if ($el.data('origSize')) { | |
459 | $el.dialog('option', $el.data('origSize')); | |
460 | $el.data('origSize', null); | |
461 | } else { | |
462 | $el.data('origSize', { | |
463 | position: 'center', | |
464 | width: $el.dialog('option', 'width'), | |
465 | height: $el.dialog('option', 'height') | |
466 | }); | |
467 | var menuHeight = $('#civicrm-menu').height(); | |
468 | $el.dialog('option', {width: '100%', height: ($(window).height() - menuHeight), position: [0, menuHeight]}); | |
469 | } | |
470 | e.preventDefault(); | |
471 | }); | |
472 | } | |
eb90857a CW |
473 | }) |
474 | .on('dialogclose', function(e) { | |
f292709b | 475 | // Restore scrollbars when closing modal |
e0fb89c1 | 476 | if ($('.ui-dialog .modal-dialog').not(e.target).length < 1) { |
eb90857a CW |
477 | $('body').css({overflow: ''}); |
478 | } | |
479 | }); | |
148c4e8d CW |
480 | |
481 | /** | |
482 | * Function to make multiselect boxes behave as fields in small screens | |
483 | */ | |
484 | function advmultiselectResize() { | |
485 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
486 | if (amswidth < 700) { | |
487 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
488 | } |
489 | else { | |
148c4e8d CW |
490 | $("form table.advmultiselect td").css('display', 'table-cell'); |
491 | } | |
492 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
493 | if (contactwidth < 600) { | |
494 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 495 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 496 | if (index > 1) { |
0f5816a6 | 497 | if (index % 2 == 0) { |
148c4e8d CW |
498 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
499 | } | |
500 | var item = $(this); | |
501 | $(this).parent().next().append(item); | |
502 | } | |
503 | }); | |
0f5816a6 KJ |
504 | } |
505 | else { | |
148c4e8d | 506 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 507 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
508 | var nitem = $(this); |
509 | var parent = $(this).parent(); | |
510 | $(this).parent().prev().append(nitem); | |
0f5816a6 | 511 | if (parent.children().size() == 0) { |
148c4e8d CW |
512 | parent.remove(); |
513 | } | |
514 | }); | |
515 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
516 | } | |
517 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 518 | |
148c4e8d CW |
519 | if (cformwidth < 720) { |
520 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
521 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
522 | if (cformwidth < 480) { | |
523 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
524 | } |
525 | else { | |
148c4e8d CW |
526 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
527 | } | |
0f5816a6 KJ |
528 | } |
529 | else { | |
148c4e8d CW |
530 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
531 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
532 | } | |
533 | } | |
0f5816a6 | 534 | |
148c4e8d | 535 | advmultiselectResize(); |
0f5816a6 | 536 | $(window).resize(function () { |
6a488035 TO |
537 | advmultiselectResize(); |
538 | }); | |
539 | ||
0f5816a6 | 540 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
541 | $(document) |
542 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
543 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
544 | $(this).addClass('crm-tooltip-active'); |
545 | var topDistance = e.pageY - $(window).scrollTop(); | |
546 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { | |
547 | $(this).addClass('crm-tooltip-down'); | |
548 | } | |
549 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
550 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
551 | $(this).children().children('.crm-tooltip') | |
552 | .html('<div class="crm-loading-element"></div>') | |
553 | .load(this.href); | |
554 | } | |
555 | }) | |
2c29c2ac RN |
556 | .on('mouseout', 'a.crm-summary-link', function () { |
557 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
558 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
559 | }) | |
2c29c2ac | 560 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
561 | }; |
562 | ||
b0ca6188 | 563 | var helpDisplay, helpPrevious; |
8e3272a1 | 564 | CRM.help = function (title, params, url) { |
55a93b02 | 565 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 566 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
567 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
568 | helpDisplay.close(); | |
b0ca6188 CW |
569 | return; |
570 | } | |
55a93b02 | 571 | helpDisplay.close(); |
b0ca6188 CW |
572 | } |
573 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
574 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
575 | params.type = 'page'; | |
b0ca6188 | 576 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 577 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
578 | { |
579 | data: params, | |
580 | dataType: 'html', | |
e24b17b9 | 581 | success: function (data) { |
6a488035 TO |
582 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
583 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
584 | }, | |
e24b17b9 | 585 | error: function () { |
6a488035 TO |
586 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
587 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
588 | } | |
589 | } | |
590 | ); | |
591 | }; | |
8960d9b9 | 592 | /** |
7442e8f6 | 593 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
8960d9b9 | 594 | */ |
1b2475e1 | 595 | CRM.status = function(options, deferred) { |
9a7ef94f | 596 | // 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 |
597 | if (typeof options === 'string') { |
598 | return CRM.status({start: options, success: options, error: options})[deferred === 'error' ? 'reject' : 'resolve'](); | |
8960d9b9 | 599 | } |
1b2475e1 CW |
600 | var opts = $.extend({ |
601 | start: ts('Saving...'), | |
9a7ef94f | 602 | success: ts('Saved'), |
1b2475e1 CW |
603 | error: function() { |
604 | CRM.alert(ts('Sorry an error occurred and your information was not saved'), ts('Error')); | |
605 | } | |
606 | }, options || {}); | |
607 | 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>') | |
608 | .appendTo('body'); | |
609 | $msg.css('min-width', $msg.width()); | |
610 | function handle(status, data) { | |
611 | var endMsg = typeof(opts[status]) === 'function' ? opts[status](data) : opts[status]; | |
612 | if (endMsg) { | |
613 | $msg.removeClass('status-start').addClass('status-' + status).find('.crm-status-box-msg').html(endMsg); | |
614 | window.setTimeout(function() { | |
615 | $msg.fadeOut('slow', function() {$msg.remove()}); | |
4bad157e CW |
616 | }, 2000); |
617 | } else { | |
1b2475e1 | 618 | $msg.remove(); |
4bad157e | 619 | } |
1b2475e1 CW |
620 | } |
621 | return (deferred || new $.Deferred()) | |
622 | .done(function(data) { | |
623 | // If the server returns an error msg call the error handler | |
624 | var status = $.isPlainObject(data) && (data.is_error || data.status === 'error') ? 'error' : 'success'; | |
625 | handle(status, data); | |
626 | }) | |
627 | .fail(function(data) { | |
628 | handle('error', data); | |
629 | }); | |
8960d9b9 | 630 | }; |
6a488035 TO |
631 | |
632 | /** | |
7442e8f6 | 633 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 634 | */ |
0f5816a6 | 635 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
636 | type = type || 'alert'; |
637 | title = title || ''; | |
638 | options = options || {}; | |
639 | if ($('#crm-notification-container').length) { | |
640 | var params = { | |
641 | text: text, | |
642 | title: title, | |
643 | type: type | |
644 | }; | |
645 | // By default, don't expire errors and messages containing links | |
646 | var extra = { | |
647 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
648 | unique: true | |
649 | }; | |
650 | options = $.extend(extra, options); | |
e24b17b9 | 651 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 652 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 653 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
654 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
655 | $('.icon.ui-notify-close', this).click(); | |
656 | } | |
657 | }); | |
658 | } | |
659 | return $('#crm-notification-container').notify('create', params, options); | |
660 | } | |
661 | else { | |
662 | if (title.length) { | |
663 | text = title + "\n" + text; | |
664 | } | |
665 | alert(text); | |
666 | return null; | |
667 | } | |
e24b17b9 | 668 | }; |
6a488035 TO |
669 | |
670 | /** | |
671 | * Close whichever alert contains the given node | |
672 | * | |
673 | * @param node | |
674 | */ | |
0f5816a6 | 675 | CRM.closeAlertByChild = function (node) { |
6a488035 | 676 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 677 | }; |
6a488035 TO |
678 | |
679 | /** | |
7442e8f6 | 680 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 681 | */ |
5fb83680 CW |
682 | CRM.confirm = function (options) { |
683 | var dialog, settings = { | |
7553cf23 CW |
684 | title: ts('Confirm Action'), |
685 | message: ts('Are you sure you want to continue?'), | |
0d5f99d4 | 686 | width: 'auto', |
5fb83680 | 687 | modal: true, |
a243158e | 688 | resizable: false, |
5fb83680 | 689 | dialogClass: 'crm-container crm-confirm', |
0f5816a6 | 690 | close: function () { |
5fb83680 | 691 | $(this).dialog('destroy').remove(); |
0f5816a6 | 692 | }, |
5fb83680 CW |
693 | options: { |
694 | no: ts('Cancel'), | |
695 | yes: ts('Continue') | |
696 | } | |
0f5816a6 | 697 | }; |
5fb83680 CW |
698 | $.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {}); |
699 | if (!settings.buttons && $.isPlainObject(settings.options)) { | |
700 | settings.buttons = []; | |
701 | $.each(settings.options, function(key, label) { | |
702 | settings.buttons.push({ | |
703 | text: label, | |
704 | click: function() { | |
705 | var event = $.Event('crmConfirm:' + key); | |
706 | $(this).trigger(event); | |
707 | if (!event.isDefaultPrevented()) { | |
708 | dialog.dialog('close'); | |
709 | } | |
710 | } | |
711 | }); | |
712 | }); | |
2a06342c | 713 | } |
5fb83680 CW |
714 | dialog = $('<div class="crm-confirm-dialog"></div>').html(settings.message); |
715 | delete settings.options; | |
716 | delete settings.message; | |
717 | if ($.isFunction(options)) { | |
718 | dialog.on('crmConfirm:yes', options); | |
7553cf23 | 719 | } |
5fb83680 | 720 | return dialog.dialog(settings).trigger('crmLoad'); |
e24b17b9 | 721 | }; |
6a488035 TO |
722 | |
723 | /** | |
7442e8f6 | 724 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 725 | */ |
0f5816a6 | 726 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
727 | title = title || ''; |
728 | text = text || ''; | |
729 | options = options || {}; | |
730 | ||
731 | var extra = { | |
732 | expires: 0 | |
733 | }; | |
734 | if ($(this).length) { | |
735 | if (title == '') { | |
736 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); | |
737 | if (label.length) { | |
738 | label.addClass('crm-error'); | |
739 | var $label = label.clone(); | |
740 | if (text == '' && $('.crm-marker', $label).length > 0) { | |
741 | text = $('.crm-marker', $label).attr('title'); | |
742 | } | |
743 | $('.crm-marker', $label).remove(); | |
744 | title = $label.text(); | |
745 | } | |
746 | } | |
747 | $(this).addClass('error'); | |
748 | } | |
749 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
750 | if ($(this).length) { | |
751 | var ele = $(this); | |
0f5816a6 KJ |
752 | setTimeout(function () { |
753 | ele.one('change', function () { | |
754 | msg && msg.close && msg.close(); | |
755 | ele.removeClass('error'); | |
756 | label.removeClass('crm-error'); | |
757 | }); | |
758 | }, 1000); | |
6a488035 TO |
759 | } |
760 | return msg; | |
e24b17b9 | 761 | }; |
6a488035 TO |
762 | |
763 | // Display system alerts through js notifications | |
764 | function messagesFromMarkup() { | |
0f5816a6 | 765 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 766 | var text, title = ''; |
6a488035 TO |
767 | $(this).removeClass('status messages'); |
768 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
769 | type = type.replace('crm-', ''); | |
770 | $('.icon', this).remove(); | |
6a488035 | 771 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 772 | text = $('.msg-text', this).html(); |
6a488035 TO |
773 | title = $('.msg-title', this).html(); |
774 | } | |
775 | else { | |
e24b17b9 | 776 | text = $(this).html(); |
6a488035 TO |
777 | } |
778 | var options = $(this).data('options') || {}; | |
779 | $(this).remove(); | |
780 | // Duplicates were already removed server-side | |
781 | options.unique = false; | |
782 | CRM.alert(text, title, type, options); | |
783 | }); | |
784 | // Handle qf form errors | |
785 | $('form :input.error', this).one('blur', function() { | |
786 | $('.ui-notify-message.error a.ui-notify-close').click(); | |
787 | $(this).removeClass('error'); | |
788 | $(this).next('span.crm-error').remove(); | |
789 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
790 | .removeClass('crm-error') | |
791 | .find('.crm-error').removeClass('crm-error'); | |
792 | }); | |
793 | } | |
794 | ||
03a7ec8f CW |
795 | // Preprocess all cj ajax calls to display messages |
796 | $(document).ajaxSuccess(function(event, xhr, settings) { | |
797 | try { | |
798 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
799 | var response = $.parseJSON(xhr.responseText); | |
800 | if (typeof(response.crmMessages) == 'object') { | |
801 | $.each(response.crmMessages, function(n, msg) { | |
802 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
803 | }) | |
804 | } | |
805 | } | |
806 | } | |
807 | // Suppress errors | |
808 | catch (e) {} | |
809 | }); | |
810 | ||
7cb72342 CW |
811 | /** |
812 | * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin | |
7442e8f6 | 813 | * FIXME: Remove this before 4.5 release |
7cb72342 CW |
814 | */ |
815 | $.widget('civi.crmAutocomplete', $.ui.autocomplete, {}); | |
816 | ||
0f5816a6 | 817 | $(function () { |
205bb8ae | 818 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 819 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 820 | |
ef3309b6 | 821 | if ($('#crm-notification-container').length) { |
6a488035 TO |
822 | // Initialize notifications |
823 | $('#crm-notification-container').notify(); | |
824 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 825 | } |
ebb9197b | 826 | |
475e9f44 | 827 | $('body') |
5fb83680 CW |
828 | // bind the event for image popup |
829 | .on('click', 'a.crm-image-popup', function(e) { | |
830 | CRM.confirm({ | |
831 | title: ts('Preview'), | |
a243158e | 832 | resizable: true, |
5fb83680 CW |
833 | message: '<div class="crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>', |
834 | options: null | |
835 | }); | |
836 | e.preventDefault(); | |
475e9f44 | 837 | }) |
ebb9197b | 838 | |
475e9f44 CW |
839 | .on('click', function (event) { |
840 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
841 | if ($(event.target).is('.btn-slide')) { | |
842 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
843 | } | |
844 | }) | |
d664f648 | 845 | |
4a143c04 CW |
846 | // Handle clear button for form elements |
847 | .on('click', 'a.crm-clear-link', function() { | |
848 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change(); | |
bcb4280c | 849 | $(this).siblings('input:text').val('').change(); |
4a143c04 CW |
850 | return false; |
851 | }) | |
852 | .on('change', 'input.crm-form-radio:checked', function() { | |
853 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
843bfb07 | 854 | }) |
6a488035 | 855 | |
843bfb07 | 856 | // Allow normal clicking of links within accordions |
cf021bc5 | 857 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
843bfb07 | 858 | e.stopPropagation(); |
cf021bc5 | 859 | }) |
843bfb07 CW |
860 | // Handle accordions |
861 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function (e) { | |
6a488035 | 862 | if ($(this).parent().hasClass('collapsed')) { |
843bfb07 | 863 | $(this).next().css('display', 'none').slideDown(200); |
6a488035 TO |
864 | } |
865 | else { | |
843bfb07 | 866 | $(this).next().css('display', 'block').slideUp(200); |
6a488035 TO |
867 | } |
868 | $(this).parent().toggleClass('collapsed'); | |
843bfb07 | 869 | e.preventDefault(); |
6a488035 | 870 | }); |
843bfb07 CW |
871 | |
872 | $().crmtooltip(); | |
873 | }); | |
874 | /** | |
875 | * @deprecated | |
876 | */ | |
877 | $.fn.crmAccordions = function () {}; | |
878 | /** | |
879 | * Collapse or expand an accordion | |
880 | * @param speed | |
881 | */ | |
0f5816a6 KJ |
882 | $.fn.crmAccordionToggle = function (speed) { |
883 | $(this).each(function () { | |
6a488035 TO |
884 | if ($(this).hasClass('collapsed')) { |
885 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
886 | } | |
887 | else { | |
888 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
889 | } | |
890 | $(this).toggleClass('collapsed'); | |
891 | }); | |
892 | }; | |
5ec182d9 CW |
893 | |
894 | /** | |
895 | * Clientside currency formatting | |
896 | * @param value | |
3bdb644f | 897 | * @param format - currency representation of the number 1234.56 |
5ec182d9 | 898 | * @return string |
3bdb644f | 899 | * @see CRM_Core_Resources::addCoreResources |
5ec182d9 CW |
900 | */ |
901 | var currencyTemplate; | |
902 | CRM.formatMoney = function(value, format) { | |
903 | var decimal, separator, sign, i, j, result; | |
904 | if (value === 'init' && format) { | |
905 | currencyTemplate = format; | |
906 | return; | |
907 | } | |
908 | format = format || currencyTemplate; | |
909 | result = /1(.?)234(.?)56/.exec(format); | |
910 | if (result === null) { | |
911 | return 'Invalid format passed to CRM.formatMoney'; | |
912 | } | |
913 | separator = result[1]; | |
914 | decimal = result[2]; | |
915 | sign = (value < 0) ? '-' : ''; | |
916 | //extracting the absolute value of the integer part of the number and converting to string | |
917 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
918 | j = ((j = i.length) > 3) ? j % 3 : 0; |
919 | 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) : ''); | |
920 | return format.replace(/1.*234.*56/, result); | |
921 | }; | |
4b513f23 | 922 | })(jQuery, _); |