From 10d38e66b1639e96477bc897c2f6cb046f44b58a Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Sep 2016 13:49:21 +0800 Subject: [PATCH] MDL-55895 core_message: do not display whole conversation on page load --- message/amd/src/message_area_messages.js | 40 ++++++++++++++---------- message/index.php | 3 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/message/amd/src/message_area_messages.js b/message/amd/src/message_area_messages.js index 9ba02cefcc7..dc988e83a71 100644 --- a/message/amd/src/message_area_messages.js +++ b/message/amd/src/message_area_messages.js @@ -96,8 +96,11 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust $(document).on(AutoRows.events.ROW_CHANGE, this._adjustMessagesAreaHeight.bind(this)); - // Scroll to the bottom of the messages when first initialised. - this._scrollBottom(); + // Check if any messages have been displayed on page load. + var messages = this.messageArea.find(this.messageArea.SELECTORS.MESSAGES); + if (messages.length) { + this._addScrollEventListener(messages.find(this.messageArea.SELECTORS.MESSAGE).length); + } }; /** @@ -143,19 +146,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust return templates.render('core_message/message_area_messages_area', data); }).then(function(html, js) { templates.replaceNodeContents(this.messageArea.find(this.messageArea.SELECTORS.MESSAGESAREA), html, js); - // Scroll to the bottom. - this._scrollBottom(); - // Only increment if data was returned. - if (numberreceived > 0) { - // Set the number of messages displayed. - this._numMessagesDisplayed = numberreceived; - } - // Now enable the ability to infinitely scroll through messages. - customEvents.define(this.messageArea.find(this.messageArea.SELECTORS.MESSAGES), [ - customEvents.events.scrollTop - ]); - // Assign the event for scrolling. - this.messageArea.onCustomEvent(customEvents.events.scrollTop, this._loadMessages.bind(this)); + this._addScrollEventListener(numberreceived); }.bind(this)).fail(notification.exception); }; @@ -370,8 +361,25 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust // Hide the items responsible for deleting messages. this._hideDeleteAction(); + }; - + /** + * Handles adding a scrolling event listener. + * + * @params {int} The number of messages received + * @private + */ + Messages.prototype._addScrollEventListener = function(numberreceived) { + // Scroll to the bottom. + this._scrollBottom(); + // Set the number of messages displayed. + this._numMessagesDisplayed = numberreceived; + // Now enable the ability to infinitely scroll through messages. + customEvents.define(this.messageArea.find(this.messageArea.SELECTORS.MESSAGES), [ + customEvents.events.scrollTop + ]); + // Assign the event for scrolling. + this.messageArea.onCustomEvent(customEvents.events.scrollTop, this._loadMessages.bind(this)); }; /** diff --git a/message/index.php b/message/index.php index 3c3262045eb..f77847272d6 100644 --- a/message/index.php +++ b/message/index.php @@ -252,13 +252,14 @@ if (!$user2realuser) { $contact = reset($contacts); $otheruserid = $contact->userid; $conversations->otheruserid = $otheruserid; - $messages = \core_message\api::get_messages($user1->id, $otheruserid); // Mark the conversation as read. if ($currentuser) { $contact->isread = 1; message_mark_all_read_for_user($user1->id, $otheruserid); } + + $messages = \core_message\api::get_messages($user1->id, $otheruserid, 0, 20, 'timecreated DESC'); } else { $messages = null; }