// https://civicrm.org/licensing
/* global CRM:true */
var CRM = CRM || {};
var cj = CRM.$ = jQuery;
CRM._ = _;
/**
* Short-named function for string translation, defined in global scope so it's available everywhere.
*
* @param text string for translating
* @param params object key:value of additional parameters
*
* @return string
*/
function ts(text, params) {
"use strict";
var d = (params && params.domain) ? ('strings::' + params.domain) : null;
if (d && CRM[d] && CRM[d][text]) {
text = CRM[d][text];
}
else if (CRM.strings[text]) {
text = CRM.strings[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;
}
// Legacy code - ignore warnings
/* jshint ignore:start */
/**
* 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.
*
* @deprecated
* @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 ...)
*/
function on_load_init_blocks(showBlocks, hideBlocks, elementType) {
if (elementType == null) {
elementType = 'block';
}
var myElement, i;
/* This loop is used to display the blocks whose IDs are present within the showBlocks array */
for (i = 0; i < showBlocks.length; i++) {
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 (i = 0; i < hideBlocks.length; i++) {
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).
*
* @deprecated
* @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
*/
function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) {
var target, j;
if (field_type == 'select') {
var trigger = trigger_value.split("|");
var selectedOptionValue = cj('#' + trigger_field_id).val();
target = target_element_id.split("|");
for (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') {
target = target_element_id.split("|");
for (j = 0; j < target.length; j++) {
if (cj('[name="' + trigger_field_id + '"]:first').is(':checked')) {
if (invert) {
cj('#' + target[j]).hide();
}
else {
cj('#' + target[j]).show();
}
}
else {
if (invert) {
cj('#' + target[j]).show();
}
else {
cj('#' + target[j]).hide();
}
}
}
}
}
}
var submitcount = 0;
/**
* Function to show / hide the row in optionFields
* @deprecated
* @param index string, element whose innerHTML is to hide else will show the hidden row.
*/
function showHideRow(index) {
if (index) {
cj('tr#optionField_' + index).hide();
if (cj('table#optionField tr:hidden:first').length) {
cj('div#optionFieldLink').show();
}
}
else {
cj('table#optionField tr:hidden:first').show();
if (!cj('table#optionField tr:hidden:last').length) {
cj('div#optionFieldLink').hide();
}
}
return false;
}
/* jshint ignore:end */
if (!CRM.utils) CRM.utils = {};
if (!CRM.strings) CRM.strings = {};
if (!CRM.vars) CRM.vars = {};
(function ($, _, undefined) {
"use strict";
/* jshint validthis: true */
// Theme classes for unattached elements
$.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container';
// https://github.com/ivaynberg/select2/pull/2090
$.fn.select2.defaults.width = 'resolve';
// Workaround for https://github.com/ivaynberg/select2/issues/1246
$.ui.dialog.prototype._allowInteraction = function(e) {
return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop, .cke_dialog, #civicrm-menu').length;
};
// Implements jQuery hook.prop
$.propHooks.disabled = {
set: function (el, value, name) {
// Sync button enabled status with wrapper css
if ($(el).is('.crm-button.crm-form-submit')) {
$(el).parent().toggleClass('crm-button-disabled', !!value);
}
// Sync button enabled status with dialog button
if ($(el).is('.ui-dialog input.crm-form-submit')) {
$(el).closest('.ui-dialog').find('.ui-dialog-buttonset button[data-identifier='+ $(el).attr('name') +']').prop('disabled', value);
}
if ($(el).is('.crm-form-date-wrapper .crm-hidden-date')) {
$(el).siblings().prop('disabled', value);
}
}
};
var scriptsLoaded = {};
CRM.loadScript = function(url, appendCacheCode) {
if (!scriptsLoaded[url]) {
var script = document.createElement('script'),
src = url;
if (appendCacheCode !== false) {
src += (_.includes(url, '?') ? '&r=' : '?r=') + CRM.config.resourceCacheCode;
}
scriptsLoaded[url] = $.Deferred();
script.onload = function () {
// Give the script time to execute
window.setTimeout(function () {
if (window.jQuery === CRM.$ && CRM.CMSjQuery) {
window.jQuery = CRM.CMSjQuery;
}
scriptsLoaded[url].resolve();
}, 100);
};
// Make jQuery global available while script is loading
if (window.jQuery !== CRM.$) {
CRM.CMSjQuery = window.jQuery;
window.jQuery = CRM.$;
}
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
}
return scriptsLoaded[url];
};
/**
* Populate a select list, overwriting the existing options except for the placeholder.
* @param select jquery selector - 1 or more select elements
* @param options array in format returned by api.getoptions
* @param placeholder string|bool - new placeholder or false (default) to keep the old one
* @param value string|array - will silently update the element with new value without triggering change
*/
CRM.utils.setOptions = function(select, options, placeholder, value) {
$(select).each(function() {
var
$elect = $(this),
val = value || $elect.val() || [],
opts = placeholder || placeholder === '' ? '' : '[value!=""]';
$elect.find('option' + opts).remove();
var newOptions = CRM.utils.renderOptions(options, val);
if (options.length == 0) {
$elect.removeClass('required');
} else if ($elect.hasClass('crm-field-required') && !$elect.hasClass('required')) {
$elect.addClass('required');
}
if (typeof placeholder === 'string') {
if ($elect.is('[multiple]')) {
select.attr('placeholder', placeholder);
} else {
newOptions = '' + newOptions;
}
}
$elect.append(newOptions);
if (!value) {
$elect.trigger('crmOptionsUpdated', $.extend({}, options)).trigger('change');
}
});
};
/**
* Render an option list
* @param options {array}
* @param val {string} default value
* @param escapeHtml {bool}
* @return string
*/
CRM.utils.renderOptions = function(options, val, escapeHtml) {
var rendered = '',
esc = escapeHtml === false ? _.identity : _.escape;
if (!$.isArray(val)) {
val = [val];
}
_.each(options, function(option) {
if (option.children) {
rendered += '';
} else {
var selected = ($.inArray('' + option.key, val) > -1) ? 'selected="selected"' : '';
rendered += '';
}
});
return rendered;
};
CRM.utils.getOptions = function(select) {
var options = [];
$('option', select).each(function() {
var option = {key: $(this).attr('value'), value: $(this).text()};
if (option.key !== '') {
options.push(option);
}
});
return options;
};
function chainSelect() {
var $form = $(this).closest('form'),
$target = $('select[data-name="' + $(this).data('target') + '"]', $form),
data = $target.data(),
val = $(this).val();
$target.prop('disabled', true);
if ($target.is('select.crm-chain-select-control')) {
$('select[data-name="' + $target.data('target') + '"]', $form).prop('disabled', true).blur();
}
if (!(val && val.length)) {
CRM.utils.setOptions($target.blur(), [], data.emptyPrompt);
} else {
$target.addClass('loading');
$.getJSON(CRM.url(data.callback), {_value: val}, function(vals) {
$target.prop('disabled', false).removeClass('loading');
CRM.utils.setOptions($target, vals || [], (vals && vals.length ? data.selectPrompt : data.nonePrompt));
});
}
}
/**
* Compare Form Input values against cached initial value.
*
* @return {Boolean} true if changes have been made.
*/
CRM.utils.initialValueChanged = function(el) {
var isDirty = false;
$(':input:visible, .select2-container:visible+:input:hidden', el).not('[type=submit], [type=button], .crm-action-menu, :disabled').each(function () {
var
initialValue = $(this).data('crm-initial-value'),
currentValue = $(this).is(':checkbox, :radio') ? $(this).prop('checked') : $(this).val();
// skip change of value for submit buttons
if (initialValue !== undefined && !_.isEqual(initialValue, currentValue)) {
isDirty = true;
}
});
return isDirty;
};
/**
* This provides defaults for ui.dialog which either need to be calculated or are different from global defaults
*
* @param settings
* @returns {*}
*/
CRM.utils.adjustDialogDefaults = function(settings) {
settings = $.extend({width: '65%', height: '65%', modal: true}, settings || {});
// Support relative height
if (typeof settings.height === 'string' && settings.height.indexOf('%') > 0) {
settings.height = parseInt($(window).height() * (parseFloat(settings.height)/100), 10);
}
// Responsive adjustment - increase percent width on small screens
if (typeof settings.width === 'string' && settings.width.indexOf('%') > 0) {
var screenWidth = $(window).width(),
percentage = parseInt(settings.width.replace('%', ''), 10),
gap = 100-percentage;
if (screenWidth < 701) {
settings.width = '100%';
}
else if (screenWidth < 1400) {
settings.width = '' + parseInt(percentage+gap-((screenWidth - 700)/7*(gap)/100), 10) + '%';
}
}
return settings;
};
function formatCrmSelect2(row) {
var icon = row.icon || $(row.element).data('icon'),
color = row.color || $(row.element).data('color'),
description = row.description || $(row.element).data('description'),
ret = '';
if (icon) {
ret += ' ';
}
if (color) {
ret += ' ';
}
return ret + _.escape(row.text) + (description ? '
' + _.escape(description) + '
' : '');
}
/**
* Helper to generate an icon with alt text.
*
* See also smarty `{icon}` and CRM_Core_Page::crmIcon() functions
*
* @param string icon
* The Font Awesome icon class to use.
* @param string text
* Alt text to display.
* @param mixed condition
* This will only display if this is truthy.
*
* @return string
* The formatted icon markup.
*/
CRM.utils.formatIcon = function (icon, text, condition) {
if (typeof condition !== 'undefined' && !condition) {
return '';
}
var title = '';
var sr = '';
if (text) {
text = _.escape(text);
title = ' title="' + text + '"';
sr = '' + text + '';
}
return '' + sr;
};
/**
* Wrapper for select2 initialization function; supplies defaults
* @param options object
*/
$.fn.crmSelect2 = function(options) {
if (options === 'destroy') {
return $(this).each(function() {
$(this)
.removeClass('crm-ajax-select')
.off('.crmSelect2')
.select2('destroy');
});
}
return $(this).each(function () {
var
$el = $(this),
iconClass,
settings = {
allowClear: !$el.hasClass('required'),
formatResult: formatCrmSelect2,
formatSelection: formatCrmSelect2
};
// quickform doesn't support optgroups so here's a hack :(
$('option[value^=crm_optgroup]', this).each(function () {
$(this).nextUntil('option[value^=crm_optgroup]').wrapAll('');
$(this).remove();
});
// quickform does not support disabled option, so yet another hack to
// add disabled property for option values
$('option[value^=crm_disabled_opt]', this).attr('disabled', 'disabled');
// Placeholder icon - total hack hikacking the escapeMarkup function but select2 3.5 dosn't have any other callbacks for this :(
if ($el.is('[class*=fa-]')) {
settings.escapeMarkup = function (m) {
var out = _.escape(m),
placeholder = settings.placeholder || $el.data('placeholder') || $el.attr('placeholder') || $('option[value=""]', $el).text();
if (m.length && placeholder === m) {
iconClass = $el.attr('class').match(/(fa-\S*)/)[1];
out = ' ' + out;
}
return out;
};
}
// Use description as title for each option
$el.on('select2-loaded.crmSelect2', function() {
$('.crm-select2-row-description', '#select2-drop').each(function() {
$(this).closest('.select2-result-label').attr('title', $(this).text());
});
});
// Defaults for single-selects
if ($el.is('select:not([multiple])')) {
settings.minimumResultsForSearch = 10;
if ($('option:first', this).val() === '') {
settings.placeholderOption = 'first';
}
}
$.extend(settings, $el.data('select-params') || {}, options || {});
if (settings.ajax) {
$el.addClass('crm-ajax-select');
}
$el.select2(settings);
});
};
/**
* @see CRM_Core_Form::addEntityRef for docs
* @param options object
*/
$.fn.crmEntityRef = function(options) {
if (options === 'destroy') {
return $(this).each(function() {
var entity = $(this).data('api-entity') || '';
$(this)
.off('.crmEntity')
.removeClass('crm-form-entityref crm-' + _.kebabCase(entity) + '-ref')
.crmSelect2('destroy');
});
}
options = options || {};
options.select = options.select || {};
return $(this).each(function() {
var
$el = $(this).off('.crmEntity'),
entity = options.entity || $el.data('api-entity') || 'Contact',
selectParams = {},
staticPresets = {
user_contact_id: {
id: 'user_contact_id',
label: ts('Select Current User'),
icon: 'fa-user-circle-o'
}
};
// Legacy: fix entity name if passed in as snake case
if (entity.charAt(0).toUpperCase() !== entity.charAt(0)) {
entity = _.capitalize(_.camelCase(entity));
}
$el.data('api-entity', entity);
$el.data('select-params', $.extend({}, $el.data('select-params') || {}, options.select));
$el.data('api-params', $.extend(true, {}, $el.data('api-params') || {}, options.api));
$el.data('create-links', options.create || $el.data('create-links'));
var staticItems = options.static || $el.data('static') || [];
_.each(staticItems, function(option, i) {
if (_.isString(option)) {
staticItems[i] = staticPresets[option];
}
});
function staticItemMarkup() {
if (!staticItems.length) {
return '';
}
var markup = '