DEV: adds discourse:focus-changed app event (#8123)

This commit is contained in:
Joffrey JAFFEUX 2019-10-02 02:53:51 +02:00 committed by Sam
parent f331b5eab2
commit 6e815ba032
2 changed files with 30 additions and 26 deletions

View File

@ -1,40 +1,43 @@
function gotFocus() { import { getOwner } from "discourse-common/lib/get-owner";
if (!Discourse.get("hasFocus")) {
Discourse.setProperties({ hasFocus: true, notify: false });
}
}
function lostFocus() {
if (Discourse.get("hasFocus")) {
Discourse.set("hasFocus", false);
}
}
let onchange;
export default Ember.Mixin.create({ export default Ember.Mixin.create({
ready() { ready() {
this._super(...arguments); this._super(...arguments);
onchange = () => { this._onChangeHandler = Ember.run.bind(this, this._onChange);
document.visibilityState === "hidden" ? lostFocus() : gotFocus();
};
// Default to true // Default to true
Discourse.set("hasFocus", true); Discourse.set("hasFocus", true);
document.addEventListener("visibilitychange", onchange); document.addEventListener("visibilitychange", this._onChangeHandler);
document.addEventListener("resume", onchange); document.addEventListener("resume", this._onChangeHandler);
document.addEventListener("freeze", onchange); document.addEventListener("freeze", this._onChangeHandler);
}, },
reset() { reset() {
this._super(...arguments); this._super(...arguments);
document.removeEventListener("visibilitychange", onchange); document.removeEventListener("visibilitychange", this._onChangeHandler);
document.removeEventListener("resume", onchange); document.removeEventListener("resume", this._onChangeHandler);
document.removeEventListener("freeze", onchange); document.removeEventListener("freeze", this._onChangeHandler);
onchange = undefined; this._onChangeHandler = null;
},
_onChange() {
const container = getOwner(this);
const appEvents = container.lookup("app-events:main");
if (document.visibilityState === "hidden") {
if (Discourse.hasFocus) {
Discourse.set("hasFocus", false);
appEvents.trigger("discourse:focus-changed", false);
}
} else {
if (!Discourse.hasFocus) {
Discourse.setProperties({ hasFocus: true, notify: false });
appEvents.trigger("discourse:focus-changed", true);
}
}
} }
}); });

View File

@ -110,6 +110,7 @@ export default Ember.Component.extend(
} }
); );
this.appEvents.on("discourse:focus-changed", this, "gotFocus");
this.appEvents.on("post:highlight", this, "_highlightPost"); this.appEvents.on("post:highlight", this, "_highlightPost");
this.appEvents.on("header:update-topic", this, "_updateTopic"); this.appEvents.on("header:update-topic", this, "_updateTopic");
}, },
@ -129,13 +130,13 @@ export default Ember.Component.extend(
// this happens after route exit, stuff could have trickled in // this happens after route exit, stuff could have trickled in
this._hideTopicInHeader(); this._hideTopicInHeader();
this.appEvents.off("discourse:focus-changed", this, "gotFocus");
this.appEvents.off("post:highlight", this, "_highlightPost"); this.appEvents.off("post:highlight", this, "_highlightPost");
this.appEvents.off("header:update-topic", this, "_updateTopic"); this.appEvents.off("header:update-topic", this, "_updateTopic");
}, },
@observes("Discourse.hasFocus") gotFocus(hasFocus) {
gotFocus() { if (hasFocus) {
if (Discourse.get("hasFocus")) {
this.scrolled(); this.scrolled();
} }
}, },