MDL-69559 course: Add course content download UI and execution page
[moodle.git] / course / amd / build / downloadcontent.min.js.map
1 {"version":3,"sources":["../src/downloadcontent.js"],"names":["init","pendingPromise","Pending","document","addEventListener","e","downloadModalTrigger","target","closest","preventDefault","displayDownloadConfirmation","resolve","ModalFactory","create","title","dataset","downloadTitle","type","types","SAVE_CANCEL","body","downloadBody","buttons","save","downloadButtonText","templateContext","classes","then","modal","show","saveButton","querySelector","cancelButton","modalContainer","on","CustomEvents","events","activate","downloadContent","destroy","downloadForm","createElement","action","downloadLink","method","downloadSesskey","name","value","Config","sesskey","appendChild","style","display","submit","removeChild"],"mappings":"mhBAwBA,OACA,OACA,OACA,OACA,O,ylBAOO,GAAMA,CAAAA,CAAI,CAAG,UAAM,CACtB,GAAMC,CAAAA,CAAc,CAAG,GAAIC,UAA3B,CAEAC,QAAQ,CAACC,gBAAT,CAA0B,OAA1B,CAAmC,SAACC,CAAD,CAAO,CACtC,GAAMC,CAAAA,CAAoB,CAAGD,CAAC,CAACE,MAAF,CAASC,OAAT,CAAiB,uBAAjB,CAA7B,CAEA,GAAIF,CAAJ,CAA0B,CACtBD,CAAC,CAACI,cAAF,GACAC,CAA2B,CAACJ,CAAD,CAC9B,CACJ,CAPD,EASAL,CAAc,CAACU,OAAf,EACH,CAbM,C,YAsBDD,CAAAA,CAA2B,CAAG,SAACJ,CAAD,CAA0B,CAC1DM,CAAY,CAACC,MAAb,CAAoB,CAChBC,KAAK,CAAER,CAAoB,CAACS,OAArB,CAA6BC,aADpB,CAEhBC,IAAI,CAAEL,CAAY,CAACM,KAAb,CAAmBC,WAFT,CAGhBC,IAAI,cAAQd,CAAoB,CAACS,OAArB,CAA6BM,YAArC,QAHY,CAIhBC,OAAO,CAAE,CACLC,IAAI,CAAEjB,CAAoB,CAACS,OAArB,CAA6BS,kBAD9B,CAJO,CAOhBC,eAAe,CAAE,CACbC,OAAO,CAAE,4BADI,CAPD,CAApB,EAWCC,IAXD,CAWM,SAAAC,CAAK,CAAI,CAE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Y,CAACU,WAAb,CAAyBL,CAAzB,EACAL,CAAY,CAACW,KAAb,CAAmBC,OAAnB,CAA6B,MAA7B,CAEAjD,QAAQ,CAACiB,IAAT,CAAc8B,WAAd,CAA0BV,CAA1B,EACAA,CAAY,CAACa,MAAb,GACAlD,QAAQ,CAACiB,IAAT,CAAckC,WAAd,CAA0Bd,CAA1B,EAGAZ,CAAK,CAACW,OAAN,EACH,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 * Functions related to downloading course content.\n *\n * @module     core_course/downloadcontent\n * @package    core_course\n * @copyright  2020 Michael Hawkins <michaelh@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Config from 'core/config';\nimport CustomEvents from 'core/custom_interaction_events';\nimport * as ModalFactory from 'core/modal_factory';\nimport jQuery from 'jquery';\nimport Pending from 'core/pending';\n\n/**\n * Set up listener to trigger the download course content modal.\n *\n * @return {void}\n */\nexport const init = () => {\n    const pendingPromise = new Pending();\n\n    document.addEventListener('click', (e) => {\n        const downloadModalTrigger = e.target.closest('[data-downloadcourse]');\n\n        if (downloadModalTrigger) {\n            e.preventDefault();\n            displayDownloadConfirmation(downloadModalTrigger);\n        }\n    });\n\n    pendingPromise.resolve();\n};\n\n/**\n * Display the download course content modal.\n *\n * @method displayDownloadConfirmation\n * @param {Object} downloadModalTrigger The DOM element that triggered the download modal.\n * @return {void}\n */\nconst displayDownloadConfirmation = (downloadModalTrigger) => {\n    ModalFactory.create({\n        title: downloadModalTrigger.dataset.downloadTitle,\n        type: ModalFactory.types.SAVE_CANCEL,\n        body: `<p>${downloadModalTrigger.dataset.downloadBody}</p>`,\n        buttons: {\n            save: downloadModalTrigger.dataset.downloadButtonText\n        },\n        templateContext: {\n            classes: 'downloadcoursecontentmodal'\n        }\n    })\n    .then(modal => {\n        // Display the modal.\n        modal.show();\n\n        const saveButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action=\"save\"]');\n        const cancelButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action=\"cancel\"]');\n        const modalContainer = document.querySelector('.modal[data-region=\"modal-container\"]');\n\n        // Create listener to trigger the download when the \"Download\" button is pressed.\n        jQuery(saveButton).on(CustomEvents.events.activate, (e) => downloadContent(e, downloadModalTrigger, modal));\n\n        // Create listener to destroy the modal when closing modal by cancelling.\n        jQuery(cancelButton).on(CustomEvents.events.activate, () => {\n            modal.destroy();\n        });\n\n        // Create listener to destroy the modal when closing modal by clicking outside of it.\n        if (modalContainer.querySelector('.downloadcoursecontentmodal')) {\n            jQuery(modalContainer).on(CustomEvents.events.activate, () => {\n                modal.destroy();\n            });\n        }\n    });\n};\n\n/**\n * Trigger downloading of course content.\n *\n * @method downloadContent\n * @param {Event} e The event triggering the download.\n * @param {Object} downloadModalTrigger The DOM element that triggered the download modal.\n * @param {Object} modal The modal object.\n * @return {void}\n */\nconst downloadContent = (e, downloadModalTrigger, modal) => {\n    e.preventDefault();\n\n    // Create a form to submit the file download request, so we can avoid sending sesskey over GET.\n    const downloadForm = document.createElement('form');\n    downloadForm.action = downloadModalTrigger.dataset.downloadLink;\n    downloadForm.method = 'POST';\n    // Open download in a new tab, so current course view is not disrupted.\n    downloadForm.target = '_blank';\n    const downloadSesskey = document.createElement('input');\n    downloadSesskey.name = 'sesskey';\n    downloadSesskey.value = Config.sesskey;\n    downloadForm.appendChild(downloadSesskey);\n    downloadForm.style.display = 'none';\n\n    document.body.appendChild(downloadForm);\n    downloadForm.submit();\n    document.body.removeChild(downloadForm);\n\n    // Destroy the modal to prevent duplicates if reopened later.\n    modal.destroy();\n};\n"],"file":"downloadcontent.min.js"}