// https://civicrm.org/licensing
+/* global CRM:true */
var CRM = CRM || {};
var cj = CRM.$ = jQuery;
CRM._ = _;
*/
function ts(text, params) {
"use strict";
- text = CRM.strings[text] || text;
+ 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') {
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
*/
function on_load_init_blocks(showBlocks, hideBlocks, elementType) {
if (elementType == null) {
- var elementType = 'block';
+ elementType = 'block';
}
+ var myElement, i;
+
/* 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]);
+ 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;
}
/* 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]);
+ 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';
* @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();
- var target = target_element_id.split("|");
- for (var j = 0; j < target.length; j++) {
+ target = target_element_id.split("|");
+ for (j = 0; j < target.length; j++) {
if (invert) {
cj('#' + target[j]).show();
}
}
else {
if (field_type == 'radio') {
- var target = target_element_id.split("|");
- for (var j = 0; j < target.length; j++) {
+ 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();
return false;
}
+/* jshint ignore:end */
+
CRM.utils = CRM.utils || {};
CRM.strings = CRM.strings || {};
(function ($, _, undefined) {
"use strict";
+ /* jshint validthis: true */
// Theme classes for unattached elements
$.fn.select2.defaults.dropdownCssClass = $.ui.dialog.prototype.options.dialogClass = 'crm-container';
return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop, .cke_dialog').length;
};
+ // Implements jQuery hook.prop
+ $.propHooks.disabled = {
+ set: function (el, value, name) {
+ // Sync button enabled status with wrapper css
+ if ($(el).is('span.crm-button > input.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);
+ }
+ }
+ };
+
/**
* Populate a select list, overwriting the existing options except for the placeholder.
* @param select jquery selector - 1 or more select elements
});
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;
+ };
+
/**
* Wrapper for select2 initialization function; supplies defaults
* @param options object
filter = $.extend({}, $el.data('user-filter') || {});
if (filter.key && filter.value) {
// Special case for contact type/sub-type combo
- if (filter.key === 'contact_type' && (filter.value.indexOf('.') > 0)) {
- combined.params.contact_type = filter.value.split('.')[0];
- combined.params.contact_sub_type = filter.value.split('.')[1];
+ if (filter.key === 'contact_type' && (filter.value.indexOf('__') > 0)) {
+ combined.params.contact_type = filter.value.split('__')[0];
+ combined.params.contact_sub_type = filter.value.split('__')[1];
} else {
// Allow json-encoded api filters e.g. {"BETWEEN":[123,456]}
combined.params[filter.key] = filter.value.charAt(0) === '{' ? $.parseJSON(filter.value) : filter.value;
return combined;
}
+ function copyAttributes($source, $target, attributes) {
+ _.each(attributes, function(name) {
+ if ($source.attr(name)) {
+ $target.attr(name, $source.attr(name));
+ }
+ });
+ }
+
+ $.fn.crmDatepicker = function(options) {
+ return $(this).each(function() {
+ if ($(this).is('.crm-form-date-wrapper .crm-hidden-date')) {
+ // Already initialized
+ return;
+ }
+ var
+ $dataField = $(this).wrap('<span class="crm-form-date-wrapper" />'),
+ settings = $.extend({}, $dataField.data('datepicker') || {}, options || {}),
+ $dateField = $(),
+ $timeField = $(),
+ $clearLink = $();
+
+ if (settings.allowClear !== undefined ? settings.allowClear : !$dataField.hasClass('required')) {
+ $clearLink = $('<a class="crm-hover-button crm-clear-link" title="'+ ts('Clear') +'"><span class="icon ui-icon-close"></span></a>')
+ .insertAfter($dataField);
+ }
+ if (settings.time !== false) {
+ $timeField = $('<input>').insertAfter($dataField);
+ copyAttributes($dataField, $timeField, ['class', 'disabled']);
+ $timeField
+ .addClass('crm-form-text crm-form-time')
+ .attr('placeholder', $dataField.attr('time-placeholder') === undefined ? ts('Time') : $dataField.attr('time-placeholder'))
+ .change(updateDataField)
+ .timeEntry({
+ spinnerImage: '',
+ show24Hours: settings.time === true || settings.time === undefined ? CRM.config.timeIs24Hr : settings.time == '24'
+ });
+ }
+ if (settings.date !== false) {
+ $dateField = $('<input>').insertAfter($dataField);
+ copyAttributes($dataField, $dateField, ['placeholder', 'style', 'class', 'disabled']);
+ $dateField.addClass('crm-form-text crm-form-date');
+ settings.dateFormat = settings.dateFormat || CRM.config.dateInputFormat;
+ settings.changeMonth = _.includes('m', settings.dateFormat);
+ settings.changeYear = _.includes('y', settings.dateFormat);
+ $dateField.datepicker(settings).change(updateDataField);
+ }
+ function updateInputFields(e, context) {
+ var val = $dataField.val(),
+ time = null;
+ if (context !== 'userInput' && context !== 'crmClear') {
+ if ($dateField.length) {
+ $dateField.datepicker('setDate', _.includes(val, '-') ? $.datepicker.parseDate('yy-mm-dd', val) : null);
+ }
+ if ($timeField.length) {
+ if (val.length === 8) {
+ time = val;
+ } else if (val.length === 19) {
+ time = val.split(' ')[1];
+ }
+ $timeField.timeEntry('setTime', time);
+ }
+ }
+ $clearLink.css('visibility', val ? 'visible' : 'hidden');
+ }
+ function updateDataField(e, context) {
+ if (context !== 'crmClear') {
+ var val = '';
+ if ($dateField.val()) {
+ val = $.datepicker.formatDate('yy-mm-dd', $dateField.datepicker('getDate'));
+ }
+ if ($timeField.val()) {
+ val += (val ? ' ' : '') + $timeField.timeEntry('getTime').toTimeString().substr(0, 8);
+ }
+ $dataField.val(val).trigger('change', ['userInput']);
+ }
+ }
+ $dataField.hide().addClass('crm-hidden-date').on('change', updateInputFields);
+ updateInputFields();
+ });
+ };
+
CRM.utils.formatSelect2Result = function (row) {
var markup = '<div class="crm-select2-row">';
if (row.image !== undefined) {
}
else if (this.key == 'contact_type' && typeof params.contact_sub_type === 'undefined') {
this.options = _.remove(this.options, function(option) {
- return option.key.indexOf(params.contact_type + '.') === 0;
+ return option.key.indexOf(params.contact_type + '__') === 0;
});
result.push(this);
}
CRM.utils.setOptions($valField, filterSpec.options, false, filter.value);
} else {
$valField.prop('disabled', true);
- CRM.api3(filterSpec.entity || $el.data('api-entity'), 'getoptions', {field: filter.key, sequential: 1})
+ CRM.api3(filterSpec.entity || $el.data('api-entity'), 'getoptions', {field: filter.key, context: 'search', sequential: 1})
.done(function(result) {
var entity = $el.data('api-entity').toLowerCase(),
globalFilterSpec = _.find(CRM.config.entityRef.filters[entity], {key: filter.key}) || {};
});
$el.dialog('option', {width: '100%', height: ($(window).height() - menuHeight), position: {my: "top", at: "top+"+menuHeight, of: window}});
}
+ $el.trigger('dialogresize');
e.preventDefault();
});
}
$('#crm-container #mainTabContainer').addClass('narrowpage');
$('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) {
if (index > 1) {
- if (index % 2 == 0) {
+ if (index % 2 === 0) {
$(this).parent().after('<tr class="narrowadded"></tr>');
}
var item = $(this);
var nitem = $(this);
var parent = $(this).parent();
$(this).parent().prev().append(nitem);
- if (parent.children().size() == 0) {
+ if (parent.children().size() === 0) {
parent.remove();
}
});
$(this).addClass('crm-processed');
$(this).addClass('crm-tooltip-active');
var topDistance = e.pageY - $(window).scrollTop();
- if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) {
+ if (topDistance < 300 || topDistance < $(this).children('.crm-tooltip-wrapper').height()) {
$(this).addClass('crm-tooltip-down');
}
if (!$(this).children('.crm-tooltip-wrapper').length) {
var opts = $.extend({
start: ts('Saving...'),
success: ts('Saved'),
- error: function() {
- CRM.alert(ts('Sorry an error occurred and your information was not saved'), ts('Error'));
+ error: function(data) {
+ var msg = $.isPlainObject(data) && data.error_message;
+ CRM.alert(msg || ts('Sorry an error occurred and your information was not saved'), ts('Error'), 'error');
}
}, options || {});
var $msg = $('<div class="crm-status-box-outer status-start"><div class="crm-status-box-inner"><div class="crm-status-box-msg">' + opts.start + '</div></div></div>')
if (endMsg) {
$msg.removeClass('status-start').addClass('status-' + status).find('.crm-status-box-msg').html(endMsg);
window.setTimeout(function() {
- $msg.fadeOut('slow', function() {$msg.remove()});
+ $msg.fadeOut('slow', function() {
+ $msg.remove();
+ });
}, 2000);
} else {
$msg.remove();
message: ts('Are you sure you want to continue?'),
url: null,
width: 'auto',
- modal: true,
+ height: 'auto',
resizable: false,
dialogClass: 'crm-container crm-confirm',
close: function () {
yes: ts('Continue')
}
};
+ if (options && options.url) {
+ settings.resizable = true;
+ settings.height = '50%';
+ }
$.extend(settings, ($.isFunction(options) ? arguments[1] : options) || {});
+ settings = CRM.utils.adjustDialogDefaults(settings);
if (!settings.buttons && $.isPlainObject(settings.options)) {
$.each(settings.options, function(op, label) {
buttons.push({
};
};
+ CRM.addStrings = function(domain, strings) {
+ var bucket = (domain == 'civicrm' ? 'strings' : 'strings::' + domain);
+ CRM[bucket] = CRM[bucket] || {};
+ _.extend(CRM[bucket], strings);
+ };
+
/**
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference
*/
expires: 0
};
if ($(this).length) {
- if (title == '') {
+ if (title === '') {
var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]');
if (label.length) {
label.addClass('crm-error');
var $label = label.clone();
- if (text == '' && $('.crm-marker', $label).length > 0) {
+ if (text === '' && $('.crm-marker', $label).length > 0) {
text = $('.crm-marker', $label).attr('title');
}
$('.crm-marker', $label).remove();
title = $label.text();
}
}
- $(this).addClass('error');
+ $(this).addClass('crm-error');
}
var msg = CRM.alert(text, title, 'error', $.extend(extra, options));
if ($(this).length) {
var ele = $(this);
setTimeout(function () {
ele.one('change', function () {
- msg && msg.close && msg.close();
+ if (msg && msg.close) msg.close();
ele.removeClass('error');
label.removeClass('crm-error');
});
if (typeof(response.crmMessages) == 'object') {
$.each(response.crmMessages, function(n, msg) {
CRM.alert(msg.text, msg.title, msg.type, msg.options);
- })
+ });
}
if (response.backtrace) {
CRM.console('log', response.backtrace);
// 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();
+ $(this).css({visibility: 'hidden'}).siblings('.crm-form-radio:checked').prop('checked', false).trigger('change', ['crmClear']);
+ $(this).siblings('input:text').val('').trigger('change', ['crmClear']);
return false;
})
.on('change', 'input.crm-form-radio:checked', function() {
/**
* @deprecated
*/
- $.fn.crmAccordions = function () {};
+ $.fn.crmAccordions = function () {
+ CRM.console('warn', 'Warning: $.crmAccordions was called. This function is deprecated and should not be used.');
+ };
/**
* Collapse or expand an accordion
* @param speed
}
}
};
+
+ // Determine if a user has a given permission.
+ // @see CRM_Core_Resources::addPermissions
+ CRM.checkPerm = function(perm) {
+ return CRM.permissions[perm];
+ };
})(jQuery, _);