initial version of 'call for sessions' form ready.
[libreplanet-static.git] / 2015 / assets / js / civicrm-4.4.Common.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * @file: global functions for CiviCRM
31 * FIXME: We are moving away from using global functions. DO NOT ADD MORE.
32 * @see CRM object - the better alternative to adding global functions
33 */
34
35 var CRM = CRM || {};
36 var cj = jQuery;
37
38 /**
39 * Short-named function for string translation, defined in global scope so it's available everywhere.
40 *
41 * @param $text string string for translating
42 * @param $params object key:value of additional parameters
43 *
44 * @return string the translated string
45 */
46 function ts(text, params) {
47 "use strict";
48 text = CRM.strings[text] || text;
49 if (typeof(params) === 'object') {
50 for (var i in params) {
51 if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') {
52 // sprintf emulation: escape % characters in the replacements to avoid conflicts
53 text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-'));
54 }
55 }
56 return text.replace(/%-crmescaped-/g, '%');
57 }
58 return text;
59 }
60
61 /**
62 * This function is called by default at the bottom of template files which have forms that have
63 * conditionally displayed/hidden sections and elements. The PHP is responsible for generating
64 * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to
65 * this function.
66 *
67 * @access public
68 * @param showBlocks Array of element Id's to be displayed
69 * @param hideBlocks Array of element Id's to be hidden
70 * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...)
71 * @return none
72 */
73 function on_load_init_blocks(showBlocks, hideBlocks, elementType) {
74 if (elementType == null) {
75 var elementType = 'block';
76 }
77
78 /* This loop is used to display the blocks whose IDs are present within the showBlocks array */
79 for (var i = 0; i < showBlocks.length; i++) {
80 var myElement = document.getElementById(showBlocks[i]);
81 /* getElementById returns null if element id doesn't exist in the document */
82 if (myElement != null) {
83 myElement.style.display = elementType;
84 }
85 else {
86 alert('showBlocks array item not in .tpl = ' + showBlocks[i]);
87 }
88 }
89
90 /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */
91 for (var i = 0; i < hideBlocks.length; i++) {
92 var myElement = document.getElementById(hideBlocks[i]);
93 /* getElementById returns null if element id doesn't exist in the document */
94 if (myElement != null) {
95 myElement.style.display = 'none';
96 }
97 else {
98 alert('showBlocks array item not in .tpl = ' + hideBlocks[i]);
99 }
100 }
101 }
102
103 /**
104 * This function is called when we need to show or hide a related form element (target_element)
105 * based on the value (trigger_value) of another form field (trigger_field).
106 *
107 * @access public
108 * @param trigger_field_id HTML id of field whose onchange is the trigger
109 * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field
110 * @param target_element_id HTML id of element to be shown or hidden
111 * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row')
112 * @param field_type Type of element radio/select
113 * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match
114 * @return none
115 */
116 function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) {
117 if (target_element_type == null) {
118 var target_element_type = 'block';
119 }
120 else {
121 if (target_element_type == 'table-row') {
122 var target_element_type = '';
123 }
124 }
125
126 if (field_type == 'select') {
127 var trigger = trigger_value.split("|");
128 var selectedOptionValue = document.getElementById(trigger_field_id).options[document.getElementById(trigger_field_id).selectedIndex].value;
129
130 var target = target_element_id.split("|");
131 for (var j = 0; j < target.length; j++) {
132 if (invert) {
133 cj('#' + target[j]).show();
134 }
135 else {
136 cj('#' + target[j]).hide();
137 }
138 for (var i = 0; i < trigger.length; i++) {
139 if (selectedOptionValue == trigger[i]) {
140 if (invert) {
141 cj('#' + target[j]).hide();
142 }
143 else {
144 cj('#' + target[j]).show();
145 }
146 }
147 }
148 }
149
150 }
151 else {
152 if (field_type == 'radio') {
153 var target = target_element_id.split("|");
154 for (var j = 0; j < target.length; j++) {
155 if (document.getElementsByName(trigger_field_id)[0].checked) {
156 if (invert) {
157 cj('#' + target[j]).hide();
158 }
159 else {
160 cj('#' + target[j]).show();
161 }
162 }
163 else {
164 if (invert) {
165 cj('#' + target[j]).show();
166 }
167 else {
168 cj('#' + target[j]).hide();
169 }
170 }
171 }
172 }
173 }
174 }
175
176 /**
177 *
178 * Function for checking ALL or unchecking ALL check boxes in a resultset page.
179 *
180 * @access public
181 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
182 * belonging to the resultset's checkbox collection
183 * @param object - checkbox
184 * Sample usage: onClick="javascript:changeCheckboxValues('chk_', cj(this) );"
185 *
186 * @return
187 */
188 function toggleCheckboxVals(fldPrefix, object) {
189 if (object.id == 'toggleSelect' && cj(object).is(':checked')) {
190 cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', true);
191 }
192 else {
193 cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', false);
194 }
195 // change the class of selected rows
196 on_load_init_checkboxes(object.form.name);
197 }
198
199 function countSelectedCheckboxes(fldPrefix, form) {
200 fieldCount = 0;
201 for (i = 0; i < form.elements.length; i++) {
202 fpLen = fldPrefix.length;
203 if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0, fpLen) == fldPrefix && form.elements[i].checked == true) {
204 fieldCount++;
205 }
206 }
207 return fieldCount;
208 }
209
210 /**
211 * Function to enable task action select
212 */
213 function toggleTaskAction(status) {
214 var radio_ts = document.getElementsByName('radio_ts');
215 if (!radio_ts[1]) {
216 radio_ts[0].checked = true;
217 }
218 if (radio_ts[0].checked || radio_ts[1].checked) {
219 status = true;
220 }
221
222 var formElements = ['task', 'Go', 'Print'];
223 for (var i = 0; i < formElements.length; i++) {
224 var element = document.getElementById(formElements[i]);
225 if (element) {
226 if (status) {
227 element.disabled = false;
228 }
229 else {
230 element.disabled = true;
231 }
232 }
233 }
234 }
235
236 /**
237 * This function is used to check if any actio is selected and also to check if any contacts are checked.
238 *
239 * @access public
240 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
241 * belonging to the resultset's checkbox collection
242 * @param form - name of form that checkboxes are part of
243 * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );"
244 *
245 */
246 function checkPerformAction(fldPrefix, form, taskButton, selection) {
247 var cnt;
248 var gotTask = 0;
249
250 // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task
251 if (taskButton == 1) {
252 gotTask = 1;
253 }
254 else {
255 if (document.forms[form].task.selectedIndex) {
256 //force user to select all search contacts, CRM-3711
257 if (document.forms[form].task.value == 13 || document.forms[form].task.value == 14) {
258 var toggleSelect = document.getElementsByName('toggleSelect');
259 if (toggleSelect[0].checked || document.forms[form].radio_ts[0].checked) {
260 return true;
261 }
262 else {
263 alert("Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
264 return false;
265 }
266 }
267 gotTask = 1;
268 }
269 }
270
271 if (gotTask == 1) {
272 // If user wants to perform action on ALL records and we have a task, return (no need to check further)
273 if (document.forms[form].radio_ts[0].checked) {
274 return true;
275 }
276
277 cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]);
278 if (!cnt) {
279 alert("Please select one or more contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
280 return false;
281 }
282 }
283 else {
284 alert("Please select an action from the drop-down menu.");
285 return false;
286 }
287 }
288
289 /**
290 * This function changes the style for a checkbox block when it is selected.
291 *
292 * @access public
293 * @param chkName - it is name of the checkbox
294 * @return null
295 */
296 function checkSelectedBox(chkName) {
297 var checkElement = cj('#' + chkName);
298 if (checkElement.attr('checked')) {
299 cj('input[value=ts_sel]:radio').attr('checked', true);
300 checkElement.parents('tr').addClass('crm-row-selected');
301 }
302 else {
303 checkElement.parents('tr').removeClass('crm-row-selected');
304 }
305 }
306
307 /**
308 * This function is to show the row with selected checkbox in different color
309 * @param form - name of form that checkboxes are part of
310 *
311 * @access public
312 * @return null
313 */
314 function on_load_init_checkboxes(form) {
315 var formName = form;
316 var fldPrefix = 'mark_x';
317 for (i = 0; i < document.forms[formName].elements.length; i++) {
318 fpLen = fldPrefix.length;
319 if (document.forms[formName].elements[i].type == 'checkbox' && document.forms[formName].elements[i].name.slice(0, fpLen) == fldPrefix) {
320 checkSelectedBox(document.forms[formName].elements[i].name, formName);
321 }
322 }
323 }
324
325 /**
326 * Function to change the color of the class
327 *
328 * @param form - name of the form
329 * @param rowid - id of the <tr>, <div> you want to change
330 *
331 * @access public
332 * @return null
333 */
334 function changeRowColor(rowid, form) {
335 switch (document.getElementById(rowid).className) {
336 case 'even-row' :
337 document.getElementById(rowid).className = 'selected even-row';
338 break;
339 case 'odd-row' :
340 document.getElementById(rowid).className = 'selected odd-row';
341 break;
342 case 'selected even-row' :
343 document.getElementById(rowid).className = 'even-row';
344 break;
345 case 'selected odd-row' :
346 document.getElementById(rowid).className = 'odd-row';
347 break;
348 case 'form-item' :
349 document.getElementById(rowid).className = 'selected';
350 break;
351 case 'selected' :
352 document.getElementById(rowid).className = 'form-item';
353 }
354 }
355
356 /**
357 * This function is to show the row with selected checkbox in different color
358 * @param form - name of form that checkboxes are part of
359 *
360 * @access public
361 * @return null
362 */
363 function on_load_init_check(form) {
364 for (i = 0; i < document.forms[form].elements.length; i++) {
365 if (( document.forms[form].elements[i].type == 'checkbox'
366 && document.forms[form].elements[i].checked == true )
367 || ( document.forms[form].elements[i].type == 'hidden'
368 && document.forms[form].elements[i].value == 1 )) {
369 var ss = document.forms[form].elements[i].id;
370 var row = 'rowid' + ss;
371 changeRowColor(row, form);
372 }
373 }
374 }
375
376 /**
377 * reset all the radio buttons with a given name
378 *
379 * @param string fieldName
380 * @param object form
381 * @return null
382 */
383 function unselectRadio(fieldName, form) {
384 for (i = 0; i < document.forms[form].elements.length; i++) {
385 if (document.forms[form].elements[i].name == fieldName) {
386 document.forms[form].elements[i].checked = false;
387 }
388 }
389 return;
390 }
391
392 /**
393 * Function to change button text and disable one it is clicked
394 *
395 * @param obj object - the button clicked
396 * @param formID string - the id of the form being submitted
397 * @param string procText - button text after user clicks it
398 * @return null
399 */
400 var submitcount = 0;
401 /* Changes button label on submit, and disables button after submit for newer browsers.
402 Puts up alert for older browsers. */
403 function submitOnce(obj, formId, procText) {
404 // if named button clicked, change text
405 if (obj.value != null) {
406 obj.value = procText + " ...";
407 }
408 if (document.getElementById) { // disable submit button for newer browsers
409 obj.disabled = true;
410 document.getElementById(formId).submit();
411 return true;
412 }
413 else { // for older browsers
414 if (submitcount == 0) {
415 submitcount++;
416 return true;
417 }
418 else {
419 alert("Your request is currently being processed ... Please wait.");
420 return false;
421 }
422 }
423 }
424
425 function popUp(URL) {
426 day = new Date();
427 id = day.getTime();
428 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');");
429 }
430
431 /**
432 * Function to show / hide the row in optionFields
433 *
434 * @param element name index, that whose innerHTML is to hide else will show the hidden row.
435 */
436 function showHideRow(index) {
437 if (index) {
438 cj('tr#optionField_' + index).hide();
439 if (cj('table#optionField tr:hidden:first').length) {
440 cj('div#optionFieldLink').show();
441 }
442 }
443 else {
444 cj('table#optionField tr:hidden:first').show();
445 if (!cj('table#optionField tr:hidden:last').length) {
446 cj('div#optionFieldLink').hide();
447 }
448 }
449 return false;
450 }
451
452 CRM.strings = CRM.strings || {};
453 CRM.validate = CRM.validate || {
454 params: {},
455 functions: []
456 };
457
458 (function ($, undefined) {
459 "use strict";
460 $(document).ready(function () {
461 $().crmtooltip();
462 $('.crm-container table.row-highlight').on('change', 'input.select-row, input.select-rows', function () {
463 var target, table = $(this).closest('table');
464 if ($(this).hasClass('select-rows')) {
465 target = $('tbody tr', table);
466 $('input.select-row', table).prop('checked', $(this).prop('checked'));
467 }
468 else {
469 target = $(this).closest('tr');
470 $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1);
471 }
472 target.toggleClass('crm-row-selected', $(this).is(':checked'));
473 });
474 $('#crm-container').live('click', function (event) {
475 if ($(event.target).is('.btn-slide')) {
476 var currentActive = $('#crm-container .btn-slide-active');
477 currentActive.children().hide();
478 currentActive.removeClass('btn-slide-active');
479 $(event.target).children().show();
480 $(event.target).addClass('btn-slide-active');
481 }
482 else {
483 $('.btn-slide .panel').hide();
484 $('.btn-slide-active').removeClass('btn-slide-active');
485 }
486 });
487 });
488
489 /**
490 * Function to make multiselect boxes behave as fields in small screens
491 */
492 function advmultiselectResize() {
493 var amswidth = $("#crm-container form:has(table.advmultiselect)").width();
494 if (amswidth < 700) {
495 $("form table.advmultiselect td").css('display', 'block');
496 }
497 else {
498 $("form table.advmultiselect td").css('display', 'table-cell');
499 }
500 var contactwidth = $('#crm-container #mainTabContainer').width();
501 if (contactwidth < 600) {
502 $('#crm-container #mainTabContainer').addClass('narrowpage');
503 $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) {
504 if (index > 1) {
505 if (index % 2 == 0) {
506 $(this).parent().after('<tr class="narrowadded"></tr>');
507 }
508 var item = $(this);
509 $(this).parent().next().append(item);
510 }
511 });
512 }
513 else {
514 $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage');
515 $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () {
516 var nitem = $(this);
517 var parent = $(this).parent();
518 $(this).parent().prev().append(nitem);
519 if (parent.children().size() == 0) {
520 parent.remove();
521 }
522 });
523 $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach();
524 }
525 var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width();
526
527 if (cformwidth < 720) {
528 $('#crm-container .contact_basic_information-section').addClass('narrowform');
529 $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon');
530 if (cformwidth < 480) {
531 $('#crm-container .contact_basic_information-section').addClass('xnarrowform');
532 }
533 else {
534 $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
535 }
536 }
537 else {
538 $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform');
539 $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
540 }
541 }
542
543 advmultiselectResize();
544 $(window).resize(function () {
545 advmultiselectResize();
546 });
547
548 $.fn.crmtooltip = function () {
549 $(document)
550 .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) {
551 $(this).addClass('crm-processed');
552 $(this).addClass('crm-tooltip-active');
553 var topDistance = e.pageY - $(window).scrollTop();
554 if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) {
555 $(this).addClass('crm-tooltip-down');
556 }
557 if (!$(this).children('.crm-tooltip-wrapper').length) {
558 $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>');
559 $(this).children().children('.crm-tooltip')
560 .html('<div class="crm-loading-element"></div>')
561 .load(this.href);
562 }
563 })
564 .on('mouseout', 'a.crm-summary-link', function () {
565 $(this).removeClass('crm-processed');
566 $(this).removeClass('crm-tooltip-active crm-tooltip-down');
567 })
568 .on('click', 'a.crm-summary-link', false);
569 };
570
571 var h;
572 CRM.help = function (title, params, url) {
573 h && h.close && h.close();
574 var options = {
575 expires: 0
576 };
577 h = CRM.alert('...', title, 'crm-help crm-msg-loading', options);
578 params.class_name = 'CRM_Core_Page_Inline_Help';
579 params.type = 'page';
580 $.ajax(url || CRM.url('civicrm/ajax/inline'),
581 {
582 data: params,
583 dataType: 'html',
584 success: function (data) {
585 $('#crm-notification-container .crm-help .notify-content:last').html(data);
586 $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info');
587 },
588 error: function () {
589 $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.');
590 $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error');
591 }
592 }
593 );
594 };
595
596 /**
597 * @param string text Displayable message
598 * @param string title Displayable title
599 * @param string type 'alert'|'info'|'success'|'error' (default: 'alert')
600 * @param {object} options
601 * @return {*}
602 * @see http://wiki.civicrm.org/confluence/display/CRM/Notifications+in+CiviCRM
603 */
604 CRM.alert = function (text, title, type, options) {
605 type = type || 'alert';
606 title = title || '';
607 options = options || {};
608 if ($('#crm-notification-container').length) {
609 var params = {
610 text: text,
611 title: title,
612 type: type
613 };
614 // By default, don't expire errors and messages containing links
615 var extra = {
616 expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000),
617 unique: true
618 };
619 options = $.extend(extra, options);
620 options.expires = options.expires === false ? 0 : parseInt(options.expires, 10);
621 if (options.unique && options.unique !== '0') {
622 $('#crm-notification-container .ui-notify-message').each(function () {
623 if (title === $('h1', this).html() && text === $('.notify-content', this).html()) {
624 $('.icon.ui-notify-close', this).click();
625 }
626 });
627 }
628 return $('#crm-notification-container').notify('create', params, options);
629 }
630 else {
631 if (title.length) {
632 text = title + "\n" + text;
633 }
634 alert(text);
635 return null;
636 }
637 };
638
639 /**
640 * Close whichever alert contains the given node
641 *
642 * @param node
643 */
644 CRM.closeAlertByChild = function (node) {
645 $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click();
646 };
647
648 /**
649 * Prompt the user for confirmation.
650 *
651 * @param buttons {object|function} key|value pairs where key == button label and value == callback function
652 * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue"
653 * @param options {object|void} Override defaults, keys include 'title', 'message',
654 * see jQuery.dialog for full list of available params
655 */
656 CRM.confirm = function (buttons, options, cancelLabel) {
657 var dialog, callbacks = {};
658 cancelLabel = cancelLabel || ts('Cancel');
659 var settings = {
660 title: ts('Confirm Action'),
661 message: ts('Are you sure you want to continue?'),
662 resizable: false,
663 modal: true,
664 width: 'auto',
665 close: function () {
666 $(dialog).remove();
667 },
668 buttons: {}
669 };
670
671 settings.buttons[cancelLabel] = function () {
672 dialog.dialog('close');
673 };
674 options = options || {};
675 $.extend(settings, options);
676 if (typeof(buttons) === 'function') {
677 callbacks[ts('Continue')] = buttons;
678 }
679 else {
680 callbacks = buttons;
681 }
682 $.each(callbacks, function (label, callback) {
683 settings.buttons[label] = function () {
684 callback.call(dialog);
685 dialog.dialog('close');
686 };
687 });
688 dialog = $('<div class="crm-container crm-confirm-dialog"></div>')
689 .html(options.message)
690 .appendTo('body')
691 .dialog(settings);
692 return dialog;
693 };
694
695 /**
696 * Sets an error message
697 * If called for a form item, title and removal condition will be handled automatically
698 */
699 $.fn.crmError = function (text, title, options) {
700 title = title || '';
701 text = text || '';
702 options = options || {};
703
704 var extra = {
705 expires: 0
706 };
707 if ($(this).length) {
708 if (title == '') {
709 var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]');
710 if (label.length) {
711 label.addClass('crm-error');
712 var $label = label.clone();
713 if (text == '' && $('.crm-marker', $label).length > 0) {
714 text = $('.crm-marker', $label).attr('title');
715 }
716 $('.crm-marker', $label).remove();
717 title = $label.text();
718 }
719 }
720 $(this).addClass('error');
721 }
722 var msg = CRM.alert(text, title, 'error', $.extend(extra, options));
723 if ($(this).length) {
724 var ele = $(this);
725 setTimeout(function () {
726 ele.one('change', function () {
727 msg && msg.close && msg.close();
728 ele.removeClass('error');
729 label.removeClass('crm-error');
730 });
731 }, 1000);
732 }
733 return msg;
734 };
735
736 // Display system alerts through js notifications
737 function messagesFromMarkup() {
738 $('div.messages:visible', this).not('.help').not('.no-popup').each(function () {
739 var text, title = '';
740 $(this).removeClass('status messages');
741 var type = $(this).attr('class').split(' ')[0] || 'alert';
742 type = type.replace('crm-', '');
743 $('.icon', this).remove();
744 if ($('.msg-text', this).length > 0) {
745 text = $('.msg-text', this).html();
746 title = $('.msg-title', this).html();
747 }
748 else {
749 text = $(this).html();
750 }
751 var options = $(this).data('options') || {};
752 $(this).remove();
753 // Duplicates were already removed server-side
754 options.unique = false;
755 CRM.alert(text, title, type, options);
756 });
757 // Handle qf form errors
758 $('form :input.error', this).one('blur', function() {
759 // ignore autocomplete fields
760 if ($(this).is('.ac_input')) {
761 return;
762 }
763
764 $('.ui-notify-message.error a.ui-notify-close').click();
765 $(this).removeClass('error');
766 $(this).next('span.crm-error').remove();
767 $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]')
768 .removeClass('crm-error')
769 .find('.crm-error').removeClass('crm-error');
770 });
771 }
772
773 $(function () {
774 if ($('#crm-notification-container').length) {
775 // Initialize notifications
776 $('#crm-notification-container').notify();
777 messagesFromMarkup.call($('#crm-container'));
778 $('#crm-container').on('crmFormLoad', '*', messagesFromMarkup);
779 }
780
781 // bind the event for image popup
782 $('body').on('click', 'a.crm-image-popup', function() {
783 var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
784
785 CRM.confirm('',
786 {
787 title: ts('Preview'),
788 message: o
789 },
790 ts('Done')
791 );
792 return false;
793 });
794 });
795
796 $.fn.crmAccordions = function (speed) {
797 var container = $('#crm-container');
798 if (speed === undefined) {
799 speed = 200;
800 }
801 if ($(this).length > 0) {
802 container = $(this);
803 }
804 if (container.length > 0 && !container.hasClass('crm-accordion-processed')) {
805 // Allow normal clicking of links
806 container.on('click', 'div.crm-accordion-header a', function (e) {
807 e.stopPropagation && e.stopPropagation();
808 });
809 container.on('click', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () {
810 if ($(this).parent().hasClass('collapsed')) {
811 $(this).next().css('display', 'none').slideDown(speed);
812 }
813 else {
814 $(this).next().css('display', 'block').slideUp(speed);
815 }
816 $(this).parent().toggleClass('collapsed');
817 return false;
818 });
819 container.addClass('crm-accordion-processed');
820 }
821 };
822 $.fn.crmAccordionToggle = function (speed) {
823 $(this).each(function () {
824 if ($(this).hasClass('collapsed')) {
825 $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed);
826 }
827 else {
828 $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed);
829 }
830 $(this).toggleClass('collapsed');
831 });
832 };
833
834 /**
835 * Clientside currency formatting
836 * @param value
837 * @param format - currency representation of the number 1234.56
838 * @return string
839 * @see CRM_Core_Resources::addCoreResources
840 */
841 var currencyTemplate;
842 CRM.formatMoney = function(value, format) {
843 var decimal, separator, sign, i, j, result;
844 if (value === 'init' && format) {
845 currencyTemplate = format;
846 return;
847 }
848 format = format || currencyTemplate;
849 result = /1(.?)234(.?)56/.exec(format);
850 if (result === null) {
851 return 'Invalid format passed to CRM.formatMoney';
852 }
853 separator = result[1];
854 decimal = result[2];
855 sign = (value < 0) ? '-' : '';
856 //extracting the absolute value of the integer part of the number and converting to string
857 i = parseInt(value = Math.abs(value).toFixed(2)) + '';
858 j = ((j = i.length) > 3) ? j % 3 : 0;
859 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) : '');
860 return format.replace(/1.*234.*56/, result);
861 };
862 })(jQuery);
863
864 // @license-end