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 selectedOptionValue = cj('#' + trigger_field_id).val();
var target = target_element_id.split("|");
for (var j = 0; j < target.length; j++) {
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 (cj('[name="' + trigger_field_id + '"]').is(':checked')) {
if (invert) {
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) {
- var val = (object.id == 'toggleSelect' && cj(object).is(':checked'));
- cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').prop('checked', val);
- // 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.prop('checked')) {
- cj('input[value=ts_sel]:radio').prop('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 <tr>, <div> you want to change
- *
- * @access public
- * @return null
- */
-function changeRowColor(rowid, form) {
- switch (document.getElementById(rowid).className) {
- case 'even-row' :
- document.getElementById(rowid).className = 'selected even-row';
- break;
- case 'odd-row' :
- document.getElementById(rowid).className = 'selected odd-row';
- break;
- case 'selected even-row' :
- document.getElementById(rowid).className = 'even-row';
- break;
- case 'selected odd-row' :
- document.getElementById(rowid).className = 'odd-row';
- break;
- case 'form-item' :
- document.getElementById(rowid).className = 'selected';
- break;
- case 'selected' :
- document.getElementById(rowid).className = 'form-item';
- }
-}
-
-/**
- * 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_check(form) {
- for (i = 0; i < document.forms[form].elements.length; i++) {
- if (( document.forms[form].elements[i].type == 'checkbox'
- && document.forms[form].elements[i].checked == true )
- || ( document.forms[form].elements[i].type == 'hidden'
- && document.forms[form].elements[i].value == 1 )) {
- var ss = document.forms[form].elements[i].id;
- var row = 'rowid' + ss;
- changeRowColor(row, form);
- }
- }
-}
-
-/**
- * reset all the radio buttons with a given name
- *
- * @param string fieldName
- * @param object form
- * @return null
- */
-function unselectRadio(fieldName, form) {
- for (i = 0; i < document.forms[form].elements.length; i++) {
- if (document.forms[form].elements[i].name == fieldName) {
- document.forms[form].elements[i].checked = false;
- }
- }
- return;
-}
-
/**
* Function to change button text and disable one it is clicked
*
return false;
}
+CRM.utils = CRM.utils || {};
CRM.strings = CRM.strings || {};
CRM.validate = CRM.validate || {
params: {},
(function ($, undefined) {
"use strict";
- $(document).ready(function () {
- $().crmtooltip();
- $('.crm-container table.row-highlight').on('change', 'input.select-row, input.select-rows', function () {
- var target, table = $(this).closest('table');
- if ($(this).hasClass('select-rows')) {
- target = $('tbody tr', table);
- $('input.select-row', table).prop('checked', $(this).prop('checked'));
+
+ // 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').length;
+ };
+
+ /**
+ * Populate a select list, overwriting the existing options except for the placeholder.
+ * @param $el jquery collection - 1 or more select elements
+ * @param options array in format returned by api.getoptions
+ */
+ CRM.utils.setOptions = function($el, options) {
+ $el.each(function() {
+ var
+ $elect = $(this),
+ val = $elect.val() || [];
+ if (typeof(val) !== 'array') {
+ val = [val];
}
- else {
- target = $(this).closest('tr');
- $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1);
+ $elect.find('option[value!=""]').remove();
+ $.each(options, function(key, option) {
+ var selected = ($.inArray(''+option.key, val) > -1) ? 'selected="selected"' : '';
+ $elect.append('<option value="' + option.key + '"' + selected + '>' + option.value + '</option>');
+ });
+ $elect.trigger('crmOptionsUpdated').trigger('change');
+ });
+ };
+
+ CRM.utils.formatSelect2Result = function(row) {
+ var markup = '<div class="crm-select2-row">';
+ if (row.image !== undefined) {
+ markup += '<div class="crm-select2-image"><img src="' + row.image + '"/></div>';
+ }
+ else if (row.icon_class) {
+ markup += '<div class="crm-select2-icon"><div class="crm-icon ' + row.icon_class + '-icon"></div></div>';
+ }
+ markup += '<div><div class="crm-select2-row-label">' + row.label + '</div>';
+ markup += '<div class="crm-select2-row-description">';
+ $.each(row.description || [], function(k, text) {
+ markup += '<p>' + text + '</p>';
+ });
+ markup += '</div></div></div>';
+ return markup;
+ };
+
+ CRM.utils.formatSelect2CreateLinks = function($el) {
+ var markup = '';
+ $.each($el.data('create-links'), function(k, link) {
+ markup += ' <a class="crm-add-entity crm-hover-button" href="' + link.url + '">';
+ if (link.name) {
+ markup += '<span class="icon ' + link.name + '-icon"></span> ';
}
- target.toggleClass('crm-row-selected', $(this).is(':checked'));
+ markup += link.label + '</a>';
});
- $('body').on('click', function (event) {
- $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide();
- if ($(event.target).is('.btn-slide')) {
- $(event.target).addClass('btn-slide-active').find('.panel').show();
+ return markup;
+ };
+
+ // Initialize widgets
+ $(document).on('crmLoad', function(e) {
+ $('table.row-highlight', e.target)
+ .off('.rowHighlight')
+ .on('change.rowHighlight', 'input.select-row, input.select-rows', function () {
+ var target, table = $(this).closest('table');
+ if ($(this).hasClass('select-rows')) {
+ target = $('tbody tr', table);
+ $('input.select-row', table).prop('checked', $(this).prop('checked'));
+ }
+ else {
+ target = $(this).closest('tr');
+ $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1);
+ }
+ target.toggleClass('crm-row-selected', $(this).is(':checked'));
+ })
+ .find('input.select-row:checked').parents('tr').addClass('crm-row-selected');
+ $('.crm-select2', e.target).each(function() {
+ var $el = $(this), options = {};
+ // quickform doesn't support optgroups so here's a hack :(
+ $('option[value^=crm_optgroup]', this).each(function() {
+ $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />');
+ $(this).remove();
+ });
+ // Defaults for single-selects
+ if ($el.is('select:not([multiple])')) {
+ options.minimumResultsForSearch = 10;
+ options.allowClear = !($el.hasClass('required'));
+ if ($('option:first', this).val() === '') {
+ options.placeholderOption = 'first';
+ }
}
+ $.extend(options, $el.data('select-params') || {});
+ // Autocomplete using the getlist api
+ if ($el.data('api-entity') && $el.hasClass('crm-form-entityref')) {
+ $el.addClass('crm-ajax-select');
+ var settings = {
+ // Use select2 ajax helper instead of CRM.api because it provides more value
+ ajax: {
+ url: CRM.url('civicrm/ajax/rest'),
+ data: function (input, page_num) {
+ var params = $el.data('api-params') || {};
+ params.input = input;
+ params.page_num = page_num;
+ return {
+ entity: $el.data('api-entity'),
+ action: 'getlist',
+ json: JSON.stringify(params)
+ };
+ },
+ results: function(data) {
+ return {more: data.more_results, results: data.values || []};
+ }
+ },
+ formatResult: CRM.utils.formatSelect2Result,
+ formatSelection: function(row) {
+ return row.label;
+ },
+ escapeMarkup: function (m) {return m;},
+ initSelection: function(el, callback) {
+ callback(el.data('entity-value'));
+ }
+ };
+ if ($el.data('create-links')) {
+ options.formatInputTooShort = function() {
+ var txt = $el.data('select-params').formatInputTooShort || $.fn.select2.defaults.formatInputTooShort;
+ return txt + ' ' + ts('or') + '<br />' + CRM.utils.formatSelect2CreateLinks($el);
+ };
+ options.formatNoMatches = function() {
+ var txt = $el.data('select-params').formatNoMatches || $.fn.select2.defaults.formatNoMatches;
+ return txt + '<br />' + CRM.utils.formatSelect2CreateLinks($el);
+ };
+ $el.on('select2-open', function() {
+ var $el = $(this);
+ $('#select2-drop').off('.crmEntity').on('click.crmEntity', 'a.crm-add-entity', function(e) {
+ $el.select2('close');
+ CRM.loadForm($(this).attr('href'), {
+ dialog: {width: 500, height: 'auto'}
+ }).on('crmFormSuccess', function(e, data) {
+ if ($el.select2('container').hasClass('select2-container-multi')) {
+ var selection = $el.select2('data');
+ selection.push(data);
+ $el.select2('data', selection);
+ } else {
+ $el.select2('data', data);
+ }
+ });
+ return false;
+ });
+ });
+ }
+ options = $.extend(settings, options);
+ }
+ options.dropdownCssClass = 'crm-container';
+ $(this).select2(options).removeClass('crm-select2');
});
});
.on('click', 'a.crm-summary-link', false);
};
- var h;
+ var helpDisplay, helpPrevious;
CRM.help = function (title, params, url) {
- h && h.close && h.close();
- var options = {
- expires: 0
- };
- h = CRM.alert('...', title, 'crm-help crm-msg-loading', options);
+ if (helpDisplay && helpDisplay.close) {
+ // If the same link is clicked twice, just close the display - todo use underscore method for this comparison
+ if (helpDisplay.isOpen && helpPrevious === JSON.stringify(params)) {
+ helpDisplay.close();
+ return;
+ }
+ helpDisplay.close();
+ }
+ helpPrevious = JSON.stringify(params);
params.class_name = 'CRM_Core_Page_Inline_Help';
params.type = 'page';
+ helpDisplay = CRM.alert('...', title, 'crm-help crm-msg-loading', {expires: 0});
$.ajax(url || CRM.url('civicrm/ajax/inline'),
{
data: params,
* passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue"
* @param options {object|void} Override defaults, keys include 'title', 'message',
* see jQuery.dialog for full list of available params
+ * @param cancelLabel {string}
*/
CRM.confirm = function (buttons, options, cancelLabel) {
var dialog, callbacks = {};
}
$.each(callbacks, function (label, callback) {
settings.buttons[label] = function () {
- callback.call(dialog);
- dialog.dialog('close');
+ if (callback.call(dialog) !== false) {
+ dialog.dialog('close');
+ }
};
});
dialog = $('<div class="crm-container crm-confirm-dialog"></div>')
catch (e) {}
});
+ /**
+ * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin
+ */
+ $.widget('civi.crmAutocomplete', $.ui.autocomplete, {});
+
$(function () {
// Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content.
$('.crm-container').trigger('crmLoad');
}
// bind the event for image popup
- $('body').on('click', 'a.crm-image-popup', function() {
- var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
+ $('body')
+ .on('click', 'a.crm-image-popup', function() {
+ var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
- CRM.confirm('',
- {
- title: ts('Preview'),
- message: o
- },
- ts('Done')
- );
- return false;
- });
+ CRM.confirm('',
+ {
+ title: ts('Preview'),
+ message: o
+ },
+ ts('Done')
+ );
+ return false;
+ })
+
+ .on('click', function (event) {
+ $('.btn-slide-active').removeClass('btn-slide-active').find('.panel').hide();
+ if ($(event.target).is('.btn-slide')) {
+ $(event.target).addClass('btn-slide-active').find('.panel').show();
+ }
+ })
+
+ .on('click', 'a.crm-option-edit-link', function() {
+ var link = $(this);
+ CRM.loadForm(this.href, {openInline: 'a:not("[href=#], .no-popup")'})
+ // Lots of things can happen once the form opens, this is the only event we can really rely on
+ .on('dialogclose', function() {
+ link.trigger('crmOptionsEdited');
+ var $elects = $('select[data-option-edit-path="' + link.data('option-edit-path') + '"]');
+ if ($elects.data('api-entity') && $elects.data('api-field')) {
+ CRM.api3($elects.data('api-entity'), 'getoptions', {sequential: 1, field: $elects.data('api-field')})
+ .done(function(data) {
+ CRM.utils.setOptions($elects, data.values);
+ });
+ }
+ });
+ return false;
+ })
+ // Handle clear button for form elements
+ .on('click', 'a.crm-clear-link', function() {
+ $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).change();
+ $(this).siblings('input:text').val('').change();
+ return false;
+ })
+ .on('change', 'input.crm-form-radio:checked', function() {
+ $(this).siblings('.crm-clear-link').css({visibility: ''});
+ });
+ $().crmtooltip();
});
$.fn.crmAccordions = function (speed) {