X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2FCommon.js;h=25e228b594c3d41cda2825aa5d98fef43099e178;hb=05a42c4a7e599684e576c174fd0db4b23b92f76f;hp=9b5f835ed37921d563d4b7e401cd07fe3bc55600;hpb=55d2ecea14d86f17524a4f51909e3cddfcb55192;p=civicrm-core.git diff --git a/js/Common.js b/js/Common.js index 9b5f835ed3..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) { @@ -860,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) { @@ -925,6 +954,13 @@ 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);