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 |
import PluginManagementTable from './plugin_management_table';
|
|
|
17 |
import {refreshTableContent} from 'core_table/dynamic';
|
|
|
18 |
import {call as fetchMany} from 'core/ajax';
|
|
|
19 |
import Pending from 'core/pending';
|
|
|
20 |
import {fetchNotifications} from 'core/notification';
|
|
|
21 |
|
|
|
22 |
export default class extends PluginManagementTable {
|
|
|
23 |
constructor() {
|
|
|
24 |
super();
|
|
|
25 |
this.addClickHandler(this.handleBlockProtectToggle);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Set the block protection state.
|
|
|
30 |
*
|
|
|
31 |
* @param {string} plugin
|
|
|
32 |
* @param {number} state
|
|
|
33 |
* @returns {Promise}
|
|
|
34 |
*/
|
|
|
35 |
setBlockProtectState(plugin, state) {
|
|
|
36 |
return fetchMany([{
|
|
|
37 |
methodname: 'core_admin_set_block_protection',
|
|
|
38 |
args: {
|
|
|
39 |
plugin,
|
|
|
40 |
state,
|
|
|
41 |
},
|
|
|
42 |
}])[0];
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Handle toggling of block protection.
|
|
|
47 |
*
|
|
|
48 |
* @param {HTMLElement} tableRoot
|
|
|
49 |
* @param {Event} e
|
|
|
50 |
*/
|
|
|
51 |
async handleBlockProtectToggle(tableRoot, e) {
|
|
|
52 |
const stateToggle = e.target.closest('[data-action="toggleprotectstate"]');
|
|
|
53 |
if (stateToggle) {
|
|
|
54 |
e.preventDefault();
|
|
|
55 |
const pendingPromise = new Pending('core_table/dynamic:processAction');
|
|
|
56 |
|
|
|
57 |
await this.setBlockProtectState(
|
|
|
58 |
stateToggle.dataset.plugin,
|
|
|
59 |
stateToggle.dataset.targetState === '1' ? 1 : 0
|
|
|
60 |
);
|
|
|
61 |
|
|
|
62 |
const [updatedRoot] = await Promise.all([
|
|
|
63 |
refreshTableContent(tableRoot),
|
|
|
64 |
fetchNotifications(),
|
|
|
65 |
]);
|
|
|
66 |
|
|
|
67 |
// Refocus on the link that as pressed in the first place.
|
|
|
68 |
updatedRoot.querySelector(`[data-action="toggleprotectstate"][data-plugin="${stateToggle.dataset.plugin}"]`).focus();
|
|
|
69 |
pendingPromise.resolve();
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|