discourse/app/assets/javascripts/discourse-common/mixins/focus-event.js.es6
Joffrey JAFFEUX bc8c77723e
FIX: puts focus-event at application level (#7568)
Also attempts to use simpler and newer APIs
2019-05-20 13:48:03 +02:00

37 lines
701 B
JavaScript

function gotFocus() {
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({
ready() {
this._super(...arguments);
onchange = () => {
document.visibilityState === "hidden" ? lostFocus() : gotFocus();
};
// Default to true
Discourse.set("hasFocus", true);
document.addEventListener("visibilitychange", onchange);
},
reset() {
this._super(...arguments);
document.removeEventListener("visibilitychange", onchange);
onchange = undefined;
}
});