MDL-69559 course: Add course content download UI and execution page

This commit is contained in:
Michael Hawkins 2020-09-03 13:55:30 +08:00
parent cdf91cc236
commit c053b93f0c
8 changed files with 308 additions and 1 deletions

View file

@ -0,0 +1,2 @@
function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("core_course/downloadcontent",["exports","core/config","core/custom_interaction_events","core/modal_factory","jquery","core/pending"],function(a,b,c,d,e,f){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=i(b);c=i(c);d=h(d);e=i(e);f=i(f);function g(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;g=function(){return a};return a}function h(a){if(a&&a.__esModule){return a}if(null===a||"object"!==_typeof(a)&&"function"!=typeof a){return{default:a}}var b=g();if(b&&b.has(a)){return b.get(a)}var c={},d=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var e in a){if(Object.prototype.hasOwnProperty.call(a,e)){var f=d?Object.getOwnPropertyDescriptor(a,e):null;if(f&&(f.get||f.set)){Object.defineProperty(c,e,f)}else{c[e]=a[e]}}}c.default=a;if(b){b.set(a,c)}return c}function i(a){return a&&a.__esModule?a:{default:a}}var j=function(){var a=new f.default;document.addEventListener("click",function(a){var b=a.target.closest("[data-downloadcourse]");if(b){a.preventDefault();k(b)}});a.resolve()};a.init=j;var k=function(a){d.create({title:a.dataset.downloadTitle,type:d.types.SAVE_CANCEL,body:"<p>".concat(a.dataset.downloadBody,"</p>"),buttons:{save:a.dataset.downloadButtonText},templateContext:{classes:"downloadcoursecontentmodal"}}).then(function(b){b.show();var d=document.querySelector(".modal .downloadcoursecontentmodal [data-action=\"save\"]"),f=document.querySelector(".modal .downloadcoursecontentmodal [data-action=\"cancel\"]"),g=document.querySelector(".modal[data-region=\"modal-container\"]");(0,e.default)(d).on(c.default.events.activate,function(c){return l(c,a,b)});(0,e.default)(f).on(c.default.events.activate,function(){b.destroy()});if(g.querySelector(".downloadcoursecontentmodal")){(0,e.default)(g).on(c.default.events.activate,function(){b.destroy()})}})},l=function(a,c,d){a.preventDefault();var e=document.createElement("form");e.action=c.dataset.downloadLink;e.method="POST";e.target="_blank";var f=document.createElement("input");f.name="sesskey";f.value=b.default.sesskey;e.appendChild(f);e.style.display="none";document.body.appendChild(e);e.submit();document.body.removeChild(e);d.destroy()}});
//# sourceMappingURL=downloadcontent.min.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,125 @@
// 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 <http://www.gnu.org/licenses/>.
/**
* Functions related to downloading course content.
*
* @module core_course/downloadcontent
* @package core_course
* @copyright 2020 Michael Hawkins <michaelh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Config from 'core/config';
import CustomEvents from 'core/custom_interaction_events';
import * as ModalFactory from 'core/modal_factory';
import jQuery from 'jquery';
import Pending from 'core/pending';
/**
* Set up listener to trigger the download course content modal.
*
* @return {void}
*/
export const init = () => {
const pendingPromise = new Pending();
document.addEventListener('click', (e) => {
const downloadModalTrigger = e.target.closest('[data-downloadcourse]');
if (downloadModalTrigger) {
e.preventDefault();
displayDownloadConfirmation(downloadModalTrigger);
}
});
pendingPromise.resolve();
};
/**
* Display the download course content modal.
*
* @method displayDownloadConfirmation
* @param {Object} downloadModalTrigger The DOM element that triggered the download modal.
* @return {void}
*/
const displayDownloadConfirmation = (downloadModalTrigger) => {
ModalFactory.create({
title: downloadModalTrigger.dataset.downloadTitle,
type: ModalFactory.types.SAVE_CANCEL,
body: `<p>${downloadModalTrigger.dataset.downloadBody}</p>`,
buttons: {
save: downloadModalTrigger.dataset.downloadButtonText
},
templateContext: {
classes: 'downloadcoursecontentmodal'
}
})
.then(modal => {
// Display the modal.
modal.show();
const saveButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action="save"]');
const cancelButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action="cancel"]');
const modalContainer = document.querySelector('.modal[data-region="modal-container"]');
// Create listener to trigger the download when the "Download" button is pressed.
jQuery(saveButton).on(CustomEvents.events.activate, (e) => downloadContent(e, downloadModalTrigger, modal));
// Create listener to destroy the modal when closing modal by cancelling.
jQuery(cancelButton).on(CustomEvents.events.activate, () => {
modal.destroy();
});
// Create listener to destroy the modal when closing modal by clicking outside of it.
if (modalContainer.querySelector('.downloadcoursecontentmodal')) {
jQuery(modalContainer).on(CustomEvents.events.activate, () => {
modal.destroy();
});
}
});
};
/**
* Trigger downloading of course content.
*
* @method downloadContent
* @param {Event} e The event triggering the download.
* @param {Object} downloadModalTrigger The DOM element that triggered the download modal.
* @param {Object} modal The modal object.
* @return {void}
*/
const downloadContent = (e, downloadModalTrigger, modal) => {
e.preventDefault();
// Create a form to submit the file download request, so we can avoid sending sesskey over GET.
const downloadForm = document.createElement('form');
downloadForm.action = downloadModalTrigger.dataset.downloadLink;
downloadForm.method = 'POST';
// Open download in a new tab, so current course view is not disrupted.
downloadForm.target = '_blank';
const downloadSesskey = document.createElement('input');
downloadSesskey.name = 'sesskey';
downloadSesskey.value = Config.sesskey;
downloadForm.appendChild(downloadSesskey);
downloadForm.style.display = 'none';
document.body.appendChild(downloadForm);
downloadForm.submit();
document.body.removeChild(downloadForm);
// Destroy the modal to prevent duplicates if reopened later.
modal.destroy();
};