diff --git a/lib/form/amd/build/submit.min.js b/lib/form/amd/build/submit.min.js new file mode 100644 index 00000000000..46f722e574d --- /dev/null +++ b/lib/form/amd/build/submit.min.js @@ -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 diff --git a/lib/form/amd/build/submit.min.js.map b/lib/form/amd/build/submit.min.js.map new file mode 100644 index 00000000000..afc8bae2508 --- /dev/null +++ b/lib/form/amd/build/submit.min.js.map @@ -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 .\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"} \ No newline at end of file diff --git a/lib/form/amd/src/submit.js b/lib/form/amd/src/submit.js new file mode 100644 index 00000000000..1e4e7ddcbe4 --- /dev/null +++ b/lib/form/amd/src/submit.js @@ -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 . + +/** + * 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); +}; diff --git a/lib/form/templates/element-submit-inline.mustache b/lib/form/templates/element-submit-inline.mustache index 8c2c04742d8..51b97002634 100644 --- a/lib/form/templates/element-submit-inline.mustache +++ b/lib/form/templates/element-submit-inline.mustache @@ -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 }} diff --git a/lib/form/templates/element-submit.mustache b/lib/form/templates/element-submit.mustache index 058e414a785..330c8beb9d5 100644 --- a/lib/form/templates/element-submit.mustache +++ b/lib/form/templates/element-submit.mustache @@ -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 }}