Commit | Line | Data |
---|---|---|
353ea873 | 1 | // https://civicrm.org/licensing |
6a488035 TO |
2 | var CRM = CRM || {}; |
3 | var cj = jQuery; | |
4 | ||
5 | /** | |
6 | * Short-named function for string translation, defined in global scope so it's available everywhere. | |
7 | * | |
353ea873 CW |
8 | * @param text string for translating |
9 | * @param params object key:value of additional parameters | |
6a488035 | 10 | * |
353ea873 | 11 | * @return string |
6a488035 TO |
12 | */ |
13 | function ts(text, params) { | |
7553cf23 | 14 | "use strict"; |
6a488035 | 15 | text = CRM.strings[text] || text; |
2788147f | 16 | if (typeof(params) === 'object') { |
6a488035 | 17 | for (var i in params) { |
32155ad6 | 18 | if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') { |
2788147f | 19 | // sprintf emulation: escape % characters in the replacements to avoid conflicts |
32155ad6 | 20 | text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-')); |
2788147f | 21 | } |
6a488035 TO |
22 | } |
23 | return text.replace(/%-crmescaped-/g, '%'); | |
24 | } | |
25 | return text; | |
26 | } | |
27 | ||
28 | /** | |
29 | * This function is called by default at the bottom of template files which have forms that have | |
30 | * conditionally displayed/hidden sections and elements. The PHP is responsible for generating | |
31 | * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to | |
32 | * this function. | |
33 | * | |
353ea873 | 34 | * @deprecated |
6a488035 TO |
35 | * @param showBlocks Array of element Id's to be displayed |
36 | * @param hideBlocks Array of element Id's to be hidden | |
37 | * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...) | |
6a488035 | 38 | */ |
0f5816a6 KJ |
39 | function on_load_init_blocks(showBlocks, hideBlocks, elementType) { |
40 | if (elementType == null) { | |
41 | var elementType = 'block'; | |
42 | } | |
43 | ||
44 | /* This loop is used to display the blocks whose IDs are present within the showBlocks array */ | |
45 | for (var i = 0; i < showBlocks.length; i++) { | |
46 | var myElement = document.getElementById(showBlocks[i]); | |
47 | /* getElementById returns null if element id doesn't exist in the document */ | |
48 | if (myElement != null) { | |
49 | myElement.style.display = elementType; | |
6a488035 | 50 | } |
0f5816a6 KJ |
51 | else { |
52 | alert('showBlocks array item not in .tpl = ' + showBlocks[i]); | |
53 | } | |
54 | } | |
6a488035 | 55 | |
0f5816a6 KJ |
56 | /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */ |
57 | for (var i = 0; i < hideBlocks.length; i++) { | |
58 | var myElement = document.getElementById(hideBlocks[i]); | |
59 | /* getElementById returns null if element id doesn't exist in the document */ | |
60 | if (myElement != null) { | |
61 | myElement.style.display = 'none'; | |
62 | } | |
63 | else { | |
64 | alert('showBlocks array item not in .tpl = ' + hideBlocks[i]); | |
6a488035 | 65 | } |
0f5816a6 | 66 | } |
6a488035 TO |
67 | } |
68 | ||
69 | /** | |
70 | * This function is called when we need to show or hide a related form element (target_element) | |
71 | * based on the value (trigger_value) of another form field (trigger_field). | |
72 | * | |
353ea873 | 73 | * @deprecated |
6a488035 TO |
74 | * @param trigger_field_id HTML id of field whose onchange is the trigger |
75 | * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field | |
76 | * @param target_element_id HTML id of element to be shown or hidden | |
77 | * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row') | |
78 | * @param field_type Type of element radio/select | |
79 | * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match | |
0f5816a6 KJ |
80 | */ |
81 | function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) { | |
82 | if (target_element_type == null) { | |
83 | var target_element_type = 'block'; | |
84 | } | |
85 | else { | |
86 | if (target_element_type == 'table-row') { | |
87 | var target_element_type = ''; | |
88 | } | |
89 | } | |
90 | ||
91 | if (field_type == 'select') { | |
92 | var trigger = trigger_value.split("|"); | |
24cc4545 | 93 | var selectedOptionValue = cj('#' + trigger_field_id).val(); |
0f5816a6 KJ |
94 | |
95 | var target = target_element_id.split("|"); | |
96 | for (var j = 0; j < target.length; j++) { | |
97 | if (invert) { | |
98 | cj('#' + target[j]).show(); | |
99 | } | |
100 | else { | |
101 | cj('#' + target[j]).hide(); | |
102 | } | |
103 | for (var i = 0; i < trigger.length; i++) { | |
104 | if (selectedOptionValue == trigger[i]) { | |
105 | if (invert) { | |
106 | cj('#' + target[j]).hide(); | |
107 | } | |
108 | else { | |
109 | cj('#' + target[j]).show(); | |
110 | } | |
6a488035 | 111 | } |
0f5816a6 KJ |
112 | } |
113 | } | |
6a488035 | 114 | |
0f5816a6 KJ |
115 | } |
116 | else { | |
117 | if (field_type == 'radio') { | |
118 | var target = target_element_id.split("|"); | |
119 | for (var j = 0; j < target.length; j++) { | |
24cc4545 | 120 | if (cj('[name="' + trigger_field_id + '"]').is(':checked')) { |
0f5816a6 KJ |
121 | if (invert) { |
122 | cj('#' + target[j]).hide(); | |
123 | } | |
124 | else { | |
125 | cj('#' + target[j]).show(); | |
126 | } | |
6a488035 | 127 | } |
0f5816a6 KJ |
128 | else { |
129 | if (invert) { | |
130 | cj('#' + target[j]).show(); | |
131 | } | |
132 | else { | |
133 | cj('#' + target[j]).hide(); | |
134 | } | |
135 | } | |
136 | } | |
6a488035 | 137 | } |
0f5816a6 | 138 | } |
6a488035 TO |
139 | } |
140 | ||
6a488035 TO |
141 | /** |
142 | * Function to change button text and disable one it is clicked | |
353ea873 | 143 | * @deprecated |
6a488035 TO |
144 | * @param obj object - the button clicked |
145 | * @param formID string - the id of the form being submitted | |
146 | * @param string procText - button text after user clicks it | |
353ea873 | 147 | * @return bool |
6a488035 | 148 | */ |
0f5816a6 | 149 | var submitcount = 0; |
c6edd786 | 150 | /* Changes button label on submit, and disables button after submit for newer browsers. |
151 | Puts up alert for older browsers. */ | |
0f5816a6 KJ |
152 | function submitOnce(obj, formId, procText) { |
153 | // if named button clicked, change text | |
154 | if (obj.value != null) { | |
155 | obj.value = procText + " ..."; | |
156 | } | |
157 | if (document.getElementById) { // disable submit button for newer browsers | |
158 | obj.disabled = true; | |
159 | document.getElementById(formId).submit(); | |
160 | return true; | |
161 | } | |
162 | else { // for older browsers | |
163 | if (submitcount == 0) { | |
164 | submitcount++; | |
165 | return true; | |
166 | } | |
167 | else { | |
168 | alert("Your request is currently being processed ... Please wait."); | |
169 | return false; | |
6a488035 | 170 | } |
0f5816a6 | 171 | } |
6a488035 | 172 | } |
353ea873 CW |
173 | /** |
174 | * @deprecated | |
175 | */ | |
6a488035 TO |
176 | function popUp(URL) { |
177 | day = new Date(); | |
0f5816a6 | 178 | id = day.getTime(); |
6a488035 TO |
179 | eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=420,left = 202,top = 184');"); |
180 | } | |
181 | ||
6a488035 TO |
182 | /** |
183 | * Function to show / hide the row in optionFields | |
353ea873 CW |
184 | * @deprecated |
185 | * @param index string, element whose innerHTML is to hide else will show the hidden row. | |
6a488035 | 186 | */ |
0f5816a6 KJ |
187 | function showHideRow(index) { |
188 | if (index) { | |
189 | cj('tr#optionField_' + index).hide(); | |
190 | if (cj('table#optionField tr:hidden:first').length) { | |
191 | cj('div#optionFieldLink').show(); | |
6a488035 | 192 | } |
0f5816a6 KJ |
193 | } |
194 | else { | |
195 | cj('table#optionField tr:hidden:first').show(); | |
196 | if (!cj('table#optionField tr:hidden:last').length) { | |
197 | cj('div#optionFieldLink').hide(); | |
198 | } | |
199 | } | |
200 | return false; | |
6a488035 TO |
201 | } |
202 | ||
475e9f44 | 203 | CRM.utils = CRM.utils || {}; |
6a488035 TO |
204 | CRM.strings = CRM.strings || {}; |
205 | CRM.validate = CRM.validate || { | |
206 | params: {}, | |
207 | functions: [] | |
208 | }; | |
209 | ||
0f5816a6 | 210 | (function ($, undefined) { |
7553cf23 | 211 | "use strict"; |
d664f648 | 212 | |
23e8a31b | 213 | $.fn.select2.defaults.dropdownCssClass = 'crm-container'; |
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) { | |
219 | return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length; | |
220 | }; | |
221 | ||
475e9f44 CW |
222 | /** |
223 | * Populate a select list, overwriting the existing options except for the placeholder. | |
224 | * @param $el jquery collection - 1 or more select elements | |
225 | * @param options array in format returned by api.getoptions | |
23e8a31b | 226 | * @param removePlaceholder bool |
475e9f44 | 227 | */ |
23e8a31b | 228 | CRM.utils.setOptions = function($el, options, removePlaceholder) { |
475e9f44 CW |
229 | $el.each(function() { |
230 | var | |
231 | $elect = $(this), | |
23e8a31b CW |
232 | val = $elect.val() || [], |
233 | opts = removePlaceholder ? '' : '[value!=""]'; | |
475e9f44 CW |
234 | if (typeof(val) !== 'array') { |
235 | val = [val]; | |
236 | } | |
23e8a31b | 237 | $elect.find('option' + opts).remove(); |
475e9f44 CW |
238 | $.each(options, function(key, option) { |
239 | var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : ''; | |
240 | $elect.append('<option value="' + option.key + '"' + selected + '>' + option.value + '</option>'); | |
241 | }); | |
87831073 | 242 | $elect.trigger('crmOptionsUpdated').trigger('change'); |
475e9f44 CW |
243 | }); |
244 | }; | |
245 | ||
ba4fb2b2 | 246 | /** |
353ea873 | 247 | * Wrapper for select2 initialization function; supplies defaults |
a88cf11a | 248 | * @param options object |
ba4fb2b2 | 249 | */ |
a88cf11a CW |
250 | $.fn.crmSelect2 = function(options) { |
251 | return $(this).each(function () { | |
252 | var | |
253 | $el = $(this), | |
254 | defaults = {allowClear: !$el.hasClass('required')}; | |
255 | // quickform doesn't support optgroups so here's a hack :( | |
256 | $('option[value^=crm_optgroup]', this).each(function () { | |
257 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
258 | $(this).remove(); | |
259 | }); | |
260 | // Defaults for single-selects | |
261 | if ($el.is('select:not([multiple])')) { | |
262 | defaults.minimumResultsForSearch = 10; | |
263 | if ($('option:first', this).val() === '') { | |
264 | defaults.placeholderOption = 'first'; | |
265 | } | |
ba4fb2b2 | 266 | } |
a88cf11a CW |
267 | $el.select2($.extend(defaults, $el.data('select-params') || {}, options || {})); |
268 | }); | |
269 | }; | |
270 | ||
271 | /** | |
353ea873 | 272 | * @see CRM_Core_Form::addEntityRef for docs |
a88cf11a CW |
273 | * @param options object |
274 | */ | |
275 | $.fn.crmEntityRef = function(options) { | |
276 | options = options || {}; | |
277 | options.select = options.select || {}; | |
278 | return $(this).each(function() { | |
279 | var | |
280 | $el = $(this), | |
281 | entity = options.entity || $el.data('api-entity') || 'contact', | |
282 | selectParams = {}; | |
283 | $el.data('api-entity', entity); | |
284 | $el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select)); | |
285 | $el.data('api-params', $.extend({}, $el.data('api-params') || {}, options.api)); | |
a4799f04 | 286 | $el.data('create-links', options.create || $el.data('create-links')); |
a88cf11a | 287 | $el.addClass('crm-ajax-select crm-' + entity + '-ref'); |
ba4fb2b2 CW |
288 | var settings = { |
289 | // Use select2 ajax helper instead of CRM.api because it provides more value | |
290 | ajax: { | |
291 | url: CRM.url('civicrm/ajax/rest'), | |
292 | data: function (input, page_num) { | |
293 | var params = $el.data('api-params') || {}; | |
294 | params.input = input; | |
295 | params.page_num = page_num; | |
296 | return { | |
297 | entity: $el.data('api-entity'), | |
298 | action: 'getlist', | |
299 | json: JSON.stringify(params) | |
300 | }; | |
301 | }, | |
302 | results: function(data) { | |
303 | return {more: data.more_results, results: data.values || []}; | |
304 | } | |
305 | }, | |
a88cf11a | 306 | minimumInputLength: 1, |
a4799f04 | 307 | formatResult: formatSelect2Result, |
ba4fb2b2 CW |
308 | formatSelection: function(row) { |
309 | return row.label; | |
310 | }, | |
311 | escapeMarkup: function (m) {return m;}, | |
a88cf11a CW |
312 | initSelection: function($el, callback) { |
313 | var | |
314 | multiple = !!$el.data('select-params').multiple, | |
315 | val = $el.val(), | |
316 | stored = $el.data('entity-value') || []; | |
317 | if (val === '') { | |
318 | return; | |
319 | } | |
320 | // If we already have this data, just return it | |
321 | if (!_.xor(val.split(','), _.pluck(stored, 'id')).length) { | |
322 | callback(multiple ? stored : stored[0]); | |
323 | } else { | |
324 | var params = $el.data('api-params') || {}; | |
325 | params.id = val; | |
326 | CRM.api3($el.data('api-entity'), 'getlist', params).done(function(result) { | |
327 | callback(multiple ? result.values : result.values[0]) | |
328 | }); | |
329 | } | |
ba4fb2b2 CW |
330 | } |
331 | }; | |
332 | if ($el.data('create-links')) { | |
a88cf11a CW |
333 | selectParams.formatInputTooShort = function() { |
334 | var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort.call(this); | |
a4799f04 CW |
335 | if ($el.data('create-links')) { |
336 | txt += ' ' + ts('or') + '<br />' + formatSelect2CreateLinks($el); | |
ba4fb2b2 CW |
337 | } |
338 | return txt; | |
339 | }; | |
a88cf11a | 340 | selectParams.formatNoMatches = function() { |
ba4fb2b2 | 341 | var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches; |
a4799f04 | 342 | return txt + '<br />' + formatSelect2CreateLinks($el); |
ba4fb2b2 | 343 | }; |
a88cf11a | 344 | $el.off('.createLinks').on('select2-open.createLinks', function() { |
ba4fb2b2 CW |
345 | var $el = $(this); |
346 | $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
347 | $el.select2('close'); | |
348 | CRM.loadForm($(this).attr('href'), { | |
349 | dialog: {width: 500, height: 'auto'} | |
350 | }).on('crmFormSuccess', function(e, data) { | |
c92f6436 CW |
351 | if (data.status === 'success' && data.id) { |
352 | if ($el.select2('container').hasClass('select2-container-multi')) { | |
353 | var selection = $el.select2('data'); | |
354 | selection.push(data); | |
355 | $el.select2('data', selection, true); | |
356 | } else { | |
357 | $el.select2('data', data, true); | |
358 | } | |
ba4fb2b2 CW |
359 | } |
360 | }); | |
361 | return false; | |
362 | }); | |
363 | }); | |
364 | } | |
a88cf11a CW |
365 | $el.crmSelect2($.extend(settings, $el.data('select-params'), selectParams)); |
366 | }); | |
ba4fb2b2 CW |
367 | }; |
368 | ||
a4799f04 | 369 | function formatSelect2Result(row) { |
88881f79 | 370 | var markup = '<div class="crm-select2-row">'; |
ff88d165 | 371 | if (row.image !== undefined) { |
88881f79 | 372 | markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>'; |
ff88d165 | 373 | } |
54bee7df | 374 | else if (row.icon_class) { |
88881f79 | 375 | markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>'; |
54bee7df | 376 | } |
88881f79 CW |
377 | markup += '<div><div class="crm-select2-row-label">' + row.label + '</div>'; |
378 | markup += '<div class="crm-select2-row-description">'; | |
379 | $.each(row.description || [], function(k, text) { | |
380 | markup += '<p>' + text + '</p>'; | |
381 | }); | |
382 | markup += '</div></div></div>'; | |
ff88d165 | 383 | return markup; |
a4799f04 CW |
384 | } |
385 | ||
386 | function formatSelect2CreateLinks($el) { | |
387 | var | |
388 | createLinks = $el.data('create-links'), | |
389 | api = $el.data('api-params') || {}, | |
390 | type = api.params ? api.params.contact_type : null; | |
391 | if (createLinks === true) { | |
392 | createLinks = type ? _.where(CRM.profile.contactCreate, {type: type}) : CRM.profile.contactCreate; | |
393 | } | |
79ae07d9 | 394 | var markup = ''; |
a4799f04 | 395 | _.each(createLinks, function(link) { |
79ae07d9 | 396 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; |
a4799f04 CW |
397 | if (link.type) { |
398 | markup += '<span class="icon ' + link.type + '-profile-icon"></span> '; | |
79ae07d9 CW |
399 | } |
400 | markup += link.label + '</a>'; | |
401 | }); | |
402 | return markup; | |
a4799f04 | 403 | } |
ff88d165 | 404 | |
f7b92fcd | 405 | // Initialize widgets |
d664f648 CW |
406 | $(document).on('crmLoad', function(e) { |
407 | $('table.row-highlight', e.target) | |
408 | .off('.rowHighlight') | |
409 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function () { | |
410 | var target, table = $(this).closest('table'); | |
411 | if ($(this).hasClass('select-rows')) { | |
412 | target = $('tbody tr', table); | |
413 | $('input.select-row', table).prop('checked', $(this).prop('checked')); | |
414 | } | |
415 | else { | |
416 | target = $(this).closest('tr'); | |
417 | $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1); | |
418 | } | |
419 | target.toggleClass('crm-row-selected', $(this).is(':checked')); | |
420 | }) | |
421 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
a88cf11a CW |
422 | $('.crm-select2:not(.select2-offscreen)', e.target).crmSelect2(); |
423 | $('.crm-form-entityref:not(.select2-offscreen)', e.target).crmEntityRef(); | |
6a488035 | 424 | }); |
148c4e8d CW |
425 | |
426 | /** | |
427 | * Function to make multiselect boxes behave as fields in small screens | |
428 | */ | |
429 | function advmultiselectResize() { | |
430 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
431 | if (amswidth < 700) { | |
432 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
433 | } |
434 | else { | |
148c4e8d CW |
435 | $("form table.advmultiselect td").css('display', 'table-cell'); |
436 | } | |
437 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
438 | if (contactwidth < 600) { | |
439 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 440 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 441 | if (index > 1) { |
0f5816a6 | 442 | if (index % 2 == 0) { |
148c4e8d CW |
443 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
444 | } | |
445 | var item = $(this); | |
446 | $(this).parent().next().append(item); | |
447 | } | |
448 | }); | |
0f5816a6 KJ |
449 | } |
450 | else { | |
148c4e8d | 451 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 452 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
453 | var nitem = $(this); |
454 | var parent = $(this).parent(); | |
455 | $(this).parent().prev().append(nitem); | |
0f5816a6 | 456 | if (parent.children().size() == 0) { |
148c4e8d CW |
457 | parent.remove(); |
458 | } | |
459 | }); | |
460 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
461 | } | |
462 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 463 | |
148c4e8d CW |
464 | if (cformwidth < 720) { |
465 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
466 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
467 | if (cformwidth < 480) { | |
468 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
469 | } |
470 | else { | |
148c4e8d CW |
471 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
472 | } | |
0f5816a6 KJ |
473 | } |
474 | else { | |
148c4e8d CW |
475 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
476 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
477 | } | |
478 | } | |
0f5816a6 | 479 | |
148c4e8d | 480 | advmultiselectResize(); |
0f5816a6 | 481 | $(window).resize(function () { |
6a488035 TO |
482 | advmultiselectResize(); |
483 | }); | |
484 | ||
0f5816a6 | 485 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
486 | $(document) |
487 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
488 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
489 | $(this).addClass('crm-tooltip-active'); |
490 | var topDistance = e.pageY - $(window).scrollTop(); | |
491 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { | |
492 | $(this).addClass('crm-tooltip-down'); | |
493 | } | |
494 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
495 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
496 | $(this).children().children('.crm-tooltip') | |
497 | .html('<div class="crm-loading-element"></div>') | |
498 | .load(this.href); | |
499 | } | |
500 | }) | |
2c29c2ac RN |
501 | .on('mouseout', 'a.crm-summary-link', function () { |
502 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
503 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
504 | }) | |
2c29c2ac | 505 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
506 | }; |
507 | ||
b0ca6188 | 508 | var helpDisplay, helpPrevious; |
8e3272a1 | 509 | CRM.help = function (title, params, url) { |
55a93b02 | 510 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 511 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
512 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
513 | helpDisplay.close(); | |
b0ca6188 CW |
514 | return; |
515 | } | |
55a93b02 | 516 | helpDisplay.close(); |
b0ca6188 CW |
517 | } |
518 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
519 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
520 | params.type = 'page'; | |
b0ca6188 | 521 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 522 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
523 | { |
524 | data: params, | |
525 | dataType: 'html', | |
e24b17b9 | 526 | success: function (data) { |
6a488035 TO |
527 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
528 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
529 | }, | |
e24b17b9 | 530 | error: function () { |
6a488035 TO |
531 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
532 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
533 | } | |
534 | } | |
535 | ); | |
536 | }; | |
8960d9b9 CW |
537 | /** |
538 | * @param startMsg string | |
539 | * @param endMsg string|function | |
540 | * @param deferred optional jQuery deferred object | |
541 | * @return jQuery deferred object - if not supplied a new one will be created | |
542 | */ | |
543 | var fadeOut; | |
544 | CRM.status = function(startMsg, endMsg, deferred) { | |
545 | var $bar = $('#civicrm-menu'); | |
546 | if (!$bar.length) { | |
547 | console && console.log && console.log('CRM.status called on a page with no menubar'); | |
548 | return; | |
549 | } | |
550 | $('.crm-menubar-status-container', $bar).remove(); | |
551 | fadeOut && window.clearTimeout(fadeOut); | |
552 | $bar.append('<li class="crm-menubar-status-container status-busy"><div class="crm-menubar-status-progressbar"><div class="crm-menubar-status-msg">' + startMsg + '</div></div></li>'); | |
553 | $('.crm-menubar-status-container', $bar).css('min-width', $('.crm-menubar-status-container', $bar).width()); | |
554 | deferred || (deferred = new $.Deferred()); | |
555 | deferred.done(function(data) { | |
556 | var msg = typeof(endMsg) === 'function' ? endMsg(data) : endMsg; | |
4bad157e CW |
557 | $('.crm-menubar-status-container', $bar).removeClass('status-busy').addClass('status-done').show().find('.crm-menubar-status-msg').html(msg); |
558 | if (msg) { | |
559 | fadeOut = window.setTimeout(function() { | |
560 | $('.crm-menubar-status-container', $bar).fadeOut('slow'); | |
561 | }, 2000); | |
562 | } else { | |
563 | $('.crm-menubar-status-container', $bar).hide(); | |
564 | } | |
8960d9b9 CW |
565 | }); |
566 | return deferred; | |
567 | }; | |
6a488035 TO |
568 | |
569 | /** | |
570 | * @param string text Displayable message | |
571 | * @param string title Displayable title | |
572 | * @param string type 'alert'|'info'|'success'|'error' (default: 'alert') | |
573 | * @param {object} options | |
574 | * @return {*} | |
575 | * @see http://wiki.civicrm.org/confluence/display/CRM/Notifications+in+CiviCRM | |
576 | */ | |
0f5816a6 | 577 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
578 | type = type || 'alert'; |
579 | title = title || ''; | |
580 | options = options || {}; | |
581 | if ($('#crm-notification-container').length) { | |
582 | var params = { | |
583 | text: text, | |
584 | title: title, | |
585 | type: type | |
586 | }; | |
587 | // By default, don't expire errors and messages containing links | |
588 | var extra = { | |
589 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
590 | unique: true | |
591 | }; | |
592 | options = $.extend(extra, options); | |
e24b17b9 | 593 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 594 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 595 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
596 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
597 | $('.icon.ui-notify-close', this).click(); | |
598 | } | |
599 | }); | |
600 | } | |
601 | return $('#crm-notification-container').notify('create', params, options); | |
602 | } | |
603 | else { | |
604 | if (title.length) { | |
605 | text = title + "\n" + text; | |
606 | } | |
607 | alert(text); | |
608 | return null; | |
609 | } | |
e24b17b9 | 610 | }; |
6a488035 TO |
611 | |
612 | /** | |
613 | * Close whichever alert contains the given node | |
614 | * | |
615 | * @param node | |
616 | */ | |
0f5816a6 | 617 | CRM.closeAlertByChild = function (node) { |
6a488035 | 618 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 619 | }; |
6a488035 TO |
620 | |
621 | /** | |
622 | * Prompt the user for confirmation. | |
623 | * | |
7553cf23 CW |
624 | * @param buttons {object|function} key|value pairs where key == button label and value == callback function |
625 | * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue" | |
626 | * @param options {object|void} Override defaults, keys include 'title', 'message', | |
627 | * see jQuery.dialog for full list of available params | |
ebb5fc58 | 628 | * @param cancelLabel {string} |
6a488035 | 629 | */ |
706cff6d | 630 | CRM.confirm = function (buttons, options, cancelLabel) { |
7553cf23 | 631 | var dialog, callbacks = {}; |
706cff6d | 632 | cancelLabel = cancelLabel || ts('Cancel'); |
7553cf23 CW |
633 | var settings = { |
634 | title: ts('Confirm Action'), | |
635 | message: ts('Are you sure you want to continue?'), | |
6a488035 TO |
636 | resizable: false, |
637 | modal: true, | |
0d5f99d4 | 638 | width: 'auto', |
0f5816a6 KJ |
639 | close: function () { |
640 | $(dialog).remove(); | |
641 | }, | |
7553cf23 CW |
642 | buttons: {} |
643 | }; | |
706cff6d KJ |
644 | |
645 | settings.buttons[cancelLabel] = function () { | |
0f5816a6 KJ |
646 | dialog.dialog('close'); |
647 | }; | |
7553cf23 CW |
648 | options = options || {}; |
649 | $.extend(settings, options); | |
650 | if (typeof(buttons) === 'function') { | |
651 | callbacks[ts('Continue')] = buttons; | |
0f5816a6 KJ |
652 | } |
653 | else { | |
7553cf23 CW |
654 | callbacks = buttons; |
655 | } | |
0f5816a6 KJ |
656 | $.each(callbacks, function (label, callback) { |
657 | settings.buttons[label] = function () { | |
ebb5fc58 CW |
658 | if (callback.call(dialog) !== false) { |
659 | dialog.dialog('close'); | |
660 | } | |
e24b17b9 | 661 | }; |
6a488035 | 662 | }); |
7553cf23 CW |
663 | dialog = $('<div class="crm-container crm-confirm-dialog"></div>') |
664 | .html(options.message) | |
665 | .appendTo('body') | |
666 | .dialog(settings); | |
667 | return dialog; | |
e24b17b9 | 668 | }; |
6a488035 TO |
669 | |
670 | /** | |
671 | * Sets an error message | |
672 | * If called for a form item, title and removal condition will be handled automatically | |
673 | */ | |
0f5816a6 | 674 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
675 | title = title || ''; |
676 | text = text || ''; | |
677 | options = options || {}; | |
678 | ||
679 | var extra = { | |
680 | expires: 0 | |
681 | }; | |
682 | if ($(this).length) { | |
683 | if (title == '') { | |
684 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); | |
685 | if (label.length) { | |
686 | label.addClass('crm-error'); | |
687 | var $label = label.clone(); | |
688 | if (text == '' && $('.crm-marker', $label).length > 0) { | |
689 | text = $('.crm-marker', $label).attr('title'); | |
690 | } | |
691 | $('.crm-marker', $label).remove(); | |
692 | title = $label.text(); | |
693 | } | |
694 | } | |
695 | $(this).addClass('error'); | |
696 | } | |
697 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
698 | if ($(this).length) { | |
699 | var ele = $(this); | |
0f5816a6 KJ |
700 | setTimeout(function () { |
701 | ele.one('change', function () { | |
702 | msg && msg.close && msg.close(); | |
703 | ele.removeClass('error'); | |
704 | label.removeClass('crm-error'); | |
705 | }); | |
706 | }, 1000); | |
6a488035 TO |
707 | } |
708 | return msg; | |
e24b17b9 | 709 | }; |
6a488035 TO |
710 | |
711 | // Display system alerts through js notifications | |
712 | function messagesFromMarkup() { | |
0f5816a6 | 713 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 714 | var text, title = ''; |
6a488035 TO |
715 | $(this).removeClass('status messages'); |
716 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
717 | type = type.replace('crm-', ''); | |
718 | $('.icon', this).remove(); | |
6a488035 | 719 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 720 | text = $('.msg-text', this).html(); |
6a488035 TO |
721 | title = $('.msg-title', this).html(); |
722 | } | |
723 | else { | |
e24b17b9 | 724 | text = $(this).html(); |
6a488035 TO |
725 | } |
726 | var options = $(this).data('options') || {}; | |
727 | $(this).remove(); | |
728 | // Duplicates were already removed server-side | |
729 | options.unique = false; | |
730 | CRM.alert(text, title, type, options); | |
731 | }); | |
732 | // Handle qf form errors | |
733 | $('form :input.error', this).one('blur', function() { | |
ef04c554 | 734 | // ignore autocomplete fields |
23246a80 | 735 | if ($(this).is('.ac_input')) { |
736 | return; | |
737 | } | |
ef04c554 | 738 | |
6a488035 TO |
739 | $('.ui-notify-message.error a.ui-notify-close').click(); |
740 | $(this).removeClass('error'); | |
741 | $(this).next('span.crm-error').remove(); | |
742 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
743 | .removeClass('crm-error') | |
744 | .find('.crm-error').removeClass('crm-error'); | |
745 | }); | |
746 | } | |
747 | ||
23223213 | 748 | $.widget('civi.crmSnippet', { |
205bb8ae | 749 | options: { |
a73f25bb | 750 | url: null, |
205bb8ae CW |
751 | block: true, |
752 | crmForm: null | |
753 | }, | |
c02f8fc4 | 754 | _originalContent: null, |
fa3a5fe2 CW |
755 | _originalUrl: null, |
756 | isOriginalUrl: function() { | |
0906345e CW |
757 | var |
758 | args = {}, | |
759 | same = true, | |
760 | newUrl = this._formatUrl(this.options.url), | |
761 | oldUrl = this._formatUrl(this._originalUrl); | |
fa3a5fe2 | 762 | // Compare path |
0906345e | 763 | if (newUrl.split('?')[0] !== oldUrl.split('?')[0]) { |
fa3a5fe2 CW |
764 | return false; |
765 | } | |
766 | // Compare arguments | |
0906345e | 767 | $.each(newUrl.split('?')[1].split('&'), function(k, v) { |
fa3a5fe2 CW |
768 | var arg = v.split('='); |
769 | args[arg[0]] = arg[1]; | |
770 | }); | |
0906345e | 771 | $.each(oldUrl.split('?')[1].split('&'), function(k, v) { |
fa3a5fe2 CW |
772 | var arg = v.split('='); |
773 | if (args[arg[0]] !== undefined && arg[1] !== args[arg[0]]) { | |
774 | same = false; | |
775 | } | |
776 | }); | |
777 | return same; | |
778 | }, | |
779 | resetUrl: function() { | |
780 | this.options.url = this._originalUrl; | |
781 | }, | |
205bb8ae | 782 | _create: function() { |
205bb8ae CW |
783 | this.element.addClass('crm-ajax-container'); |
784 | if (!this.element.is('.crm-container *')) { | |
785 | this.element.addClass('crm-container'); | |
786 | } | |
4a140040 | 787 | this._handleOrderLinks(); |
5d92a7e7 CW |
788 | // Set default if not supplied |
789 | this.options.url = this.options.url || document.location.href; | |
fa3a5fe2 | 790 | this._originalUrl = this.options.url; |
205bb8ae CW |
791 | }, |
792 | _onFailure: function(data) { | |
793 | this.options.block && this.element.unblock(); | |
794 | this.element.trigger('crmAjaxFail', data); | |
795 | CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); | |
796 | }, | |
797 | _formatUrl: function(url) { | |
0906345e CW |
798 | // Strip hash |
799 | url = url.split('#')[0]; | |
205bb8ae CW |
800 | // Add snippet argument to url |
801 | if (url.search(/[&?]snippet=/) < 0) { | |
fc05b8da | 802 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json'; |
8547369d CW |
803 | } else { |
804 | url = url.replace(/snippet=[^&]*/, 'snippet=json'); | |
205bb8ae CW |
805 | } |
806 | return url; | |
807 | }, | |
d6539f93 | 808 | // Hack to deal with civicrm legacy sort functionality |
4a140040 CW |
809 | _handleOrderLinks: function() { |
810 | var that = this; | |
811 | $('a.crm-weight-arrow', that.element).click(function(e) { | |
812 | that.options.block && that.element.block(); | |
813 | $.getJSON(that._formatUrl(this.href)).done(function() { | |
814 | that.refresh(); | |
815 | }); | |
816 | e.stopImmediatePropagation(); | |
817 | return false; | |
818 | }); | |
819 | }, | |
205bb8ae CW |
820 | refresh: function() { |
821 | var that = this; | |
822 | var url = this._formatUrl(this.options.url); | |
4a140040 | 823 | this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block(); |
205bb8ae CW |
824 | $.getJSON(url, function(data) { |
825 | if (typeof(data) != 'object' || typeof(data.content) != 'string') { | |
826 | that._onFailure(data); | |
827 | return; | |
828 | } | |
829 | data.url = url; | |
c02f8fc4 CW |
830 | that.element.trigger('crmBeforeLoad', data); |
831 | if (that._originalContent === null) { | |
832 | that._originalContent = that.element.contents().detach(); | |
833 | } | |
834 | that.element.html(data.content); | |
4a140040 CW |
835 | that._handleOrderLinks(); |
836 | that.element.trigger('crmLoad', data); | |
205bb8ae CW |
837 | that.options.crmForm && that.element.trigger('crmFormLoad', data); |
838 | }).fail(function() { | |
839 | that._onFailure(); | |
840 | }); | |
4b628e67 CW |
841 | }, |
842 | _destroy: function() { | |
843 | this.element.removeClass('crm-ajax-container'); | |
c02f8fc4 CW |
844 | if (this._originalContent !== null) { |
845 | this.element.empty().append(this._originalContent); | |
846 | } | |
205bb8ae CW |
847 | } |
848 | }); | |
849 | ||
0e017a41 | 850 | var dialogCount = 0; |
d4e4e9df | 851 | CRM.loadPage = function(url, options) { |
d4e4e9df | 852 | var settings = { |
0e017a41 | 853 | target: '#crm-ajax-dialog-' + (dialogCount++), |
8547369d CW |
854 | dialog: false |
855 | }; | |
856 | if (!options || !options.target) { | |
857 | settings.dialog = { | |
d4e4e9df | 858 | modal: true, |
f84151fd CW |
859 | width: '65%', |
860 | height: parseInt($(window).height() * .75), | |
d4e4e9df | 861 | close: function() { |
c02f8fc4 | 862 | $(this).dialog('destroy').remove(); |
d4e4e9df | 863 | } |
8547369d CW |
864 | }; |
865 | } | |
205bb8ae | 866 | options && $.extend(true, settings, options); |
0e017a41 | 867 | settings.url = url; |
83df6b4a | 868 | // Create new dialog |
8547369d | 869 | if (settings.dialog) { |
205bb8ae | 870 | $('<div id="'+ settings.target.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog); |
d4e4e9df | 871 | } |
205bb8ae | 872 | if (settings.dialog && !settings.dialog.title) { |
cbc9c3b2 CW |
873 | $(settings.target).on('crmLoad', function(e, data) { |
874 | if (e.target === $(settings.target)[0] && data && data.title) { | |
875 | $(this).dialog('option', 'title', data.title); | |
876 | } | |
205bb8ae CW |
877 | }); |
878 | } | |
5d92a7e7 CW |
879 | $(settings.target).crmSnippet(settings).crmSnippet('refresh'); |
880 | return $(settings.target); | |
d4e4e9df CW |
881 | }; |
882 | ||
883 | CRM.loadForm = function(url, options) { | |
d4e4e9df | 884 | var settings = { |
205bb8ae CW |
885 | crmForm: { |
886 | ajaxForm: {}, | |
205bb8ae CW |
887 | autoClose: true, |
888 | validate: true, | |
889 | refreshAction: ['next_new', 'submit_savenext'], | |
890 | cancelButton: '.cancel.form-submit', | |
23223213 | 891 | openInline: 'a.button:not("[href=#], .no-popup")', |
205bb8ae CW |
892 | onCancel: function(event) {}, |
893 | onError: function(data) { | |
894 | var $el = $(this); | |
5d92a7e7 | 895 | $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data).trigger('crmFormError', data); |
205bb8ae CW |
896 | if (typeof(data.errors) == 'object') { |
897 | $.each(data.errors, function(formElement, msg) { | |
898 | $('[name="'+formElement+'"]', $el).crmError(msg); | |
899 | }); | |
900 | } | |
d4e4e9df | 901 | } |
d4e4e9df CW |
902 | } |
903 | }; | |
a10432db CW |
904 | // Hack to make delete dialogs smaller |
905 | if (url.indexOf('/delete') > 0 || url.indexOf('action=delete') > 0) { | |
906 | settings.dialog = { | |
907 | width: 400, | |
908 | height: 300 | |
909 | }; | |
910 | } | |
205bb8ae CW |
911 | // Move options that belong to crmForm. Others will be passed through to crmSnippet |
912 | options && $.each(options, function(key, value) { | |
913 | if (typeof(settings.crmForm[key]) !== 'undefined') { | |
914 | settings.crmForm[key] = value; | |
915 | } | |
916 | else { | |
917 | settings[key] = value; | |
918 | } | |
919 | }); | |
205bb8ae CW |
920 | |
921 | var widget = CRM.loadPage(url, settings); | |
922 | ||
923 | widget.on('crmFormLoad', function(event, data) { | |
924 | var $el = $(this); | |
660c46f3 | 925 | var settings = $el.crmSnippet('option', 'crmForm'); |
205bb8ae CW |
926 | settings.cancelButton && $(settings.cancelButton, this).click(function(event) { |
927 | var returnVal = settings.onCancel.call($el, event); | |
928 | if (returnVal !== false) { | |
929 | $el.trigger('crmFormCancel', event); | |
660c46f3 | 930 | if ($el.data('uiDialog') && settings.autoClose) { |
fa3a5fe2 CW |
931 | $el.dialog('close'); |
932 | } | |
933 | else if (!settings.autoClose) { | |
934 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
935 | } | |
0e017a41 | 936 | } |
205bb8ae CW |
937 | return returnVal === false; |
938 | }); | |
d4e4e9df | 939 | if (settings.validate) { |
0e017a41 | 940 | $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); |
d4e4e9df | 941 | } |
205bb8ae | 942 | $("form", this).ajaxForm($.extend({ |
fa3a5fe2 | 943 | url: data.url.replace(/reset=1[&]?/, ''), |
d4e4e9df CW |
944 | dataType: 'json', |
945 | success: function(response) { | |
34866662 | 946 | if (response.status !== 'form_error') { |
36876f55 | 947 | $el.crmSnippet('option', 'block') && $el.unblock(); |
205bb8ae | 948 | $el.trigger('crmFormSuccess', response); |
0e017a41 | 949 | // Reset form for e.g. "save and new" |
d6539f93 | 950 | if (response.userContext && settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0) { |
205bb8ae | 951 | $el.crmSnippet('option', 'url', response.userContext).crmSnippet('refresh'); |
0e017a41 | 952 | } |
660c46f3 | 953 | else if ($el.data('uiDialog') && settings.autoClose) { |
205bb8ae | 954 | $el.dialog('close'); |
83df6b4a | 955 | } |
d6539f93 CW |
956 | else if (settings.autoClose === false) { |
957 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
958 | } | |
d4e4e9df CW |
959 | } |
960 | else { | |
25bbc4c0 | 961 | response.url = data.url; |
205bb8ae | 962 | settings.onError.call($el, response); |
d4e4e9df | 963 | } |
23223213 CW |
964 | }, |
965 | beforeSerialize: function(form, options) { | |
966 | if (window.CKEDITOR && window.CKEDITOR.instances) { | |
a10432db CW |
967 | $.each(CKEDITOR.instances, function() { |
968 | this.updateElement && this.updateElement(); | |
969 | }); | |
23223213 | 970 | } |
83df6b4a | 971 | }, |
205bb8ae | 972 | beforeSubmit: function(submission) { |
36876f55 | 973 | $el.crmSnippet('option', 'block') && $el.block(); |
205bb8ae | 974 | $el.trigger('crmFormSubmit', submission); |
d4e4e9df | 975 | } |
205bb8ae | 976 | }, settings.ajaxForm)); |
fa3a5fe2 CW |
977 | if (settings.openInline) { |
978 | settings.autoClose = $el.crmSnippet('isOriginalUrl'); | |
979 | $(settings.openInline, this).click(function(event) { | |
980 | $el.crmSnippet('option', 'url', $(this).attr('href')).crmSnippet('refresh'); | |
981 | return false; | |
982 | }); | |
983 | } | |
81c81624 CW |
984 | // For convenience, focus the first field |
985 | $('input[type=text], textarea, select', this).filter(':visible').first().focus(); | |
205bb8ae CW |
986 | }); |
987 | return widget; | |
d4e4e9df CW |
988 | }; |
989 | ||
03a7ec8f CW |
990 | // Preprocess all cj ajax calls to display messages |
991 | $(document).ajaxSuccess(function(event, xhr, settings) { | |
992 | try { | |
993 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
994 | var response = $.parseJSON(xhr.responseText); | |
995 | if (typeof(response.crmMessages) == 'object') { | |
996 | $.each(response.crmMessages, function(n, msg) { | |
997 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
998 | }) | |
999 | } | |
1000 | } | |
1001 | } | |
1002 | // Suppress errors | |
1003 | catch (e) {} | |
1004 | }); | |
1005 | ||
7cb72342 CW |
1006 | /** |
1007 | * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin | |
1008 | */ | |
1009 | $.widget('civi.crmAutocomplete', $.ui.autocomplete, {}); | |
1010 | ||
0f5816a6 | 1011 | $(function () { |
205bb8ae | 1012 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 1013 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 1014 | |
ef3309b6 | 1015 | if ($('#crm-notification-container').length) { |
6a488035 TO |
1016 | // Initialize notifications |
1017 | $('#crm-notification-container').notify(); | |
1018 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 1019 | } |
ebb9197b C |
1020 | |
1021 | // bind the event for image popup | |
475e9f44 CW |
1022 | $('body') |
1023 | .on('click', 'a.crm-image-popup', function() { | |
1024 | var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>'); | |
1025 | ||
1026 | CRM.confirm('', | |
1027 | { | |
1028 | title: ts('Preview'), | |
1029 | message: o | |
1030 | }, | |
1031 | ts('Done') | |
1032 | ); | |
1033 | return false; | |
1034 | }) | |
ebb9197b | 1035 | |
475e9f44 CW |
1036 | .on('click', function (event) { |
1037 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
1038 | if ($(event.target).is('.btn-slide')) { | |
1039 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
1040 | } | |
1041 | }) | |
d664f648 | 1042 | |
87831073 CW |
1043 | .on('click', 'a.crm-option-edit-link', function() { |
1044 | var link = $(this); | |
1045 | CRM.loadForm(this.href, {openInline: 'a:not("[href=#], .no-popup")'}) | |
1046 | // Lots of things can happen once the form opens, this is the only event we can really rely on | |
475e9f44 | 1047 | .on('dialogclose', function() { |
87831073 CW |
1048 | link.trigger('crmOptionsEdited'); |
1049 | var $elects = $('select[data-option-edit-path="' + link.data('option-edit-path') + '"]'); | |
1050 | if ($elects.data('api-entity') && $elects.data('api-field')) { | |
1051 | CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) | |
1052 | .done(function(data) { | |
1053 | CRM.utils.setOptions($elects, data.values); | |
1054 | }); | |
1055 | } | |
475e9f44 CW |
1056 | }); |
1057 | return false; | |
4a143c04 CW |
1058 | }) |
1059 | // Handle clear button for form elements | |
1060 | .on('click', 'a.crm-clear-link', function() { | |
1061 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change(); | |
bcb4280c | 1062 | $(this).siblings('input:text').val('').change(); |
4a143c04 CW |
1063 | return false; |
1064 | }) | |
1065 | .on('change', 'input.crm-form-radio:checked', function() { | |
1066 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
475e9f44 | 1067 | }); |
d664f648 | 1068 | $().crmtooltip(); |
6a488035 TO |
1069 | }); |
1070 | ||
0f5816a6 | 1071 | $.fn.crmAccordions = function (speed) { |
cf021bc5 CW |
1072 | var container = $(this).length > 0 ? $(this) : $('.crm-container'); |
1073 | speed = speed === undefined ? 200 : speed; | |
1074 | container | |
1075 | .off('click.crmAccordions') | |
6a488035 | 1076 | // Allow normal clicking of links |
cf021bc5 | 1077 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
6a488035 | 1078 | e.stopPropagation && e.stopPropagation(); |
cf021bc5 CW |
1079 | }) |
1080 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () { | |
6a488035 TO |
1081 | if ($(this).parent().hasClass('collapsed')) { |
1082 | $(this).next().css('display', 'none').slideDown(speed); | |
1083 | } | |
1084 | else { | |
1085 | $(this).next().css('display', 'block').slideUp(speed); | |
1086 | } | |
1087 | $(this).parent().toggleClass('collapsed'); | |
1088 | return false; | |
1089 | }); | |
6a488035 | 1090 | }; |
0f5816a6 KJ |
1091 | $.fn.crmAccordionToggle = function (speed) { |
1092 | $(this).each(function () { | |
6a488035 TO |
1093 | if ($(this).hasClass('collapsed')) { |
1094 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
1095 | } | |
1096 | else { | |
1097 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
1098 | } | |
1099 | $(this).toggleClass('collapsed'); | |
1100 | }); | |
1101 | }; | |
5ec182d9 CW |
1102 | |
1103 | /** | |
1104 | * Clientside currency formatting | |
1105 | * @param value | |
3bdb644f | 1106 | * @param format - currency representation of the number 1234.56 |
5ec182d9 | 1107 | * @return string |
3bdb644f | 1108 | * @see CRM_Core_Resources::addCoreResources |
5ec182d9 CW |
1109 | */ |
1110 | var currencyTemplate; | |
1111 | CRM.formatMoney = function(value, format) { | |
1112 | var decimal, separator, sign, i, j, result; | |
1113 | if (value === 'init' && format) { | |
1114 | currencyTemplate = format; | |
1115 | return; | |
1116 | } | |
1117 | format = format || currencyTemplate; | |
1118 | result = /1(.?)234(.?)56/.exec(format); | |
1119 | if (result === null) { | |
1120 | return 'Invalid format passed to CRM.formatMoney'; | |
1121 | } | |
1122 | separator = result[1]; | |
1123 | decimal = result[2]; | |
1124 | sign = (value < 0) ? '-' : ''; | |
1125 | //extracting the absolute value of the integer part of the number and converting to string | |
1126 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
1127 | j = ((j = i.length) > 3) ? j % 3 : 0; |
1128 | 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) : ''); | |
1129 | return format.replace(/1.*234.*56/, result); | |
1130 | }; | |
6a488035 | 1131 | })(jQuery); |