1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Controls the preferences for the list of notification types on the
18 * message preference page
20 * @module core_message/preferences_notifications_list_controller
21 * @class preferences_notifications_list_controller
23 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define(['jquery', 'core/ajax', 'core/notification', 'core/custom_interaction_events', 'core_message/notification_preference',
27 'core_message/notification_processor_settings'],
28 function($, Ajax, Notification, CustomEvents, NotificationPreference, NotificationProcessorSettings) {
31 DISABLE_NOTIFICATIONS: '[data-region="disable-notification-container"] [data-disable-notifications]',
32 DISABLE_NOTIFICATIONS_CONTAINER: '[data-region="disable-notification-container"]',
33 PREFERENCE: '[data-state]',
34 PREFERENCE_ROW: '[data-region="preference-row"]',
35 PREFERENCE_INPUT: '[data-state] input',
36 PROCESSOR_SETTING: '[data-processor-setting]',
40 * Constructor for the PreferencesController.
42 * @param {object} element jQuery object root element of the preference
44 var PreferencesController = function(element) {
45 this.root = $(element);
46 this.userId = this.root.attr('data-user-id');
48 this.registerEventListeners();
52 * Check if the preferences are all disabled.
57 PreferencesController.prototype.isDisabled = function() {
58 return this.root.hasClass('disabled');
62 * Disable all of the preferences.
66 PreferencesController.prototype.setDisabled = function() {
67 this.root.addClass('disabled');
68 this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', true);
72 * Enable all of the preferences.
76 PreferencesController.prototype.setEnabled = function() {
77 this.root.removeClass('disabled');
78 this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
82 * Update the disable all notifications user property in the DOM and
83 * send a request to update on the server.
85 * @method toggleDisableAllStatus
88 PreferencesController.prototype.toggleDisableAllStatus = function() {
89 var checkbox = $(SELECTORS.DISABLE_NOTIFICATIONS);
90 var container = $(SELECTORS.DISABLE_NOTIFICATIONS_CONTAINER);
91 var ischecked = checkbox.prop('checked');
93 if (container.hasClass('loading')) {
94 return $.Deferred().resolve();
97 container.addClass('loading');
100 methodname: 'core_user_update_user_preferences',
103 emailstop: ischecked ? 1 : 0,
107 return Ajax.call([request])[0]
116 container.removeClass('loading');
118 .fail(Notification.exception);
122 * Set up all of the event listeners for the PreferencesController.
124 * @method registerEventListeners
126 PreferencesController.prototype.registerEventListeners = function() {
127 var disabledNotificationsElement = $(SELECTORS.DISABLE_NOTIFICATIONS);
129 CustomEvents.define(this.root, [
130 CustomEvents.events.activate,
133 this.root.on('change', function(e) {
134 if (!this.isDisabled()) {
135 var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
136 var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW);
137 var preference = new NotificationPreference(preferenceRow, this.userId);
139 preferenceElement.addClass('loading');
140 preference.save().always(function() {
141 preferenceElement.removeClass('loading');
146 this.root.on(CustomEvents.events.activate, SELECTORS.PROCESSOR_SETTING, function(e) {
147 var element = $(e.target).closest(SELECTORS.PROCESSOR_SETTING);
148 var processorSettings = new NotificationProcessorSettings(element);
149 processorSettings.show();
152 CustomEvents.define(disabledNotificationsElement, [
153 CustomEvents.events.activate
156 disabledNotificationsElement.on(CustomEvents.events.activate, function() {
157 this.toggleDisableAllStatus();
161 return PreferencesController;