2018-05-03 01:53:53 +08:00
|
|
|
import { log } from '/imports/ui/services/api';
|
|
|
|
|
2018-05-23 01:03:08 +08:00
|
|
|
const VISIBILITY_TIMEOUT = 5000;
|
|
|
|
|
2018-05-03 01:53:53 +08:00
|
|
|
export default class VisibilityEvent {
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
this._onVisible = null;
|
|
|
|
this._onHidden = null;
|
|
|
|
|
|
|
|
this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
|
|
|
|
|
|
|
|
this._registerVisibilityEvent();
|
2018-05-23 01:03:08 +08:00
|
|
|
|
|
|
|
this._onHiddenTimeout = null;
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onVisible (f) {
|
2018-05-23 01:03:08 +08:00
|
|
|
this._onVisible = () => {
|
|
|
|
if (!this._isHidden()) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
};
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onHidden(f) {
|
2018-05-23 01:03:08 +08:00
|
|
|
this._onHidden = () => {
|
|
|
|
if (this._isHidden()) {
|
|
|
|
f();
|
|
|
|
this._onHiddenTimeout = null;
|
|
|
|
}
|
|
|
|
};
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListeners() {
|
|
|
|
let event = this._getVisibilityEvent();
|
|
|
|
document.removeEventListener(event, this._handleVisibilityChange, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleVisibilityChange () {
|
|
|
|
|
|
|
|
if (!this._isHidden()) {
|
|
|
|
if (this._onVisible) {
|
2018-05-23 01:03:08 +08:00
|
|
|
if (this._onHiddenTimeout) {
|
|
|
|
clearTimeout(this._onHiddenTimeout);
|
|
|
|
this._onHiddenTimeout = null;
|
|
|
|
} else {
|
|
|
|
this._onVisible();
|
|
|
|
}
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this._onHidden) {
|
2018-05-23 01:03:08 +08:00
|
|
|
if (!this._onHiddenTimeout) {
|
|
|
|
this._onHiddenTimeout = setTimeout(this._onHidden.bind(this), VISIBILITY_TIMEOUT);
|
|
|
|
}
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getHiddenProp () {
|
|
|
|
let prefixes = ['webkit', 'moz', 'ms', 'o'];
|
|
|
|
|
|
|
|
// if 'hidden' is natively supported just return it
|
|
|
|
if ('hidden' in document) return 'hidden';
|
|
|
|
|
|
|
|
// otherwise loop over all the known prefixes unti we find one
|
|
|
|
for (var i = 0; i < prefixes.length; i++) {
|
|
|
|
if ((prefixes[i] + 'Hidden') in document) {
|
|
|
|
return prefixes[i] + 'Hidden';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise it's not supported
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isHidden () {
|
|
|
|
var prop = this._getHiddenProp();
|
|
|
|
|
|
|
|
if (!prop) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return document[prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
_getVisibilityEvent() {
|
|
|
|
let hidden, visibilityChange;
|
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
|
|
visibilityChange = "visibilitychange";
|
|
|
|
} else if (typeof document.msHidden !== "undefined") {
|
|
|
|
visibilityChange = "msvisibilitychange";
|
|
|
|
} else if (typeof document.webkitHidden !== "undefined") {
|
|
|
|
visibilityChange = "webkitvisibilitychange";
|
|
|
|
}
|
|
|
|
|
|
|
|
return visibilityChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
_registerVisibilityEvent () {
|
|
|
|
let event = this._getVisibilityEvent();
|
|
|
|
document.addEventListener(event, this._handleVisibilityChange, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|