{"version":3,"file":"submit.min.js","sources":["../src/submit.js"],"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\nimport {formEventTypes} 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(formEventTypes.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(formEventTypes.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"],"names":["cookieListener","cookieListeningButtons","getCookieName","M","cfg","sesskey","clearDownloadCookie","document","cookie","encodeURIComponent","Date","toUTCString","elementId","button","getElementById","addEventListener","formEventTypes","uploadStarted","e","window","console","log","target","disabled","uploadCompleted","form","dataset","doubleSubmitProtection","event","disableAction","defaultPrevented","push","setInterval","split","length","clearInterval","forEach","listenForDownloadCookie","setTimeout","removeEventListener"],"mappings":";;;;;;;;;;;IA6BIA,eAAiB,EAGfC,uBAAyB,GAuCzBC,cAAgB,iBACX,kBAAoBC,EAAEC,IAAIC,SAM/BC,oBAAsB,WACxBC,SAASC,OAASC,mBAAmBP,iBAAmB,qBAAuB,IAAIQ,KAAK,GAAGC,6BAQ3E,SAACC,eACXC,OAASN,SAASO,eAAeF,WAGvCL,SAASQ,iBAAiBC,uBAAeC,eAAe,SAAAC,GACpDC,OAAOC,QAAQC,IAAIH,EAAEI,QACrBT,OAAOU,UAAW,KAItBhB,SAASQ,iBAAiBC,uBAAeQ,iBAAiB,SAAAN,GACtDC,OAAOC,QAAQC,IAAIH,EAAEI,QACrBT,OAAOU,UAAW,KAI6B,QAA/CV,OAAOY,KAAKC,QAAQC,wBAGxBd,OAAOY,KAAKV,iBAAiB,UAAU,SAASa,WAGtCC,cAAgB,WAEdD,MAAME,kBAAoBjB,OAAOU,WAIrCV,OAAOU,UAAW,EAClBjB,sBAvEoB,SAACO,QAC7BZ,uBAAuB8B,KAAKlB,QACvBb,iBACDA,eAAiBgC,aAAY,WAGL,GADNzB,SAASC,OAAOyB,MAAM/B,gBAAkB,KAC5CgC,SAEN5B,sBACA6B,cAAcnC,gBACdA,eAAiB,EAGjBC,uBAAuBmC,SAAQ,SAACvB,QAC5BA,OAAOU,UAAW,QAG3B,MAuDCc,CAAwBxB,UAE5BM,OAAOJ,iBAAiB,eAAgBc,eAIxCS,YAAW,WACPnB,OAAOoB,oBAAoB,eAAgBV,iBAC5C,MACJ"}