X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2FCommon.js;h=25e228b594c3d41cda2825aa5d98fef43099e178;hb=0fc59d7a3a4ca8447e8f228c360021c2e2b1f9dc;hp=ce8546c5708edf2e4cf03397d9332f54a00665ee;hpb=1fd00049e69653227b53343bbc2976a3132f3b97;p=civicrm-core.git diff --git a/js/Common.js b/js/Common.js index ce8546c570..25e228b594 100644 --- a/js/Common.js +++ b/js/Common.js @@ -144,17 +144,11 @@ function showHideByValue(trigger_field_id, trigger_value, target_element_id, tar } } +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) { @@ -578,7 +572,6 @@ if (!CRM.vars) CRM.vars = {}; }; $el.on('select2-open.crmEntity', function() { var $el = $(this); - renderEntityRefFilterValue($el); $('#select2-drop') .off('.crmEntity') .on('click.crmEntity', 'a.crm-add-entity', function(e) { @@ -609,7 +602,7 @@ if (!CRM.vars) CRM.vars = {}; 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'); @@ -747,14 +740,14 @@ if (!CRM.vars) CRM.vars = {}; '' + entityRefFilterValueMarkup(filter, filterSpec) + ''; + '' + entityRefFilterValueMarkup($el, filter, filterSpec) + ''; 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 = '', @@ -768,7 +761,12 @@ if (!CRM.vars) CRM.vars = {}; attrs += ' ' + attr + '="' + val + '"'; }); if (filterSpec.type === 'select') { - markup = ''; + var fieldName = _.last(filter.key.split('.')), + options = [{key: '', value: ts('- select -')}]; + if (filterSpec.options) { + options = options.concat(getEntityRefFilterOptions(fieldName, $el, filterSpec)); + } + markup = '' + CRM.utils.renderOptions(options, filter.value) + ''; } else { markup = ''; } @@ -787,7 +785,7 @@ if (!CRM.vars) CRM.vars = {}; $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); @@ -802,10 +800,9 @@ if (!CRM.vars) CRM.vars = {}; */ 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); @@ -815,19 +812,19 @@ if (!CRM.vars) CRM.vars = {}; // 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) @@ -857,6 +854,41 @@ if (!CRM.vars) CRM.vars = {}; }); }; + // 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) { @@ -876,6 +908,7 @@ if (!CRM.vars) CRM.vars = {}; } }) .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 @@ -921,9 +954,17 @@ if (!CRM.vars) CRM.vars = {}; 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'); @@ -940,6 +981,9 @@ if (!CRM.vars) CRM.vars = {}; $(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'), @@ -958,6 +1002,9 @@ if (!CRM.vars) CRM.vars = {}; 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