Skip to content
Permalink
Browse files

MDL-54698 message: split message preferences page js

  • Loading branch information...
ryanwyllie authored and markn86 committed Jul 19, 2016
1 parent eda6bc1 commit 39986f504403f43115df87f6421d32c7742474d3
@@ -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 <ryan@moodle.com>
* @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;
});
@@ -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 <ryan@moodle.com>
* @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;
});
@@ -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 <ryan@moodle.com>
* @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;
});

0 comments on commit 39986f5

Please sign in to comment.
You can’t perform that action at this time.