|
| 1 | +/*! Idle Timer - v1.1.1 - 2020-06-25 |
| 2 | +* https://github.com/thorst/jquery-idletimer |
| 3 | +* Copyright (c) 2020 Paul Irish; Licensed MIT */ |
| 4 | +/* |
| 5 | + mousewheel (deprecated) -> IE6.0, Chrome, Opera, Safari |
| 6 | + DOMMouseScroll (deprecated) -> Firefox 1.0 |
| 7 | + wheel (standard) -> Chrome 31, Firefox 17, IE9, Firefox Mobile 17.0 |
| 8 | +
|
| 9 | + //No need to use, use DOMMouseScroll |
| 10 | + MozMousePixelScroll -> Firefox 3.5, Firefox Mobile 1.0 |
| 11 | +
|
| 12 | + //Events |
| 13 | + WheelEvent -> see wheel |
| 14 | + MouseWheelEvent -> see mousewheel |
| 15 | + MouseScrollEvent -> Firefox 3.5, Firefox Mobile 1.0 |
| 16 | +*/ |
| 17 | +(function ($) { |
| 18 | + |
| 19 | + $.idleTimer = function (firstParam, elem) { |
| 20 | + var opts; |
| 21 | + if ( typeof firstParam === "object" ) { |
| 22 | + opts = firstParam; |
| 23 | + firstParam = null; |
| 24 | + } else if (typeof firstParam === "number") { |
| 25 | + opts = { timeout: firstParam }; |
| 26 | + firstParam = null; |
| 27 | + } |
| 28 | + |
| 29 | + // element to watch |
| 30 | + elem = elem || document; |
| 31 | + |
| 32 | + // defaults that are to be stored as instance props on the elem |
| 33 | + opts = $.extend({ |
| 34 | + idle: false, // indicates if the user is idle |
| 35 | + timeout: 30000, // the amount of time (ms) before the user is considered idle |
| 36 | + events: "mousemove keydown wheel DOMMouseScroll mousewheel mousedown touchstart touchmove MSPointerDown MSPointerMove" // define active events |
| 37 | + }, opts); |
| 38 | + |
| 39 | + var jqElem = $(elem), |
| 40 | + obj = jqElem.data("idleTimerObj") || {}, |
| 41 | + |
| 42 | + /* (intentionally not documented) |
| 43 | + * Toggles the idle state and fires an appropriate event. |
| 44 | + * @return {void} |
| 45 | + */ |
| 46 | + toggleIdleState = function (e) { |
| 47 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 48 | + |
| 49 | + // toggle the state |
| 50 | + obj.idle = !obj.idle; |
| 51 | + |
| 52 | + // store toggle state date time |
| 53 | + obj.olddate = +new Date(); |
| 54 | + |
| 55 | + // create a custom event, with state and name space |
| 56 | + var event = $.Event((obj.idle ? "idle" : "active") + ".idleTimer"); |
| 57 | + |
| 58 | + // trigger event on object with elem and copy of obj |
| 59 | + $(elem).trigger(event, [elem, $.extend({}, obj), e]); |
| 60 | + }, |
| 61 | + /** |
| 62 | + * Handle event triggers |
| 63 | + * @return {void} |
| 64 | + * @method event |
| 65 | + * @static |
| 66 | + */ |
| 67 | + handleEvent = function (e) { |
| 68 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 69 | + |
| 70 | + // ignore writting to storage unless related to idleTimer |
| 71 | + if (e.type === "storage" && e.originalEvent.key !== obj.timerSyncId) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // this is already paused, ignore events for now |
| 76 | + if (obj.remaining != null) { return; } |
| 77 | + |
| 78 | + /* |
| 79 | + mousemove is kinda buggy, it can be triggered when it should be idle. |
| 80 | + Typically is happening between 115 - 150 milliseconds after idle triggered. |
| 81 | + @psyafter & @kaellis report "always triggered if using modal (jQuery ui, with overlay)" |
| 82 | + @thorst has similar issues on ios7 "after $.scrollTop() on text area" |
| 83 | + */ |
| 84 | + if (e.type === "mousemove") { |
| 85 | + // if coord are same, it didn't move |
| 86 | + if (e.pageX === obj.pageX && e.pageY === obj.pageY) { |
| 87 | + return; |
| 88 | + } |
| 89 | + // if coord don't exist how could it move |
| 90 | + if (typeof e.pageX === "undefined" && typeof e.pageY === "undefined") { |
| 91 | + return; |
| 92 | + } |
| 93 | + // under 200 ms is hard to do, and you would have to stop, as continuous activity will bypass this |
| 94 | + var elapsed = (+new Date()) - obj.olddate; |
| 95 | + if (elapsed < 200) { |
| 96 | + return; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // clear any existing timeout |
| 101 | + clearTimeout(obj.tId); |
| 102 | + |
| 103 | + // if the idle timer is enabled, flip |
| 104 | + if (obj.idle) { |
| 105 | + toggleIdleState(e); |
| 106 | + } |
| 107 | + |
| 108 | + // store when user was last active |
| 109 | + obj.lastActive = +new Date(); |
| 110 | + |
| 111 | + // update mouse coord |
| 112 | + obj.pageX = e.pageX; |
| 113 | + obj.pageY = e.pageY; |
| 114 | + |
| 115 | + // sync lastActive |
| 116 | + if (e.type !== "storage" && obj.timerSyncId) { |
| 117 | + if (typeof(localStorage) !== "undefined") { |
| 118 | + localStorage.setItem(obj.timerSyncId, obj.lastActive); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // set a new timeout |
| 123 | + obj.tId = setTimeout(toggleIdleState, obj.timeout); |
| 124 | + }, |
| 125 | + /** |
| 126 | + * Restore initial settings and restart timer |
| 127 | + * @return {void} |
| 128 | + * @method reset |
| 129 | + * @static |
| 130 | + */ |
| 131 | + reset = function () { |
| 132 | + |
| 133 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 134 | + |
| 135 | + // reset settings |
| 136 | + obj.idle = obj.idleBackup; |
| 137 | + obj.olddate = +new Date(); |
| 138 | + obj.lastActive = obj.olddate; |
| 139 | + obj.remaining = null; |
| 140 | + |
| 141 | + // reset Timers |
| 142 | + clearTimeout(obj.tId); |
| 143 | + if (!obj.idle) { |
| 144 | + obj.tId = setTimeout(toggleIdleState, obj.timeout); |
| 145 | + } |
| 146 | + |
| 147 | + }, |
| 148 | + /** |
| 149 | + * Store remaining time, stop timer |
| 150 | + * You can pause from an idle OR active state |
| 151 | + * @return {void} |
| 152 | + * @method pause |
| 153 | + * @static |
| 154 | + */ |
| 155 | + pause = function () { |
| 156 | + |
| 157 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 158 | + |
| 159 | + // this is already paused |
| 160 | + if ( obj.remaining != null ) { return; } |
| 161 | + |
| 162 | + // define how much is left on the timer |
| 163 | + obj.remaining = obj.timeout - ((+new Date()) - obj.olddate); |
| 164 | + |
| 165 | + // clear any existing timeout |
| 166 | + clearTimeout(obj.tId); |
| 167 | + }, |
| 168 | + /** |
| 169 | + * Start timer with remaining value |
| 170 | + * @return {void} |
| 171 | + * @method resume |
| 172 | + * @static |
| 173 | + */ |
| 174 | + resume = function () { |
| 175 | + |
| 176 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 177 | + |
| 178 | + // this isn't paused yet |
| 179 | + if ( obj.remaining == null ) { return; } |
| 180 | + |
| 181 | + // start timer |
| 182 | + if ( !obj.idle ) { |
| 183 | + obj.tId = setTimeout(toggleIdleState, obj.remaining); |
| 184 | + } |
| 185 | + |
| 186 | + // clear remaining |
| 187 | + obj.remaining = null; |
| 188 | + }, |
| 189 | + /** |
| 190 | + * Stops the idle timer. This removes appropriate event handlers |
| 191 | + * and cancels any pending timeouts. |
| 192 | + * @return {void} |
| 193 | + * @method destroy |
| 194 | + * @static |
| 195 | + */ |
| 196 | + destroy = function () { |
| 197 | + |
| 198 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 199 | + |
| 200 | + //clear any pending timeouts |
| 201 | + clearTimeout(obj.tId); |
| 202 | + |
| 203 | + //Remove data |
| 204 | + jqElem.removeData("idleTimerObj"); |
| 205 | + |
| 206 | + //detach the event handlers |
| 207 | + jqElem.off("._idleTimer"); |
| 208 | + }, |
| 209 | + /** |
| 210 | + * Returns the time until becoming idle |
| 211 | + * @return {number} |
| 212 | + * @method remainingtime |
| 213 | + * @static |
| 214 | + */ |
| 215 | + remainingtime = function () { |
| 216 | + |
| 217 | + var obj = $.data(elem, "idleTimerObj") || {}; |
| 218 | + |
| 219 | + //If idle there is no time remaining |
| 220 | + if ( obj.idle ) { return 0; } |
| 221 | + |
| 222 | + //If its paused just return that |
| 223 | + if ( obj.remaining != null ) { return obj.remaining; } |
| 224 | + |
| 225 | + //Determine remaining, if negative idle didn't finish flipping, just return 0 |
| 226 | + var remaining = obj.timeout - ((+new Date()) - obj.lastActive); |
| 227 | + if (remaining < 0) { remaining = 0; } |
| 228 | + |
| 229 | + //If this is paused return that number, else return current remaining |
| 230 | + return remaining; |
| 231 | + }; |
| 232 | + |
| 233 | + |
| 234 | + // determine which function to call |
| 235 | + if (firstParam === null && typeof obj.idle !== "undefined") { |
| 236 | + // they think they want to init, but it already is, just reset |
| 237 | + reset(); |
| 238 | + return jqElem; |
| 239 | + } else if (firstParam === null) { |
| 240 | + // they want to init |
| 241 | + } else if (firstParam !== null && typeof obj.idle === "undefined") { |
| 242 | + // they want to do something, but it isnt init |
| 243 | + // not sure the best way to handle this |
| 244 | + return false; |
| 245 | + } else if (firstParam === "destroy") { |
| 246 | + destroy(); |
| 247 | + return jqElem; |
| 248 | + } else if (firstParam === "pause") { |
| 249 | + pause(); |
| 250 | + return jqElem; |
| 251 | + } else if (firstParam === "resume") { |
| 252 | + resume(); |
| 253 | + return jqElem; |
| 254 | + } else if (firstParam === "reset") { |
| 255 | + reset(); |
| 256 | + return jqElem; |
| 257 | + } else if (firstParam === "getRemainingTime") { |
| 258 | + return remainingtime(); |
| 259 | + } else if (firstParam === "getElapsedTime") { |
| 260 | + return (+new Date()) - obj.olddate; |
| 261 | + } else if (firstParam === "getLastActiveTime") { |
| 262 | + return obj.lastActive; |
| 263 | + } else if (firstParam === "isIdle") { |
| 264 | + return obj.idle; |
| 265 | + } |
| 266 | + |
| 267 | + // Test via a getter in the options object to see if the passive property is accessed |
| 268 | + // This isnt working in jquery, though is planned for 4.0 |
| 269 | + // https://github.com/jquery/jquery/issues/2871 |
| 270 | + /*var supportsPassive = false; |
| 271 | + try { |
| 272 | + var Popts = Object.defineProperty({}, "passive", { |
| 273 | + get: function() { |
| 274 | + supportsPassive = true; |
| 275 | + } |
| 276 | + }); |
| 277 | + window.addEventListener("test", null, Popts); |
| 278 | + } catch (e) {} |
| 279 | + */ |
| 280 | + |
| 281 | + /* (intentionally not documented) |
| 282 | + * Handles a user event indicating that the user isn't idle. namespaced with internal idleTimer |
| 283 | + * @param {Event} event A DOM2-normalized event object. |
| 284 | + * @return {void} |
| 285 | + */ |
| 286 | + jqElem.on((opts.events + " ").split(" ").join("._idleTimer ").trim(), function (e) { |
| 287 | + handleEvent(e); |
| 288 | + }); |
| 289 | + //}, supportsPassive ? { passive: true } : false); |
| 290 | + |
| 291 | + if (opts.timerSyncId) { |
| 292 | + $(window).on("storage", handleEvent); |
| 293 | + } |
| 294 | + |
| 295 | + // Internal Object Properties, This isn't all necessary, but we |
| 296 | + // explicitly define all keys here so we know what we are working with |
| 297 | + obj = $.extend({}, { |
| 298 | + olddate : +new Date(), // the last time state changed |
| 299 | + lastActive: +new Date(), // the last time timer was active |
| 300 | + idle : opts.idle, // current state |
| 301 | + idleBackup : opts.idle, // backup of idle parameter since it gets modified |
| 302 | + timeout : opts.timeout, // the interval to change state |
| 303 | + remaining : null, // how long until state changes |
| 304 | + timerSyncId : opts.timerSyncId, // localStorage key to use for syncing this timer |
| 305 | + tId : null, // the idle timer setTimeout |
| 306 | + pageX : null, // used to store the mouse coord |
| 307 | + pageY : null |
| 308 | + }); |
| 309 | + |
| 310 | + // set a timeout to toggle state. May wish to omit this in some situations |
| 311 | + if (!obj.idle) { |
| 312 | + obj.tId = setTimeout(toggleIdleState, obj.timeout); |
| 313 | + } |
| 314 | + |
| 315 | + // store our instance on the object |
| 316 | + $.data(elem, "idleTimerObj", obj); |
| 317 | + |
| 318 | + return jqElem; |
| 319 | + }; |
| 320 | + |
| 321 | + // This allows binding to element |
| 322 | + $.fn.idleTimer = function (firstParam) { |
| 323 | + if (this[0]) { |
| 324 | + return $.idleTimer(firstParam, this[0]); |
| 325 | + } |
| 326 | + |
| 327 | + return this; |
| 328 | + }; |
| 329 | + |
| 330 | +})(jQuery); |
0 commit comments