4dc1b47074135d6f6576611ab868d9909b5abd22
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
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
37 * Short-named function for string translation, defined in global scope so it's available everywhere.
39 * @param $text string string for translating
40 * @param $params object key:value of additional parameters
42 * @return string the translated string
44 function ts(text
, params
) {
46 text
= CRM
.strings
[text
] || text
;
47 if (typeof(params
) === 'object') {
48 for (var i
in params
) {
49 if (typeof(params
[i
]) === 'string' || typeof(params
[i
]) === 'number') {
50 // sprintf emulation: escape % characters in the replacements to avoid conflicts
51 text
= text
.replace(new RegExp('%' + i
, 'g'), String(params
[i
]).replace(/%/g
, '%-crmescaped-'));
54 return text
.replace(/%-crmescaped-/g, '%');
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
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 ...)
71 function on_load_init_blocks(showBlocks
, hideBlocks
, elementType
) {
72 if (elementType
== null) {
73 var elementType
= 'block';
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
;
84 alert('showBlocks array item not in .tpl = ' + showBlocks
[i
]);
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';
96 alert('showBlocks array item not in .tpl = ' + hideBlocks
[i
]);
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).
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
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';
119 if (target_element_type
== 'table-row') {
120 var target_element_type
= '';
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
;
128 var target
= target_element_id
.split("|");
129 for (var j
= 0; j
< target
.length
; j
++) {
131 cj('#' + target
[j
]).show();
134 cj('#' + target
[j
]).hide();
136 for (var i
= 0; i
< trigger
.length
; i
++) {
137 if (selectedOptionValue
== trigger
[i
]) {
139 cj('#' + target
[j
]).hide();
142 cj('#' + target
[j
]).show();
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
) {
155 cj('#' + target
[j
]).hide();
158 cj('#' + target
[j
]).show();
163 cj('#' + target
[j
]).show();
166 cj('#' + target
[j
]).hide();
175 * reset all the radio buttons with a given name
177 * @param string fieldName
181 function unselectRadio(fieldName
, form
) {
182 for (i
= 0; i
< document
.forms
[form
].elements
.length
; i
++) {
183 if (document
.forms
[form
].elements
[i
].name
== fieldName
) {
184 document
.forms
[form
].elements
[i
].checked
= false;
191 * Function to change button text and disable one it is clicked
193 * @param obj object - the button clicked
194 * @param formID string - the id of the form being submitted
195 * @param string procText - button text after user clicks it
199 /* Changes button label on submit, and disables button after submit for newer browsers.
200 Puts up alert for older browsers. */
201 function submitOnce(obj
, formId
, procText
) {
202 // if named button clicked, change text
203 if (obj
.value
!= null) {
204 obj
.value
= procText
+ " ...";
206 if (document
.getElementById
) { // disable submit button for newer browsers
208 document
.getElementById(formId
).submit();
211 else { // for older browsers
212 if (submitcount
== 0) {
217 alert("Your request is currently being processed ... Please wait.");
223 function popUp(URL
) {
226 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');");
230 * Function to show / hide the row in optionFields
232 * @param element name index, that whose innerHTML is to hide else will show the hidden row.
234 function showHideRow(index
) {
236 cj('tr#optionField_' + index
).hide();
237 if (cj('table#optionField tr:hidden:first').length
) {
238 cj('div#optionFieldLink').show();
242 cj('table#optionField tr:hidden:first').show();
243 if (!cj('table#optionField tr:hidden:last').length
) {
244 cj('div#optionFieldLink').hide();
250 CRM
.strings
= CRM
.strings
|| {};
251 CRM
.validate
= CRM
.validate
|| {
256 (function ($, undefined) {
259 $(document
).on('crmLoad', function(e
) {
260 $('table.row-highlight', e
.target
)
261 .off('.rowHighlight')
262 .on('change.rowHighlight', 'input.select-row, input.select-rows', function () {
263 var target
, table
= $(this).closest('table');
264 if ($(this).hasClass('select-rows')) {
265 target
= $('tbody tr', table
);
266 $('input.select-row', table
).prop('checked', $(this).prop('checked'));
269 target
= $(this).closest('tr');
270 $('input.select-rows', table
).prop('checked', $(".select-row:not(':checked')", table
).length
< 1);
272 target
.toggleClass('crm-row-selected', $(this).is(':checked'));
274 .find('input.select-row:checked').parents('tr').addClass('crm-row-selected');
278 * Function to make multiselect boxes behave as fields in small screens
280 function advmultiselectResize() {
281 var amswidth
= $("#crm-container form:has(table.advmultiselect)").width();
282 if (amswidth
< 700) {
283 $("form table.advmultiselect td").css('display', 'block');
286 $("form table.advmultiselect td").css('display', 'table-cell');
288 var contactwidth
= $('#crm-container #mainTabContainer').width();
289 if (contactwidth
< 600) {
290 $('#crm-container #mainTabContainer').addClass('narrowpage');
291 $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index
) {
293 if (index
% 2 == 0) {
294 $(this).parent().after('<tr class="narrowadded"></tr>');
297 $(this).parent().next().append(item
);
302 $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage');
303 $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () {
305 var parent
= $(this).parent();
306 $(this).parent().prev().append(nitem
);
307 if (parent
.children().size() == 0) {
311 $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach();
313 var cformwidth
= $('#crm-container #Contact .contact_basic_information-section').width();
315 if (cformwidth
< 720) {
316 $('#crm-container .contact_basic_information-section').addClass('narrowform');
317 $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon');
318 if (cformwidth
< 480) {
319 $('#crm-container .contact_basic_information-section').addClass('xnarrowform');
322 $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
326 $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform');
327 $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
331 advmultiselectResize();
332 $(window
).resize(function () {
333 advmultiselectResize();
336 $.fn
.crmtooltip = function () {
338 .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e
) {
339 $(this).addClass('crm-processed');
340 $(this).addClass('crm-tooltip-active');
341 var topDistance
= e
.pageY
- $(window
).scrollTop();
342 if (topDistance
< 300 | topDistance
< $(this).children('.crm-tooltip-wrapper').height()) {
343 $(this).addClass('crm-tooltip-down');
345 if (!$(this).children('.crm-tooltip-wrapper').length
) {
346 $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>');
347 $(this).children().children('.crm-tooltip')
348 .html('<div class="crm-loading-element"></div>')
352 .on('mouseout', 'a.crm-summary-link', function () {
353 $(this).removeClass('crm-processed');
354 $(this).removeClass('crm-tooltip-active crm-tooltip-down');
356 .on('click', 'a.crm-summary-link', false);
360 CRM
.help = function (title
, params
, url
) {
361 h
&& h
.close
&& h
.close();
365 h
= CRM
.alert('...', title
, 'crm-help crm-msg-loading', options
);
366 params
.class_name
= 'CRM_Core_Page_Inline_Help';
367 params
.type
= 'page';
368 $.ajax(url
|| CRM
.url('civicrm/ajax/inline'),
372 success: function (data
) {
373 $('#crm-notification-container .crm-help .notify-content:last').html(data
);
374 $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info');
377 $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.');
378 $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error');
384 * @param startMsg string
385 * @param endMsg string|function
386 * @param deferred optional jQuery deferred object
387 * @return jQuery deferred object - if not supplied a new one will be created
390 CRM
.status = function(startMsg
, endMsg
, deferred
) {
391 var $bar
= $('#civicrm-menu');
393 console
&& console
.log
&& console
.log('CRM.status called on a page with no menubar');
396 $('.crm-menubar-status-container', $bar
).remove();
397 fadeOut
&& window
.clearTimeout(fadeOut
);
398 $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>');
399 $('.crm-menubar-status-container', $bar
).css('min-width', $('.crm-menubar-status-container', $bar
).width());
400 deferred
|| (deferred
= new $.Deferred());
401 deferred
.done(function(data
) {
402 var msg
= typeof(endMsg
) === 'function' ? endMsg(data
) : endMsg
;
403 $('.crm-menubar-status-container', $bar
).removeClass('status-busy').addClass('status-done').show().find('.crm-menubar-status-msg').html(msg
);
405 fadeOut
= window
.setTimeout(function() {
406 $('.crm-menubar-status-container', $bar
).fadeOut('slow');
409 $('.crm-menubar-status-container', $bar
).hide();
416 * @param string text Displayable message
417 * @param string title Displayable title
418 * @param string type 'alert'|'info'|'success'|'error' (default: 'alert')
419 * @param {object} options
421 * @see http://wiki.civicrm.org/confluence/display/CRM/Notifications+in+CiviCRM
423 CRM
.alert = function (text
, title
, type
, options
) {
424 type
= type
|| 'alert';
426 options
= options
|| {};
427 if ($('#crm-notification-container').length
) {
433 // By default, don't expire errors and messages containing links
435 expires
: (type
== 'error' || text
.indexOf('<a ') > -1) ? 0 : (text
? 10000 : 5000),
438 options
= $.extend(extra
, options
);
439 options
.expires
= options
.expires
=== false ? 0 : parseInt(options
.expires
, 10);
440 if (options
.unique
&& options
.unique
!== '0') {
441 $('#crm-notification-container .ui-notify-message').each(function () {
442 if (title
=== $('h1', this).html() && text
=== $('.notify-content', this).html()) {
443 $('.icon.ui-notify-close', this).click();
447 return $('#crm-notification-container').notify('create', params
, options
);
451 text
= title
+ "\n" + text
;
459 * Close whichever alert contains the given node
463 CRM
.closeAlertByChild = function (node
) {
464 $(node
).closest('.ui-notify-message').find('.icon.ui-notify-close').click();
468 * Prompt the user for confirmation.
470 * @param buttons {object|function} key|value pairs where key == button label and value == callback function
471 * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue"
472 * @param options {object|void} Override defaults, keys include 'title', 'message',
473 * see jQuery.dialog for full list of available params
475 CRM
.confirm = function (buttons
, options
, cancelLabel
) {
476 var dialog
, callbacks
= {};
477 cancelLabel
= cancelLabel
|| ts('Cancel');
479 title
: ts('Confirm Action'),
480 message
: ts('Are you sure you want to continue?'),
490 settings
.buttons
[cancelLabel
] = function () {
491 dialog
.dialog('close');
493 options
= options
|| {};
494 $.extend(settings
, options
);
495 if (typeof(buttons
) === 'function') {
496 callbacks
[ts('Continue')] = buttons
;
501 $.each(callbacks
, function (label
, callback
) {
502 settings
.buttons
[label
] = function () {
503 callback
.call(dialog
);
504 dialog
.dialog('close');
507 dialog
= $('<div class="crm-container crm-confirm-dialog"></div>')
508 .html(options
.message
)
515 * Sets an error message
516 * If called for a form item, title and removal condition will be handled automatically
518 $.fn
.crmError = function (text
, title
, options
) {
521 options
= options
|| {};
526 if ($(this).length
) {
528 var label
= $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]');
530 label
.addClass('crm-error');
531 var $label
= label
.clone();
532 if (text
== '' && $('.crm-marker', $label
).length
> 0) {
533 text
= $('.crm-marker', $label
).attr('title');
535 $('.crm-marker', $label
).remove();
536 title
= $label
.text();
539 $(this).addClass('error');
541 var msg
= CRM
.alert(text
, title
, 'error', $.extend(extra
, options
));
542 if ($(this).length
) {
544 setTimeout(function () {
545 ele
.one('change', function () {
546 msg
&& msg
.close
&& msg
.close();
547 ele
.removeClass('error');
548 label
.removeClass('crm-error');
555 // Display system alerts through js notifications
556 function messagesFromMarkup() {
557 $('div.messages:visible', this).not('.help').not('.no-popup').each(function () {
558 var text
, title
= '';
559 $(this).removeClass('status messages');
560 var type
= $(this).attr('class').split(' ')[0] || 'alert';
561 type
= type
.replace('crm-', '');
562 $('.icon', this).remove();
563 if ($('.msg-text', this).length
> 0) {
564 text
= $('.msg-text', this).html();
565 title
= $('.msg-title', this).html();
568 text
= $(this).html();
570 var options
= $(this).data('options') || {};
572 // Duplicates were already removed server-side
573 options
.unique
= false;
574 CRM
.alert(text
, title
, type
, options
);
576 // Handle qf form errors
577 $('form :input.error', this).one('blur', function() {
578 // ignore autocomplete fields
579 if ($(this).is('.ac_input')) {
583 $('.ui-notify-message.error a.ui-notify-close').click();
584 $(this).removeClass('error');
585 $(this).next('span.crm-error').remove();
586 $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]')
587 .removeClass('crm-error')
588 .find('.crm-error').removeClass('crm-error');
592 $.widget('civi.crmSnippet', {
598 _originalContent
: null,
600 isOriginalUrl: function() {
604 newUrl
= this._formatUrl(this.options
.url
),
605 oldUrl
= this._formatUrl(this._originalUrl
);
607 if (newUrl
.split('?')[0] !== oldUrl
.split('?')[0]) {
611 $.each(newUrl
.split('?')[1].split('&'), function(k
, v
) {
612 var arg
= v
.split('=');
613 args
[arg
[0]] = arg
[1];
615 $.each(oldUrl
.split('?')[1].split('&'), function(k
, v
) {
616 var arg
= v
.split('=');
617 if (args
[arg
[0]] !== undefined && arg
[1] !== args
[arg
[0]]) {
623 resetUrl: function() {
624 this.options
.url
= this._originalUrl
;
626 _create: function() {
627 this.element
.addClass('crm-ajax-container');
628 if (!this.element
.is('.crm-container *')) {
629 this.element
.addClass('crm-container');
631 this._handleOrderLinks();
632 // Set default if not supplied
633 this.options
.url
= this.options
.url
|| document
.location
.href
;
634 this._originalUrl
= this.options
.url
;
636 _onFailure: function(data
) {
637 this.options
.block
&& this.element
.unblock();
638 this.element
.trigger('crmAjaxFail', data
);
639 CRM
.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
641 _formatUrl: function(url
) {
643 url
= url
.split('#')[0];
644 // Add snippet argument to url
645 if (url
.search(/[&?]snippet=/) < 0) {
646 url
+= (url
.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
648 url
= url
.replace(/snippet=[^&]*/, 'snippet=json');
652 // Hack to deal with civicrm legacy sort functionality
653 _handleOrderLinks: function() {
655 $('a.crm-weight-arrow', that
.element
).click(function(e
) {
656 that
.options
.block
&& that
.element
.block();
657 $.getJSON(that
._formatUrl(this.href
)).done(function() {
660 e
.stopImmediatePropagation();
664 refresh: function() {
666 var url
= this._formatUrl(this.options
.url
);
667 this.options
.block
&& $('.blockOverlay', this.element
).length
< 1 && this.element
.block();
668 $.getJSON(url
, function(data
) {
669 if (typeof(data
) != 'object' || typeof(data
.content
) != 'string') {
670 that
._onFailure(data
);
674 that
.element
.trigger('crmBeforeLoad', data
);
675 if (that
._originalContent
=== null) {
676 that
._originalContent
= that
.element
.contents().detach();
678 that
.element
.html(data
.content
);
679 that
._handleOrderLinks();
680 that
.element
.trigger('crmLoad', data
);
681 that
.options
.crmForm
&& that
.element
.trigger('crmFormLoad', data
);
686 _destroy: function() {
687 this.element
.removeClass('crm-ajax-container');
688 if (this._originalContent
!== null) {
689 this.element
.empty().append(this._originalContent
);
695 CRM
.loadPage = function(url
, options
) {
697 target
: '#crm-ajax-dialog-' + (dialogCount
++),
700 if (!options
|| !options
.target
) {
704 height
: parseInt($(window
).height() * .75),
706 $(this).dialog('destroy').remove();
710 options
&& $.extend(true, settings
, options
);
713 if (settings
.dialog
) {
714 $('<div id="'+ settings
.target
.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings
.dialog
);
716 if (settings
.dialog
&& !settings
.dialog
.title
) {
717 $(settings
.target
).on('crmLoad', function(event
, data
) {
718 data
.title
&& $(this).dialog('option', 'title', data
.title
);
721 $(settings
.target
).crmSnippet(settings
).crmSnippet('refresh');
722 return $(settings
.target
);
725 CRM
.loadForm = function(url
, options
) {
731 refreshAction
: ['next_new', 'submit_savenext'],
732 cancelButton
: '.cancel.form-submit',
733 openInline
: 'a.button:not("[href=#], .no-popup")',
734 onCancel: function(event
) {},
735 onError: function(data
) {
737 $el
.html(data
.content
).trigger('crmLoad', data
).trigger('crmFormLoad', data
).trigger('crmFormError', data
);
738 if (typeof(data
.errors
) == 'object') {
739 $.each(data
.errors
, function(formElement
, msg
) {
740 $('[name="'+formElement
+'"]', $el
).crmError(msg
);
746 // Hack to make delete dialogs smaller
747 if (url
.indexOf('/delete') > 0 || url
.indexOf('action=delete') > 0) {
753 // Move options that belong to crmForm. Others will be passed through to crmSnippet
754 options
&& $.each(options
, function(key
, value
) {
755 if (typeof(settings
.crmForm
[key
]) !== 'undefined') {
756 settings
.crmForm
[key
] = value
;
759 settings
[key
] = value
;
763 var widget
= CRM
.loadPage(url
, settings
);
765 widget
.on('crmFormLoad', function(event
, data
) {
767 var settings
= $el
.crmSnippet('option', 'crmForm');
768 settings
.cancelButton
&& $(settings
.cancelButton
, this).click(function(event
) {
769 var returnVal
= settings
.onCancel
.call($el
, event
);
770 if (returnVal
!== false) {
771 $el
.trigger('crmFormCancel', event
);
772 if ($el
.data('uiDialog') && settings
.autoClose
) {
775 else if (!settings
.autoClose
) {
776 $el
.crmSnippet('resetUrl').crmSnippet('refresh');
779 return returnVal
=== false;
781 if (settings
.validate
) {
782 $("form", this).validate(typeof(settings
.validate
) == 'object' ? settings
.validate
: CRM
.validate
.params
);
784 $("form", this).ajaxForm($.extend({
785 url
: data
.url
.replace(/reset=1[&]?/, ''),
787 success: function(response
) {
788 if (response
.status
!== 'form_error') {
789 $el
.crmSnippet('option', 'block') && $el
.unblock();
790 $el
.trigger('crmFormSuccess', response
);
791 // Reset form for e.g. "save and new"
792 if (response
.userContext
&& settings
.refreshAction
&& $.inArray(response
.buttonName
, settings
.refreshAction
) >= 0) {
793 $el
.crmSnippet('option', 'url', response
.userContext
).crmSnippet('refresh');
795 else if ($el
.data('uiDialog') && settings
.autoClose
) {
798 else if (settings
.autoClose
=== false) {
799 $el
.crmSnippet('resetUrl').crmSnippet('refresh');
803 response
.url
= data
.url
;
804 settings
.onError
.call($el
, response
);
807 beforeSerialize: function(form
, options
) {
808 if (window
.CKEDITOR
&& window
.CKEDITOR
.instances
) {
809 $.each(CKEDITOR
.instances
, function() {
810 this.updateElement
&& this.updateElement();
814 beforeSubmit: function(submission
) {
815 $el
.crmSnippet('option', 'block') && $el
.block();
816 $el
.trigger('crmFormSubmit', submission
);
818 }, settings
.ajaxForm
));
819 if (settings
.openInline
) {
820 settings
.autoClose
= $el
.crmSnippet('isOriginalUrl');
821 $(settings
.openInline
, this).click(function(event
) {
822 $el
.crmSnippet('option', 'url', $(this).attr('href')).crmSnippet('refresh');
830 // Preprocess all cj ajax calls to display messages
831 $(document
).ajaxSuccess(function(event
, xhr
, settings
) {
833 if ((!settings
.dataType
|| settings
.dataType
== 'json') && xhr
.responseText
) {
834 var response
= $.parseJSON(xhr
.responseText
);
835 if (typeof(response
.crmMessages
) == 'object') {
836 $.each(response
.crmMessages
, function(n
, msg
) {
837 CRM
.alert(msg
.text
, msg
.title
, msg
.type
, msg
.options
);
847 // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content.
848 $('.crm-container').trigger('crmLoad');
850 if ($('#crm-notification-container').length
) {
851 // Initialize notifications
852 $('#crm-notification-container').notify();
853 messagesFromMarkup
.call($('#crm-container'));
856 // bind the event for image popup
857 $('body').on('click', 'a.crm-image-popup', function() {
858 var o
= $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
862 title
: ts('Preview'),
871 $('body').on('click', function (event
) {
872 $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide();
873 if ($(event
.target
).is('.btn-slide')) {
874 $(event
.target
).addClass('btn-slide-active').find('.panel').show();
879 $.fn
.crmAccordions = function (speed
) {
880 var container
= $(this).length
> 0 ? $(this) : $('.crm-container');
881 speed
= speed
=== undefined ? 200 : speed
;
883 .off('click.crmAccordions')
884 // Allow normal clicking of links
885 .on('click.crmAccordions', 'div.crm-accordion-header a', function (e
) {
886 e
.stopPropagation
&& e
.stopPropagation();
888 .on('click.crmAccordions', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () {
889 if ($(this).parent().hasClass('collapsed')) {
890 $(this).next().css('display', 'none').slideDown(speed
);
893 $(this).next().css('display', 'block').slideUp(speed
);
895 $(this).parent().toggleClass('collapsed');
899 $.fn
.crmAccordionToggle = function (speed
) {
900 $(this).each(function () {
901 if ($(this).hasClass('collapsed')) {
902 $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed
);
905 $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed
);
907 $(this).toggleClass('collapsed');
912 * Clientside currency formatting
914 * @param format - currency representation of the number 1234.56
916 * @see CRM_Core_Resources::addCoreResources
918 var currencyTemplate
;
919 CRM
.formatMoney = function(value
, format
) {
920 var decimal, separator
, sign
, i
, j
, result
;
921 if (value
=== 'init' && format
) {
922 currencyTemplate
= format
;
925 format
= format
|| currencyTemplate
;
926 result
= /1(.?)234(.?)56/.exec(format
);
927 if (result
=== null) {
928 return 'Invalid format passed to CRM.formatMoney';
930 separator
= result
[1];
932 sign
= (value
< 0) ? '-' : '';
933 //extracting the absolute value of the integer part of the number and converting to string
934 i
= parseInt(value
= Math
.abs(value
).toFixed(2)) + '';
935 j
= ((j
= i
.length
) > 3) ? j
% 3 : 0;
936 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) : '');
937 return format
.replace(/1.*234.*56/, result
);