MDL-57968 core_message: Remove multiple unnecessary AJAX requests

Remove AJAX requests core_message_get_unread_conversations_count and
message_popup_get_unread_popup_notification_count and
render the unread messages and notifications into mustache template.
This commit is contained in:
cescobedo 2018-06-06 01:02:00 +02:00
parent 5b4ca9eb5b
commit 84e03ed859
7 changed files with 12 additions and 38 deletions

View File

@ -1 +1 @@
define(["jquery","core/ajax","core/templates","core/str","core/notification","core/custom_interaction_events","core/popover_region_controller","core_message/message_repository","core/url"],function(a,b,c,d,e,f,g,h,i){var j={MARK_ALL_READ_BUTTON:'[data-action="mark-all-read"]',CONTENT:'[data-region="messages"]',CONTENT_ITEM_CONTAINER:'[data-region="message-content-item-container"]',EMPTY_MESSAGE:'[data-region="empty-message"]',COUNT_CONTAINER:'[data-region="count-container"]'},k=function(a){g.call(this,a),this.markAllReadButton=this.root.find(j.MARK_ALL_READ_BUTTON),this.content=this.root.find(j.CONTENT),this.userId=this.root.attr("data-userid"),this.limit=20,this.offset=0,this.loadedAll=!1,this.initialLoad=!1,this.loadUnreadMessageCount()};return k.prototype=Object.create(g.prototype),k.prototype.constructor=k,k.prototype.getContent=function(){return this.content},k.prototype.incrementOffset=function(){this.offset+=this.limit},k.prototype.updateButtonAriaLabel=function(){this.isMenuOpen()?d.get_string("hidemessagewindow","message").done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this)):this.unreadCount?d.get_string("showmessagewindowwithcount","message",this.unreadCount).done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this)):d.get_string("showmessagewindownonew","message").done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this))},k.prototype.renderUnreadCount=function(){var a=this.root.find(j.COUNT_CONTAINER);this.unreadCount?(a.text(this.unreadCount),a.removeClass("hidden")):a.addClass("hidden")},k.prototype.hideUnreadCount=function(){this.root.find(j.COUNT_CONTAINER).addClass("hidden")},k.prototype.loadUnreadMessageCount=function(){h.countUnreadConversations({useridto:this.userId}).then(function(a){this.unreadCount=a,this.renderUnreadCount(),this.updateButtonAriaLabel()}.bind(this))["catch"](e.exception)},k.prototype.renderMessages=function(b,d){var e=[];return a.each(b,function(a,b){b.contexturl=i.relativeUrl("/message/index.php",{user:this.userId,id:b.userid}),b.profileurl=i.relativeUrl("/user/profile.php",{id:b.userid});var d=c.render("message_popup/message_content_item",b).then(function(a,b){return{html:a,js:b}});e.push(d)}.bind(this)),a.when.apply(a,e).then(function(){a.each(arguments,function(a,b){d.append(b.html),c.runTemplateJS(b.js)})})},k.prototype.loadMoreMessages=function(){if(this.isLoading||this.loadedAll)return a.Deferred().resolve();this.startLoading();var b={userid:this.userId,limit:this.limit,offset:this.offset},c=this.getContent();return h.query(b).then(function(a){var b=a.contacts;return this.loadedAll=!b.length||b.length<this.limit,this.initialLoad=!0,this.updateButtonAriaLabel(),!!b.length&&(this.incrementOffset(),this.renderMessages(b,c))}.bind(this)).always(function(){this.stopLoading()}.bind(this))},k.prototype.markAllAsRead=function(){return this.markAllReadButton.hasClass("loading")?a.Deferred().resolve():(this.markAllReadButton.addClass("loading"),h.markAllAsRead({useridto:this.userId}).then(function(){this.unreadCount=0,this.hideUnreadCount(),this.getContent().find(j.CONTENT_ITEM_CONTAINER).removeClass("unread")}.bind(this)).always(function(){this.markAllReadButton.removeClass("loading")}.bind(this)))},k.prototype.registerEventListeners=function(){f.define(this.root,[f.events.keyboardActivate]),this.root.on(this.events().menuOpened,function(){this.hideUnreadCount(),this.updateButtonAriaLabel(),this.initialLoad||this.loadMoreMessages()}.bind(this)),this.root.on(this.events().menuClosed,function(){this.renderUnreadCount(),this.updateButtonAriaLabel()}.bind(this)),this.root.on(f.events.scrollBottom,function(){this.loadMoreMessages()}.bind(this)),this.root.on(f.events.activate,j.MARK_ALL_READ_BUTTON,function(a,b){this.markAllAsRead(),a.stopPropagation(),b.originalEvent.preventDefault()}.bind(this)),f.define(this.getContentContainer(),[f.events.scrollLock]),a(document).on("messagearea:conversationselected",function(){this.unreadCount--,this.renderUnreadCount()}.bind(this))},k});
define(["jquery","core/ajax","core/templates","core/str","core/notification","core/custom_interaction_events","core/popover_region_controller","core_message/message_repository","core/url"],function(a,b,c,d,e,f,g,h,i){var j={MARK_ALL_READ_BUTTON:'[data-action="mark-all-read"]',CONTENT:'[data-region="messages"]',CONTENT_ITEM_CONTAINER:'[data-region="message-content-item-container"]',EMPTY_MESSAGE:'[data-region="empty-message"]',COUNT_CONTAINER:'[data-region="count-container"]'},k=function(a){g.call(this,a),this.markAllReadButton=this.root.find(j.MARK_ALL_READ_BUTTON),this.content=this.root.find(j.CONTENT),this.userId=this.root.attr("data-userid"),this.limit=20,this.offset=0,this.loadedAll=!1,this.initialLoad=!1,this.unreadCount=this.root.find(j.COUNT_CONTAINER).html()};return k.prototype=Object.create(g.prototype),k.prototype.constructor=k,k.prototype.getContent=function(){return this.content},k.prototype.incrementOffset=function(){this.offset+=this.limit},k.prototype.updateButtonAriaLabel=function(){this.isMenuOpen()?d.get_string("hidemessagewindow","message").done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this)):this.unreadCount?d.get_string("showmessagewindowwithcount","message",this.unreadCount).done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this)):d.get_string("showmessagewindownonew","message").done(function(a){this.menuToggle.attr("aria-label",a)}.bind(this))},k.prototype.renderUnreadCount=function(){var a=this.root.find(j.COUNT_CONTAINER);this.unreadCount?(a.text(this.unreadCount),a.removeClass("hidden")):a.addClass("hidden")},k.prototype.hideUnreadCount=function(){this.root.find(j.COUNT_CONTAINER).addClass("hidden")},k.prototype.renderMessages=function(b,d){var e=[];return a.each(b,function(a,b){b.contexturl=i.relativeUrl("/message/index.php",{user:this.userId,id:b.userid}),b.profileurl=i.relativeUrl("/user/profile.php",{id:b.userid});var d=c.render("message_popup/message_content_item",b).then(function(a,b){return{html:a,js:b}});e.push(d)}.bind(this)),a.when.apply(a,e).then(function(){a.each(arguments,function(a,b){d.append(b.html),c.runTemplateJS(b.js)})})},k.prototype.loadMoreMessages=function(){if(this.isLoading||this.loadedAll)return a.Deferred().resolve();this.startLoading();var b={userid:this.userId,limit:this.limit,offset:this.offset},c=this.getContent();return h.query(b).then(function(a){var b=a.contacts;return this.loadedAll=!b.length||b.length<this.limit,this.initialLoad=!0,this.updateButtonAriaLabel(),!!b.length&&(this.incrementOffset(),this.renderMessages(b,c))}.bind(this)).always(function(){this.stopLoading()}.bind(this))},k.prototype.markAllAsRead=function(){return this.markAllReadButton.hasClass("loading")?a.Deferred().resolve():(this.markAllReadButton.addClass("loading"),h.markAllAsRead({useridto:this.userId}).then(function(){this.unreadCount=0,this.hideUnreadCount(),this.getContent().find(j.CONTENT_ITEM_CONTAINER).removeClass("unread")}.bind(this)).always(function(){this.markAllReadButton.removeClass("loading")}.bind(this)))},k.prototype.registerEventListeners=function(){f.define(this.root,[f.events.keyboardActivate]),this.root.on(this.events().menuOpened,function(){this.hideUnreadCount(),this.updateButtonAriaLabel(),this.initialLoad||this.loadMoreMessages()}.bind(this)),this.root.on(this.events().menuClosed,function(){this.renderUnreadCount(),this.updateButtonAriaLabel()}.bind(this)),this.root.on(f.events.scrollBottom,function(){this.loadMoreMessages()}.bind(this)),this.root.on(f.events.activate,j.MARK_ALL_READ_BUTTON,function(a,b){this.markAllAsRead(),a.stopPropagation(),b.originalEvent.preventDefault()}.bind(this)),f.define(this.getContentContainer(),[f.events.scrollLock]),a(document).on("messagearea:conversationselected",function(){this.unreadCount--,this.renderUnreadCount()}.bind(this))},k});

File diff suppressed because one or more lines are too long

View File

@ -57,7 +57,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str',
this.initialLoad = false;
// Let's find out how many unread messages there are.
this.loadUnreadMessageCount();
this.unreadCount = this.root.find(SELECTORS.COUNT_CONTAINER).html();
};
/**
@ -139,21 +139,6 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str',
this.root.find(SELECTORS.COUNT_CONTAINER).addClass('hidden');
};
/**
* 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.
*
* @method loadUnreadMessageCount
*/
MessagePopoverController.prototype.loadUnreadMessageCount = function() {
MessageRepo.countUnreadConversations({useridto: this.userId}).then(function(count) {
this.unreadCount = count;
this.renderUnreadCount();
this.updateButtonAriaLabel();
}.bind(this)).catch(Notification.exception);
};
/**
* Render the message data with the appropriate template and add it to the DOM.
*

View File

@ -60,7 +60,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
this.initialLoad = false;
// Let's find out how many unread notifications there are.
this.loadUnreadNotificationCount();
this.unreadCount = this.root.find(SELECTORS.COUNT_CONTAINER).html();
};
/**
@ -188,21 +188,6 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
this.root.find(SELECTORS.COUNT_CONTAINER).addClass('hidden');
};
/**
* Ask the server how many unread notifications are left, render the value
* as a badge on the menu toggle and update the aria labels on the menu
* toggle.
*
* @method loadUnreadNotificationCount
*/
NotificationPopoverController.prototype.loadUnreadNotificationCount = function() {
NotificationRepo.countUnread({useridto: this.userId}).then(function(count) {
this.unreadCount = count;
this.renderUnreadCount();
this.updateButtonAriaLabel();
}.bind(this)).catch(DebugNotification.exception);
};
/**
* Find the notification element for the given id.
*

View File

@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
/**
* Contains standard functions for message_popup.
*
@ -24,6 +22,8 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Renders the popup.
*
@ -45,8 +45,10 @@ function message_popup_render_navbar_output(\renderer_base $renderer) {
// Add the messages popover.
if (!empty($CFG->messaging)) {
$unreadcount = \core_message\api::count_unread_conversations($USER);
$context = [
'userid' => $USER->id,
'unreadcount' => $unreadcount,
'urls' => [
'seeall' => (new moodle_url('/message/index.php'))->out(),
'writeamessage' => (new moodle_url('/message/index.php', ['contactsfirst' => 1]))->out(),
@ -59,8 +61,10 @@ function message_popup_render_navbar_output(\renderer_base $renderer) {
// Add the notifications popover.
$enabled = \core_message\api::is_processor_enabled("popup");
if ($enabled) {
$unreadcount = \message_popup\api::count_unread_popup_notifications($USER->id);
$context = [
'userid' => $USER->id,
'unreadcount' => $unreadcount,
'urls' => [
'seeall' => (new moodle_url('/message/output/popup/notifications.php'))->out(),
'preferences' => (new moodle_url('/message/notificationpreferences.php', ['userid' => $USER->id]))->out(),

View File

@ -45,7 +45,7 @@
{{$togglelabel}}{{#str}} showmessagewindownonew, message {{/str}}{{/togglelabel}}
{{$togglecontent}}
{{#pix}} t/message, core, {{#str}} togglemessagemenu, message {{/str}} {{/pix}}
<div class="count-container hidden" data-region="count-container"></div>
<div class="count-container {{^unreadcount}}hidden{{/unreadcount}}" data-region="count-container">{{unreadcount}}</div>
{{/togglecontent}}
{{$containerlabel}}{{#str}} notificationwindow, message {{/str}}{{/containerlabel}}

View File

@ -45,7 +45,7 @@
{{$togglelabel}}{{#str}} shownotificationwindownonew, message {{/str}}{{/togglelabel}}
{{$togglecontent}}
{{#pix}} i/notifications, core, {{#str}} togglenotificationmenu, message {{/str}} {{/pix}}
<div class="count-container hidden" data-region="count-container"></div>
<div class="count-container {{^unreadcount}}hidden{{/unreadcount}}" data-region="count-container">{{unreadcount}}</div>
{{/togglecontent}}
{{$containerlabel}}{{#str}} notificationwindow, message {{/str}}{{/containerlabel}}