DEV: Convert document-title to a native class (#23570)

Included: removed the `reset` method, used private fields, added explicit service injections
This commit is contained in:
Jarek Radosz 2023-09-13 22:12:33 +02:00 committed by GitHub
parent 355aba50cf
commit 6c20d8cc8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 43 deletions

View File

@ -1,45 +1,38 @@
import Service, { inject as service } from "@ember/service"; import Service, { inject as service } from "@ember/service";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import updateTabCount from "discourse/lib/update-tab-count"; import updateTabCount from "discourse/lib/update-tab-count";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
export default Service.extend({ @disableImplicitInjections
appEvents: service(), export default class DocumentElement extends Service {
currentUser: service(), @service appEvents;
contextCount: null, @service currentUser;
notificationCount: null, @service session;
_title: null, @service siteSettings;
_backgroundNotify: null,
init() { contextCount = 0;
this._super(...arguments); notificationCount = 0;
this.reset(); #title = null;
}, #backgroundNotify = null;
reset() {
this.contextCount = 0;
this.notificationCount = 0;
this._title = null;
this._backgroundNotify = null;
},
getTitle() { getTitle() {
return this._title; return this.#title;
}, }
setTitle(title) { setTitle(title) {
this._title = title; this.#title = title;
this._renderTitle(); this._renderTitle();
}, }
setFocus(focus) { setFocus(focus) {
let { session } = this; let { session } = this;
session.hasFocus = focus; session.hasFocus = focus;
if (session.hasFocus && this._backgroundNotify) { if (session.hasFocus && this.#backgroundNotify) {
this.updateContextCount(0); this.updateContextCount(0);
} }
this._backgroundNotify = false; this.#backgroundNotify = false;
if (session.hasFocus) { if (session.hasFocus) {
this.notificationCount = 0; this.notificationCount = 0;
@ -47,12 +40,12 @@ export default Service.extend({
this.appEvents.trigger("discourse:focus-changed", session.hasFocus); this.appEvents.trigger("discourse:focus-changed", session.hasFocus);
this._renderFavicon(); this._renderFavicon();
this._renderTitle(); this._renderTitle();
}, }
updateContextCount(count) { updateContextCount(count) {
this.contextCount = count; this.contextCount = count;
this._renderTitle(); this._renderTitle();
}, }
updateNotificationCount(count, { forced = false } = {}) { updateNotificationCount(count, { forced = false } = {}) {
if (!this.session.hasFocus || forced) { if (!this.session.hasFocus || forced) {
@ -60,26 +53,25 @@ export default Service.extend({
this._renderFavicon(); this._renderFavicon();
this._renderTitle(); this._renderTitle();
} }
}, }
incrementBackgroundContextCount() { incrementBackgroundContextCount() {
if (!this.session.hasFocus) { if (!this.session.hasFocus) {
this._backgroundNotify = true; this.#backgroundNotify = true;
this.contextCount += 1; this.contextCount += 1;
this._renderFavicon(); this._renderFavicon();
this._renderTitle(); this._renderTitle();
} }
}, }
_displayCount() { _displayCount() {
return this.currentUser && return this.currentUser?.user_option.title_count_mode === "notifications"
this.currentUser.user_option.title_count_mode === "notifications"
? this.notificationCount ? this.notificationCount
: this.contextCount; : this.contextCount;
}, }
_renderTitle() { _renderTitle() {
let title = this._title || this.siteSettings.title; let title = this.#title || this.siteSettings.title;
let displayCount = this._displayCount(); let displayCount = this._displayCount();
let dynamicFavicon = this.currentUser?.user_option.dynamic_favicon; let dynamicFavicon = this.currentUser?.user_option.dynamic_favicon;
@ -94,7 +86,7 @@ export default Service.extend({
} }
document.title = title; document.title = title;
}, }
_renderFavicon() { _renderFavicon() {
if (this.currentUser?.user_option.dynamic_favicon) { if (this.currentUser?.user_option.dynamic_favicon) {
@ -109,5 +101,5 @@ export default Service.extend({
updateTabCount(url, this._displayCount()); updateTabCount(url, this._displayCount());
} }
}, }
}); }

View File

@ -8,16 +8,10 @@ module("Unit | Service | document-title", function (hooks) {
setupTest(hooks); setupTest(hooks);
hooks.beforeEach(function () { hooks.beforeEach(function () {
const session = Session.current(); Session.current().hasFocus = true;
session.hasFocus = true;
this.documentTitle = getOwner(this).lookup("service:document-title"); this.documentTitle = getOwner(this).lookup("service:document-title");
}); });
hooks.afterEach(function () {
this.documentTitle.reset();
});
test("it updates the document title", function (assert) { test("it updates the document title", function (assert) {
this.documentTitle.setTitle("Test Title"); this.documentTitle.setTitle("Test Title");
assert.strictEqual(document.title, "Test Title", "title is correct"); assert.strictEqual(document.title, "Test Title", "title is correct");