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