REFACTOR: Move Page title / focus / counts logic to service
We had a handful of methods attached to the root `Discourse` object related to focus and notification counts. This patch pulls them out into a service called `document-title` for updating the title, and a component called `d-document` to attach and listen for browser events related to focus. It also removes some computed properties and observers in favor of plain old Javascript objects.
This commit is contained in:
@@ -10,7 +10,8 @@ moduleFor("controller:topic", "controller:topic", {
|
||||
needs: [
|
||||
"controller:composer",
|
||||
"controller:application",
|
||||
"service:app-events"
|
||||
"service:app-events",
|
||||
"service:document-title"
|
||||
],
|
||||
beforeEach() {
|
||||
this.registry.injection("controller", "appEvents", "service:app-events");
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
import { logIn, updateCurrentUser } from "helpers/qunit-helpers";
|
||||
|
||||
QUnit.module("lib:discourse");
|
||||
|
||||
QUnit.test("title counts are updated correctly", assert => {
|
||||
Discourse.set("hasFocus", true);
|
||||
Discourse.set("contextCount", 0);
|
||||
Discourse.set("notificationCount", 0);
|
||||
|
||||
Discourse.set("_docTitle", "Test Title");
|
||||
|
||||
assert.equal(document.title, "Test Title", "title is correct");
|
||||
|
||||
Discourse.updateNotificationCount(5);
|
||||
assert.equal(document.title, "Test Title", "title doesn't change with focus");
|
||||
|
||||
Discourse.incrementBackgroundContextCount();
|
||||
assert.equal(document.title, "Test Title", "title doesn't change with focus");
|
||||
|
||||
Discourse.set("hasFocus", false);
|
||||
|
||||
Discourse.updateNotificationCount(5);
|
||||
assert.equal(
|
||||
document.title,
|
||||
"Test Title",
|
||||
"notification count ignored for anon"
|
||||
);
|
||||
|
||||
Discourse.incrementBackgroundContextCount();
|
||||
assert.equal(
|
||||
document.title,
|
||||
"(1) Test Title",
|
||||
"title changes when incremented for anon"
|
||||
);
|
||||
|
||||
logIn();
|
||||
updateCurrentUser({ dynamic_favicon: false });
|
||||
|
||||
Discourse.set("hasFocus", true);
|
||||
Discourse.set("hasFocus", false);
|
||||
|
||||
Discourse.incrementBackgroundContextCount();
|
||||
assert.equal(
|
||||
document.title,
|
||||
"Test Title",
|
||||
"title doesn't change when incremented for logged in"
|
||||
);
|
||||
|
||||
Discourse.updateNotificationCount(3);
|
||||
assert.equal(
|
||||
document.title,
|
||||
"(3) Test Title",
|
||||
"title includes notification count for logged in user"
|
||||
);
|
||||
|
||||
Discourse.set("hasFocus", false);
|
||||
Discourse.set("hasFocus", true);
|
||||
|
||||
assert.equal(
|
||||
document.title,
|
||||
"Test Title",
|
||||
"counter dissappears after focus, and doesn't reappear until another notification arrives"
|
||||
);
|
||||
});
|
||||
@@ -1,76 +0,0 @@
|
||||
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
||||
import Session from "discourse/models/session";
|
||||
import ScreenTrack from "discourse/lib/screen-track";
|
||||
import pretender from "helpers/create-pretender";
|
||||
|
||||
let clock;
|
||||
|
||||
QUnit.module("lib:screen-track", {
|
||||
beforeEach() {
|
||||
clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime());
|
||||
},
|
||||
|
||||
afterEach() {
|
||||
clock.restore();
|
||||
}
|
||||
});
|
||||
|
||||
// skip for now test leaks state
|
||||
QUnit.skip("Correctly flushes posts as needed", assert => {
|
||||
const timings = [];
|
||||
|
||||
pretender.post("/topics/timings", t => {
|
||||
timings.push(t);
|
||||
return [200, {}, ""];
|
||||
});
|
||||
|
||||
const trackingState = TopicTrackingState.create();
|
||||
const siteSettings = {
|
||||
flush_timings_secs: 60
|
||||
};
|
||||
|
||||
const currentUser = { id: 1, username: "sam" };
|
||||
|
||||
const tracker = new ScreenTrack(
|
||||
trackingState,
|
||||
siteSettings,
|
||||
Session.current(),
|
||||
currentUser
|
||||
);
|
||||
|
||||
const topicController = null;
|
||||
|
||||
Discourse.set("hasFocus", true);
|
||||
|
||||
tracker.reset();
|
||||
tracker.start(1, topicController);
|
||||
|
||||
tracker.setOnscreen([1, 2, 3], [1, 2, 3]);
|
||||
|
||||
clock.tick(1050);
|
||||
clock.tick(1050);
|
||||
|
||||
// no ajax yet
|
||||
assert.equal(0, timings.length);
|
||||
|
||||
tracker.setOnscreen([1, 2, 3, 4], [1, 2, 3]);
|
||||
|
||||
clock.tick(1050);
|
||||
clock.tick(1050);
|
||||
|
||||
// we should be rushed now cause there is a new thing on the screen
|
||||
assert.equal(1, timings.length);
|
||||
|
||||
const req =
|
||||
"timings%5B1%5D=3000&timings%5B2%5D=3000&timings%5B3%5D=3000&timings%5B4%5D=1000&topic_time=3000&topic_id=1";
|
||||
assert.equal(timings[0].requestBody, req);
|
||||
|
||||
tracker.stop();
|
||||
|
||||
assert.equal(2, timings.length);
|
||||
|
||||
const req2 =
|
||||
"timings%5B1%5D=1200&timings%5B2%5D=1200&timings%5B3%5D=1200&timings%5B4%5D=1200&topic_time=1200&topic_id=1";
|
||||
|
||||
assert.equal(timings[1].requestBody, req2);
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
import { discourseModule } from "helpers/qunit-helpers";
|
||||
import { currentUser } from "helpers/qunit-helpers";
|
||||
|
||||
discourseModule("service:document-title", {
|
||||
beforeEach() {
|
||||
this.documentTitle = this.container.lookup("service:document-title");
|
||||
this.documentTitle.currentUser = null;
|
||||
this.container.lookup("session:main").hasFocus = true;
|
||||
},
|
||||
afterEach() {
|
||||
this.documentTitle.reset();
|
||||
}
|
||||
});
|
||||
|
||||
QUnit.test("it updates the document title", function(assert) {
|
||||
this.documentTitle.setTitle("Test Title");
|
||||
assert.equal(document.title, "Test Title", "title is correct");
|
||||
});
|
||||
|
||||
QUnit.test(
|
||||
"it doesn't display notification counts for anonymous users",
|
||||
function(assert) {
|
||||
this.documentTitle.setTitle("test notifications");
|
||||
this.documentTitle.updateNotificationCount(5);
|
||||
assert.equal(document.title, "test notifications");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.updateNotificationCount(6);
|
||||
assert.equal(document.title, "test notifications");
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test("it displays notification counts for logged in users", function(
|
||||
assert
|
||||
) {
|
||||
this.documentTitle.currentUser = currentUser();
|
||||
this.documentTitle.currentUser.dynamic_favicon = false;
|
||||
this.documentTitle.setTitle("test notifications");
|
||||
this.documentTitle.updateNotificationCount(5);
|
||||
assert.equal(document.title, "test notifications");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.updateNotificationCount(6);
|
||||
assert.equal(document.title, "(6) test notifications");
|
||||
this.documentTitle.setFocus(true);
|
||||
assert.equal(document.title, "test notifications");
|
||||
});
|
||||
|
||||
QUnit.test(
|
||||
"it doesn't increment background context counts when focused",
|
||||
function(assert) {
|
||||
this.documentTitle.setTitle("background context");
|
||||
this.documentTitle.setFocus(true);
|
||||
this.documentTitle.incrementBackgroundContextCount();
|
||||
assert.equal(document.title, "background context");
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test("it increments background context counts when not focused", function(
|
||||
assert
|
||||
) {
|
||||
this.documentTitle.setTitle("background context");
|
||||
this.documentTitle.setFocus(false);
|
||||
this.documentTitle.incrementBackgroundContextCount();
|
||||
assert.equal(document.title, "(1) background context");
|
||||
this.documentTitle.setFocus(true);
|
||||
assert.equal(document.title, "background context");
|
||||
});
|
||||
Reference in New Issue
Block a user