MDL-38555 Forms: Prevent multiple clicking of submit button

This commit is contained in:
sam marshall 2019-08-22 11:21:34 +01:00
parent 310d6901f9
commit 109c77d37d
5 changed files with 65 additions and 0 deletions

2
lib/form/amd/build/submit.min.js vendored Normal file
View File

@ -0,0 +1,2 @@
define ("core_form/submit",["exports"],function(a){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;a.init=function init(a){var b=document.getElementById(a);b.form.addEventListener("submit",function(){var a=function(){b.disabled=!0};window.addEventListener("beforeunload",a);setTimeout(function(){window.removeEventListener("beforeunload",a)},0)},!1)}});
//# sourceMappingURL=submit.min.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../src/submit.js"],"names":["init","elementId","button","document","getElementById","form","addEventListener","disableAction","disabled","window","setTimeout","removeEventListener"],"mappings":"sIA+BoB,QAAPA,CAAAA,IAAO,CAACC,CAAD,CAAe,CAC/B,GAAMC,CAAAA,CAAM,CAAGC,QAAQ,CAACC,cAAT,CAAwBH,CAAxB,CAAf,CACAC,CAAM,CAACG,IAAP,CAAYC,gBAAZ,CAA6B,QAA7B,CAAuC,UAAW,CAG9C,GAAMC,CAAAA,CAAa,CAAG,UAAW,CAC7BL,CAAM,CAACM,QAAP,GACH,CAFD,CAGAC,MAAM,CAACH,gBAAP,CAAwB,cAAxB,CAAwCC,CAAxC,EAIAG,UAAU,CAAC,UAAW,CAClBD,MAAM,CAACE,mBAAP,CAA2B,cAA3B,CAA2CJ,CAA3C,CACH,CAFS,CAEP,CAFO,CAGb,CAbD,IAcH,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Submit button JavaScript. All submit buttons will be automatically disabled once the form is\n * submitted, unless that submission results in an error/cancelling the submit.\n *\n * @module core_form/submit\n * @package core_form\n * @copyright 2019 The Open University\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.8\n */\n\n/**\n * Initialises submit buttons.\n *\n * @param {String} elementId Form element\n */\nexport const init = (elementId) => {\n const button = document.getElementById(elementId);\n button.form.addEventListener('submit', function() {\n // Only disable it if the browser is really going to another page as a result of the\n // submit.\n const disableAction = function() {\n button.disabled = true;\n };\n window.addEventListener('beforeunload', disableAction);\n // If there is no beforeunload event as a result of this form submit, then the form\n // submit must have been cancelled, so don't disable the button if the page is\n // unloaded later.\n setTimeout(function() {\n window.removeEventListener('beforeunload', disableAction);\n }, 0);\n }, false);\n};\n"],"file":"submit.min.js"}

View File

@ -0,0 +1,48 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Submit button JavaScript. All submit buttons will be automatically disabled once the form is
* submitted, unless that submission results in an error/cancelling the submit.
*
* @module core_form/submit
* @package core_form
* @copyright 2019 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.8
*/
/**
* Initialises submit buttons.
*
* @param {String} elementId Form element
*/
export const init = (elementId) => {
const button = document.getElementById(elementId);
button.form.addEventListener('submit', function() {
// Only disable it if the browser is really going to another page as a result of the
// submit.
const disableAction = function() {
button.disabled = true;
};
window.addEventListener('beforeunload', disableAction);
// If there is no beforeunload event as a result of this form submit, then the form
// submit must have been cancelled, so don't disable the button if the page is
// unloaded later.
setTimeout(function() {
window.removeEventListener('beforeunload', disableAction);
}, 0);
}, false);
};

View File

@ -16,3 +16,10 @@
{{/element.frozen}}
{{/element}}
{{/ core_form/element-template-inline }}
{{# js }}
{{^element.frozen}}
require(['core_form/submit'], function(Submit) {
Submit.init("{{ element.id }}");
});
{{/element.frozen}}
{{/ js }}

View File

@ -20,3 +20,10 @@
{{/element.frozen}}
{{/element}}
{{/ core_form/element-template }}
{{# js }}
{{^element.frozen}}
require(['core_form/submit'], function(Submit) {
Submit.init("{{ element.id }}");
});
{{/element.frozen}}
{{/ js }}