1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Functions related to downloading course content.
|
|
|
18 |
*
|
|
|
19 |
* @module core_course/downloadcontent
|
|
|
20 |
* @copyright 2020 Michael Hawkins <michaelh@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import Config from 'core/config';
|
|
|
25 |
import CustomEvents from 'core/custom_interaction_events';
|
|
|
26 |
import SaveCancelModal from 'core/modal_save_cancel';
|
|
|
27 |
import jQuery from 'jquery';
|
|
|
28 |
import Pending from 'core/pending';
|
|
|
29 |
import {enter, space} from 'core/key_codes';
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Set up listener to trigger the download course content modal.
|
|
|
33 |
*
|
|
|
34 |
* @return {void}
|
|
|
35 |
*/
|
|
|
36 |
export const init = () => {
|
|
|
37 |
const pendingPromise = new Pending();
|
|
|
38 |
|
|
|
39 |
// Add event listeners for click and enter/space keys.
|
|
|
40 |
jQuery('[data-downloadcourse]').on('click keydown', (e) => {
|
|
|
41 |
if (e.type === 'click' || e.which === enter || e.which === space) {
|
|
|
42 |
e.preventDefault();
|
|
|
43 |
displayDownloadConfirmation(e.currentTarget);
|
|
|
44 |
}
|
|
|
45 |
});
|
|
|
46 |
|
|
|
47 |
pendingPromise.resolve();
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Display the download course content modal.
|
|
|
52 |
*
|
|
|
53 |
* @method displayDownloadConfirmation
|
|
|
54 |
* @param {Object} downloadModalTrigger The DOM element that triggered the download modal.
|
|
|
55 |
* @return {void}
|
|
|
56 |
*/
|
|
|
57 |
const displayDownloadConfirmation = (downloadModalTrigger) => {
|
|
|
58 |
return SaveCancelModal.create({
|
|
|
59 |
title: downloadModalTrigger.dataset.downloadTitle,
|
|
|
60 |
body: `<p>${downloadModalTrigger.dataset.downloadBody}</p>`,
|
|
|
61 |
buttons: {
|
|
|
62 |
save: downloadModalTrigger.dataset.downloadButtonText
|
|
|
63 |
},
|
|
|
64 |
templateContext: {
|
|
|
65 |
classes: 'downloadcoursecontentmodal'
|
|
|
66 |
}
|
|
|
67 |
})
|
|
|
68 |
.then((modal) => {
|
|
|
69 |
// Display the modal.
|
|
|
70 |
modal.show();
|
|
|
71 |
|
|
|
72 |
const saveButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action="save"]');
|
|
|
73 |
const cancelButton = document.querySelector('.modal .downloadcoursecontentmodal [data-action="cancel"]');
|
|
|
74 |
const modalContainer = document.querySelector('.modal[data-region="modal-container"]');
|
|
|
75 |
|
|
|
76 |
// Create listener to trigger the download when the "Download" button is pressed.
|
|
|
77 |
jQuery(saveButton).on(CustomEvents.events.activate, (e) => downloadContent(e, downloadModalTrigger, modal));
|
|
|
78 |
|
|
|
79 |
// Create listener to destroy the modal when closing modal by cancelling.
|
|
|
80 |
jQuery(cancelButton).on(CustomEvents.events.activate, () => {
|
|
|
81 |
modal.destroy();
|
|
|
82 |
});
|
|
|
83 |
|
|
|
84 |
// Create listener to destroy the modal when closing modal by clicking outside of it.
|
|
|
85 |
if (modalContainer.querySelector('.downloadcoursecontentmodal')) {
|
|
|
86 |
jQuery(modalContainer).on(CustomEvents.events.activate, () => {
|
|
|
87 |
modal.destroy();
|
|
|
88 |
});
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return modal;
|
|
|
92 |
});
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Trigger downloading of course content.
|
|
|
97 |
*
|
|
|
98 |
* @method downloadContent
|
|
|
99 |
* @param {Event} e The event triggering the download.
|
|
|
100 |
* @param {Object} downloadModalTrigger The DOM element that triggered the download modal.
|
|
|
101 |
* @param {Object} modal The modal object.
|
|
|
102 |
* @return {void}
|
|
|
103 |
*/
|
|
|
104 |
const downloadContent = (e, downloadModalTrigger, modal) => {
|
|
|
105 |
e.preventDefault();
|
|
|
106 |
|
|
|
107 |
// Create a form to submit the file download request, so we can avoid sending sesskey over GET.
|
|
|
108 |
const downloadForm = document.createElement('form');
|
|
|
109 |
downloadForm.action = downloadModalTrigger.dataset.downloadLink;
|
|
|
110 |
downloadForm.method = 'POST';
|
|
|
111 |
// Open download in a new tab, so current course view is not disrupted.
|
|
|
112 |
downloadForm.target = '_blank';
|
|
|
113 |
const downloadSesskey = document.createElement('input');
|
|
|
114 |
downloadSesskey.name = 'sesskey';
|
|
|
115 |
downloadSesskey.value = Config.sesskey;
|
|
|
116 |
downloadForm.appendChild(downloadSesskey);
|
|
|
117 |
downloadForm.style.display = 'none';
|
|
|
118 |
|
|
|
119 |
document.body.appendChild(downloadForm);
|
|
|
120 |
downloadForm.submit();
|
|
|
121 |
document.body.removeChild(downloadForm);
|
|
|
122 |
|
|
|
123 |
// Destroy the modal to prevent duplicates if reopened later.
|
|
|
124 |
modal.destroy();
|
|
|
125 |
};
|