DEV: Remove `Discourse` constants from focus mixin.

Also removes the mixin which was only used in `app/app`
This commit is contained in:
Robin Ward 2020-05-06 10:35:15 -04:00
parent 10ca6968af
commit 7c2d3275f4
3 changed files with 37 additions and 48 deletions

View File

@ -1,45 +0,0 @@
import { bind } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner";
import Mixin from "@ember/object/mixin";
export default Mixin.create({
ready() {
this._super(...arguments);
this._onChangeHandler = bind(this, this._onChange);
// Default to true
Discourse.set("hasFocus", true);
document.addEventListener("visibilitychange", this._onChangeHandler);
document.addEventListener("resume", this._onChangeHandler);
document.addEventListener("freeze", this._onChangeHandler);
},
reset() {
this._super(...arguments);
document.removeEventListener("visibilitychange", this._onChangeHandler);
document.removeEventListener("resume", this._onChangeHandler);
document.removeEventListener("freeze", this._onChangeHandler);
this._onChangeHandler = null;
},
_onChange() {
const container = getOwner(this);
const appEvents = container.lookup("service:app-events");
if (document.visibilityState === "hidden") {
if (Discourse.hasFocus) {
Discourse.set("hasFocus", false);
appEvents.trigger("discourse:focus-changed", false);
}
} else {
if (!Discourse.hasFocus) {
Discourse.set("hasFocus", true);
appEvents.trigger("discourse:focus-changed", true);
}
}
}
});

View File

@ -2,15 +2,18 @@
import Application from "@ember/application";
import { computed } from "@ember/object";
import { buildResolver } from "discourse-common/resolver";
import { bind } from "@ember/runloop";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import FocusEvent from "discourse-common/mixins/focus-event";
const _pluginCallbacks = [];
const Discourse = Application.extend(FocusEvent, {
const Discourse = Application.extend({
rootElement: "#main",
_docTitle: document.title,
__widget_helpers: {},
hasFocus: null,
_boundFocusChange: null,
customEvents: {
paste: "paste"
},
@ -18,6 +21,24 @@ const Discourse = Application.extend(FocusEvent, {
reset() {
this._super(...arguments);
Mousetrap.reset();
document.removeEventListener("visibilitychange", this._boundFocusChange);
document.removeEventListener("resume", this._boundFocusChange);
document.removeEventListener("freeze", this._boundFocusChange);
this._boundFocusChange = null;
},
ready() {
this._super(...arguments);
this._boundFocusChange = bind(this, this._focusChanged);
// Default to true
this.set("hasFocus", true);
document.addEventListener("visibilitychange", this._boundFocusChange);
document.addEventListener("resume", this._boundFocusChange);
document.addEventListener("freeze", this._boundFocusChange);
},
getURL(url) {
@ -178,7 +199,19 @@ const Discourse = Application.extend(FocusEvent, {
}
return this.currentAssetVersion;
}
})
}),
_focusChanged() {
if (document.visibilityState === "hidden") {
if (this.hasFocus) {
this.set("hasFocus", false);
this.appEvents.trigger("discourse:focus-changed", false);
}
} else if (!this.hasFocus) {
this.set("hasFocus", true);
this.appEvents.trigger("discourse:focus-changed", true);
}
}
});
export default Discourse;

View File

@ -20,6 +20,7 @@ export default {
// backwards compatibility: remove when plugins have updated
app.register("store:main", Store);
app.appEvents = container.lookup("service:app-events");
if (!app.hasRegistration("service:store")) {
app.register("service:store", Store);