MDL-54682 message: fix eslint issues
[moodle.git] / message / amd / src / preferences_notifications_list_controller.js
blobf55c26253b15c71f411aa712382b904dd88047af
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.
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', '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) {
30     var SELECTORS = {
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]',
37     };
39     /**
40      * Constructor for the PreferencesController.
41      *
42      * @param {object} element jQuery object root element of the preference
43      */
44     var PreferencesController = function(element) {
45         this.root = $(element);
46         this.userId = this.root.attr('data-user-id');
48         this.registerEventListeners();
49     };
51     /**
52      * Check if the preferences are all disabled.
53      *
54      * @method isDisabled
55      * @return {bool}
56      */
57     PreferencesController.prototype.isDisabled = function() {
58         return this.root.hasClass('disabled');
59     };
61     /**
62      * Disable all of the preferences.
63      *
64      * @method setDisabled
65      */
66     PreferencesController.prototype.setDisabled = function() {
67         this.root.addClass('disabled');
68         this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', true);
69     };
71     /**
72      * Enable all of the preferences.
73      *
74      * @method setEnabled
75      */
76     PreferencesController.prototype.setEnabled = function() {
77         this.root.removeClass('disabled');
78         this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
79     };
81     /**
82       * Update the disable all notifications user property in the DOM and
83       * send a request to update on the server.
84       *
85       * @method toggleDisableAllStatus
86       * @return {Promise}
87       */
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();
95         }
97         container.addClass('loading');
99         var request = {
100             methodname: 'core_user_update_user_preferences',
101             args: {
102                 userid: this.userId,
103                 emailstop: ischecked ? 1 : 0,
104             }
105         };
107         return Ajax.call([request])[0]
108             .done(function() {
109                 if (ischecked) {
110                     this.setDisabled();
111                 } else {
112                     this.setEnabled();
113                 }
114             }.bind(this))
115             .always(function() {
116                 container.removeClass('loading');
117             })
118             .fail(Notification.exception);
119     };
121     /**
122       * Set up all of the event listeners for the PreferencesController.
123       *
124       * @method registerEventListeners
125       */
126     PreferencesController.prototype.registerEventListeners = function() {
127         var disabledNotificationsElement = $(SELECTORS.DISABLE_NOTIFICATIONS);
129         CustomEvents.define(this.root, [
130             CustomEvents.events.activate,
131         ]);
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');
142                 });
143             }
144         }.bind(this));
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();
150         });
152         CustomEvents.define(disabledNotificationsElement, [
153             CustomEvents.events.activate
154         ]);
156         disabledNotificationsElement.on(CustomEvents.events.activate, function() {
157             this.toggleDisableAllStatus();
158         }.bind(this));
159     };
161     return PreferencesController;