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!=""]'; | |
475e9f44 CW |
229 | if (typeof(val) !== 'array') { |
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 | ||
ba4fb2b2 | 241 | /** |
353ea873 | 242 | * Wrapper for select2 initialization function; supplies defaults |
a88cf11a | 243 | * @param options object |
ba4fb2b2 | 244 | */ |
a88cf11a CW |
245 | $.fn.crmSelect2 = function(options) { |
246 | return $(this).each(function () { | |
247 | var | |
248 | $el = $(this), | |
249 | defaults = {allowClear: !$el.hasClass('required')}; | |
250 | // quickform doesn't support optgroups so here's a hack :( | |
251 | $('option[value^=crm_optgroup]', this).each(function () { | |
252 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
253 | $(this).remove(); | |
254 | }); | |
255 | // Defaults for single-selects | |
256 | if ($el.is('select:not([multiple])')) { | |
257 | defaults.minimumResultsForSearch = 10; | |
258 | if ($('option:first', this).val() === '') { | |
259 | defaults.placeholderOption = 'first'; | |
260 | } | |
ba4fb2b2 | 261 | } |
a88cf11a CW |
262 | $el.select2($.extend(defaults, $el.data('select-params') || {}, options || {})); |
263 | }); | |
264 | }; | |
265 | ||
266 | /** | |
353ea873 | 267 | * @see CRM_Core_Form::addEntityRef for docs |
a88cf11a CW |
268 | * @param options object |
269 | */ | |
270 | $.fn.crmEntityRef = function(options) { | |
271 | options = options || {}; | |
272 | options.select = options.select || {}; | |
273 | return $(this).each(function() { | |
274 | var | |
275 | $el = $(this), | |
276 | entity = options.entity || $el.data('api-entity') || 'contact', | |
277 | selectParams = {}; | |
278 | $el.data('api-entity', entity); | |
279 | $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); | |
280 | $el.data('api-params', $.extend({}, $el.data('api-params') || {}, options.api)); | |
a4799f04 | 281 | $el.data('create-links', options.create || $el.data('create-links')); |
a88cf11a | 282 | $el.addClass('crm-ajax-select crm-' + entity + '-ref'); |
ba4fb2b2 CW |
283 | var settings = { |
284 | // Use select2 ajax helper instead of CRM.api because it provides more value | |
285 | ajax: { | |
286 | url: CRM.url('civicrm/ajax/rest'), | |
287 | data: function (input, page_num) { | |
288 | var params = $el.data('api-params') || {}; | |
289 | params.input = input; | |
290 | params.page_num = page_num; | |
291 | return { | |
292 | entity: $el.data('api-entity'), | |
293 | action: 'getlist', | |
294 | json: JSON.stringify(params) | |
295 | }; | |
296 | }, | |
297 | results: function(data) { | |
298 | return {more: data.more_results, results: data.values || []}; | |
299 | } | |
300 | }, | |
a88cf11a | 301 | minimumInputLength: 1, |
a4799f04 | 302 | formatResult: formatSelect2Result, |
ba4fb2b2 CW |
303 | formatSelection: function(row) { |
304 | return row.label; | |
305 | }, | |
306 | escapeMarkup: function (m) {return m;}, | |
a88cf11a CW |
307 | initSelection: function($el, callback) { |
308 | var | |
309 | multiple = !!$el.data('select-params').multiple, | |
310 | val = $el.val(), | |
311 | stored = $el.data('entity-value') || []; | |
312 | if (val === '') { | |
313 | return; | |
314 | } | |
315 | // If we already have this data, just return it | |
316 | if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { | |
317 | callback(multiple ? stored : stored[0]); | |
318 | } else { | |
78b203e5 | 319 | var params = $.extend({}, $el.data('api-params') || {}, {id: val}); |
a88cf11a CW |
320 | CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { |
321 | callback(multiple ? result.values : result.values[0]) | |
322 | }); | |
323 | } | |
ba4fb2b2 CW |
324 | } |
325 | }; | |
326 | if ($el.data('create-links')) { | |
a88cf11a CW |
327 | selectParams.formatInputTooShort = function() { |
328 | var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this); | |
a4799f04 CW |
329 | if ($el.data('create-links')) { |
330 | txt += ' ' + ts('or') + '<br />' + formatSelect2CreateLinks($el); | |
ba4fb2b2 CW |
331 | } |
332 | return txt; | |
333 | }; | |
a88cf11a | 334 | selectParams.formatNoMatches = function() { |
ba4fb2b2 | 335 | var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches; |
a4799f04 | 336 | return txt + '<br />' + formatSelect2CreateLinks($el); |
ba4fb2b2 | 337 | }; |
a88cf11a | 338 | $el.off('.createLinks').on('select2-open.createLinks', function() { |
ba4fb2b2 CW |
339 | var $el = $(this); |
340 | $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
341 | $el.select2('close'); | |
342 | CRM.loadForm($(this).attr('href'), { | |
343 | dialog: {width: 500, height: 'auto'} | |
344 | }).on('crmFormSuccess', function(e, data) { | |
c92f6436 | 345 | if (data.status === 'success' && data.id) { |
e11b84ce | 346 | CRM.status(ts('%1 Created', {1: data.label})); |
c92f6436 CW |
347 | if ($el.select2('container').hasClass('select2-container-multi')) { |
348 | var selection = $el.select2('data'); | |
349 | selection.push(data); | |
350 | $el.select2('data', selection, true); | |
351 | } else { | |
352 | $el.select2('data', data, true); | |
353 | } | |
ba4fb2b2 CW |
354 | } |
355 | }); | |
356 | return false; | |
357 | }); | |
358 | }); | |
359 | } | |
a88cf11a CW |
360 | $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)); |
361 | }); | |
ba4fb2b2 CW |
362 | }; |
363 | ||
a4799f04 | 364 | function formatSelect2Result(row) { |
88881f79 | 365 | var markup = '<div class="crm-select2-row">'; |
ff88d165 | 366 | if (row.image !== undefined) { |
88881f79 | 367 | markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>'; |
ff88d165 | 368 | } |
54bee7df | 369 | else if (row.icon_class) { |
88881f79 | 370 | markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>'; |
54bee7df | 371 | } |
88881f79 CW |
372 | markup += '<div><div class="crm-select2-row-label">' + row.label + '</div>'; |
373 | markup += '<div class="crm-select2-row-description">'; | |
374 | $.each(row.description || [], function(k, text) { | |
375 | markup += '<p>' + text + '</p>'; | |
376 | }); | |
377 | markup += '</div></div></div>'; | |
ff88d165 | 378 | return markup; |
a4799f04 CW |
379 | } |
380 | ||
381 | function formatSelect2CreateLinks($el) { | |
382 | var | |
383 | createLinks = $el.data('create-links'), | |
384 | api = $el.data('api-params') || {}, | |
385 | type = api.params ? api.params.contact_type : null; | |
386 | if (createLinks === true) { | |
387 | createLinks = type ? _.where(CRM.profile.contactCreate, {type: type}) : CRM.profile.contactCreate; | |
388 | } | |
79ae07d9 | 389 | var markup = ''; |
a4799f04 | 390 | _.each(createLinks, function(link) { |
79ae07d9 | 391 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; |
a4799f04 CW |
392 | if (link.type) { |
393 | markup += '<span class="icon ' + link.type + '-profile-icon"></span> '; | |
79ae07d9 CW |
394 | } |
395 | markup += link.label + '</a>'; | |
396 | }); | |
397 | return markup; | |
a4799f04 | 398 | } |
ff88d165 | 399 | |
f7b92fcd | 400 | // Initialize widgets |
eb90857a CW |
401 | $(document) |
402 | .on('crmLoad', function(e) { | |
403 | $('table.row-highlight', e.target) | |
404 | .off('.rowHighlight') | |
405 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function () { | |
406 | var target, table = $(this).closest('table'); | |
407 | if ($(this).hasClass('select-rows')) { | |
408 | target = $('tbody tr', table); | |
409 | $('input.select-row', table).prop('checked', $(this).prop('checked')); | |
410 | } | |
411 | else { | |
412 | target = $(this).closest('tr'); | |
413 | $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1); | |
414 | } | |
415 | target.toggleClass('crm-row-selected', $(this).is(':checked')); | |
416 | }) | |
417 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
5f34e50b CW |
418 | $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); |
419 | $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); | |
eb90857a CW |
420 | }) |
421 | // Modal dialogs should disable scrollbars | |
422 | .on('dialogopen', function(e) { | |
423 | if ($(e.target).dialog('option', 'modal')) { | |
424 | $(e.target).addClass('modal-dialog'); | |
425 | $('body').css({overflow: 'hidden'}); | |
426 | } | |
427 | }) | |
428 | .on('dialogclose', function(e) { | |
e0fb89c1 | 429 | if ($('.ui-dialog .modal-dialog').not(e.target).length < 1) { |
eb90857a CW |
430 | $('body').css({overflow: ''}); |
431 | } | |
432 | }); | |
148c4e8d CW |
433 | |
434 | /** | |
435 | * Function to make multiselect boxes behave as fields in small screens | |
436 | */ | |
437 | function advmultiselectResize() { | |
438 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
439 | if (amswidth < 700) { | |
440 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
441 | } |
442 | else { | |
148c4e8d CW |
443 | $("form table.advmultiselect td").css('display', 'table-cell'); |
444 | } | |
445 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
446 | if (contactwidth < 600) { | |
447 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 448 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 449 | if (index > 1) { |
0f5816a6 | 450 | if (index % 2 == 0) { |
148c4e8d CW |
451 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
452 | } | |
453 | var item = $(this); | |
454 | $(this).parent().next().append(item); | |
455 | } | |
456 | }); | |
0f5816a6 KJ |
457 | } |
458 | else { | |
148c4e8d | 459 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 460 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
461 | var nitem = $(this); |
462 | var parent = $(this).parent(); | |
463 | $(this).parent().prev().append(nitem); | |
0f5816a6 | 464 | if (parent.children().size() == 0) { |
148c4e8d CW |
465 | parent.remove(); |
466 | } | |
467 | }); | |
468 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
469 | } | |
470 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 471 | |
148c4e8d CW |
472 | if (cformwidth < 720) { |
473 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
474 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
475 | if (cformwidth < 480) { | |
476 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
477 | } |
478 | else { | |
148c4e8d CW |
479 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
480 | } | |
0f5816a6 KJ |
481 | } |
482 | else { | |
148c4e8d CW |
483 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
484 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
485 | } | |
486 | } | |
0f5816a6 | 487 | |
148c4e8d | 488 | advmultiselectResize(); |
0f5816a6 | 489 | $(window).resize(function () { |
6a488035 TO |
490 | advmultiselectResize(); |
491 | }); | |
492 | ||
0f5816a6 | 493 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
494 | $(document) |
495 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
496 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
497 | $(this).addClass('crm-tooltip-active'); |
498 | var topDistance = e.pageY - $(window).scrollTop(); | |
499 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { | |
500 | $(this).addClass('crm-tooltip-down'); | |
501 | } | |
502 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
503 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
504 | $(this).children().children('.crm-tooltip') | |
505 | .html('<div class="crm-loading-element"></div>') | |
506 | .load(this.href); | |
507 | } | |
508 | }) | |
2c29c2ac RN |
509 | .on('mouseout', 'a.crm-summary-link', function () { |
510 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
511 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
512 | }) | |
2c29c2ac | 513 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
514 | }; |
515 | ||
b0ca6188 | 516 | var helpDisplay, helpPrevious; |
8e3272a1 | 517 | CRM.help = function (title, params, url) { |
55a93b02 | 518 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 519 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
520 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
521 | helpDisplay.close(); | |
b0ca6188 CW |
522 | return; |
523 | } | |
55a93b02 | 524 | helpDisplay.close(); |
b0ca6188 CW |
525 | } |
526 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
527 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
528 | params.type = 'page'; | |
b0ca6188 | 529 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 530 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
531 | { |
532 | data: params, | |
533 | dataType: 'html', | |
e24b17b9 | 534 | success: function (data) { |
6a488035 TO |
535 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
536 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
537 | }, | |
e24b17b9 | 538 | error: function () { |
6a488035 TO |
539 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
540 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
541 | } | |
542 | } | |
543 | ); | |
544 | }; | |
8960d9b9 | 545 | /** |
7442e8f6 | 546 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
8960d9b9 | 547 | */ |
1b2475e1 | 548 | CRM.status = function(options, deferred) { |
9a7ef94f | 549 | // 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 |
550 | if (typeof options === 'string') { |
551 | return CRM.status({start: options, success: options, error: options})[deferred === 'error' ? 'reject' : 'resolve'](); | |
8960d9b9 | 552 | } |
1b2475e1 CW |
553 | var opts = $.extend({ |
554 | start: ts('Saving...'), | |
9a7ef94f | 555 | success: ts('Saved'), |
1b2475e1 CW |
556 | error: function() { |
557 | CRM.alert(ts('Sorry an error occurred and your information was not saved'), ts('Error')); | |
558 | } | |
559 | }, options || {}); | |
560 | 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>') | |
561 | .appendTo('body'); | |
562 | $msg.css('min-width', $msg.width()); | |
563 | function handle(status, data) { | |
564 | var endMsg = typeof(opts[status]) === 'function' ? opts[status](data) : opts[status]; | |
565 | if (endMsg) { | |
566 | $msg.removeClass('status-start').addClass('status-' + status).find('.crm-status-box-msg').html(endMsg); | |
567 | window.setTimeout(function() { | |
568 | $msg.fadeOut('slow', function() {$msg.remove()}); | |
4bad157e CW |
569 | }, 2000); |
570 | } else { | |
1b2475e1 | 571 | $msg.remove(); |
4bad157e | 572 | } |
1b2475e1 CW |
573 | } |
574 | return (deferred || new $.Deferred()) | |
575 | .done(function(data) { | |
576 | // If the server returns an error msg call the error handler | |
577 | var status = $.isPlainObject(data) && (data.is_error || data.status === 'error') ? 'error' : 'success'; | |
578 | handle(status, data); | |
579 | }) | |
580 | .fail(function(data) { | |
581 | handle('error', data); | |
582 | }); | |
8960d9b9 | 583 | }; |
6a488035 TO |
584 | |
585 | /** | |
7442e8f6 | 586 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 587 | */ |
0f5816a6 | 588 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
589 | type = type || 'alert'; |
590 | title = title || ''; | |
591 | options = options || {}; | |
592 | if ($('#crm-notification-container').length) { | |
593 | var params = { | |
594 | text: text, | |
595 | title: title, | |
596 | type: type | |
597 | }; | |
598 | // By default, don't expire errors and messages containing links | |
599 | var extra = { | |
600 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
601 | unique: true | |
602 | }; | |
603 | options = $.extend(extra, options); | |
e24b17b9 | 604 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 605 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 606 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
607 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
608 | $('.icon.ui-notify-close', this).click(); | |
609 | } | |
610 | }); | |
611 | } | |
612 | return $('#crm-notification-container').notify('create', params, options); | |
613 | } | |
614 | else { | |
615 | if (title.length) { | |
616 | text = title + "\n" + text; | |
617 | } | |
618 | alert(text); | |
619 | return null; | |
620 | } | |
e24b17b9 | 621 | }; |
6a488035 TO |
622 | |
623 | /** | |
624 | * Close whichever alert contains the given node | |
625 | * | |
626 | * @param node | |
627 | */ | |
0f5816a6 | 628 | CRM.closeAlertByChild = function (node) { |
6a488035 | 629 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 630 | }; |
6a488035 TO |
631 | |
632 | /** | |
7442e8f6 | 633 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 634 | */ |
5fb83680 CW |
635 | CRM.confirm = function (options) { |
636 | var dialog, settings = { | |
7553cf23 CW |
637 | title: ts('Confirm Action'), |
638 | message: ts('Are you sure you want to continue?'), | |
0d5f99d4 | 639 | width: 'auto', |
5fb83680 CW |
640 | modal: true, |
641 | dialogClass: 'crm-container crm-confirm', | |
0f5816a6 | 642 | close: function () { |
5fb83680 | 643 | $(this).dialog('destroy').remove(); |
0f5816a6 | 644 | }, |
5fb83680 CW |
645 | options: { |
646 | no: ts('Cancel'), | |
647 | yes: ts('Continue') | |
648 | } | |
0f5816a6 | 649 | }; |
5fb83680 CW |
650 | $.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {}); |
651 | if (!settings.buttons && $.isPlainObject(settings.options)) { | |
652 | settings.buttons = []; | |
653 | $.each(settings.options, function(key, label) { | |
654 | settings.buttons.push({ | |
655 | text: label, | |
656 | click: function() { | |
657 | var event = $.Event('crmConfirm:' + key); | |
658 | $(this).trigger(event); | |
659 | if (!event.isDefaultPrevented()) { | |
660 | dialog.dialog('close'); | |
661 | } | |
662 | } | |
663 | }); | |
664 | }); | |
2a06342c | 665 | } |
5fb83680 CW |
666 | dialog = $('<div class="crm-confirm-dialog"></div>').html(settings.message); |
667 | delete settings.options; | |
668 | delete settings.message; | |
669 | if ($.isFunction(options)) { | |
670 | dialog.on('crmConfirm:yes', options); | |
7553cf23 | 671 | } |
5fb83680 | 672 | return dialog.dialog(settings).trigger('crmLoad'); |
e24b17b9 | 673 | }; |
6a488035 TO |
674 | |
675 | /** | |
7442e8f6 | 676 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference |
6a488035 | 677 | */ |
0f5816a6 | 678 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
679 | title = title || ''; |
680 | text = text || ''; | |
681 | options = options || {}; | |
682 | ||
683 | var extra = { | |
684 | expires: 0 | |
685 | }; | |
686 | if ($(this).length) { | |
687 | if (title == '') { | |
688 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); | |
689 | if (label.length) { | |
690 | label.addClass('crm-error'); | |
691 | var $label = label.clone(); | |
692 | if (text == '' && $('.crm-marker', $label).length > 0) { | |
693 | text = $('.crm-marker', $label).attr('title'); | |
694 | } | |
695 | $('.crm-marker', $label).remove(); | |
696 | title = $label.text(); | |
697 | } | |
698 | } | |
699 | $(this).addClass('error'); | |
700 | } | |
701 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
702 | if ($(this).length) { | |
703 | var ele = $(this); | |
0f5816a6 KJ |
704 | setTimeout(function () { |
705 | ele.one('change', function () { | |
706 | msg && msg.close && msg.close(); | |
707 | ele.removeClass('error'); | |
708 | label.removeClass('crm-error'); | |
709 | }); | |
710 | }, 1000); | |
6a488035 TO |
711 | } |
712 | return msg; | |
e24b17b9 | 713 | }; |
6a488035 TO |
714 | |
715 | // Display system alerts through js notifications | |
716 | function messagesFromMarkup() { | |
0f5816a6 | 717 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 718 | var text, title = ''; |
6a488035 TO |
719 | $(this).removeClass('status messages'); |
720 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
721 | type = type.replace('crm-', ''); | |
722 | $('.icon', this).remove(); | |
6a488035 | 723 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 724 | text = $('.msg-text', this).html(); |
6a488035 TO |
725 | title = $('.msg-title', this).html(); |
726 | } | |
727 | else { | |
e24b17b9 | 728 | text = $(this).html(); |
6a488035 TO |
729 | } |
730 | var options = $(this).data('options') || {}; | |
731 | $(this).remove(); | |
732 | // Duplicates were already removed server-side | |
733 | options.unique = false; | |
734 | CRM.alert(text, title, type, options); | |
735 | }); | |
736 | // Handle qf form errors | |
737 | $('form :input.error', this).one('blur', function() { | |
738 | $('.ui-notify-message.error a.ui-notify-close').click(); | |
739 | $(this).removeClass('error'); | |
740 | $(this).next('span.crm-error').remove(); | |
741 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
742 | .removeClass('crm-error') | |
743 | .find('.crm-error').removeClass('crm-error'); | |
744 | }); | |
745 | } | |
746 | ||
03a7ec8f CW |
747 | // Preprocess all cj ajax calls to display messages |
748 | $(document).ajaxSuccess(function(event, xhr, settings) { | |
749 | try { | |
750 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
751 | var response = $.parseJSON(xhr.responseText); | |
752 | if (typeof(response.crmMessages) == 'object') { | |
753 | $.each(response.crmMessages, function(n, msg) { | |
754 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
755 | }) | |
756 | } | |
757 | } | |
758 | } | |
759 | // Suppress errors | |
760 | catch (e) {} | |
761 | }); | |
762 | ||
7cb72342 CW |
763 | /** |
764 | * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin | |
7442e8f6 | 765 | * FIXME: Remove this before 4.5 release |
7cb72342 CW |
766 | */ |
767 | $.widget('civi.crmAutocomplete', $.ui.autocomplete, {}); | |
768 | ||
0f5816a6 | 769 | $(function () { |
205bb8ae | 770 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 771 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 772 | |
ef3309b6 | 773 | if ($('#crm-notification-container').length) { |
6a488035 TO |
774 | // Initialize notifications |
775 | $('#crm-notification-container').notify(); | |
776 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 777 | } |
ebb9197b | 778 | |
475e9f44 | 779 | $('body') |
5fb83680 CW |
780 | // bind the event for image popup |
781 | .on('click', 'a.crm-image-popup', function(e) { | |
782 | CRM.confirm({ | |
783 | title: ts('Preview'), | |
784 | message: '<div class="crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>', | |
785 | options: null | |
786 | }); | |
787 | e.preventDefault(); | |
475e9f44 | 788 | }) |
ebb9197b | 789 | |
475e9f44 CW |
790 | .on('click', function (event) { |
791 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
792 | if ($(event.target).is('.btn-slide')) { | |
793 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
794 | } | |
795 | }) | |
d664f648 | 796 | |
4a143c04 CW |
797 | // Handle clear button for form elements |
798 | .on('click', 'a.crm-clear-link', function() { | |
799 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change(); | |
bcb4280c | 800 | $(this).siblings('input:text').val('').change(); |
4a143c04 CW |
801 | return false; |
802 | }) | |
803 | .on('change', 'input.crm-form-radio:checked', function() { | |
804 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
475e9f44 | 805 | }); |
1a62cf90 | 806 | |
d664f648 | 807 | $().crmtooltip(); |
6a488035 TO |
808 | }); |
809 | ||
0f5816a6 | 810 | $.fn.crmAccordions = function (speed) { |
cf021bc5 CW |
811 | var container = $(this).length > 0 ? $(this) : $('.crm-container'); |
812 | speed = speed === undefined ? 200 : speed; | |
813 | container | |
814 | .off('click.crmAccordions') | |
6a488035 | 815 | // Allow normal clicking of links |
cf021bc5 | 816 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
6a488035 | 817 | e.stopPropagation && e.stopPropagation(); |
cf021bc5 CW |
818 | }) |
819 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () { | |
6a488035 TO |
820 | if ($(this).parent().hasClass('collapsed')) { |
821 | $(this).next().css('display', 'none').slideDown(speed); | |
822 | } | |
823 | else { | |
824 | $(this).next().css('display', 'block').slideUp(speed); | |
825 | } | |
826 | $(this).parent().toggleClass('collapsed'); | |
827 | return false; | |
828 | }); | |
6a488035 | 829 | }; |
0f5816a6 KJ |
830 | $.fn.crmAccordionToggle = function (speed) { |
831 | $(this).each(function () { | |
6a488035 TO |
832 | if ($(this).hasClass('collapsed')) { |
833 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
834 | } | |
835 | else { | |
836 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
837 | } | |
838 | $(this).toggleClass('collapsed'); | |
839 | }); | |
840 | }; | |
5ec182d9 CW |
841 | |
842 | /** | |
843 | * Clientside currency formatting | |
844 | * @param value | |
3bdb644f | 845 | * @param format - currency representation of the number 1234.56 |
5ec182d9 | 846 | * @return string |
3bdb644f | 847 | * @see CRM_Core_Resources::addCoreResources |
5ec182d9 CW |
848 | */ |
849 | var currencyTemplate; | |
850 | CRM.formatMoney = function(value, format) { | |
851 | var decimal, separator, sign, i, j, result; | |
852 | if (value === 'init' && format) { | |
853 | currencyTemplate = format; | |
854 | return; | |
855 | } | |
856 | format = format || currencyTemplate; | |
857 | result = /1(.?)234(.?)56/.exec(format); | |
858 | if (result === null) { | |
859 | return 'Invalid format passed to CRM.formatMoney'; | |
860 | } | |
861 | separator = result[1]; | |
862 | decimal = result[2]; | |
863 | sign = (value < 0) ? '-' : ''; | |
864 | //extracting the absolute value of the integer part of the number and converting to string | |
865 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
866 | j = ((j = i.length) > 3) ? j % 3 : 0; |
867 | 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) : ''); | |
868 | return format.replace(/1.*234.*56/, result); | |
869 | }; | |
4b513f23 | 870 | })(jQuery, _); |