Skip to content

Commit

Permalink
MDL-54698 message: move general settings from preference page
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie authored and mdjnelson committed Oct 7, 2016
1 parent 1e5f751 commit 34eb5fc
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 474 deletions.
5 changes: 3 additions & 2 deletions lang/en/message.php
Expand Up @@ -48,14 +48,14 @@
$string['deletemessage'] = 'Delete message';
$string['deletemessageconfirmation'] = 'Are you sure you want to delete this message? It will only be deleted from your messaging history and will still be viewable by the user who sent or received the message.';
$string['deletemessagesdays'] = 'Number of days before old messages are automatically deleted';
$string['disableall'] = 'Temporarily disable notifications';
$string['disableall_help'] = 'Temporarily disable all notifications except those marked as "forced" by the site administrator';
$string['disableall'] = 'Disable notifications';
$string['disabled'] = 'Messaging is disabled on this site';
$string['disallowed'] = 'Disallowed';
$string['discussion'] = 'Discussion';
$string['emailmessages'] = 'Email messages when I am offline';
$string['emailtagline'] = 'This is a copy of a message sent to you at "{$a->sitename}". Go to {$a->url} to reply.';
$string['emptysearchstring'] = 'You must search for something';
$string['enableall'] = 'Enable notifications';
$string['enabled'] = 'Enabled';
$string['errorcallingprocessor'] = 'Error calling defined output';
$string['errorwhilesendingmessage'] = 'An error occurred while sending the message; please try again later.';
Expand Down Expand Up @@ -174,6 +174,7 @@
$string['touserdoesntexist'] = 'You can not send a message to a user id ({$a}) that doesn\'t exist';
$string['unabletomessageuser'] = 'You are not permitted to send a message to that user';
$string['unblockcontact'] = 'Unblock contact';
$string['unblocknoncontacts'] = 'Allow non-contacts to message me';
$string['unreadmessages'] = 'Unread messages ({$a})';
$string['unreadnewmessages'] = 'New messages ({$a})';
$string['unreadnewmessage'] = 'New message from {$a}';
Expand Down
2 changes: 2 additions & 0 deletions lib/outputrenderers.php
Expand Up @@ -3205,6 +3205,7 @@ public function notification_menu() {
if (isloggedin() && $processor->enabled) {
$context = [
'userid' => $USER->id,
'allnotificationsdisabled' => !empty($USER->emailstop),
'urls' => [
'preferences' => (new moodle_url('/message/edit.php', ['id' => $USER->id]))->out(),
],
Expand All @@ -3226,6 +3227,7 @@ public function message_menu() {
if (isloggedin()) {
$context = [
'userid' => $USER->id,
'blocknoncontacts' => get_user_preferences('message_blocknoncontacts', '', $USER->id),
'urls' => [
'preferences' => (new moodle_url('/message/edit.php', ['id' => $USER->id]))->out(),
],
Expand Down
83 changes: 0 additions & 83 deletions message/amd/src/disable_all_preference.js

This file was deleted.

113 changes: 93 additions & 20 deletions message/amd/src/message_popover_controller.js
Expand Up @@ -26,9 +26,9 @@
* @since 3.2
*/
define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates', 'core/str',
'core/custom_interaction_events', 'core/popover_region_controller',
'core/notification', 'core/custom_interaction_events', 'core/popover_region_controller',
'core_message/message_repository', 'core/url'],
function($, bootstrap, ajax, templates, str, CustomEvents,
function($, Bootstrap, Ajax, Templates, Str, Notification, CustomEvents,
PopoverController, MessageRepo, URL) {

var SELECTORS = {
Expand All @@ -39,6 +39,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
CONTENT_ITEM_CONTAINER: '.content-item-container',
EMPTY_MESSAGE: '.empty-message',
LINK_URL: '[data-link-url]',
BLOCK_NON_CONTACTS_BUTTON: '[data-block-non-contacts]',
};

/**
Expand All @@ -53,6 +54,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
PopoverController.call(this, element);

this.markAllReadButton = this.root.find(SELECTORS.MARK_ALL_READ_BUTTON);
this.blockNonContactsButton = this.root.find(SELECTORS.BLOCK_NON_CONTACTS_BUTTON);
this.content = this.root.find(SELECTORS.CONTENT);
this.userId = this.root.attr(SELECTORS.USER_ID);
this.limit = 20;
Expand All @@ -70,6 +72,11 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
*/
MessagePopoverController.prototype = Object.create(PopoverController.prototype);

/**
* Make sure the constructor is set correctly.
*/
MessagePopoverController.prototype.constructor = MessagePopoverController;

/**
* Get the element holding the messages.
*
Expand All @@ -91,22 +98,22 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'

/**
* Set the correct aria label on the menu toggle button to be read out by screen
* readers. The message will indicate the state of the unread notifications.
* readers. The message will indicate the state of the unread messages.
*
* @method updateButtonAriaLabel
*/
MessagePopoverController.prototype.updateButtonAriaLabel = function() {
if (this.isMenuOpen()) {
str.get_string('hidemessagewindow', 'message').done(function(string) {
Str.get_string('hidemessagewindow', 'message').done(function(string) {
this.menuToggle.attr('aria-label', string);
}.bind(this));
} else {
if (this.unreadCount) {
str.get_string('showmessagewindowwithcount', 'message', this.unreadCount).done(function(string) {
Str.get_string('showmessagewindowwithcount', 'message', this.unreadCount).done(function(string) {
this.menuToggle.attr('aria-label', string);
}.bind(this));
} else {
str.get_string('showmessagewindownonew', 'message').done(function(string) {
Str.get_string('showmessagewindownonew', 'message').done(function(string) {
this.menuToggle.attr('aria-label', string);
}.bind(this));
}
Expand Down Expand Up @@ -134,8 +141,8 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Show the unread notification count badge on the menu toggle if there
* are unread notifications, otherwise hide it.
* Show the unread message count badge on the menu toggle if there
* are unread messages, otherwise hide it.
*
* @method renderUnreadCount
*/
Expand All @@ -151,7 +158,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Hide the unread notification count badge on the menu toggle.
* Hide the unread message count badge on the menu toggle.
*
* @method hideUnreadCount
*/
Expand All @@ -160,7 +167,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Ask the server how many unread notifications are left, render the value
* Ask the server how many unread messages are left, render the value
* as a badge on the menu toggle and update the aria labels on the menu
* toggle.
*
Expand All @@ -175,12 +182,12 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Render the notification data with the appropriate template and add it to the DOM.
* Render the message data with the appropriate template and add it to the DOM.
*
* @method renderMessages
* @param messages array message data
* @param container jQuery object the container to append the rendered messages
* @return jQuery promise that is resolved when all notifications have been
* @return jQuery promise that is resolved when all messages have been
* rendered and added to the DOM
*/
MessagePopoverController.prototype.renderMessages = function(messages, container) {
Expand All @@ -197,10 +204,10 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
id: message.userid,
});

var promise = templates.render('message/message_content_item', message);
var promise = Templates.render('message/message_content_item', message);
promise.then(function(html, js) {
container.append(html);
templates.runTemplateJS(js);
Templates.runTemplateJS(js);
}.bind(this));

promises.push(promise);
Expand All @@ -215,7 +222,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
* loading some and haven't already loaded all of them.
*
* @method loadMoreMessages
* @return jQuery promise that is resolved when notifications have been
* @return jQuery promise that is resolved when messages have been
* retrieved and added to the DOM
*/
MessagePopoverController.prototype.loadMoreMessages = function() {
Expand Down Expand Up @@ -248,8 +255,8 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Send a request to the server to mark all unread notifications as read and update
* the unread count and unread notification elements appropriately.
* Send a request to the server to mark all unread messages as read and update
* the unread count and unread messages elements appropriately.
*
* @method markAllAsRead
*/
Expand All @@ -265,7 +272,65 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
};

/**
* Add all of the required event listeners for this notification popover.
* Update the block messages from non-contacts user preference in the DOM and
* send a request to update on the server.
*
* @method toggleBlockNonContactsStatus
*/
MessagePopoverController.prototype.toggleBlockNonContactsStatus = function() {
var button = this.blockNonContactsButton;
var ischecked = (button.attr('aria-checked') === 'true');
var blockstring = '';
var unblockstring = '';

button.addClass('loading');

return Str.get_strings([
{
key: 'blocknoncontacts',
component: 'message',
},
{
key: 'unblocknoncontacts',
component: 'message',
}
]).then(function(strings) {
// If we could load the strings then update the user preferences.
blockstring = strings[0];
unblockstring = strings[1];

var request = {
methodname: 'core_user_update_user',
args: {
user: {
preferences: [
{
type: button.attr('data-preference-key'),
value: ischecked ? 0 : 1,
}
]
}
}
};

return Ajax.call([request])[0];
})
.done(function() {
// If everything executed correctly then update the DOM.
if (ischecked) {
button.attr('aria-checked', false)
button.attr('data-original-title', blockstring);
} else {
button.attr('aria-checked', true)
button.attr('data-original-title', unblockstring);
}
})
.fail(Notification.exception)
.always(function() { button.removeClass('loading') });
};

/**
* Add all of the required event listeners for this messages popover.
*
* @method registerEventListeners
*/
Expand All @@ -274,7 +339,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
CustomEvents.events.keyboardActivate,
]);

// Update the notification information when the menu is opened.
// Update the message information when the menu is opened.
this.root.on(this.events().menuOpened, function() {
this.hideUnreadCount();
this.updateButtonAriaLabel();
Expand All @@ -284,7 +349,7 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
}
}.bind(this));

// Update the notification information when the menu is opened.
// Update the message information when the menu is opened.
this.root.on(this.events().menuClosed, function() {
this.renderUnreadCount();
this.updateButtonAriaLabel();
Expand Down Expand Up @@ -314,6 +379,14 @@ define(['jquery', 'theme_bootstrapbase/bootstrap', 'core/ajax', 'core/templates'
this.navigateToLinkURL(linkItem, false);
e.stopPropagation();
}.bind(this));

// Update the state of non-contact blocking when button is activated.
this.root.on(CustomEvents.events.activate, SELECTORS.BLOCK_NON_CONTACTS_BUTTON, function(e, data) {
this.toggleBlockNonContactsStatus();

e.stopPropagation();
data.originalEvent.preventDefault();
}.bind(this));
};

return MessagePopoverController;
Expand Down

0 comments on commit 34eb5fc

Please sign in to comment.