8b7311377adb2b4c89a2e5fdff409e4f3eb7b360
[moodle.git] / message / amd / src / preferences_notifications_list_controller.js
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/>.
16 /**
17  * Controls the preferences for the list of notification types on the
18  * message preference page
19  *
20  * @module     core_message/preferences_notifications_list_controller
21  * @class      preferences_notifications_list_controller
22  * @package    message
23  * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
26 define(['jquery',
27         'core/ajax',
28         'core/notification',
29         'core/custom_interaction_events',
30         'core_message/notification_preference',
31         'core_message/notification_processor_settings',
32         'core/modal_factory',
33         ],
34         function(
35           $,
36           Ajax,
37           Notification,
38           CustomEvents,
39           NotificationPreference,
40           NotificationProcessorSettings,
41           ModalFactory
42         ) {
44     var SELECTORS = {
45         DISABLE_NOTIFICATIONS: '[data-region="disable-notification-container"] [data-disable-notifications]',
46         DISABLE_NOTIFICATIONS_CONTAINER: '[data-region="disable-notification-container"]',
47         PREFERENCE: '[data-state]',
48         PREFERENCE_ROW: '[data-region="preference-row"]',
49         PREFERENCE_INPUT: '[data-state] input',
50         PROCESSOR_SETTING: '[data-processor-setting]',
51     };
53     /**
54      * Constructor for the PreferencesController.
55      *
56      * @param {object} element jQuery object root element of the preference
57      */
58     var PreferencesController = function(element) {
59         this.root = $(element);
60         this.userId = this.root.attr('data-user-id');
62         this.registerEventListeners();
63     };
65     /**
66      * Check if the preferences are all disabled.
67      *
68      * @method isDisabled
69      * @return {bool}
70      */
71     PreferencesController.prototype.isDisabled = function() {
72         return this.root.hasClass('disabled');
73     };
75     /**
76      * Disable all of the preferences.
77      *
78      * @method setDisabled
79      */
80     PreferencesController.prototype.setDisabled = function() {
81         this.root.addClass('disabled');
82         this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', true);
83     };
85     /**
86      * Enable all of the preferences.
87      *
88      * @method setEnabled
89      */
90     PreferencesController.prototype.setEnabled = function() {
91         this.root.removeClass('disabled');
92         this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
93     };
95     /**
96       * Update the disable all notifications user property in the DOM and
97       * send a request to update on the server.
98       *
99       * @method toggleDisableAllStatus
100       * @return {Promise}
101       */
102     PreferencesController.prototype.toggleDisableAllStatus = function() {
103         var checkbox = $(SELECTORS.DISABLE_NOTIFICATIONS);
104         var container = $(SELECTORS.DISABLE_NOTIFICATIONS_CONTAINER);
105         var ischecked = checkbox.prop('checked');
107         if (container.hasClass('loading')) {
108             return $.Deferred().resolve();
109         }
111         container.addClass('loading');
113         var request = {
114             methodname: 'core_user_update_user_preferences',
115             args: {
116                 userid: this.userId,
117                 emailstop: ischecked ? 1 : 0,
118             }
119         };
121         return Ajax.call([request])[0]
122             .done(function() {
123                 if (ischecked) {
124                     this.setDisabled();
125                 } else {
126                     this.setEnabled();
127                 }
128             }.bind(this))
129             .always(function() {
130                 container.removeClass('loading');
131             })
132             .fail(Notification.exception);
133     };
135     /**
136       * Set up all of the event listeners for the PreferencesController.
137       *
138       * @method registerEventListeners
139       */
140     PreferencesController.prototype.registerEventListeners = function() {
141         var disabledNotificationsElement = $(SELECTORS.DISABLE_NOTIFICATIONS);
143         CustomEvents.define(this.root, [
144             CustomEvents.events.activate,
145         ]);
147         this.root.on('change', function(e) {
148             if (!this.isDisabled()) {
149                 var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
150                 var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW);
151                 var preference = new NotificationPreference(preferenceRow, this.userId);
153                 preferenceElement.addClass('loading');
154                 preference.save().always(function() {
155                     preferenceElement.removeClass('loading');
156                 });
157             }
158         }.bind(this));
160         var eventFormPromise = ModalFactory.create({
161             type: NotificationProcessorSettings.TYPE,
162         });
164         this.root.on(CustomEvents.events.activate, SELECTORS.PROCESSOR_SETTING, function(e) {
165             var element = $(e.target).closest(SELECTORS.PROCESSOR_SETTING);
167             e.preventDefault();
168             eventFormPromise.then(function(modal) {
169                 // Configure modal with element settings.
170                 modal.setUserId($(element).attr('data-user-id'));
171                 modal.setName($(element).attr('data-name'));
172                 modal.setContextId($(element).attr('data-context-id'));
173                 modal.setElement(element);
174                 modal.show();
176                 e.stopImmediatePropagation();
177                 return;
178             }).fail(Notification.exception);
179         });
181         CustomEvents.define(disabledNotificationsElement, [
182             CustomEvents.events.activate
183         ]);
185         disabledNotificationsElement.on(CustomEvents.events.activate, function() {
186             this.toggleDisableAllStatus();
187         }.bind(this));
188     };
190     return PreferencesController;
191 });