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