Skip to content

A platform for community discussion. Free, open, simple.

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

Jarek Radosz6c20d8cc8c7e
diff --git a/app/assets/javascripts/discourse/app/services/document-title.js b/app/assets/javascripts/discourse/app/services/document-title.js
index 6e2869c62b7..e8c5c5f0fe8 100644
--- a/app/assets/javascripts/discourse/app/services/document-title.js
+++ b/app/assets/javascripts/discourse/app/services/document-title.js
@@ -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,
-
- init() {
- this._super(...arguments);
- this.reset();
- },
-
- reset() {
- this.contextCount = 0;
- this.notificationCount = 0;
- this._title = null;
- this._backgroundNotify = null;
- },
+@disableImplicitInjections
+export default class DocumentElement extends Service {
+ @service appEvents;
+ @service currentUser;
+ @service session;
+ @service siteSettings;
+
+ 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());
}
- },
-});
+ }
+}
diff --git a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js
index d058c42569c..9ec75b35daa 100644
--- a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js
@@ -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");