Merge branch 'MDL-57331-master' of https://github.com/xow/moodle

This commit is contained in:
David Monllao 2016-12-28 22:07:12 +01:00 committed by Dan Poltawski
commit edda5120e2
2 changed files with 58 additions and 38 deletions

File diff suppressed because one or more lines are too long

View File

@ -75,6 +75,9 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
/** @type {int} the number of messagess displayed */
Messages.prototype._numMessagesDisplayed = 0;
/** @type {array} the messages displayed or about to be displayed on the page */
Messages.prototype._messageQueue = [];
/** @type {int} the number of messages to retrieve */
Messages.prototype._numMessagesToRetrieve = 20;
@ -294,36 +297,8 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
}
// Keep track of the number of messages received.
var numberreceived = 0;
return this._getMessages(this._getUserId(), true).then(function(data) {
// Filter out any messages already rendered.
var messagesArea = this.messageArea.find(SELECTORS.MESSAGES);
data.messages = data.messages.filter(function(message) {
var id = "" + message.id + message.isread;
var result = messagesArea.find(SELECTORS.MESSAGE + '[data-id="' + id + '"]');
return !result.length;
});
numberreceived = data.messages.length;
// We have the data - lets render the template with it.
return Templates.render('core_message/message_area_messages', data);
}.bind(this)).then(function(html, js) {
// Check if we got something to do.
if (numberreceived > 0) {
var newHtml = $('<div>' + html + '</div>');
if (this._hasMatchingBlockTime(this.messageArea.node, newHtml, false)) {
newHtml.find(SELECTORS.BLOCKTIME + ':first').remove();
}
// Show the new content.
Templates.appendNodeContents(this.messageArea.find(SELECTORS.MESSAGES), newHtml, js);
// Scroll the new message into view.
if (shouldScrollBottom) {
this._scrollBottom();
}
// Increment the number of messages displayed.
this._numMessagesDisplayed += numberreceived;
// Reset the poll timer because the user may be active.
this._backoffTimer.restart();
}
return this._addMessagesToDom(data.messages, shouldScrollBottom);
}.bind(this)).always(function() {
// Mark that we are no longer busy loading data.
this._isLoadingMessages = false;
@ -435,7 +410,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
// Fire an event to say the message was sent.
this.messageArea.trigger(Events.MESSAGESENT, [this._getUserId(), text]);
// Update the messaging area.
return this._addMessageToDom();
return this._addLastMessageToDom();
}.bind(this)).then(function() {
// Ok, we are no longer sending a message.
this._isSendingMessage = false;
@ -624,10 +599,58 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
/**
* Handles adding messages to the DOM.
*
* @param {array} messages An array of messages to be added to the DOM.
* @param {boolean} shouldScrollBottom True will scroll to the bottom of the message window and show the new messages.
* @return {Promise} The promise resolved when the messages have been added to the DOM.
* @private
*/
Messages.prototype._addMessagesToDom = function(messages, shouldScrollBottom) {
var numberreceived = 0;
var messagesArea = this.messageArea.find(SELECTORS.MESSAGES);
messages = messages.filter(function(message) {
var id = "" + message.id + message.isread;
// If the message is already queued to be rendered, remove from the list of messages.
if (this._messageQueue[id]) {
return false;
}
// Filter out any messages already rendered.
var result = messagesArea.find(SELECTORS.MESSAGE + '[data-id="' + id + '"]');
// Any message we are rendering should go in the messageQueue.
if (!result.length) {
this._messageQueue[id] = true;
}
return !result.length;
}.bind(this));
numberreceived = messages.length;
// We have the data - lets render the template with it.
return Templates.render('core_message/message_area_messages', {messages: messages}).then(function(html, js) {
// Check if we got something to do.
if (numberreceived > 0) {
var newHtml = $('<div>' + html + '</div>');
if (this._hasMatchingBlockTime(this.messageArea.node, newHtml, false)) {
newHtml.find(SELECTORS.BLOCKTIME + ':first').remove();
}
// Show the new content.
Templates.appendNodeContents(this.messageArea.find(SELECTORS.MESSAGES), newHtml, js);
// Scroll the new message into view.
if (shouldScrollBottom) {
this._scrollBottom();
}
// Increment the number of messages displayed.
this._numMessagesDisplayed += numberreceived;
// Reset the poll timer because the user may be active.
this._backoffTimer.restart();
}
}.bind(this));
};
/**
* Handles adding the last message to the DOM.
*
* @return {Promise} The promise resolved when the message has been added to the DOM.
* @private
*/
Messages.prototype._addMessageToDom = function() {
Messages.prototype._addLastMessageToDom = function() {
// Call the web service to return how the message should look.
var promises = Ajax.call([{
methodname: 'core_message_data_for_messagearea_get_most_recent_message',
@ -639,13 +662,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust
// Add the message.
return promises[0].then(function(data) {
return Templates.render('core_message/message_area_message', data);
}).then(function(html, js) {
Templates.appendNodeContents(this.messageArea.find(SELECTORS.MESSAGES), html, js);
// Empty the response text area.
return this._addMessagesToDom([data], true);
}.bind(this)).always(function() {
// Empty the response text area.text
this.messageArea.find(SELECTORS.SENDMESSAGETEXT).val('').trigger('input');
// Scroll down.
this._scrollBottom();
}.bind(this)).fail(Notification.exception);
};