From 39986f504403f43115df87f6421d32c7742474d3 Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Tue, 19 Jul 2016 02:01:08 +0000 Subject: [PATCH] MDL-54698 message: split message preferences page js --- message/amd/src/disable_all_preference.js | 83 +++++++ message/amd/src/notification_preference.js | 186 +++++++++++++++ message/amd/src/notification_processor.js | 93 ++++++++ ...preferences_general_settings_controller.js | 158 +------------ ...eferences_notifications_list_controller.js | 220 +----------------- .../src/preferences_processors_controller.js | 75 +----- message/amd/src/processor_form.js | 97 ++++++++ message/amd/src/user_preference.js | 128 ++++++++++ 8 files changed, 597 insertions(+), 443 deletions(-) create mode 100644 message/amd/src/disable_all_preference.js create mode 100644 message/amd/src/notification_preference.js create mode 100644 message/amd/src/notification_processor.js create mode 100644 message/amd/src/processor_form.js create mode 100644 message/amd/src/user_preference.js diff --git a/message/amd/src/disable_all_preference.js b/message/amd/src/disable_all_preference.js new file mode 100644 index 00000000000..065d88469b2 --- /dev/null +++ b/message/amd/src/disable_all_preference.js @@ -0,0 +1,83 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Handle the disable all notifications user preference on the message + * preferences page + * + * @module core_message/disable_all_preference + * @class disable_all_preference + * @package message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.2 + */ +define(['jquery', 'core_message/user_preference'], function($, UserPreference) { + /** + * Constructor for the DisableAlPreference. This is a special type + * of UserPreference. + * + * Subclasses UserPreference. + * + * @param element jQuery object root element of the processor + * @param int the current user id + * @return object DisableAllPreference + */ + var DisableAllPreference = function(element, userId) { + UserPreference.call(this, element, userId); + }; + + /** + * Clone the UserPreference prototype. + */ + DisableAllPreference.prototype = Object.create(UserPreference.prototype); + + /** + * Return the request arguments for the save function. + * + * Override UserPreference.prototype.getRequestArguments + * + * @method getRequestArguments + * @return object + */ + DisableAllPreference.prototype.getRequestArguments = function() { + return { + user: { + emailstop: this.isChecked() ? 1 : 0, + }, + }; + }; + + /** + * Persist the preference and fire relevant events after the + * successfully saving. + * + * Override UserPreference.prototype.save + * + * @method save + * @return promise + */ + DisableAllPreference.prototype.save = function() { + return UserPreference.prototype.save.call(this).done(function() { + if (this.isChecked()) { + $(document).trigger('messageprefs:disableall'); + } else { + $(document).trigger('messageprefs:enableall'); + } + }.bind(this)); + }; + + return DisableAllPreference; +}); diff --git a/message/amd/src/notification_preference.js b/message/amd/src/notification_preference.js new file mode 100644 index 00000000000..88fdc96027a --- /dev/null +++ b/message/amd/src/notification_preference.js @@ -0,0 +1,186 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Controls the preference for an individual notification type on the + * message preference page. + * + * @module core_message/notification_preference + * @class notification_preference + * @package message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.2 + */ +define(['jquery', 'core/ajax', 'core/notification', 'core_message/notification_processor'], + function($, ajax, notification, NotificationProcessor) { + + var SELECTORS = { + PROCESSOR: '[data-processor-name]', + STATE_INPUTS: '[data-state] input', + }; + + /** + * Constructor for the Preference. + * + * @param element jQuery object root element of the preference + * @param int the current user id + * @return object Preference + */ + var NotificationPreference = function(element, userId) { + this.root = $(element); + this.userId = userId; + }; + + /** + * Get the unique prefix key that identifies this user preference. + * + * @method getPreferenceKey + * @return string + */ + NotificationPreference.prototype.getPreferenceKey = function() { + return this.root.attr('data-preference-key'); + }; + + /** + * Get the unique key for the logged in preference. + * + * @method getLoggedInPreferenceKey + * @return string + */ + NotificationPreference.prototype.getLoggedInPreferenceKey = function() { + return this.getPreferenceKey() + '_loggedin'; + }; + + /** + * Get the unique key for the logged off preference. + * + * @method getLoggedOffPreferenceKey + * @return string + */ + NotificationPreference.prototype.getLoggedOffPreferenceKey = function() { + return this.getPreferenceKey() + '_loggedoff'; + }; + + /** + * Get the list of Processors available for this preference. + * + * @method getProcessors + * @return array + */ + NotificationPreference.prototype.getProcessors = function() { + return this.root.find(SELECTORS.PROCESSOR).map(function(index, element) { + return new NotificationProcessor($(element)); + }); + }; + + /** + * Flag the preference as loading. + * + * @method startLoading + */ + NotificationPreference.prototype.startLoading = function() { + this.root.addClass('loading'); + this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', true); + }; + + /** + * Remove the loading flag for this preference. + * + * @method stopLoading + */ + NotificationPreference.prototype.stopLoading = function() { + this.root.removeClass('loading'); + this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', false); + }; + + /** + * Check if the preference is loading. + * + * @method isLoading + */ + NotificationPreference.prototype.isLoading = function() { + return this.root.hasClass('loading'); + }; + + /** + * Persist the current state of the processors for this preference. + * + * @method save + * @return promise + */ + NotificationPreference.prototype.save = function() { + if (this.isLoading()) { + return $.Deferred().resolve(); + } + + this.startLoading(); + + var loggedInValue = ''; + var loggedOffValue = ''; + + this.getProcessors().each(function(index, processor) { + if (processor.isLoggedInEnabled()) { + if (loggedInValue === '') { + loggedInValue = processor.getName(); + } else { + loggedInValue += ',' + processor.getName(); + } + } + + if (processor.isLoggedOffEnabled()) { + if (loggedOffValue === '') { + loggedOffValue = processor.getName(); + } else { + loggedOffValue += ',' + processor.getName(); + } + } + }); + + if (loggedInValue === '') { + loggedInValue = 'none'; + } + + if (loggedOffValue === '') { + loggedOffValue = 'none'; + } + + var args = { + user: { + preferences: [ + { + type: this.getLoggedInPreferenceKey(), + value: loggedInValue, + }, + { + type: this.getLoggedOffPreferenceKey(), + value: loggedOffValue, + }, + ], + } + }; + + var request = { + methodname: 'core_user_update_user', + args: args, + }; + + return ajax.call([request])[0] + .fail(notification.exception) + .always(function() { this.stopLoading(); }.bind(this)); + }; + + return NotificationPreference; +}); diff --git a/message/amd/src/notification_processor.js b/message/amd/src/notification_processor.js new file mode 100644 index 00000000000..16774afc7d5 --- /dev/null +++ b/message/amd/src/notification_processor.js @@ -0,0 +1,93 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Represents the notification processor (e.g. email, popup, jabber) + * + * @module core_message/notification_processor + * @class notification_processor + * @package message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.2 + */ +define(['jquery'], function($) { + var SELECTORS = { + STATE_NONE: '[data-state="none"]', + STATE_BOTH: '[data-state="both"]', + STATE_LOGGED_IN: '[data-state="loggedin"]', + STATE_LOGGED_OFF: '[data-state="loggedoff"]', + }; + + /** + * Constructor for the notification processor. + * + * @param element jQuery object root element of the processor + * @return object NotificationProcessor + */ + var NotificationProcessor = function(element) { + this.root = $(element); + }; + + /** + * Get the processor name. + * + * @method getName + * @return string + */ + NotificationProcessor.prototype.getName = function() { + return this.root.attr('data-processor-name'); + }; + + /** + * Check if the processor is enabled when the user is logged in. + * + * @method isLoggedInEnabled + * @return bool + */ + NotificationProcessor.prototype.isLoggedInEnabled = function() { + var none = this.root.find(SELECTORS.STATE_NONE).find('input'); + + if (none.prop('checked')) { + return false; + } + + var both = this.root.find(SELECTORS.STATE_BOTH).find('input'); + var loggedIn = this.root.find(SELECTORS.STATE_LOGGED_IN).find('input'); + + return loggedIn.prop('checked') || both.prop('checked'); + }; + + /** + * Check if the processor is enabled when the user is logged out. + * + * @method isLoggedOffEnabled + * @return bool + */ + NotificationProcessor.prototype.isLoggedOffEnabled = function() { + var none = this.root.find(SELECTORS.STATE_NONE).find('input'); + + if (none.prop('checked')) { + return false; + } + + var both = this.root.find(SELECTORS.STATE_BOTH).find('input'); + var loggedOff = this.root.find(SELECTORS.STATE_LOGGED_OFF).find('input'); + + return loggedOff.prop('checked') || both.prop('checked'); + }; + + return NotificationProcessor; +}); diff --git a/message/amd/src/preferences_general_settings_controller.js b/message/amd/src/preferences_general_settings_controller.js index adb7264e13c..356b5c34d37 100644 --- a/message/amd/src/preferences_general_settings_controller.js +++ b/message/amd/src/preferences_general_settings_controller.js @@ -23,165 +23,13 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 3.2 */ -define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) { +define(['jquery', 'core_message/user_preference', 'core_message/disable_all_preference'], + function($, UserPreference, DisableAllPreference) { + var SELECTORS = { SETTING: '[data-preference-key]', }; - /** - * Constructor for the UserPreference. - * - * @param element jQuery object root element of the processor - * @param int the current user id - * @return object UserPreference - */ - var UserPreference = function(element, userId) { - this.root = $(element); - this.userId = userId; - }; - - /** - * Check if the preference is checked (enabled). - * - * @return bool - */ - UserPreference.prototype.isChecked = function() { - return this.root.find('input').prop('checked'); - }; - - /** - * Get the unique key that identifies this user preference. - * - * @method getPreferenceKey - * @return string - */ - UserPreference.prototype.getPreferenceKey = function() { - return this.root.attr('data-preference-key'); - }; - - /** - * Flag the preference as loading. - * - * @method startLoading - */ - UserPreference.prototype.startLoading = function() { - this.root.addClass('loading'); - this.root.find('input').prop('disabled', true); - }; - - /** - * Remove the loading flag for this preference. - * - * @method stopLoading - */ - UserPreference.prototype.stopLoading = function() { - this.root.removeClass('loading'); - this.root.find('input').prop('disabled', false); - }; - - /** - * Check if the preference is loading. - * - * @method isLoading - */ - UserPreference.prototype.isLoading = function() { - return this.root.hasClass('loading'); - }; - - /** - * Generate the request arguments for the save function. - * - * @method getRequestArguments - * @return object - */ - UserPreference.prototype.getRequestArguments = function() { - return { - user: { - preferences: [{ - type: this.getPreferenceKey(), - value: this.isChecked() ? 1 : 0, - }], - } - }; - }; - - /** - * Persist the user preference in the server. - * - * @method save - * @return promise - */ - UserPreference.prototype.save = function() { - if (this.isLoading()) { - return $.Deferred(); - } - - this.startLoading(); - - var request = { - methodname: 'core_user_update_user', - args: this.getRequestArguments(), - }; - - return ajax.call([request])[0] - .fail(notification.exception) - .always(function() { this.stopLoading(); }.bind(this)); - }; - - /** - * Constructor for the DisableAlPreference. This is a special type - * of UserPreference. - * - * Subclasses UserPreference. - * - * @param element jQuery object root element of the processor - * @param int the current user id - * @return object DisableAllPreference - */ - var DisableAllPreference = function(element, userId) { - UserPreference.call(this, element, userId); - }; - - /** - * Clone the UserPreference prototype. - */ - DisableAllPreference.prototype = Object.create(UserPreference.prototype); - - /** - * Return the request arguments for the save function. - * - * Override UserPreference.prototype.getRequestArguments - * - * @method getRequestArguments - * @return object - */ - DisableAllPreference.prototype.getRequestArguments = function() { - return { - user: { - emailstop: this.isChecked() ? 1 : 0, - }, - }; - }; - - /** - * Persist the preference and fire relevant events after the - * successfully saving. - * - * Override UserPreference.prototype.save - * - * @method save - * @return promise - */ - DisableAllPreference.prototype.save = function() { - return UserPreference.prototype.save.call(this).done(function() { - if (this.isChecked()) { - $(document).trigger('messageprefs:disableall'); - } else { - $(document).trigger('messageprefs:enableall'); - } - }.bind(this)); - }; - /** * Constructor for the GeneralSettingsController. * diff --git a/message/amd/src/preferences_notifications_list_controller.js b/message/amd/src/preferences_notifications_list_controller.js index 00e033f15c2..7de542f6fc2 100644 --- a/message/amd/src/preferences_notifications_list_controller.js +++ b/message/amd/src/preferences_notifications_list_controller.js @@ -14,7 +14,8 @@ // along with Moodle. If not, see . /** - * Controls the preferences page + * Controls the preferences for the list of notification types on the + * message preference page * * @module core_message/preferences_notifications_list_controller * @class preferences_notifications_list_controller @@ -23,225 +24,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 3.2 */ -define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) { +define(['jquery', 'core_message/notification_preference'], function($, NotificationPreference) { var SELECTORS = { PREFERENCE_ROW: '.preference-row', - PROCESSOR: '[data-processor-name]', - STATE_NONE: '[data-state="none"]', - STATE_BOTH: '[data-state="both"]', - STATE_LOGGED_IN: '[data-state="loggedin"]', - STATE_LOGGED_OFF: '[data-state="loggedoff"]', STATE_INPUTS: '[data-state] input', }; - /** - * Constructor for the Processor. - * - * @param element jQuery object root element of the processor - * @return object Processor - */ - var Processor = function(element) { - this.root = $(element); - }; - - /** - * Get the processor name. - * - * @method getName - * @return string - */ - Processor.prototype.getName = function() { - return this.root.attr('data-processor-name'); - }; - - /** - * Check if the processor is enabled when the user is logged in. - * - * @method isLoggedInEnabled - * @return bool - */ - Processor.prototype.isLoggedInEnabled = function() { - var none = this.root.find(SELECTORS.STATE_NONE).find('input'); - - if (none.prop('checked')) { - return false; - } - - var both = this.root.find(SELECTORS.STATE_BOTH).find('input'); - var loggedIn = this.root.find(SELECTORS.STATE_LOGGED_IN).find('input'); - - return loggedIn.prop('checked') || both.prop('checked'); - }; - - /** - * Check if the processor is enabled when the user is logged out. - * - * @method isLoggedOffEnabled - * @return bool - */ - Processor.prototype.isLoggedOffEnabled = function() { - var none = this.root.find(SELECTORS.STATE_NONE).find('input'); - - if (none.prop('checked')) { - return false; - } - - var both = this.root.find(SELECTORS.STATE_BOTH).find('input'); - var loggedOff = this.root.find(SELECTORS.STATE_LOGGED_OFF).find('input'); - - return loggedOff.prop('checked') || both.prop('checked'); - }; - - /** - * Constructor for the Preference. - * - * @param element jQuery object root element of the preference - * @param int the current user id - * @return object Preference - */ - var Preference = function(element, userId) { - this.root = $(element); - this.userId = userId; - }; - - /** - * Get the unique prefix key that identifies this user preference. - * - * @method getPreferenceKey - * @return string - */ - Preference.prototype.getPreferenceKey = function() { - return this.root.attr('data-preference-key'); - }; - - /** - * Get the unique key for the logged in preference. - * - * @method getLoggedInPreferenceKey - * @return string - */ - Preference.prototype.getLoggedInPreferenceKey = function() { - return this.getPreferenceKey() + '_loggedin'; - }; - - /** - * Get the unique key for the logged off preference. - * - * @method getLoggedOffPreferenceKey - * @return string - */ - Preference.prototype.getLoggedOffPreferenceKey = function() { - return this.getPreferenceKey() + '_loggedoff'; - }; - - /** - * Get the list of Processors available for this preference. - * - * @method getProcessors - * @return array - */ - Preference.prototype.getProcessors = function() { - return this.root.find(SELECTORS.PROCESSOR).map(function(index, element) { - return new Processor($(element)); - }); - }; - - /** - * Flag the preference as loading. - * - * @method startLoading - */ - Preference.prototype.startLoading = function() { - this.root.addClass('loading'); - this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', true); - }; - - /** - * Remove the loading flag for this preference. - * - * @method stopLoading - */ - Preference.prototype.stopLoading = function() { - this.root.removeClass('loading'); - this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', false); - }; - - /** - * Check if the preference is loading. - * - * @method isLoading - */ - Preference.prototype.isLoading = function() { - return this.root.hasClass('loading'); - }; - - /** - * Persist the current state of the processors for this preference. - * - * @method save - * @return promise - */ - Preference.prototype.save = function() { - if (this.isLoading()) { - return $.Deferred(); - } - - this.startLoading(); - - var loggedInValue = ''; - var loggedOffValue = ''; - - this.getProcessors().each(function(index, processor) { - if (processor.isLoggedInEnabled()) { - if (loggedInValue === '') { - loggedInValue = processor.getName(); - } else { - loggedInValue += ',' + processor.getName(); - } - } - - if (processor.isLoggedOffEnabled()) { - if (loggedOffValue === '') { - loggedOffValue = processor.getName(); - } else { - loggedOffValue += ',' + processor.getName(); - } - } - }); - - if (loggedInValue === '') { - loggedInValue = 'none'; - } - - if (loggedOffValue === '') { - loggedOffValue = 'none'; - } - - var args = { - user: { - preferences: [ - { - type: this.getLoggedInPreferenceKey(), - value: loggedInValue, - }, - { - type: this.getLoggedOffPreferenceKey(), - value: loggedOffValue, - }, - ], - } - }; - - var request = { - methodname: 'core_user_update_user', - args: args, - }; - - return ajax.call([request])[0] - .fail(notification.exception) - .always(function() { this.stopLoading(); }.bind(this)); - }; - /** * Constructor for the PreferencesController. * @@ -255,7 +43,7 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat this.root.on('change', function(e) { if (!this.isDisabled()) { var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW); - var preference = new Preference(preferenceRow, this.userId); + var preference = new NotificationPreference(preferenceRow, this.userId); preference.save(); } }.bind(this)); diff --git a/message/amd/src/preferences_processors_controller.js b/message/amd/src/preferences_processors_controller.js index c527e8fc01b..44fde380e3b 100644 --- a/message/amd/src/preferences_processors_controller.js +++ b/message/amd/src/preferences_processors_controller.js @@ -14,7 +14,7 @@ // along with Moodle. If not, see . /** - * Controls the processors page + * Controls the processors config section on the message preference page * * @module core_message/preferences_processors_controller * @class preferences_processors_controller @@ -23,80 +23,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 3.2 */ -define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) { +define(['jquery', 'core/ajax', 'core/notification', 'core_message/processor_form'], function($, ajax, notification, ProcessorForm) { var SELECTORS = { PROCESSOR: '[data-processor-name]', }; - /** - * Constructor for the Processor. - * - * @param element jQuery object root element of the preference - * @param int the current user id - * @return object Processor - */ - var Processor = function(element, userId) { - this.root = $(element); - this.userId = userId; - this.name = this.root.attr('data-processor-name'); - }; - - /** - * Flag the processor as loading. - * - * @method startLoading - */ - Processor.prototype.startLoading = function() { - this.root.addClass('loading'); - }; - - /** - * Remove the loading flag for this processor. - * - * @method stopLoading - */ - Processor.prototype.stopLoading = function() { - this.root.removeClass('loading'); - }; - - /** - * Check if this processor is loading. - * - * @method isLoading - * @return bool - */ - Processor.prototype.isLoading = function() { - return this.root.hasClass('loading'); - }; - - /** - * Persist the processor configuration. - * - * @method save - * @return promise - */ - Processor.prototype.save = function() { - if (this.isLoading()) { - return $.Deferred(); - } - - this.startLoading(); - - var data = this.root.find('form').serializeArray(); - var request = { - methodname: 'core_message_message_processor_config_form', - args: { - userid: this.userId, - name: this.name, - formvalues: data, - } - }; - - return ajax.call([request])[0] - .fail(notification.exception) - .always(function() { this.stopLoading(); }.bind(this)); - }; - /** * Constructor for the ProcessorsController. * @@ -109,7 +40,7 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat this.root.on('change', function(e) { var element = $(e.target).closest(SELECTORS.PROCESSOR); - var processor = new Processor(element, this.userId); + var processor = new ProcessorForm(element, this.userId); processor.save(); }.bind(this)); }; diff --git a/message/amd/src/processor_form.js b/message/amd/src/processor_form.js new file mode 100644 index 00000000000..258faba81e4 --- /dev/null +++ b/message/amd/src/processor_form.js @@ -0,0 +1,97 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Manages the processor form on the message preferences page. + * + * @module core_message/processor_form + * @class processor_form + * @package message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.2 + */ +define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) { + /** + * Constructor for the ProcessorForm. + * + * @param element jQuery object root element of the preference + * @param int the current user id + * @return object ProcessorForm + */ + var ProcessorForm = function(element, userId) { + this.root = $(element); + this.userId = userId; + this.name = this.root.attr('data-processor-name'); + }; + + /** + * Flag the processor as loading. + * + * @method startLoading + */ + ProcessorForm.prototype.startLoading = function() { + this.root.addClass('loading'); + }; + + /** + * Remove the loading flag for this processor. + * + * @method stopLoading + */ + ProcessorForm.prototype.stopLoading = function() { + this.root.removeClass('loading'); + }; + + /** + * Check if this processor is loading. + * + * @method isLoading + * @return bool + */ + ProcessorForm.prototype.isLoading = function() { + return this.root.hasClass('loading'); + }; + + /** + * Persist the processor configuration. + * + * @method save + * @return promise + */ + ProcessorForm.prototype.save = function() { + if (this.isLoading()) { + return $.Deferred(); + } + + this.startLoading(); + + var data = this.root.find('form').serializeArray(); + var request = { + methodname: 'core_message_message_processor_config_form', + args: { + userid: this.userId, + name: this.name, + formvalues: data, + } + }; + + return ajax.call([request])[0] + .fail(notification.exception) + .always(function() { this.stopLoading(); }.bind(this)); + }; + + return ProcessorForm; +}); diff --git a/message/amd/src/user_preference.js b/message/amd/src/user_preference.js new file mode 100644 index 00000000000..7786066f5d8 --- /dev/null +++ b/message/amd/src/user_preference.js @@ -0,0 +1,128 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Controls a user preference on the message preferences page. + * + * @module core_message/user_preference + * @class user_preference + * @package message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since 3.2 + */ +define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) { + /** + * Constructor for the UserPreference. + * + * @param element jQuery object root element of the processor + * @param int the current user id + * @return object UserPreference + */ + var UserPreference = function(element, userId) { + this.root = $(element); + this.userId = userId; + }; + + /** + * Check if the preference is checked (enabled). + * + * @return bool + */ + UserPreference.prototype.isChecked = function() { + return this.root.find('input').prop('checked'); + }; + + /** + * Get the unique key that identifies this user preference. + * + * @method getPreferenceKey + * @return string + */ + UserPreference.prototype.getPreferenceKey = function() { + return this.root.attr('data-preference-key'); + }; + + /** + * Flag the preference as loading. + * + * @method startLoading + */ + UserPreference.prototype.startLoading = function() { + this.root.addClass('loading'); + this.root.find('input').prop('disabled', true); + }; + + /** + * Remove the loading flag for this preference. + * + * @method stopLoading + */ + UserPreference.prototype.stopLoading = function() { + this.root.removeClass('loading'); + this.root.find('input').prop('disabled', false); + }; + + /** + * Check if the preference is loading. + * + * @method isLoading + */ + UserPreference.prototype.isLoading = function() { + return this.root.hasClass('loading'); + }; + + /** + * Generate the request arguments for the save function. + * + * @method getRequestArguments + * @return object + */ + UserPreference.prototype.getRequestArguments = function() { + return { + user: { + preferences: [{ + type: this.getPreferenceKey(), + value: this.isChecked() ? 1 : 0, + }], + } + }; + }; + + /** + * Persist the user preference in the server. + * + * @method save + * @return promise + */ + UserPreference.prototype.save = function() { + if (this.isLoading()) { + return $.Deferred().resolve(); + } + + this.startLoading(); + + var request = { + methodname: 'core_user_update_user', + args: this.getRequestArguments(), + }; + + return ajax.call([request])[0] + .fail(notification.exception) + .always(function() { this.stopLoading(); }.bind(this)); + }; + + return UserPreference; +});