MDL-61575 message_notification: replace lp/dialogue with core/modal
[moodle.git] / message / amd / src / preferences_notifications_list_controller.js
CommitLineData
a0eabdd3
RW
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/**
39986f50
RW
17 * Controls the preferences for the list of notification types on the
18 * message preference page
a0eabdd3 19 *
9e8a29c9 20 * @module core_message/preferences_notifications_list_controller
a0eabdd3
RW
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
a0eabdd3 25 */
cd03a6fe 26define(['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 ) {
643b015d 43
a0eabdd3 44 var SELECTORS = {
6af2bd09
RW
45 DISABLE_NOTIFICATIONS: '[data-region="disable-notification-container"] [data-disable-notifications]',
46 DISABLE_NOTIFICATIONS_CONTAINER: '[data-region="disable-notification-container"]',
e9cce46c 47 PREFERENCE: '[data-state]',
6af2bd09 48 PREFERENCE_ROW: '[data-region="preference-row"]',
e9cce46c 49 PREFERENCE_INPUT: '[data-state] input',
643b015d 50 PROCESSOR_SETTING: '[data-processor-setting]',
a0eabdd3
RW
51 };
52
a0eabdd3
RW
53 /**
54 * Constructor for the PreferencesController.
55 *
7b55aaa1 56 * @param {object} element jQuery object root element of the preference
a0eabdd3
RW
57 */
58 var PreferencesController = function(element) {
59 this.root = $(element);
60 this.userId = this.root.attr('data-user-id');
61
643b015d 62 this.registerEventListeners();
a0eabdd3
RW
63 };
64
65 /**
66 * Check if the preferences are all disabled.
67 *
68 * @method isDisabled
7b55aaa1 69 * @return {bool}
a0eabdd3
RW
70 */
71 PreferencesController.prototype.isDisabled = function() {
72 return this.root.hasClass('disabled');
73 };
74
75 /**
76 * Disable all of the preferences.
77 *
78 * @method setDisabled
79 */
80 PreferencesController.prototype.setDisabled = function() {
81 this.root.addClass('disabled');
e9cce46c 82 this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', true);
a0eabdd3
RW
83 };
84
85 /**
86 * Enable all of the preferences.
87 *
88 * @method setEnabled
89 */
90 PreferencesController.prototype.setEnabled = function() {
91 this.root.removeClass('disabled');
e9cce46c 92 this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
a0eabdd3
RW
93 };
94
812cc6f6
MN
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
7b55aaa1 100 * @return {Promise}
812cc6f6
MN
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');
106
107 if (container.hasClass('loading')) {
108 return $.Deferred().resolve();
109 }
110
111 container.addClass('loading');
112
113 var request = {
c598f278 114 methodname: 'core_user_update_user_preferences',
812cc6f6 115 args: {
c598f278
MN
116 userid: this.userId,
117 emailstop: ischecked ? 1 : 0,
812cc6f6
MN
118 }
119 };
120
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 };
134
7b55aaa1
MN
135 /**
136 * Set up all of the event listeners for the PreferencesController.
137 *
138 * @method registerEventListeners
139 */
643b015d 140 PreferencesController.prototype.registerEventListeners = function() {
812cc6f6
MN
141 var disabledNotificationsElement = $(SELECTORS.DISABLE_NOTIFICATIONS);
142
643b015d
RW
143 CustomEvents.define(this.root, [
144 CustomEvents.events.activate,
145 ]);
146
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);
152
153 preferenceElement.addClass('loading');
154 preference.save().always(function() {
155 preferenceElement.removeClass('loading');
156 });
157 }
158 }.bind(this));
159
cd03a6fe 160 var eventFormPromise = ModalFactory.create({
161 type: NotificationProcessorSettings.TYPE,
162 });
163
164 this.root.on(CustomEvents.events.activate, SELECTORS.PROCESSOR_SETTING, function(e) {
643b015d 165 var element = $(e.target).closest(SELECTORS.PROCESSOR_SETTING);
cd03a6fe 166
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();
175
176 e.stopImmediatePropagation();
177 return;
178 }).fail(Notification.exception);
643b015d
RW
179 });
180
812cc6f6
MN
181 CustomEvents.define(disabledNotificationsElement, [
182 CustomEvents.events.activate
183 ]);
643b015d 184
412a4a6d 185 disabledNotificationsElement.on(CustomEvents.events.activate, function() {
812cc6f6 186 this.toggleDisableAllStatus();
643b015d 187 }.bind(this));
2be15a66 188 };
643b015d 189
a0eabdd3
RW
190 return PreferencesController;
191});