| 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 |
* Javascript module for using an existing template
|
|
|
18 |
*
|
|
|
19 |
* @module mod_feedback/usetemplate
|
|
|
20 |
* @copyright 2021 Peter Dias
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import ModalForm from 'core_form/modalform';
|
|
|
25 |
import Notification from 'core/notification';
|
|
|
26 |
import {getString} from 'core/str';
|
|
|
27 |
|
|
|
28 |
const selectors = {
|
|
|
29 |
modaltrigger: '[data-action="usetemplate"]',
|
|
|
30 |
};
|
|
|
31 |
|
| 1441 |
ariadna |
32 |
let initialized = false;
|
|
|
33 |
|
| 1 |
efrain |
34 |
/**
|
|
|
35 |
* Initialize module
|
|
|
36 |
*/
|
|
|
37 |
export const init = () => {
|
|
|
38 |
|
| 1441 |
ariadna |
39 |
if (initialized) {
|
|
|
40 |
// We already added the event listeners (can be called multiple times).
|
|
|
41 |
return;
|
|
|
42 |
}
|
| 1 |
efrain |
43 |
|
| 1441 |
ariadna |
44 |
document.addEventListener('click', event => {
|
|
|
45 |
// Use the template.
|
|
|
46 |
const trigger = event.target.closest(selectors.modaltrigger);
|
|
|
47 |
if (trigger) {
|
|
|
48 |
event.preventDefault();
|
| 1 |
efrain |
49 |
|
| 1441 |
ariadna |
50 |
const modalForm = new ModalForm({
|
|
|
51 |
modalConfig: {
|
|
|
52 |
title: getString('use_this_template', 'mod_feedback'),
|
|
|
53 |
},
|
|
|
54 |
formClass: 'mod_feedback\\form\\use_template_form',
|
|
|
55 |
args: {
|
|
|
56 |
id: trigger.getAttribute('data-dataid'),
|
|
|
57 |
templateid: trigger.getAttribute('data-templateid')
|
|
|
58 |
},
|
|
|
59 |
saveButtonText: getString('save', 'core')
|
|
|
60 |
});
|
| 1 |
efrain |
61 |
|
| 1441 |
ariadna |
62 |
// Show a toast notification when the form is submitted.
|
|
|
63 |
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
|
|
|
64 |
if (event.detail.result) {
|
|
|
65 |
window.location.assign(event.detail.url);
|
|
|
66 |
} else {
|
|
|
67 |
Notification.addNotification({
|
|
|
68 |
type: 'error',
|
|
|
69 |
message: getString('saving_failed', 'mod_feedback')
|
|
|
70 |
});
|
|
|
71 |
}
|
|
|
72 |
});
|
|
|
73 |
|
|
|
74 |
modalForm.show();
|
|
|
75 |
}
|
| 1 |
efrain |
76 |
});
|
| 1441 |
ariadna |
77 |
|
|
|
78 |
initialized = true;
|
| 1 |
efrain |
79 |
};
|