Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"downloadcontent.min.js","sources":["../src/downloadcontent.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 * Functions related to downloading course content.\n *\n * @module     core_course/downloadcontent\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 SaveCancelModal from 'core/modal_save_cancel';\nimport jQuery from 'jquery';\nimport Pending from 'core/pending';\nimport {enter, space} from 'core/key_codes';\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    // Add event listeners for click and enter/space keys.\n    jQuery('[data-downloadcourse]').on('click keydown', (e) => {\n        if (e.type === 'click' || e.which === enter || e.which === space) {\n            e.preventDefault();\n            displayDownloadConfirmation(e.currentTarget);\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    return SaveCancelModal.create({\n        title: downloadModalTrigger.dataset.downloadTitle,\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        return modal;\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"],"names":["pendingPromise","Pending","on","e","type","which","enter","space","preventDefault","displayDownloadConfirmation","currentTarget","resolve","downloadModalTrigger","SaveCancelModal","create","title","dataset","downloadTitle","body","downloadBody","buttons","save","downloadButtonText","templateContext","classes","then","modal","show","saveButton","document","querySelector","cancelButton","modalContainer","CustomEvents","events","activate","downloadContent","destroy","downloadForm","createElement","action","downloadLink","method","target","downloadSesskey","name","value","Config","sesskey","appendChild","style","display","submit","removeChild"],"mappings":";;;;;;;sWAmCoB,WACVA,eAAiB,IAAIC,qCAGpB,yBAAyBC,GAAG,iBAAkBC,IAClC,UAAXA,EAAEC,MAAoBD,EAAEE,QAAUC,kBAASH,EAAEE,QAAUE,mBACvDJ,EAAEK,iBACFC,4BAA4BN,EAAEO,mBAItCV,eAAeW,iBAUbF,4BAA+BG,sBAC1BC,2BAAgBC,OAAO,CAC1BC,MAAOH,qBAAqBI,QAAQC,cACpCC,kBAAYN,qBAAqBI,QAAQG,qBACzCC,QAAS,CACLC,KAAMT,qBAAqBI,QAAQM,oBAEvCC,gBAAiB,CACbC,QAAS,gCAGhBC,MAAMC,QAEHA,MAAMC,aAEAC,WAAaC,SAASC,cAAc,2DACpCC,aAAeF,SAASC,cAAc,6DACtCE,eAAiBH,SAASC,cAAc,mEAGvCF,YAAY1B,GAAG+B,mCAAaC,OAAOC,UAAWhC,GAAMiC,gBAAgBjC,EAAGS,qBAAsBc,6BAG7FK,cAAc7B,GAAG+B,mCAAaC,OAAOC,UAAU,KAClDT,MAAMW,aAINL,eAAeF,cAAc,oDACtBE,gBAAgB9B,GAAG+B,mCAAaC,OAAOC,UAAU,KACpDT,MAAMW,aAIPX,SAaTU,gBAAkB,CAACjC,EAAGS,qBAAsBc,SAC9CvB,EAAEK,uBAGI8B,aAAeT,SAASU,cAAc,QAC5CD,aAAaE,OAAS5B,qBAAqBI,QAAQyB,aACnDH,aAAaI,OAAS,OAEtBJ,aAAaK,OAAS,eAChBC,gBAAkBf,SAASU,cAAc,SAC/CK,gBAAgBC,KAAO,UACvBD,gBAAgBE,MAAQC,gBAAOC,QAC/BV,aAAaW,YAAYL,iBACzBN,aAAaY,MAAMC,QAAU,OAE7BtB,SAASX,KAAK+B,YAAYX,cAC1BA,aAAac,SACbvB,SAASX,KAAKmC,YAAYf,cAG1BZ,MAAMW"}