Skip to content

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

DEV: Remove `Discourse` constants from focus mixin.

Robin Ward7c2d3275f4c4
diff --git a/app/assets/javascripts/discourse-common/addon/mixins/focus-event.js b/app/assets/javascripts/discourse-common/addon/mixins/focus-event.js
deleted file mode 100644
index 0688c90831e..00000000000
--- a/app/assets/javascripts/discourse-common/addon/mixins/focus-event.js
+++ /dev/null
@@ -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);
- }
- }
- }
-});
diff --git a/app/assets/javascripts/discourse/app/app.js b/app/assets/javascripts/discourse/app/app.js
index 23ea0b16675..b484afcf522 100644
--- a/app/assets/javascripts/discourse/app/app.js
+++ b/app/assets/javascripts/discourse/app/app.js
@@ -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;
diff --git a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
index 8140c5f1e88..30d1fb4e024 100644
--- a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
+++ b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
@@ -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);