mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
127 lines
3 KiB
JavaScript
127 lines
3 KiB
JavaScript
|
// https://github.com/shawnmclean/Idle.js
|
||
|
(function() {
|
||
|
"use strict";
|
||
|
var Idle;
|
||
|
|
||
|
Idle = {};
|
||
|
|
||
|
Idle = (function() {
|
||
|
Idle.isAway = false;
|
||
|
|
||
|
Idle.awayTimeout = 3000;
|
||
|
|
||
|
Idle.awayTimestamp = 0;
|
||
|
|
||
|
Idle.awayTimer = null;
|
||
|
|
||
|
Idle.onAway = null;
|
||
|
|
||
|
Idle.onAwayBack = null;
|
||
|
|
||
|
Idle.onVisible = null;
|
||
|
|
||
|
Idle.onHidden = null;
|
||
|
|
||
|
function Idle(options) {
|
||
|
var activeMethod, activity;
|
||
|
|
||
|
if (options) {
|
||
|
this.awayTimeout = parseInt(options.awayTimeout, 10);
|
||
|
this.onAway = options.onAway;
|
||
|
this.onAwayBack = options.onAwayBack;
|
||
|
this.onVisible = options.onVisible;
|
||
|
this.onHidden = options.onHidden;
|
||
|
}
|
||
|
activity = this;
|
||
|
activeMethod = function() {
|
||
|
return activity.onActive();
|
||
|
};
|
||
|
window.onclick = activeMethod;
|
||
|
window.onmousemove = activeMethod;
|
||
|
window.onmouseenter = activeMethod;
|
||
|
window.onkeydown = activeMethod;
|
||
|
window.onscroll = activeMethod;
|
||
|
window.onmousewheel = activeMethod;
|
||
|
document.addEventListener("visibilitychange", (function() {
|
||
|
return activity.handleVisibilityChange();
|
||
|
}), false);
|
||
|
document.addEventListener("webkitvisibilitychange", (function() {
|
||
|
return activity.handleVisibilityChange();
|
||
|
}), false);
|
||
|
document.addEventListener("msvisibilitychange", (function() {
|
||
|
return activity.handleVisibilityChange();
|
||
|
}), false);
|
||
|
}
|
||
|
|
||
|
Idle.prototype.onActive = function() {
|
||
|
this.awayTimestamp = new Date().getTime() + this.awayTimeout;
|
||
|
if (this.isAway) {
|
||
|
if (this.onAwayBack) {
|
||
|
this.onAwayBack();
|
||
|
}
|
||
|
this.start();
|
||
|
}
|
||
|
this.isAway = false;
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
Idle.prototype.start = function() {
|
||
|
var activity;
|
||
|
|
||
|
this.awayTimestamp = new Date().getTime() + this.awayTimeout;
|
||
|
if (this.awayTimer !== null) {
|
||
|
clearTimeout(this.awayTimer);
|
||
|
}
|
||
|
activity = this;
|
||
|
this.awayTimer = setTimeout((function() {
|
||
|
return activity.checkAway();
|
||
|
}), this.awayTimeout + 100);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
Idle.prototype.setAwayTimeout = function(ms) {
|
||
|
this.awayTimeout = parseInt(ms, 10);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
Idle.prototype.checkAway = function() {
|
||
|
var activity, t;
|
||
|
|
||
|
t = new Date().getTime();
|
||
|
if (t < this.awayTimestamp) {
|
||
|
this.isAway = false;
|
||
|
activity = this;
|
||
|
this.awayTimer = setTimeout((function() {
|
||
|
return activity.checkAway();
|
||
|
}), this.awayTimestamp - t + 100);
|
||
|
return;
|
||
|
}
|
||
|
if (this.awayTimer !== null) {
|
||
|
clearTimeout(this.awayTimer);
|
||
|
}
|
||
|
this.isAway = true;
|
||
|
if (this.onAway) {
|
||
|
return this.onAway();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Idle.prototype.handleVisibilityChange = function() {
|
||
|
if (document.hidden || document.msHidden || document.webkitHidden) {
|
||
|
if (this.onHidden) {
|
||
|
return this.onHidden();
|
||
|
}
|
||
|
} else {
|
||
|
if (this.onVisible) {
|
||
|
return this.onVisible();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return Idle;
|
||
|
|
||
|
})();
|
||
|
|
||
|
window.Idle = Idle;
|
||
|
|
||
|
}).call(this);
|