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 message preference page.
19 * @module core_message/message_preferences
20 * @class message_preferences
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/ajax', 'core/notification',
26 'core_message/message_notification_preference', 'core/custom_interaction_events'],
27 function($, Ajax, Notification, MessageNotificationPreference, CustomEvents) {
30 PREFERENCE: '[data-state]',
31 PREFERENCES_CONTAINER: '[data-region="preferences-container"]',
32 BLOCK_NON_CONTACTS: '[data-region="block-non-contacts-container"] [data-block-non-contacts]',
33 BLOCK_NON_CONTACTS_CONTAINER: '[data-region="block-non-contacts-container"]',
37 * Constructor for the MessagePreferences.
39 * @param {object} element The root element for the message preferences
41 var MessagePreferences = function(element) {
42 this.root = $(element);
43 this.userId = this.root.find(SELECTORS.PREFERENCES_CONTAINER).attr('data-user-id');
45 this.registerEventListeners();
49 * Check if the preferences have been disabled on this page.
51 * @method preferencesDisabled
54 MessagePreferences.prototype.preferencesDisabled = function() {
55 return this.root.find(SELECTORS.PREFERENCES_CONTAINER).hasClass('disabled');
59 * Update the block messages from non-contacts user preference in the DOM and
60 * send a request to update on the server.
63 * @method saveBlockNonContactsStatus
65 MessagePreferences.prototype.saveBlockNonContactsStatus = function() {
66 var checkbox = this.root.find(SELECTORS.BLOCK_NON_CONTACTS);
67 var container = this.root.find(SELECTORS.BLOCK_NON_CONTACTS_CONTAINER);
68 var ischecked = checkbox.prop('checked');
70 if (container.hasClass('loading')) {
71 return $.Deferred().resolve();
74 container.addClass('loading');
77 methodname: 'core_user_update_user_preferences',
82 type: checkbox.attr('data-preference-key'),
83 value: ischecked ? 1 : 0,
89 return Ajax.call([request])[0]
90 .fail(Notification.exception)
92 container.removeClass('loading');
97 * Create all of the event listeners for the message preferences page.
99 * @method registerEventListeners
101 MessagePreferences.prototype.registerEventListeners = function() {
102 CustomEvents.define(this.root, [
103 CustomEvents.events.activate
106 this.root.on(CustomEvents.events.activate, SELECTORS.BLOCK_NON_CONTACTS, function() {
107 this.saveBlockNonContactsStatus();
110 this.root.on('change', function(e) {
111 if (!this.preferencesDisabled()) {
112 var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
113 var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
114 var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
116 preferenceElement.addClass('loading');
117 messagePreference.save().always(function() {
118 preferenceElement.removeClass('loading');
124 return MessagePreferences;