This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/header.js.es6
Sam Saffron 2042ed02ec BUGFIX: notifications cleared incorrectly
The notifications panel would reset itself
if you got a notification while it was open

New behavior, we refresh the panel live, blue notification
is cleared automatically if its open.

If window is not visible it will close the notifications panel,
that way you don't miss notifications by accident.
2014-06-10 11:44:49 +10:00

79 lines
2.0 KiB
JavaScript

/**
This controller supports actions on the site header
@class HeaderController
@extends Discourse.Controller
@namespace Discourse
@module Discourse
**/
export default Discourse.Controller.extend({
topic: null,
showExtraInfo: null,
notifications: null,
loading_notifications: null,
showStarButton: function() {
return Discourse.User.current() && !this.get('topic.isPrivateMessage');
}.property('topic.isPrivateMessage'),
resetCachedNotifications: function(){
// a bit hacky, but if we have no focus, hide notifications first
var visible = $("#notifications-dropdown").is(":visible");
if(!Discourse.get("hasFocus")) {
if(visible){
$("html").click();
}
this.set("notifications", null);
return;
}
if(visible){
this.refreshNotifications();
} else {
this.set("notifications", null);
}
}.observes("currentUser.lastNotificationChange"),
refreshNotifications: function(){
var self = this;
if(self.get("loading_notifications")){return;}
self.set("loading_notifications", true);
Discourse.ajax("/notifications").then(function(result) {
self.set('currentUser.unread_notifications', 0);
self.setProperties({
notifications: result,
loading_notifications: false
});
}, function(){
self.set("loading_notifications", false);
});
},
actions: {
toggleStar: function() {
var topic = this.get('topic');
if (topic) topic.toggleStar();
return false;
},
showNotifications: function(headerView) {
var self = this;
if (self.get('currentUser.unread_notifications') || self.get('currentUser.unread_private_messages') || !self.get('notifications')) {
self.refreshNotifications();
}
headerView.showDropdownBySelector("#user-notifications");
},
jumpToTopPost: function () {
var topic = this.get('topic');
if (topic) {
Discourse.URL.routeTo(topic.get('firstPostUrl'));
}
}
}
});