From f7f8c901f4bc39c3ed0a2bdfe1cbaa1ee6957999 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 1 Jun 2015 18:04:25 +0800 Subject: Marked as 0.2.9 --- public/vendor/idle.js | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100755 public/vendor/idle.js (limited to 'public/vendor/idle.js') diff --git a/public/vendor/idle.js b/public/vendor/idle.js new file mode 100755 index 00000000..4c10461e --- /dev/null +++ b/public/vendor/idle.js @@ -0,0 +1,160 @@ +(function() { + var Idle; + + /** IE8/old browser support **/ + if (!document.addEventListener) { + if (document.attachEvent) { + document.addEventListener = function(event, callback, useCapture) { + return document.attachEvent("on" + event, callback, useCapture); + }; + } else { + document.addEventListener = function() { + return {}; + }; + } + } + + if (!document.removeEventListener) { + if (document.detachEvent) { + document.removeEventListener = function(event, callback) { + return document.detachEvent("on" + event, callback); + }; + } else { + document.removeEventListener = function() { + return {}; + }; + } + } + + "use strict"; + + 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; + } + + Idle.prototype.onActive = function() { + this.awayTimestamp = new Date().getTime() + this.awayTimeout; + if (this.isAway) { + this.isAway = false; + if (this.onAwayBack) { + this.onAwayBack(); + } + this.start(); + } + return true; + }; + + Idle.prototype.start = function() { + var activity; + if (!this.listener) { + this.listener = (function() { + return activity.handleVisibilityChange(); + }); + document.addEventListener("visibilitychange", this.listener, false); + document.addEventListener("webkitvisibilitychange", this.listener, false); + document.addEventListener("msvisibilitychange", this.listener, false); + } + 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.stop = function() { + if (this.awayTimer !== null) { + clearTimeout(this.awayTimer); + } + if (this.listener !== null) { + document.removeEventListener("visibilitychange", this.listener); + document.removeEventListener("webkitvisibilitychange", this.listener); + document.removeEventListener("msvisibilitychange", this.listener); + this.listener = null; + } + 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); -- cgit v1.2.3