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

View File

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