| 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 |
* Controls the default settings for the list of notification types on the
|
|
|
18 |
* notifications admin page
|
|
|
19 |
*
|
|
|
20 |
* @module core_message/default_notification_preferences
|
|
|
21 |
* @class default_notification_preferences
|
|
|
22 |
* @copyright 2021 Moodle
|
|
|
23 |
* @author Pau Ferrer Ocaña <pau@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
| 1441 |
ariadna |
26 |
import * as Ajax from 'core/ajax';
|
|
|
27 |
import Notification from 'core/notification';
|
|
|
28 |
import {add as addToast} from 'core/toast';
|
| 1 |
efrain |
29 |
|
|
|
30 |
const selectors = {
|
|
|
31 |
provider: '.defaultmessageoutputs .provider_enabled',
|
|
|
32 |
lockSetting: '.locked_message_setting',
|
|
|
33 |
enabledSetting: '.enabled_message_setting',
|
|
|
34 |
allSettings: '.locked_message_setting, .enabled_message_setting'
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Register event listeners for the default_notification_preferences page.
|
|
|
39 |
*/
|
|
|
40 |
const registerEventListeners = () => {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Update the dimmed status of the "enabled" toggle on the notification setting.
|
|
|
44 |
*
|
|
|
45 |
* @param {HTMLElement} lockedElement Element that receives the event.
|
|
|
46 |
*/
|
|
|
47 |
const toggleLockSetting = (lockedElement) => {
|
|
|
48 |
const isEnabled = lockedElement.checked || false;
|
|
|
49 |
const enabledId = lockedElement.id.replace('_locked[', '_enabled[');
|
|
|
50 |
|
| 1441 |
ariadna |
51 |
const enabledElement = document.getElementById(enabledId).closest('div.form-check');
|
| 1 |
efrain |
52 |
enabledElement.classList.toggle('dimmed_text', isEnabled);
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Enable/Disable all settings of the provider.
|
|
|
57 |
*
|
|
|
58 |
* @param {HTMLElement} providerEnabledElement Element that receives the event.
|
|
|
59 |
*/
|
|
|
60 |
const toggleEnableProviderSettings = (providerEnabledElement) => {
|
|
|
61 |
const isEnabled = providerEnabledElement.checked || false;
|
|
|
62 |
const parentRow = providerEnabledElement.closest('tr');
|
|
|
63 |
|
|
|
64 |
const elements = parentRow.querySelectorAll(selectors.allSettings);
|
|
|
65 |
elements.forEach((element) => {
|
|
|
66 |
element.toggleAttribute('disabled', !isEnabled);
|
|
|
67 |
});
|
|
|
68 |
};
|
|
|
69 |
|
| 1441 |
ariadna |
70 |
/**
|
|
|
71 |
* AJAX call to update the default notification element status on the server.
|
|
|
72 |
*
|
|
|
73 |
* @param {Boolean} isEnabled
|
|
|
74 |
* @param {string} preference
|
|
|
75 |
*/
|
|
|
76 |
const setDefaultNotification = (isEnabled, preference) => {
|
|
|
77 |
// AJAX call to update the provider's enable status on the server.
|
|
|
78 |
Ajax.call([{
|
|
|
79 |
methodname: 'core_message_set_default_notification',
|
|
|
80 |
args: {
|
|
|
81 |
preference: preference,
|
|
|
82 |
state: isEnabled ? 1 : 0
|
|
|
83 |
}
|
|
|
84 |
}])[0]
|
|
|
85 |
.then((data) => addToast(data.successmessage))
|
|
|
86 |
.catch(Notification.exception);
|
|
|
87 |
};
|
|
|
88 |
|
| 1 |
efrain |
89 |
const container = document.querySelector('.preferences-container');
|
|
|
90 |
|
|
|
91 |
container.querySelectorAll(selectors.provider).forEach((providerEnabledElement) => {
|
|
|
92 |
// Set the initial statuses.
|
|
|
93 |
if (!providerEnabledElement.checked) {
|
|
|
94 |
toggleEnableProviderSettings(providerEnabledElement);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
providerEnabledElement.addEventListener('change', (e) => {
|
|
|
98 |
toggleEnableProviderSettings(e.target);
|
| 1441 |
ariadna |
99 |
|
|
|
100 |
setDefaultNotification(
|
|
|
101 |
e.target.checked,
|
|
|
102 |
providerEnabledElement.parentElement.parentElement.dataset.preference);
|
| 1 |
efrain |
103 |
});
|
|
|
104 |
});
|
|
|
105 |
|
|
|
106 |
container.querySelectorAll(selectors.lockSetting).forEach((lockedElement) => {
|
|
|
107 |
// Set the initial statuses.
|
|
|
108 |
if (lockedElement.checked) {
|
|
|
109 |
toggleLockSetting(lockedElement);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
lockedElement.addEventListener('change', (e) => {
|
|
|
113 |
toggleLockSetting(e.target);
|
| 1441 |
ariadna |
114 |
|
|
|
115 |
setDefaultNotification(
|
|
|
116 |
e.target.checked,
|
|
|
117 |
lockedElement.parentElement.parentElement.dataset.preference);
|
| 1 |
efrain |
118 |
});
|
|
|
119 |
});
|
| 1441 |
ariadna |
120 |
|
|
|
121 |
container.querySelectorAll(selectors.enabledSetting).forEach((enabledElement) => {
|
|
|
122 |
enabledElement.addEventListener('change', (e) => {
|
|
|
123 |
setDefaultNotification(
|
|
|
124 |
e.target.checked,
|
|
|
125 |
enabledElement.parentElement.parentElement.dataset.preference);
|
|
|
126 |
});
|
|
|
127 |
});
|
| 1 |
efrain |
128 |
};
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Initialize the page.
|
|
|
132 |
*/
|
|
|
133 |
const init = () => {
|
|
|
134 |
registerEventListeners();
|
|
|
135 |
};
|
|
|
136 |
|
|
|
137 |
export default {
|
|
|
138 |
init: init,
|
|
|
139 |
};
|