IdleTimer =
new
Class({
Implements: [Events, Options],
options: {
timeout: 60000,
events: [
'mousemove'
,
'keydown'
,
'mousewheel'
,
'mousedown'
,
'touchstart'
,
'touchmove'
]
},
initialize:
function
(element, options) {
this
.setOptions(options);
this
.element = document.id(element);
this
.activeBound =
this
.active.bind(
this
);
this
.isIdle =
false
;
this
.started =
false
;
this
.lastPos =
false
;
},
stop:
function
() {
clearTimeout(
this
.timer);
for
(
var
i = 0; i <
this
.options.events.length; i++) {
this
.element.removeEvent(
this
.options.events[i],
this
.activeBound);
}
this
.bound =
false
;
this
.started =
false
;
this
.lastPos =
false
;
this
.fireEvent(
'stop'
);
return
this
;
},
active:
function
(e) {
if
(e.event.type ==
'mousemove'
)
{
var
pos = [e.event.clientX, e.event.clientY];
if
(
this
.lastPos ===
false
||
(
this
.lastPos[0] != pos[0] &&
this
.lastPos[1] != pos[1]))
this
.lastPos = pos;
else
return
;
}
clearTimeout(
this
.timer);
if
(
this
.isIdle)
this
.fireEvent(
'active'
);
this
.isIdle =
false
;
this
.start();
},
idle:
function
() {
if
(
this
.timer) clearTimeout(
this
.timer);
this
.isIdle =
true
;
this
.fireEvent(
'idle'
);
},
start:
function
() {
if
(
this
.timer) clearTimeout(
this
.timer);
this
.timer =
this
.idle.delay(
this
.options.timeout,
this
);
this
.lastActive = Date.now();
if
(!
this
.bound)
this
.bind();
this
.started =
true
;
return
this
;
},
bind:
function
() {
for
(
var
i = 0; i <
this
.options.events.length; i++) {
this
.element.addEvent(
this
.options.events[i],
this
.activeBound);
}
this
.bound =
true
;
this
.fireEvent(
'start'
);
},
getIdleTime:
function
(returnSeconds) {
return
returnSeconds ? Math.round((Date.now() -
this
.lastActive) / 1000) : Date.now() -
this
.lastActive;
},
setTimeout:
function
(newTime, whenActive) {
var
old =
this
.options.timeout;
this
.options.timeout = newTime;
if
(whenActive)
return
this
;
clearTimeout(
this
.timer);
this
.fireEvent(
'timeoutChanged'
, newTime);
var
elapsed =
this
.getIdleTime();
if
(elapsed < newTime &&
this
.isIdle) {
this
.fireEvent(
'active'
);
this
.isIdle =
false
;
}
else
if
(elapsed >= newTime) {
this
.idle();
return
this
;
}
this
.timer =
this
.idle.delay(newTime - elapsed,
this
);
return
this
;
}
});
Element.Properties.idle = {
set:
function
(options) {
var
idle =
this
.retrieve('idle
');
if (idle) idle.stop();
return this.eliminate('
idle
').store('
idle:options
', options);
},
get: function(options) {
if (options || !this.retrieve('
idle
')) {
if (options || !this.retrieve('
idle:options
')) this.set('
idle
', options);
this.store('
idle
', new IdleTimer(this, this.retrieve('
idle:options
')));
}
return this.retrieve('
idle
');
}
};
Element.Events.idle = {
onAdd: function(fn) {
var global = this.get ? false : true;
var idler = global ? window.idleTimer : this.get('
idle
');
if(global && !idler) { idler = window.idleTimer = new IdleTimer(Browser.ie ? document : this); }
if(!idler.started) idler.start();
idler.addEvent('
idle
', fn);
}
};
Element.Events.active = {
onAdd: function(fn) {
(this.get ? this.get('
idle
') : window.idleTimer).addEvent('
active', fn);
}
};