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 | ||
ff88d165 CW |
276 | CRM.utils.formatSelect2Result = function(row) { |
277 | var markup = '<table class="crm-select2-row"><tr>'; | |
278 | if (row.image !== undefined) { | |
3a9e5e69 | 279 | markup += '<td class="crm-select2-image"><img src="' + row.image + '"/></td>'; |
ff88d165 | 280 | } |
3a9e5e69 | 281 | markup += '<td><div class="crm-select2-row-label">' + row.label + '</div>'; |
ff88d165 CW |
282 | markup += '<div class="crm-select2-row-description">' + (row.description || '') + '</div>'; |
283 | markup += '</td></tr></table>'; | |
284 | return markup; | |
285 | }; | |
79ae07d9 CW |
286 | |
287 | CRM.utils.formatSelect2CreateLinks = function($el) { | |
288 | var markup = ''; | |
289 | $.each($el.data('create-links'), function(k, link) { | |
290 | markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">'; | |
291 | if (link.name) { | |
292 | markup += '<span class="icon ' + link.name + '-icon"></span> '; | |
293 | } | |
294 | markup += link.label + '</a>'; | |
295 | }); | |
296 | return markup; | |
297 | }; | |
ff88d165 | 298 | |
f7b92fcd | 299 | // Initialize widgets |
d664f648 CW |
300 | $(document).on('crmLoad', function(e) { |
301 | $('table.row-highlight', e.target) | |
302 | .off('.rowHighlight') | |
303 | .on('change.rowHighlight', 'input.select-row, input.select-rows', function () { | |
304 | var target, table = $(this).closest('table'); | |
305 | if ($(this).hasClass('select-rows')) { | |
306 | target = $('tbody tr', table); | |
307 | $('input.select-row', table).prop('checked', $(this).prop('checked')); | |
308 | } | |
309 | else { | |
310 | target = $(this).closest('tr'); | |
311 | $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1); | |
312 | } | |
313 | target.toggleClass('crm-row-selected', $(this).is(':checked')); | |
314 | }) | |
315 | .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); | |
f7b92fcd | 316 | $('.crm-select2', e.target).each(function() { |
ff88d165 | 317 | var $el = $(this); |
f411b7d5 CW |
318 | // quickform doesn't support optgroups so here's a hack :( |
319 | $('option[value^=crm_optgroup]', this).each(function() { | |
320 | $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />'); | |
321 | $(this).remove(); | |
322 | }); | |
79ae07d9 CW |
323 | // Get a copy of the data rather than a reference |
324 | var options = $.extend({}, $el.data('select-params') || {}); | |
a3162197 | 325 | // Set placeholder from markup if not specified |
ff88d165 CW |
326 | if ($el.is('select:not([multiple])')) { |
327 | options.allowClear = options.allowClear !== undefined ? options.allowClear : !($el.hasClass('required')); | |
a3162197 CW |
328 | if (options.placeHolder === undefined && $('option:first', this).val() === '') { |
329 | options.placeholderOption = 'first'; | |
330 | } | |
331 | } | |
76ec9ca7 | 332 | // Autocomplete using the getlist api |
ff88d165 CW |
333 | if ($el.data('api-entity') && $el.hasClass('crm-form-entityref')) { |
334 | $el.addClass('crm-ajax-select'); | |
79ae07d9 CW |
335 | var settings = { |
336 | // Use select2 ajax helper instead of CRM.api because it provides more value | |
ff88d165 CW |
337 | ajax: { |
338 | url: CRM.url('civicrm/ajax/rest'), | |
339 | data: function (input, page_num) { | |
340 | var params = $el.data('api-params') || {}; | |
341 | params.input = input; | |
342 | params.page_num = page_num; | |
343 | return { | |
344 | entity: $el.data('api-entity'), | |
345 | action: 'getlist', | |
346 | json: JSON.stringify(params) | |
347 | }; | |
348 | }, | |
349 | results: function(data) { | |
350 | return {more: data.more_results, results: data.values || []}; | |
47f21f3a | 351 | } |
ff88d165 CW |
352 | }, |
353 | formatResult: CRM.utils.formatSelect2Result, | |
354 | formatSelection: function(row) { | |
355 | return row.label; | |
356 | }, | |
357 | escapeMarkup: function (m) {return m;}, | |
358 | initSelection: function(el, callback) { | |
359 | callback(el.data('entity-value')); | |
360 | } | |
79ae07d9 CW |
361 | }; |
362 | if ($el.data('create-links')) { | |
363 | options.formatInputTooShort = function() { | |
364 | return $el.data('select-params').formatInputTooShort + ' ' + ts('or') + '<br />' + CRM.utils.formatSelect2CreateLinks($el); | |
365 | }; | |
366 | options.formatNoMatches = function() { | |
367 | return $el.data('select-params').formatNoMatches + '<br />' + CRM.utils.formatSelect2CreateLinks($el); | |
368 | }; | |
369 | $el.on('select2-open', function() { | |
370 | var $el = $(this); | |
371 | $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) { | |
372 | $el.select2('close'); | |
373 | CRM.loadForm($(this).attr('href'), { | |
374 | dialog: {width: 500, height: 'auto'} | |
375 | }).on('crmFormSuccess', function(e, data) { | |
376 | if ($el.select2('container').hasClass('select2-container-multi')) { | |
c5c9fd78 CW |
377 | var selection = $el.select2('data'); |
378 | selection.push(data); | |
79ae07d9 CW |
379 | $el.select2('data', selection); |
380 | } else { | |
381 | $el.select2('data', data); | |
382 | } | |
383 | }); | |
384 | return false; | |
385 | }); | |
386 | }); | |
387 | } | |
388 | options = $.extend(settings, options); | |
47f21f3a | 389 | } |
79ae07d9 | 390 | options.dropdownCssClass = 'crm-container'; |
ab345ca5 | 391 | $(this).select2(options).removeClass('crm-select2'); |
f7b92fcd | 392 | }); |
6a488035 | 393 | }); |
148c4e8d CW |
394 | |
395 | /** | |
396 | * Function to make multiselect boxes behave as fields in small screens | |
397 | */ | |
398 | function advmultiselectResize() { | |
399 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); | |
400 | if (amswidth < 700) { | |
401 | $("form table.advmultiselect td").css('display', 'block'); | |
0f5816a6 KJ |
402 | } |
403 | else { | |
148c4e8d CW |
404 | $("form table.advmultiselect td").css('display', 'table-cell'); |
405 | } | |
406 | var contactwidth = $('#crm-container #mainTabContainer').width(); | |
407 | if (contactwidth < 600) { | |
408 | $('#crm-container #mainTabContainer').addClass('narrowpage'); | |
0f5816a6 | 409 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
148c4e8d | 410 | if (index > 1) { |
0f5816a6 | 411 | if (index % 2 == 0) { |
148c4e8d CW |
412 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
413 | } | |
414 | var item = $(this); | |
415 | $(this).parent().next().append(item); | |
416 | } | |
417 | }); | |
0f5816a6 KJ |
418 | } |
419 | else { | |
148c4e8d | 420 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
0f5816a6 | 421 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
148c4e8d CW |
422 | var nitem = $(this); |
423 | var parent = $(this).parent(); | |
424 | $(this).parent().prev().append(nitem); | |
0f5816a6 | 425 | if (parent.children().size() == 0) { |
148c4e8d CW |
426 | parent.remove(); |
427 | } | |
428 | }); | |
429 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); | |
430 | } | |
431 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); | |
0f5816a6 | 432 | |
148c4e8d CW |
433 | if (cformwidth < 720) { |
434 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); | |
435 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); | |
436 | if (cformwidth < 480) { | |
437 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); | |
0f5816a6 KJ |
438 | } |
439 | else { | |
148c4e8d CW |
440 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
441 | } | |
0f5816a6 KJ |
442 | } |
443 | else { | |
148c4e8d CW |
444 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
445 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); | |
446 | } | |
447 | } | |
0f5816a6 | 448 | |
148c4e8d | 449 | advmultiselectResize(); |
0f5816a6 | 450 | $(window).resize(function () { |
6a488035 TO |
451 | advmultiselectResize(); |
452 | }); | |
453 | ||
0f5816a6 | 454 | $.fn.crmtooltip = function () { |
2c29c2ac RN |
455 | $(document) |
456 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { | |
457 | $(this).addClass('crm-processed'); | |
e24b17b9 CW |
458 | $(this).addClass('crm-tooltip-active'); |
459 | var topDistance = e.pageY - $(window).scrollTop(); | |
460 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { | |
461 | $(this).addClass('crm-tooltip-down'); | |
462 | } | |
463 | if (!$(this).children('.crm-tooltip-wrapper').length) { | |
6a488035 TO |
464 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
465 | $(this).children().children('.crm-tooltip') | |
466 | .html('<div class="crm-loading-element"></div>') | |
467 | .load(this.href); | |
468 | } | |
469 | }) | |
2c29c2ac RN |
470 | .on('mouseout', 'a.crm-summary-link', function () { |
471 | $(this).removeClass('crm-processed'); | |
e24b17b9 CW |
472 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
473 | }) | |
2c29c2ac | 474 | .on('click', 'a.crm-summary-link', false); |
6a488035 TO |
475 | }; |
476 | ||
b0ca6188 | 477 | var helpDisplay, helpPrevious; |
8e3272a1 | 478 | CRM.help = function (title, params, url) { |
55a93b02 | 479 | if (helpDisplay && helpDisplay.close) { |
b0ca6188 | 480 | // If the same link is clicked twice, just close the display - todo use underscore method for this comparison |
55a93b02 CW |
481 | if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) { |
482 | helpDisplay.close(); | |
b0ca6188 CW |
483 | return; |
484 | } | |
55a93b02 | 485 | helpDisplay.close(); |
b0ca6188 CW |
486 | } |
487 | helpPrevious = JSON.stringify(params); | |
6a488035 TO |
488 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
489 | params.type = 'page'; | |
b0ca6188 | 490 | helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0}); |
8e3272a1 | 491 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
6a488035 TO |
492 | { |
493 | data: params, | |
494 | dataType: 'html', | |
e24b17b9 | 495 | success: function (data) { |
6a488035 TO |
496 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
497 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); | |
498 | }, | |
e24b17b9 | 499 | error: function () { |
6a488035 TO |
500 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
501 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); | |
502 | } | |
503 | } | |
504 | ); | |
505 | }; | |
8960d9b9 CW |
506 | /** |
507 | * @param startMsg string | |
508 | * @param endMsg string|function | |
509 | * @param deferred optional jQuery deferred object | |
510 | * @return jQuery deferred object - if not supplied a new one will be created | |
511 | */ | |
512 | var fadeOut; | |
513 | CRM.status = function(startMsg, endMsg, deferred) { | |
514 | var $bar = $('#civicrm-menu'); | |
515 | if (!$bar.length) { | |
516 | console && console.log && console.log('CRM.status called on a page with no menubar'); | |
517 | return; | |
518 | } | |
519 | $('.crm-menubar-status-container', $bar).remove(); | |
520 | fadeOut && window.clearTimeout(fadeOut); | |
521 | $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>'); | |
522 | $('.crm-menubar-status-container', $bar).css('min-width', $('.crm-menubar-status-container', $bar).width()); | |
523 | deferred || (deferred = new $.Deferred()); | |
524 | deferred.done(function(data) { | |
525 | var msg = typeof(endMsg) === 'function' ? endMsg(data) : endMsg; | |
4bad157e CW |
526 | $('.crm-menubar-status-container', $bar).removeClass('status-busy').addClass('status-done').show().find('.crm-menubar-status-msg').html(msg); |
527 | if (msg) { | |
528 | fadeOut = window.setTimeout(function() { | |
529 | $('.crm-menubar-status-container', $bar).fadeOut('slow'); | |
530 | }, 2000); | |
531 | } else { | |
532 | $('.crm-menubar-status-container', $bar).hide(); | |
533 | } | |
8960d9b9 CW |
534 | }); |
535 | return deferred; | |
536 | }; | |
6a488035 TO |
537 | |
538 | /** | |
539 | * @param string text Displayable message | |
540 | * @param string title Displayable title | |
541 | * @param string type 'alert'|'info'|'success'|'error' (default: 'alert') | |
542 | * @param {object} options | |
543 | * @return {*} | |
544 | * @see http://wiki.civicrm.org/confluence/display/CRM/Notifications+in+CiviCRM | |
545 | */ | |
0f5816a6 | 546 | CRM.alert = function (text, title, type, options) { |
6a488035 TO |
547 | type = type || 'alert'; |
548 | title = title || ''; | |
549 | options = options || {}; | |
550 | if ($('#crm-notification-container').length) { | |
551 | var params = { | |
552 | text: text, | |
553 | title: title, | |
554 | type: type | |
555 | }; | |
556 | // By default, don't expire errors and messages containing links | |
557 | var extra = { | |
558 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), | |
559 | unique: true | |
560 | }; | |
561 | options = $.extend(extra, options); | |
e24b17b9 | 562 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
6a488035 | 563 | if (options.unique && options.unique !== '0') { |
0f5816a6 | 564 | $('#crm-notification-container .ui-notify-message').each(function () { |
6a488035 TO |
565 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
566 | $('.icon.ui-notify-close', this).click(); | |
567 | } | |
568 | }); | |
569 | } | |
570 | return $('#crm-notification-container').notify('create', params, options); | |
571 | } | |
572 | else { | |
573 | if (title.length) { | |
574 | text = title + "\n" + text; | |
575 | } | |
576 | alert(text); | |
577 | return null; | |
578 | } | |
e24b17b9 | 579 | }; |
6a488035 TO |
580 | |
581 | /** | |
582 | * Close whichever alert contains the given node | |
583 | * | |
584 | * @param node | |
585 | */ | |
0f5816a6 | 586 | CRM.closeAlertByChild = function (node) { |
6a488035 | 587 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
e24b17b9 | 588 | }; |
6a488035 TO |
589 | |
590 | /** | |
591 | * Prompt the user for confirmation. | |
592 | * | |
7553cf23 CW |
593 | * @param buttons {object|function} key|value pairs where key == button label and value == callback function |
594 | * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue" | |
595 | * @param options {object|void} Override defaults, keys include 'title', 'message', | |
596 | * see jQuery.dialog for full list of available params | |
6a488035 | 597 | */ |
706cff6d | 598 | CRM.confirm = function (buttons, options, cancelLabel) { |
7553cf23 | 599 | var dialog, callbacks = {}; |
706cff6d | 600 | cancelLabel = cancelLabel || ts('Cancel'); |
7553cf23 CW |
601 | var settings = { |
602 | title: ts('Confirm Action'), | |
603 | message: ts('Are you sure you want to continue?'), | |
6a488035 TO |
604 | resizable: false, |
605 | modal: true, | |
0d5f99d4 | 606 | width: 'auto', |
0f5816a6 KJ |
607 | close: function () { |
608 | $(dialog).remove(); | |
609 | }, | |
7553cf23 CW |
610 | buttons: {} |
611 | }; | |
706cff6d KJ |
612 | |
613 | settings.buttons[cancelLabel] = function () { | |
0f5816a6 KJ |
614 | dialog.dialog('close'); |
615 | }; | |
7553cf23 CW |
616 | options = options || {}; |
617 | $.extend(settings, options); | |
618 | if (typeof(buttons) === 'function') { | |
619 | callbacks[ts('Continue')] = buttons; | |
0f5816a6 KJ |
620 | } |
621 | else { | |
7553cf23 CW |
622 | callbacks = buttons; |
623 | } | |
0f5816a6 KJ |
624 | $.each(callbacks, function (label, callback) { |
625 | settings.buttons[label] = function () { | |
7553cf23 CW |
626 | callback.call(dialog); |
627 | dialog.dialog('close'); | |
e24b17b9 | 628 | }; |
6a488035 | 629 | }); |
7553cf23 CW |
630 | dialog = $('<div class="crm-container crm-confirm-dialog"></div>') |
631 | .html(options.message) | |
632 | .appendTo('body') | |
633 | .dialog(settings); | |
634 | return dialog; | |
e24b17b9 | 635 | }; |
6a488035 TO |
636 | |
637 | /** | |
638 | * Sets an error message | |
639 | * If called for a form item, title and removal condition will be handled automatically | |
640 | */ | |
0f5816a6 | 641 | $.fn.crmError = function (text, title, options) { |
6a488035 TO |
642 | title = title || ''; |
643 | text = text || ''; | |
644 | options = options || {}; | |
645 | ||
646 | var extra = { | |
647 | expires: 0 | |
648 | }; | |
649 | if ($(this).length) { | |
650 | if (title == '') { | |
651 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); | |
652 | if (label.length) { | |
653 | label.addClass('crm-error'); | |
654 | var $label = label.clone(); | |
655 | if (text == '' && $('.crm-marker', $label).length > 0) { | |
656 | text = $('.crm-marker', $label).attr('title'); | |
657 | } | |
658 | $('.crm-marker', $label).remove(); | |
659 | title = $label.text(); | |
660 | } | |
661 | } | |
662 | $(this).addClass('error'); | |
663 | } | |
664 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); | |
665 | if ($(this).length) { | |
666 | var ele = $(this); | |
0f5816a6 KJ |
667 | setTimeout(function () { |
668 | ele.one('change', function () { | |
669 | msg && msg.close && msg.close(); | |
670 | ele.removeClass('error'); | |
671 | label.removeClass('crm-error'); | |
672 | }); | |
673 | }, 1000); | |
6a488035 TO |
674 | } |
675 | return msg; | |
e24b17b9 | 676 | }; |
6a488035 TO |
677 | |
678 | // Display system alerts through js notifications | |
679 | function messagesFromMarkup() { | |
0f5816a6 | 680 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
e24b17b9 | 681 | var text, title = ''; |
6a488035 TO |
682 | $(this).removeClass('status messages'); |
683 | var type = $(this).attr('class').split(' ')[0] || 'alert'; | |
684 | type = type.replace('crm-', ''); | |
685 | $('.icon', this).remove(); | |
6a488035 | 686 | if ($('.msg-text', this).length > 0) { |
e24b17b9 | 687 | text = $('.msg-text', this).html(); |
6a488035 TO |
688 | title = $('.msg-title', this).html(); |
689 | } | |
690 | else { | |
e24b17b9 | 691 | text = $(this).html(); |
6a488035 TO |
692 | } |
693 | var options = $(this).data('options') || {}; | |
694 | $(this).remove(); | |
695 | // Duplicates were already removed server-side | |
696 | options.unique = false; | |
697 | CRM.alert(text, title, type, options); | |
698 | }); | |
699 | // Handle qf form errors | |
700 | $('form :input.error', this).one('blur', function() { | |
ef04c554 | 701 | // ignore autocomplete fields |
23246a80 | 702 | if ($(this).is('.ac_input')) { |
703 | return; | |
704 | } | |
ef04c554 | 705 | |
6a488035 TO |
706 | $('.ui-notify-message.error a.ui-notify-close').click(); |
707 | $(this).removeClass('error'); | |
708 | $(this).next('span.crm-error').remove(); | |
709 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') | |
710 | .removeClass('crm-error') | |
711 | .find('.crm-error').removeClass('crm-error'); | |
712 | }); | |
713 | } | |
714 | ||
23223213 | 715 | $.widget('civi.crmSnippet', { |
205bb8ae | 716 | options: { |
a73f25bb | 717 | url: null, |
205bb8ae CW |
718 | block: true, |
719 | crmForm: null | |
720 | }, | |
c02f8fc4 | 721 | _originalContent: null, |
fa3a5fe2 CW |
722 | _originalUrl: null, |
723 | isOriginalUrl: function() { | |
0906345e CW |
724 | var |
725 | args = {}, | |
726 | same = true, | |
727 | newUrl = this._formatUrl(this.options.url), | |
728 | oldUrl = this._formatUrl(this._originalUrl); | |
fa3a5fe2 | 729 | // Compare path |
0906345e | 730 | if (newUrl.split('?')[0] !== oldUrl.split('?')[0]) { |
fa3a5fe2 CW |
731 | return false; |
732 | } | |
733 | // Compare arguments | |
0906345e | 734 | $.each(newUrl.split('?')[1].split('&'), function(k, v) { |
fa3a5fe2 CW |
735 | var arg = v.split('='); |
736 | args[arg[0]] = arg[1]; | |
737 | }); | |
0906345e | 738 | $.each(oldUrl.split('?')[1].split('&'), function(k, v) { |
fa3a5fe2 CW |
739 | var arg = v.split('='); |
740 | if (args[arg[0]] !== undefined && arg[1] !== args[arg[0]]) { | |
741 | same = false; | |
742 | } | |
743 | }); | |
744 | return same; | |
745 | }, | |
746 | resetUrl: function() { | |
747 | this.options.url = this._originalUrl; | |
748 | }, | |
205bb8ae | 749 | _create: function() { |
205bb8ae CW |
750 | this.element.addClass('crm-ajax-container'); |
751 | if (!this.element.is('.crm-container *')) { | |
752 | this.element.addClass('crm-container'); | |
753 | } | |
4a140040 | 754 | this._handleOrderLinks(); |
5d92a7e7 CW |
755 | // Set default if not supplied |
756 | this.options.url = this.options.url || document.location.href; | |
fa3a5fe2 | 757 | this._originalUrl = this.options.url; |
205bb8ae CW |
758 | }, |
759 | _onFailure: function(data) { | |
760 | this.options.block && this.element.unblock(); | |
761 | this.element.trigger('crmAjaxFail', data); | |
762 | CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); | |
763 | }, | |
764 | _formatUrl: function(url) { | |
0906345e CW |
765 | // Strip hash |
766 | url = url.split('#')[0]; | |
205bb8ae CW |
767 | // Add snippet argument to url |
768 | if (url.search(/[&?]snippet=/) < 0) { | |
fc05b8da | 769 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json'; |
8547369d CW |
770 | } else { |
771 | url = url.replace(/snippet=[^&]*/, 'snippet=json'); | |
205bb8ae CW |
772 | } |
773 | return url; | |
774 | }, | |
d6539f93 | 775 | // Hack to deal with civicrm legacy sort functionality |
4a140040 CW |
776 | _handleOrderLinks: function() { |
777 | var that = this; | |
778 | $('a.crm-weight-arrow', that.element).click(function(e) { | |
779 | that.options.block && that.element.block(); | |
780 | $.getJSON(that._formatUrl(this.href)).done(function() { | |
781 | that.refresh(); | |
782 | }); | |
783 | e.stopImmediatePropagation(); | |
784 | return false; | |
785 | }); | |
786 | }, | |
205bb8ae CW |
787 | refresh: function() { |
788 | var that = this; | |
789 | var url = this._formatUrl(this.options.url); | |
4a140040 | 790 | this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block(); |
205bb8ae CW |
791 | $.getJSON(url, function(data) { |
792 | if (typeof(data) != 'object' || typeof(data.content) != 'string') { | |
793 | that._onFailure(data); | |
794 | return; | |
795 | } | |
796 | data.url = url; | |
c02f8fc4 CW |
797 | that.element.trigger('crmBeforeLoad', data); |
798 | if (that._originalContent === null) { | |
799 | that._originalContent = that.element.contents().detach(); | |
800 | } | |
801 | that.element.html(data.content); | |
4a140040 CW |
802 | that._handleOrderLinks(); |
803 | that.element.trigger('crmLoad', data); | |
205bb8ae CW |
804 | that.options.crmForm && that.element.trigger('crmFormLoad', data); |
805 | }).fail(function() { | |
806 | that._onFailure(); | |
807 | }); | |
4b628e67 CW |
808 | }, |
809 | _destroy: function() { | |
810 | this.element.removeClass('crm-ajax-container'); | |
c02f8fc4 CW |
811 | if (this._originalContent !== null) { |
812 | this.element.empty().append(this._originalContent); | |
813 | } | |
205bb8ae CW |
814 | } |
815 | }); | |
816 | ||
0e017a41 | 817 | var dialogCount = 0; |
d4e4e9df | 818 | CRM.loadPage = function(url, options) { |
d4e4e9df | 819 | var settings = { |
0e017a41 | 820 | target: '#crm-ajax-dialog-' + (dialogCount++), |
8547369d CW |
821 | dialog: false |
822 | }; | |
823 | if (!options || !options.target) { | |
824 | settings.dialog = { | |
d4e4e9df | 825 | modal: true, |
f84151fd CW |
826 | width: '65%', |
827 | height: parseInt($(window).height() * .75), | |
d4e4e9df | 828 | close: function() { |
c02f8fc4 | 829 | $(this).dialog('destroy').remove(); |
d4e4e9df | 830 | } |
8547369d CW |
831 | }; |
832 | } | |
205bb8ae | 833 | options && $.extend(true, settings, options); |
0e017a41 | 834 | settings.url = url; |
83df6b4a | 835 | // Create new dialog |
8547369d | 836 | if (settings.dialog) { |
205bb8ae | 837 | $('<div id="'+ settings.target.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog); |
d4e4e9df | 838 | } |
205bb8ae CW |
839 | if (settings.dialog && !settings.dialog.title) { |
840 | $(settings.target).on('crmLoad', function(event, data) { | |
841 | data.title && $(this).dialog('option', 'title', data.title); | |
842 | }); | |
843 | } | |
5d92a7e7 CW |
844 | $(settings.target).crmSnippet(settings).crmSnippet('refresh'); |
845 | return $(settings.target); | |
d4e4e9df CW |
846 | }; |
847 | ||
848 | CRM.loadForm = function(url, options) { | |
d4e4e9df | 849 | var settings = { |
205bb8ae CW |
850 | crmForm: { |
851 | ajaxForm: {}, | |
205bb8ae CW |
852 | autoClose: true, |
853 | validate: true, | |
854 | refreshAction: ['next_new', 'submit_savenext'], | |
855 | cancelButton: '.cancel.form-submit', | |
23223213 | 856 | openInline: 'a.button:not("[href=#], .no-popup")', |
205bb8ae CW |
857 | onCancel: function(event) {}, |
858 | onError: function(data) { | |
859 | var $el = $(this); | |
5d92a7e7 | 860 | $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data).trigger('crmFormError', data); |
205bb8ae CW |
861 | if (typeof(data.errors) == 'object') { |
862 | $.each(data.errors, function(formElement, msg) { | |
863 | $('[name="'+formElement+'"]', $el).crmError(msg); | |
864 | }); | |
865 | } | |
d4e4e9df | 866 | } |
d4e4e9df CW |
867 | } |
868 | }; | |
a10432db CW |
869 | // Hack to make delete dialogs smaller |
870 | if (url.indexOf('/delete') > 0 || url.indexOf('action=delete') > 0) { | |
871 | settings.dialog = { | |
872 | width: 400, | |
873 | height: 300 | |
874 | }; | |
875 | } | |
205bb8ae CW |
876 | // Move options that belong to crmForm. Others will be passed through to crmSnippet |
877 | options && $.each(options, function(key, value) { | |
878 | if (typeof(settings.crmForm[key]) !== 'undefined') { | |
879 | settings.crmForm[key] = value; | |
880 | } | |
881 | else { | |
882 | settings[key] = value; | |
883 | } | |
884 | }); | |
205bb8ae CW |
885 | |
886 | var widget = CRM.loadPage(url, settings); | |
887 | ||
888 | widget.on('crmFormLoad', function(event, data) { | |
889 | var $el = $(this); | |
660c46f3 | 890 | var settings = $el.crmSnippet('option', 'crmForm'); |
205bb8ae CW |
891 | settings.cancelButton && $(settings.cancelButton, this).click(function(event) { |
892 | var returnVal = settings.onCancel.call($el, event); | |
893 | if (returnVal !== false) { | |
894 | $el.trigger('crmFormCancel', event); | |
660c46f3 | 895 | if ($el.data('uiDialog') && settings.autoClose) { |
fa3a5fe2 CW |
896 | $el.dialog('close'); |
897 | } | |
898 | else if (!settings.autoClose) { | |
899 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
900 | } | |
0e017a41 | 901 | } |
205bb8ae CW |
902 | return returnVal === false; |
903 | }); | |
d4e4e9df | 904 | if (settings.validate) { |
0e017a41 | 905 | $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); |
d4e4e9df | 906 | } |
205bb8ae | 907 | $("form", this).ajaxForm($.extend({ |
fa3a5fe2 | 908 | url: data.url.replace(/reset=1[&]?/, ''), |
d4e4e9df CW |
909 | dataType: 'json', |
910 | success: function(response) { | |
34866662 | 911 | if (response.status !== 'form_error') { |
36876f55 | 912 | $el.crmSnippet('option', 'block') && $el.unblock(); |
205bb8ae | 913 | $el.trigger('crmFormSuccess', response); |
0e017a41 | 914 | // Reset form for e.g. "save and new" |
d6539f93 | 915 | if (response.userContext && settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0) { |
205bb8ae | 916 | $el.crmSnippet('option', 'url', response.userContext).crmSnippet('refresh'); |
0e017a41 | 917 | } |
660c46f3 | 918 | else if ($el.data('uiDialog') && settings.autoClose) { |
205bb8ae | 919 | $el.dialog('close'); |
83df6b4a | 920 | } |
d6539f93 CW |
921 | else if (settings.autoClose === false) { |
922 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
923 | } | |
d4e4e9df CW |
924 | } |
925 | else { | |
25bbc4c0 | 926 | response.url = data.url; |
205bb8ae | 927 | settings.onError.call($el, response); |
d4e4e9df | 928 | } |
23223213 CW |
929 | }, |
930 | beforeSerialize: function(form, options) { | |
931 | if (window.CKEDITOR && window.CKEDITOR.instances) { | |
a10432db CW |
932 | $.each(CKEDITOR.instances, function() { |
933 | this.updateElement && this.updateElement(); | |
934 | }); | |
23223213 | 935 | } |
83df6b4a | 936 | }, |
205bb8ae | 937 | beforeSubmit: function(submission) { |
36876f55 | 938 | $el.crmSnippet('option', 'block') && $el.block(); |
205bb8ae | 939 | $el.trigger('crmFormSubmit', submission); |
d4e4e9df | 940 | } |
205bb8ae | 941 | }, settings.ajaxForm)); |
fa3a5fe2 CW |
942 | if (settings.openInline) { |
943 | settings.autoClose = $el.crmSnippet('isOriginalUrl'); | |
944 | $(settings.openInline, this).click(function(event) { | |
945 | $el.crmSnippet('option', 'url', $(this).attr('href')).crmSnippet('refresh'); | |
946 | return false; | |
947 | }); | |
948 | } | |
205bb8ae CW |
949 | }); |
950 | return widget; | |
d4e4e9df CW |
951 | }; |
952 | ||
03a7ec8f CW |
953 | // Preprocess all cj ajax calls to display messages |
954 | $(document).ajaxSuccess(function(event, xhr, settings) { | |
955 | try { | |
956 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { | |
957 | var response = $.parseJSON(xhr.responseText); | |
958 | if (typeof(response.crmMessages) == 'object') { | |
959 | $.each(response.crmMessages, function(n, msg) { | |
960 | CRM.alert(msg.text, msg.title, msg.type, msg.options); | |
961 | }) | |
962 | } | |
963 | } | |
964 | } | |
965 | // Suppress errors | |
966 | catch (e) {} | |
967 | }); | |
968 | ||
7cb72342 CW |
969 | /** |
970 | * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin | |
971 | */ | |
972 | $.widget('civi.crmAutocomplete', $.ui.autocomplete, {}); | |
973 | ||
0f5816a6 | 974 | $(function () { |
205bb8ae | 975 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
8547369d | 976 | $('.crm-container').trigger('crmLoad'); |
205bb8ae | 977 | |
ef3309b6 | 978 | if ($('#crm-notification-container').length) { |
6a488035 TO |
979 | // Initialize notifications |
980 | $('#crm-notification-container').notify(); | |
981 | messagesFromMarkup.call($('#crm-container')); | |
6a488035 | 982 | } |
ebb9197b C |
983 | |
984 | // bind the event for image popup | |
475e9f44 CW |
985 | $('body') |
986 | .on('click', 'a.crm-image-popup', function() { | |
987 | var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>'); | |
988 | ||
989 | CRM.confirm('', | |
990 | { | |
991 | title: ts('Preview'), | |
992 | message: o | |
993 | }, | |
994 | ts('Done') | |
995 | ); | |
996 | return false; | |
997 | }) | |
ebb9197b | 998 | |
475e9f44 CW |
999 | .on('click', function (event) { |
1000 | $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide(); | |
1001 | if ($(event.target).is('.btn-slide')) { | |
1002 | $(event.target).addClass('btn-slide-active').find('.panel').show(); | |
1003 | } | |
1004 | }) | |
d664f648 | 1005 | |
475e9f44 CW |
1006 | .on('click', 'a.crm-edit-optionvalue-link', function() { |
1007 | var url = $(this).data('option-group-url'); | |
faa2cea0 | 1008 | CRM.loadForm(CRM.url(url, {reset: 1}), {openInline: 'a:not("[href=#], .no-popup")'}) |
475e9f44 CW |
1009 | .on('dialogclose', function() { |
1010 | var $elects = $('select[data-option-group-url="' + url + '"]'); | |
e869b07d | 1011 | CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')}) |
475e9f44 CW |
1012 | .done(function(data) { |
1013 | CRM.utils.setOptions($elects, data.values); | |
1014 | }); | |
1015 | }); | |
1016 | return false; | |
4a143c04 CW |
1017 | }) |
1018 | // Handle clear button for form elements | |
1019 | .on('click', 'a.crm-clear-link', function() { | |
1020 | $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change(); | |
bcb4280c | 1021 | $(this).siblings('input:text').val('').change(); |
4a143c04 CW |
1022 | return false; |
1023 | }) | |
1024 | .on('change', 'input.crm-form-radio:checked', function() { | |
1025 | $(this).siblings('.crm-clear-link').css({visibility: ''}); | |
475e9f44 | 1026 | }); |
d664f648 | 1027 | $().crmtooltip(); |
6a488035 TO |
1028 | }); |
1029 | ||
0f5816a6 | 1030 | $.fn.crmAccordions = function (speed) { |
cf021bc5 CW |
1031 | var container = $(this).length > 0 ? $(this) : $('.crm-container'); |
1032 | speed = speed === undefined ? 200 : speed; | |
1033 | container | |
1034 | .off('click.crmAccordions') | |
6a488035 | 1035 | // Allow normal clicking of links |
cf021bc5 | 1036 | .on('click.crmAccordions', 'div.crm-accordion-header a', function (e) { |
6a488035 | 1037 | e.stopPropagation && e.stopPropagation(); |
cf021bc5 CW |
1038 | }) |
1039 | .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () { | |
6a488035 TO |
1040 | if ($(this).parent().hasClass('collapsed')) { |
1041 | $(this).next().css('display', 'none').slideDown(speed); | |
1042 | } | |
1043 | else { | |
1044 | $(this).next().css('display', 'block').slideUp(speed); | |
1045 | } | |
1046 | $(this).parent().toggleClass('collapsed'); | |
1047 | return false; | |
1048 | }); | |
6a488035 | 1049 | }; |
0f5816a6 KJ |
1050 | $.fn.crmAccordionToggle = function (speed) { |
1051 | $(this).each(function () { | |
6a488035 TO |
1052 | if ($(this).hasClass('collapsed')) { |
1053 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); | |
1054 | } | |
1055 | else { | |
1056 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); | |
1057 | } | |
1058 | $(this).toggleClass('collapsed'); | |
1059 | }); | |
1060 | }; | |
5ec182d9 CW |
1061 | |
1062 | /** | |
1063 | * Clientside currency formatting | |
1064 | * @param value | |
3bdb644f | 1065 | * @param format - currency representation of the number 1234.56 |
5ec182d9 | 1066 | * @return string |
3bdb644f | 1067 | * @see CRM_Core_Resources::addCoreResources |
5ec182d9 CW |
1068 | */ |
1069 | var currencyTemplate; | |
1070 | CRM.formatMoney = function(value, format) { | |
1071 | var decimal, separator, sign, i, j, result; | |
1072 | if (value === 'init' && format) { | |
1073 | currencyTemplate = format; | |
1074 | return; | |
1075 | } | |
1076 | format = format || currencyTemplate; | |
1077 | result = /1(.?)234(.?)56/.exec(format); | |
1078 | if (result === null) { | |
1079 | return 'Invalid format passed to CRM.formatMoney'; | |
1080 | } | |
1081 | separator = result[1]; | |
1082 | decimal = result[2]; | |
1083 | sign = (value < 0) ? '-' : ''; | |
1084 | //extracting the absolute value of the integer part of the number and converting to string | |
1085 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; | |
5ec182d9 CW |
1086 | j = ((j = i.length) > 3) ? j % 3 : 0; |
1087 | 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) : ''); | |
1088 | return format.replace(/1.*234.*56/, result); | |
1089 | }; | |
6a488035 | 1090 | })(jQuery); |