├── README └── Ext.ux.ActivityMonitor.js /README: -------------------------------------------------------------------------------- 1 | Ext.ux.ActivityMonitor() is a utility class intended for use with ExtJS 4.x. 2 | 3 | ActivityMonitor() watches the browser's BODY element for mouse movement and keystrokes - a realistic way to judge if the user is actively viewing your web application. 4 | 5 | Usage: 6 | 7 | ===== 8 | 9 | Ext.ux.ActivityMonitor.init({ verbose : true }); 10 | Ext.ux.ActivityMonitor.start(); 11 | 12 | ===== 13 | 14 | Configs: 15 | 16 | - verbose (Boolean): Whether or not the ActivityMonitor() should output messages to the JavaScript console. 17 | - interval (Integer): How often (in millseconds) the monitorUI() method is executed after calling start() 18 | - maxInactive (Integer): The longest amount of time to consider the user "active" without regestering new mouse movement or keystrokes 19 | 20 | - isActive (Function): Called each time monitorUI() detects the user is currently active (defaults to Ext.emptyFn) 21 | - isInactive (Funtion): Called when monitorUI() detects the user is inactive (defaults to Ext.emptyFn) 22 | 23 | -------------------------------------------------------------------------------- /Ext.ux.ActivityMonitor.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @class Ext.ux.ActivityMonitor 4 | * @author Arthur Kay (http://www.akawebdesign.com) 5 | * @singleton 6 | * @version 1.0 7 | * 8 | * GitHub Project: https://github.com/arthurakay/ExtJS-Activity-Monitor 9 | * 10 | * The MIT License (MIT) 11 | * 12 | * Copyright (c) <2011> Arthur Kay 13 | * 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to deal 16 | * in the Software without restriction, including without limitation the rights 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | * copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | * THE SOFTWARE. 31 | * 32 | */ 33 | Ext.define('Ext.ux.ActivityMonitor', { 34 | singleton : true, 35 | 36 | ui : null, 37 | runner : null, 38 | task : null, 39 | lastActive : null, 40 | 41 | ready : false, 42 | verbose : false, 43 | interval : (1000 * 60 * 1), //1 minute 44 | maxInactive : (1000 * 60 * 5), //5 minutes 45 | 46 | init : function(config) { 47 | if (!config) { config = {}; } 48 | 49 | Ext.apply(this, config, { 50 | runner : new Ext.util.TaskRunner(), 51 | ui : Ext.getBody(), 52 | task : { 53 | run : this.monitorUI, 54 | interval : config.interval || this.interval, 55 | scope : this 56 | } 57 | }); 58 | 59 | this.ready = true; 60 | }, 61 | 62 | isReady : function() { 63 | return this.ready; 64 | }, 65 | 66 | isActive : Ext.emptyFn, 67 | isInactive : Ext.emptyFn, 68 | 69 | start : function() { 70 | if (!this.isReady()) { 71 | this.log('Please run ActivityMonitor.init()'); 72 | return false; 73 | } 74 | 75 | this.ui.on('mousemove', this.captureActivity, this); 76 | this.ui.on('keydown', this.captureActivity, this); 77 | 78 | this.lastActive = new Date(); 79 | this.log('ActivityMonitor has been started.'); 80 | 81 | this.runner.start(this.task); 82 | }, 83 | 84 | stop : function() { 85 | if (!this.isReady()) { 86 | this.log('Please run ActivityMonitor.init()'); 87 | return false; 88 | } 89 | 90 | this.runner.stop(this.task); 91 | this.lastActive = null; 92 | 93 | this.ui.un('mousemove', this.captureActivity); 94 | this.ui.un('keydown', this.captureActivity); 95 | 96 | this.log('ActivityMonitor has been stopped.'); 97 | }, 98 | 99 | captureActivity : function(eventObj, el, eventOptions) { 100 | this.lastActive = new Date(); 101 | }, 102 | 103 | monitorUI : function() { 104 | var now = new Date(), 105 | inactive = (now - this.lastActive); 106 | 107 | if (inactive >= this.maxInactive) { 108 | this.log('MAXIMUM INACTIVE TIME HAS BEEN REACHED'); 109 | this.stop(); //remove event listeners 110 | 111 | this.isInactive(); 112 | } 113 | else { 114 | this.log('CURRENTLY INACTIVE FOR ' + inactive + ' (ms)'); 115 | this.isActive(); 116 | } 117 | }, 118 | 119 | log : function(msg) { 120 | if (this.verbose) { 121 | window.console.log(msg); 122 | } 123 | } 124 | 125 | }); 126 | --------------------------------------------------------------------------------