// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt /* +--------------------------------------------------------------------+ | CiviCRM version 4.4 | +--------------------------------------------------------------------+ | Copyright CiviCRM LLC (c) 2004-2013 | +--------------------------------------------------------------------+ | This file is a part of CiviCRM. | | | | CiviCRM is free software; you can copy, modify, and distribute it | | under the terms of the GNU Affero General Public License | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | | | CiviCRM is distributed in the hope that it will be useful, but | | WITHOUT ANY WARRANTY; without even the implied warranty of | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | See the GNU Affero General Public License for more details. | | | | You should have received a copy of the GNU Affero General Public | | License and the CiviCRM Licensing Exception along | | with this program; if not, contact CiviCRM LLC | | at info[AT]civicrm[DOT]org. If you have questions about the | | GNU Affero General Public License or the licensing of CiviCRM, | | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ */ /** * @file: global functions for CiviCRM * FIXME: We are moving away from using global functions. DO NOT ADD MORE. * @see CRM object - the better alternative to adding global functions */ var CRM = CRM || {}; var cj = jQuery; /** * Short-named function for string translation, defined in global scope so it's available everywhere. * * @param $text string string for translating * @param $params object key:value of additional parameters * * @return string the translated string */ function ts(text, params) { "use strict"; text = CRM.strings[text] || text; if (typeof(params) === 'object') { for (var i in params) { if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') { // sprintf emulation: escape % characters in the replacements to avoid conflicts text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-')); } } return text.replace(/%-crmescaped-/g, '%'); } return text; } /** * This function is called by default at the bottom of template files which have forms that have * conditionally displayed/hidden sections and elements. The PHP is responsible for generating * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to * this function. * * @access public * @param showBlocks Array of element Id's to be displayed * @param hideBlocks Array of element Id's to be hidden * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...) * @return none */ function on_load_init_blocks(showBlocks, hideBlocks, elementType) { if (elementType == null) { var elementType = 'block'; } /* This loop is used to display the blocks whose IDs are present within the showBlocks array */ for (var i = 0; i < showBlocks.length; i++) { var myElement = document.getElementById(showBlocks[i]); /* getElementById returns null if element id doesn't exist in the document */ if (myElement != null) { myElement.style.display = elementType; } else { alert('showBlocks array item not in .tpl = ' + showBlocks[i]); } } /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */ for (var i = 0; i < hideBlocks.length; i++) { var myElement = document.getElementById(hideBlocks[i]); /* getElementById returns null if element id doesn't exist in the document */ if (myElement != null) { myElement.style.display = 'none'; } else { alert('showBlocks array item not in .tpl = ' + hideBlocks[i]); } } } /** * This function is called when we need to show or hide a related form element (target_element) * based on the value (trigger_value) of another form field (trigger_field). * * @access public * @param trigger_field_id HTML id of field whose onchange is the trigger * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field * @param target_element_id HTML id of element to be shown or hidden * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row') * @param field_type Type of element radio/select * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match * @return none */ function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) { if (target_element_type == null) { var target_element_type = 'block'; } else { if (target_element_type == 'table-row') { var target_element_type = ''; } } if (field_type == 'select') { var trigger = trigger_value.split("|"); var selectedOptionValue = document.getElementById(trigger_field_id).options[document.getElementById(trigger_field_id).selectedIndex].value; var target = target_element_id.split("|"); for (var j = 0; j < target.length; j++) { if (invert) { cj('#' + target[j]).show(); } else { cj('#' + target[j]).hide(); } for (var i = 0; i < trigger.length; i++) { if (selectedOptionValue == trigger[i]) { if (invert) { cj('#' + target[j]).hide(); } else { cj('#' + target[j]).show(); } } } } } else { if (field_type == 'radio') { var target = target_element_id.split("|"); for (var j = 0; j < target.length; j++) { if (document.getElementsByName(trigger_field_id)[0].checked) { if (invert) { cj('#' + target[j]).hide(); } else { cj('#' + target[j]).show(); } } else { if (invert) { cj('#' + target[j]).show(); } else { cj('#' + target[j]).hide(); } } } } } } /** * * Function for checking ALL or unchecking ALL check boxes in a resultset page. * * @access public * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as * belonging to the resultset's checkbox collection * @param object - checkbox * Sample usage: onClick="javascript:changeCheckboxValues('chk_', cj(this) );" * * @return */ function toggleCheckboxVals(fldPrefix, object) { if (object.id == 'toggleSelect' && cj(object).is(':checked')) { cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', true); } else { cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', false); } // change the class of selected rows on_load_init_checkboxes(object.form.name); } function countSelectedCheckboxes(fldPrefix, form) { fieldCount = 0; for (i = 0; i < form.elements.length; i++) { fpLen = fldPrefix.length; if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0, fpLen) == fldPrefix && form.elements[i].checked == true) { fieldCount++; } } return fieldCount; } /** * Function to enable task action select */ function toggleTaskAction(status) { var radio_ts = document.getElementsByName('radio_ts'); if (!radio_ts[1]) { radio_ts[0].checked = true; } if (radio_ts[0].checked || radio_ts[1].checked) { status = true; } var formElements = ['task', 'Go', 'Print']; for (var i = 0; i < formElements.length; i++) { var element = document.getElementById(formElements[i]); if (element) { if (status) { element.disabled = false; } else { element.disabled = true; } } } } /** * This function is used to check if any actio is selected and also to check if any contacts are checked. * * @access public * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as * belonging to the resultset's checkbox collection * @param form - name of form that checkboxes are part of * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );" * */ function checkPerformAction(fldPrefix, form, taskButton, selection) { var cnt; var gotTask = 0; // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task if (taskButton == 1) { gotTask = 1; } else { if (document.forms[form].task.selectedIndex) { //force user to select all search contacts, CRM-3711 if (document.forms[form].task.value == 13 || document.forms[form].task.value == 14) { var toggleSelect = document.getElementsByName('toggleSelect'); if (toggleSelect[0].checked || document.forms[form].radio_ts[0].checked) { return true; } else { alert("Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button."); return false; } } gotTask = 1; } } if (gotTask == 1) { // If user wants to perform action on ALL records and we have a task, return (no need to check further) if (document.forms[form].radio_ts[0].checked) { return true; } cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]); if (!cnt) { 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."); return false; } } else { alert("Please select an action from the drop-down menu."); return false; } } /** * This function changes the style for a checkbox block when it is selected. * * @access public * @param chkName - it is name of the checkbox * @return null */ function checkSelectedBox(chkName) { var checkElement = cj('#' + chkName); if (checkElement.attr('checked')) { cj('input[value=ts_sel]:radio').attr('checked', true); checkElement.parents('tr').addClass('crm-row-selected'); } else { checkElement.parents('tr').removeClass('crm-row-selected'); } } /** * This function is to show the row with selected checkbox in different color * @param form - name of form that checkboxes are part of * * @access public * @return null */ function on_load_init_checkboxes(form) { var formName = form; var fldPrefix = 'mark_x'; for (i = 0; i < document.forms[formName].elements.length; i++) { fpLen = fldPrefix.length; if (document.forms[formName].elements[i].type == 'checkbox' && document.forms[formName].elements[i].name.slice(0, fpLen) == fldPrefix) { checkSelectedBox(document.forms[formName].elements[i].name, formName); } } } /** * Function to change the color of the class * * @param form - name of the form * @param rowid - id of the