MDL-54698 message: split message preferences page js

This commit is contained in:
Ryan Wyllie
2016-10-07 16:26:40 +08:00
committed by Mark Nelson
parent eda6bc19e6
commit 39986f5044
8 changed files with 597 additions and 443 deletions
+83
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* 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 <[email protected]>
* @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;
});
+186
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* 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 <[email protected]>
* @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;
});
+93
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* Represents the notification processor (e.g. email, popup, jabber)
*
* @module core_message/notification_processor
* @class notification_processor
* @package message
* @copyright 2016 Ryan Wyllie <[email protected]>
* @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;
});
@@ -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.
*
@@ -14,7 +14,8 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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));
@@ -14,7 +14,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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));
};
+97
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* Manages the processor form on the message preferences page.
*
* @module core_message/processor_form
* @class processor_form
* @package message
* @copyright 2016 Ryan Wyllie <[email protected]>
* @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;
});
+128
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* Controls a user preference on the message preferences page.
*
* @module core_message/user_preference
* @class user_preference
* @package message
* @copyright 2016 Ryan Wyllie <[email protected]>
* @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;
});