}
}
+var submitcount = 0;
/**
- * Function to change button text and disable one it is clicked
+ * Old submit-once function. Will be removed soon.
* @deprecated
- * @param obj object - the button clicked
- * @param formID string - the id of the form being submitted
- * @param string procText - button text after user clicks it
- * @return bool
*/
-var submitcount = 0;
-/* Changes button label on submit, and disables button after submit for newer browsers.
- Puts up alert for older browsers. */
function submitOnce(obj, formId, procText) {
// if named button clicked, change text
if (obj.value != null) {
};
$el.on('select2-open.crmEntity', function() {
var $el = $(this);
- renderEntityRefFilterValue($el);
$('#select2-drop')
.off('.crmEntity')
.on('click.crmEntity', 'a.crm-add-entity', function(e) {
filter.value = $(this).val();
$(this).toggleClass('active', !!filter.value);
$el.data('user-filter', filter);
- if (filter.value) {
+ if (filter.value && $(this).is('select')) {
// Once a filter has been chosen, rerender create links and refocus the search box
$el.select2('close');
$el.select2('open');
'<select class="crm-entityref-filter-key' + (filter.key ? ' active' : '') + '">' +
'<option value="">' + _.escape(ts('Refine search...')) + '</option>' +
CRM.utils.renderOptions(filters, filter.key) +
- '</select>' + entityRefFilterValueMarkup(filter, filterSpec) + '</div>';
+ '</select>' + entityRefFilterValueMarkup($el, filter, filterSpec) + '</div>';
return markup;
}
/**
* Provide markup for entity ref filter value field
*/
- function entityRefFilterValueMarkup(filter, filterSpec) {
+ function entityRefFilterValueMarkup($el, filter, filterSpec) {
var markup = '';
if (filterSpec) {
var attrs = '',
attrs += ' ' + attr + '="' + val + '"';
});
if (filterSpec.type === 'select') {
- markup = '<select' + attrs + '><option value="">' + _.escape(ts('- select -')) + '</option></select>';
+ var fieldName = _.last(filter.key.split('.')),
+ options = [{key: '', value: ts('- select -')}];
+ if (filterSpec.options) {
+ options = options.concat(getEntityRefFilterOptions(fieldName, $el, filterSpec));
+ }
+ markup = '<select' + attrs + '>' + CRM.utils.renderOptions(options, filter.value) + '</select>';
} else {
markup = '<input' + attrs + '/>';
}
$valField = null;
if (filterSpec) {
$('.crm-entityref-filter-value', '#select2-drop').remove();
- $valField = $(entityRefFilterValueMarkup(filter, filterSpec));
+ $valField = $(entityRefFilterValueMarkup($el, filter, filterSpec));
$keyField.after($valField);
if (filterSpec.type === 'select') {
loadEntityRefFilterOptions(filter, filterSpec, $valField, $el);
*/
function loadEntityRefFilterOptions(filter, filterSpec, $valField, $el) {
// Fieldname may be prefixed with joins - strip those out
- var fieldName = _.last(filter.key.split('.')),
- params = $.extend({params: {}}, $el.data('api-params') || {}).params;
+ var fieldName = _.last(filter.key.split('.'));
if (filterSpec.options) {
- setEntityRefFilterOptions($valField, fieldName, params, filterSpec);
+ CRM.utils.setOptions($valField, getEntityRefFilterOptions(fieldName, $el, filterSpec), false, filter.value);
return;
}
$('.crm-entityref-filters select', '#select2-drop').prop('disabled', true);
// Store options globally so we don't have to look them up again
filterSpec.options = result.values;
$('.crm-entityref-filters select', '#select2-drop').prop('disabled', false);
- setEntityRefFilterOptions($valField, fieldName, params, filterSpec);
- $valField.val(filter.value || '');
+ CRM.utils.setOptions($valField, getEntityRefFilterOptions(fieldName, $el, filterSpec), false, filter.value);
});
}
- function setEntityRefFilterOptions($valField, fieldName, params, filterSpec) {
- var values = _.cloneDeep(filterSpec.options);
+ function getEntityRefFilterOptions(fieldName, $el, filterSpec) {
+ var values = _.cloneDeep(filterSpec.options),
+ params = $.extend({params: {}}, $el.data('api-params') || {}).params;
if (fieldName === 'contact_type' && params.contact_type) {
values = _.remove(values, function(option) {
return option.key.indexOf(params.contact_type + '__') === 0;
});
}
- CRM.utils.setOptions($valField, values);
+ return values;
}
//CRM-15598 - Override url validator method to allow relative url's (e.g. /index.htm)
});
};
+ // Submit-once
+ var submitted = [],
+ submitButton;
+ function submitOnceForm(e) {
+ if (e.isDefaultPrevented()) {
+ return;
+ }
+ if (_.contains(submitted, e.target)) {
+ return false;
+ }
+ submitted.push(e.target);
+ // Spin submit button icon
+ if (submitButton && $(submitButton, e.target).length) {
+ // Dialog button
+ if ($(e.target).closest('.ui-dialog .crm-ajax-container')) {
+ var identifier = $(submitButton).attr('name') || $(submitButton).attr('href');
+ if (identifier) {
+ submitButton = $(e.target).closest('.ui-dialog').find('button[data-identifier="' + identifier + '"]')[0] || submitButton;
+ }
+ }
+ var $icon = $(submitButton).siblings('.crm-i').add('.crm-i, .ui-icon', submitButton);
+ $icon.data('origClass', $icon.attr('class')).removeClass().addClass('crm-i crm-submit-icon fa-spinner fa-pulse');
+ }
+ }
+
+ // If form fails validation, restore button icon and reset the submitted array
+ function submitFormInvalid(form) {
+ submitted = [];
+ $('.crm-i.crm-submit-icon').each(function() {
+ if ($(this).data('origClass')) {
+ $(this).removeClass().addClass($(this).data('origClass'));
+ }
+ });
+ }
+
// Initialize widgets
$(document)
.on('crmLoad', function(e) {
}
})
.find('input.select-row:checked').parents('tr').addClass('crm-row-selected');
+ $('.crm-sortable-list', e.target).sortable();
$('table.crm-sortable', e.target).DataTable();
$('table.crm-ajax-table', e.target).each(function() {
var
CRM.wysiwyg.create(this);
}
});
+ // Submit once handlers
+ $('form[data-submit-once]', e.target)
+ .submit(submitOnceForm)
+ .on('invalid-form', submitFormInvalid);
+ $('form[data-submit-once] input[type=submit]', e.target).click(function(e) {
+ submitButton = e.target;
+ });
})
.on('dialogopen', function(e) {
var $el = $(e.target);
+ $('body').addClass('ui-dialog-open');
// Modal dialogs should disable scrollbars
if ($el.dialog('option', 'modal')) {
$el.addClass('modal-dialog');
$(this).button('option', 'icons', {primary: 'fa-expand'});
} else {
var menuHeight = $('#civicrm-menu').outerHeight();
+ if ($('body').hasClass('crm-menubar-below-cms-menu')) {
+ menuHeight += $('#civicrm-menu').offset().top;
+ }
$el.data('origSize', {
position: {my: 'center', at: 'center center+' + (menuHeight / 2), of: window},
width: $el.dialog('option', 'width'),
if ($('.ui-dialog .modal-dialog:visible').not(e.target).length < 1) {
$('body').css({overflow: ''});
}
+ if ($('.ui-dialog-content:visible').not(e.target).length < 1) {
+ $('body').removeClass('ui-dialog-open');
+ }
})
.on('submit', function(e) {
// CRM-14353 - disable changes warn when submitting a form