{"version":3,"sources":["https:\/\/dl-ice.spbstu.ru\/lib\/form\/amd\/src\/submit.js"],"names":["cookieListener","cookieListeningButtons","listenForDownloadCookie","button","push","setInterval","parts","document","cookie","split","getCookieName","length","clearDownloadCookie","clearInterval","forEach","disabled","M","cfg","sesskey","encodeURIComponent","Date","toUTCString","init","elementId","getElementById","addEventListener","types","uploadStarted","e","window","console","log","target","uploadCompleted","form","dataset","doubleSubmitProtection","event","disableAction","defaultPrevented","setTimeout","removeEventListener"],"mappings":"uJA6BIA,CAAAA,CAAc,CAAG,C,CAGfC,CAAsB,CAAG,E,CAazBC,CAAuB,CAAG,SAACC,CAAD,CAAY,CACxCF,CAAsB,CAACG,IAAvB,CAA4BD,CAA5B,EACA,GAAI,CAACH,CAAL,CAAqB,CACjBA,CAAc,CAAGK,WAAW,CAAC,UAAM,CAE\/B,GAAMC,CAAAA,CAAK,CAAGC,QAAQ,CAACC,MAAT,CAAgBC,KAAhB,CAAsBC,CAAa,GAAK,GAAxC,CAAd,CACA,GAAoB,CAAhB,EAAAJ,CAAK,CAACK,MAAV,CAAuB,CAEnBC,CAAmB,GACnBC,aAAa,CAACb,CAAD,CAAb,CACAA,CAAc,CAAG,CAAjB,CAGAC,CAAsB,CAACa,OAAvB,CAA+B,SAACX,CAAD,CAAY,CACvCA,CAAM,CAACY,QAAP,GACH,CAFD,CAGH,CACJ,CAd2B,CAczB,GAdyB,CAe\/B,CACJ,C,CAOKL,CAAa,CAAG,UAAM,CACxB,MAAO,kBAAoBM,CAAC,CAACC,GAAF,CAAMC,OACpC,C,CAKKN,CAAmB,CAAG,UAAM,CAC9BL,QAAQ,CAACC,MAAT,CAAkBW,kBAAkB,CAACT,CAAa,EAAd,CAAlB,CAAsC,oBAAtC,CAA6D,GAAIU,CAAAA,IAAJ,CAAS,CAAT,EAAYC,WAAZ,EAClF,C,QAOmB,QAAPC,CAAAA,IAAO,CAACC,CAAD,CAAe,CAC\/B,GAAMpB,CAAAA,CAAM,CAAGI,QAAQ,CAACiB,cAAT,CAAwBD,CAAxB,CAAf,CAGAhB,QAAQ,CAACkB,gBAAT,CAA0BC,QAAMC,aAAhC,CAA+C,SAAAC,CAAC,CAAI,CAChDC,MAAM,CAACC,OAAP,CAAeC,GAAf,CAAmBH,CAAC,CAACI,MAArB,EACA7B,CAAM,CAACY,QAAP,GACH,CAHD,EAMAR,QAAQ,CAACkB,gBAAT,CAA0BC,QAAMO,eAAhC,CAAiD,SAAAL,CAAC,CAAI,CAClDC,MAAM,CAACC,OAAP,CAAeC,GAAf,CAAmBH,CAAC,CAACI,MAArB,EACA7B,CAAM,CAACY,QAAP,GACH,CAHD,EAMA,GAAmD,KAA\/C,GAAAZ,CAAM,CAAC+B,IAAP,CAAYC,OAAZ,CAAoBC,sBAAxB,CAA0D,CACtD,MACH,CACDjC,CAAM,CAAC+B,IAAP,CAAYT,gBAAZ,CAA6B,QAA7B,CAAuC,SAASY,CAAT,CAAgB,CAGnD,GAAMC,CAAAA,CAAa,CAAG,UAAW,CAE7B,GAAID,CAAK,CAACE,gBAAN,EAA0BpC,CAAM,CAACY,QAArC,CAA+C,CAC3C,MACH,CAEDZ,CAAM,CAACY,QAAP,IACAH,CAAmB,GACnBV,CAAuB,CAACC,CAAD,CAC1B,CATD,CAUA0B,MAAM,CAACJ,gBAAP,CAAwB,cAAxB,CAAwCa,CAAxC,EAIAE,UAAU,CAAC,UAAW,CAClBX,MAAM,CAACY,mBAAP,CAA2B,cAA3B,CAA2CH,CAA3C,CACH,CAFS,CAEP,CAFO,CAGb,CApBD,IAqBH,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\nimport {types} from 'core_form\/events';\n\n\/** @type {number} ID for setInterval used when polling for download cookie *\/\nlet cookieListener = 0;\n\n\/** @type {Array} Array of buttons that need re-enabling if we get a download cookie *\/\nconst cookieListeningButtons = [];\n\n\/**\n * Listens in case a download cookie is provided.\n *\n * This function is used to detect file downloads. If there is a file download then we get a\n * beforeunload event, but the page is never unloaded and when the file download completes we\n * should re-enable the buttons. We detect this by watching for a specific cookie.\n *\n * PHP function \\core_form\\util::form_download_complete() can be used to send this cookie.\n *\n * @param {HTMLElement} button Button to re-enable\n *\/\nconst listenForDownloadCookie = (button) => {\n cookieListeningButtons.push(button);\n if (!cookieListener) {\n cookieListener = setInterval(() => {\n \/\/ Look for cookie.\n const parts = document.cookie.split(getCookieName() + '=');\n if (parts.length == 2) {\n \/\/ We found the cookie, so the file is ready. Expire the cookie and cancel polling.\n clearDownloadCookie();\n clearInterval(cookieListener);\n cookieListener = 0;\n\n \/\/ Re-enable all the buttons.\n cookieListeningButtons.forEach((button) => {\n button.disabled = false;\n });\n }\n }, 500);\n }\n};\n\n\/**\n * Gets a unique name for the download cookie.\n *\n * @returns {string} Cookie name\n *\/\nconst getCookieName = () => {\n return 'moodledownload_' + M.cfg.sesskey;\n};\n\n\/**\n * Clears the download cookie if there is one.\n *\/\nconst clearDownloadCookie = () => {\n document.cookie = encodeURIComponent(getCookieName()) + '=deleted; expires=' + new Date(0).toUTCString();\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\n \/\/ Add event listener for file upload start.\n document.addEventListener(types.uploadStarted, e => {\n window.console.log(e.target); \/\/ This will be the element\/section where the file is uploaded to.\n button.disabled = true;\n });\n\n \/\/ Add event listener for file upload complete.\n document.addEventListener(types.uploadCompleted, e => {\n window.console.log(e.target); \/\/ This will be the element\/section where the file is uploaded to.\n button.disabled = false;\n });\n\n \/\/ If the form has double submit protection disabled, do nothing.\n if (button.form.dataset.doubleSubmitProtection === 'off') {\n return;\n }\n button.form.addEventListener('submit', function(event) {\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 \/\/ If the submit was cancelled, or the button is already disabled, don't do anything.\n if (event.defaultPrevented || button.disabled) {\n return;\n }\n\n button.disabled = true;\n clearDownloadCookie();\n listenForDownloadCookie(button);\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"}