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 |
* Module to handle AJAX interactions with user private files
|
|
|
18 |
*
|
|
|
19 |
* @module core_user/private_files
|
|
|
20 |
* @copyright 2020 Marina Glancy
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
import DynamicForm from 'core_form/dynamicform';
|
|
|
24 |
import ModalForm from 'core_form/modalform';
|
|
|
25 |
import {getString} from 'core/str';
|
|
|
26 |
import {add as addToast} from 'core/toast';
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Initialize private files form as AJAX form
|
|
|
30 |
*
|
|
|
31 |
* @param {String} containerSelector
|
|
|
32 |
* @param {String} formClass
|
|
|
33 |
*/
|
|
|
34 |
export const initDynamicForm = (containerSelector, formClass) => {
|
|
|
35 |
const form = new DynamicForm(document.querySelector(containerSelector), formClass);
|
|
|
36 |
|
|
|
37 |
// When form is saved, refresh it to remove validation errors, if any:
|
|
|
38 |
form.addEventListener(form.events.FORM_SUBMITTED, () => {
|
|
|
39 |
form.load();
|
|
|
40 |
getString('changessaved')
|
|
|
41 |
.then(addToast)
|
|
|
42 |
.catch(null);
|
|
|
43 |
});
|
|
|
44 |
|
|
|
45 |
// Reload the page on cancel.
|
|
|
46 |
form.addEventListener(form.events.CANCEL_BUTTON_PRESSED, () => window.location.reload());
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Initialize private files form as Modal form
|
|
|
51 |
*
|
|
|
52 |
* @param {String} elementSelector
|
|
|
53 |
* @param {String} formClass
|
|
|
54 |
*/
|
|
|
55 |
export const initModal = (elementSelector, formClass) => {
|
|
|
56 |
document.querySelector(elementSelector).addEventListener('click', function(e) {
|
|
|
57 |
e.preventDefault();
|
|
|
58 |
const form = new ModalForm({
|
|
|
59 |
formClass,
|
|
|
60 |
args: {nosubmit: true},
|
|
|
61 |
modalConfig: {title: getString('privatefilesmanage')},
|
|
|
62 |
returnFocus: e.target,
|
|
|
63 |
});
|
|
|
64 |
form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());
|
|
|
65 |
form.show();
|
|
|
66 |
});
|
|
|
67 |
};
|