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/app/services/document-title.js
Rafael dos Santos Silva 5157e3b6e3
FIX: Favicon count was not updated when window focus returned (#10875)
This misses a test because Favcount doesn't exposes a get to the counter.

Also, since this code deals with all possible notifications configs we support:

- favicon notification
- favicon new content
- title notification
- title new content

the code is a bit complicated to follow. We may look into refactoring it when a
good opportunity arises, like if https://w3c.github.io/badging/ setClientBadge() method
gives us a cleaner way to notify users.
2020-10-09 10:51:39 +11:00

107 lines
2.5 KiB
JavaScript

import Service from "@ember/service";
import { inject as service } from "@ember/service";
import getURL from "discourse-common/lib/get-url";
export default Service.extend({
appEvents: service(),
contextCount: null,
notificationCount: null,
_title: null,
_backgroundNotify: null,
init() {
this._super(...arguments);
this.reset();
},
reset() {
this.contextCount = 0;
this.notificationCount = 0;
this._title = null;
this._backgroundNotify = null;
},
getTitle() {
return this._title;
},
setTitle(title) {
this._title = title;
this._renderTitle();
},
setFocus(focus) {
let { session } = this;
session.hasFocus = focus;
if (session.hasFocus && this._backgroundNotify) {
this.updateContextCount(0);
}
this._backgroundNotify = false;
if (session.hasFocus) {
this.notificationCount = 0;
}
this.appEvents.trigger("discourse:focus-changed", session.hasFocus);
this._renderFavicon();
this._renderTitle();
},
updateContextCount(count) {
this.contextCount = count;
this._renderTitle();
},
updateNotificationCount(count) {
if (!this.session.hasFocus) {
this.notificationCount = count;
this._renderFavicon();
this._renderTitle();
}
},
incrementBackgroundContextCount() {
if (!this.session.hasFocus) {
this._backgroundNotify = true;
this.contextCount += 1;
this._renderFavicon();
this._renderTitle();
}
},
_displayCount() {
return this.currentUser &&
this.currentUser.title_count_mode === "notifications"
? this.notificationCount
: this.contextCount;
},
_renderTitle() {
let title = this._title || this.siteSettings.title;
let displayCount = this._displayCount();
let dynamicFavicon = this.currentUser && this.currentUser.dynamic_favicon;
if (displayCount > 0 && !dynamicFavicon) {
title = `(${displayCount}) ${title}`;
}
document.title = title;
},
_renderFavicon() {
if (this.currentUser && this.currentUser.dynamic_favicon) {
let url = this.siteSettings.site_favicon_url;
// Since the favicon is cached on the browser for a really long time, we
// append the favicon_url as query params to the path so that the cache
// is not used when the favicon changes.
if (/^http/.test(url)) {
url = getURL("/favicon/proxied?" + encodeURIComponent(url));
}
new window.Favcount(url).set(this._displayCount());
}
},
});