Jon Atkins 9b3caa8eb9 add function to start 'idle' mode - for use in the mobile app (rather than it forcing the idle timer)
the existing idle resume function can be used on returning to iitc

also, fix the long-standing bug on mobile where the map disappears after repeated touch panning/zooming. seems leaflet gets confused about zoom levels and ends up with a non-ingeger. when this is found, force it back to an ingeger zoom level when this happens
2013-09-16 00:01:25 +01:00

71 lines
1.7 KiB
JavaScript

// IDLE HANDLING /////////////////////////////////////////////////////
window.idleTime = 0; // in seconds
window._idleTimeLimit = MAX_IDLE_TIME;
var IDLE_POLL_TIME = 10;
var idlePoll = function() {
window.idleTime += IDLE_POLL_TIME;
var hidden = (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden || false);
if (hidden) {
window.idleSet();
}
}
setInterval(idlePoll, IDLE_POLL_TIME*1000);
var idleReset = function () {
// update immediately when the user comes back
if(isIdle()) {
window.idleTime = 0;
$.each(window._onResumeFunctions, function(ind, f) {
f();
});
}
window.idleTime = 0;
window._idleTimeLimit = MAX_IDLE_TIME;
};
var idleSet = function() {
// force IITC to idle. used by the mobile app when switching to something else
if (!isIdle()) {
window._idleTImeLimit = 0;
}
}
// only reset idle on mouse move where the coordinates are actually different.
// some browsers send the event when not moving!
var _lastMouseX=-1, _lastMouseY=-1;
var idleMouseMove = function(e) {
var dX = _lastMouseX-e.clientX;
var dY = _lastMouseY-e.clientY;
var deltaSquared = dX*dX + dY*dY;
// only treat movements over 3 pixels as enough to reset us
if (deltaSquared > 3*3) {
_lastMouseX = e.clientX;
_lastMouseY = e.clientY;
idleReset();
}
}
window.setupIdle = function() {
$('body').keypress(idleReset);
$('body').mousemove(idleMouseMove);
}
window.isIdle = function() {
return window.idleTime >= window._idleTimeLimit;
}
window._onResumeFunctions = [];
// add your function here if you want to be notified when the user
// resumes from being idle
window.addResumeFunction = function(f) {
window._onResumeFunctions.push(f);
}