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 |
* @module core_group/index
|
|
|
17 |
* @copyright 2022 Matthew Hilton <matthewhilton@catalyst-au.net>
|
|
|
18 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
import GroupPicker from "./grouppicker";
|
|
|
22 |
|
|
|
23 |
const groupPicker = new GroupPicker();
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Initialise page.
|
|
|
27 |
*/
|
|
|
28 |
export const init = () => {
|
|
|
29 |
// Init event listeners.
|
|
|
30 |
groupPicker.getDomElement().addEventListener("change", updateBulkActionButtons);
|
|
|
31 |
|
|
|
32 |
// Call initially to set initial button state.
|
|
|
33 |
updateBulkActionButtons();
|
|
|
34 |
};
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Updates the bulk action buttons depending on specific conditions.
|
|
|
38 |
*/
|
|
|
39 |
export const updateBulkActionButtons = () => {
|
|
|
40 |
const groupsSelected = groupPicker.getSelectedValues();
|
|
|
41 |
const aGroupIsSelected = groupsSelected.length !== 0;
|
|
|
42 |
|
|
|
43 |
// Collate the conditions where each button is enabled/disabled.
|
|
|
44 |
const bulkActionsEnabledStatuses = {
|
|
|
45 |
'enablemessaging': aGroupIsSelected,
|
|
|
46 |
'disablemessaging': aGroupIsSelected
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
// Update the status of each button.
|
|
|
50 |
Object.entries(bulkActionsEnabledStatuses).map(([buttonId, enabled]) => setElementEnabled(buttonId, enabled));
|
|
|
51 |
};
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Adds or removes the given element's disabled attribute.
|
|
|
55 |
* @param {string} domElementId ID of the dom element (without the #)
|
|
|
56 |
* @param {bool} enabled If false, the disable attribute is applied, else it is removed.
|
|
|
57 |
*/
|
|
|
58 |
export const setElementEnabled = (domElementId, enabled) => {
|
|
|
59 |
const element = document.getElementById(domElementId);
|
|
|
60 |
|
|
|
61 |
if (!element) {
|
|
|
62 |
// If there is no element, we do nothing.
|
|
|
63 |
// The element could be purposefully hidden or removed.
|
|
|
64 |
return;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if (!enabled) {
|
|
|
68 |
element.setAttribute('disabled', 'disabled');
|
|
|
69 |
} else {
|
|
|
70 |
element.removeAttribute('disabled');
|
|
|
71 |
}
|
|
|
72 |
};
|