From 7a766faab7be20ba21cb7f24ff7335c6dcfe6805 Mon Sep 17 00:00:00 2001 From: Robin Orheden Date: Tue, 19 Jan 2016 12:33:25 +0100 Subject: [PATCH] fix configuration issue, bump version to 0.7.0 --- README.md | 10 +++++----- bower.json | 2 +- dist/activity.js | 39 +++++++++++++++++---------------------- dist/activity.js.map | 2 +- dist/activity.min.js | 2 +- dist/activity.min.js.map | 2 +- example/console.html | 18 ++++++++++++++++++ package.json | 2 +- src/activity.js | 39 +++++++++++++++++---------------------- 9 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 example/console.html diff --git a/README.md b/README.md index 55f13b1..d375bf0 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $ bower install activity --save ## Usage -At the end of your code, right before ``, add the following: +At the end of your code, right before ``, call the following: ```javascript Activity.detect(); @@ -34,12 +34,12 @@ Activity.detect(); Once this is done, you'll be able to detect user activity by listening to either the `user_active` or `user_inactive` events, as shown below: ```javascript -window.addEventListener('user_active', function () { - console.log("The user is active!"); +Activity.on('active', function () { + console.log("The user is active!"); }); -window.addEventListener('user_inactive', function () { - console.log("The user is inactive!"); +Activity.on('inactive', function () { + console.log("The user is inactive!"); }); ``` diff --git a/bower.json b/bower.json index f0b66e9..3850acd 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "activity", - "version": "0.6.0", + "version": "0.7.0", "main": [ "dist/activity.min.js" ], diff --git a/dist/activity.js b/dist/activity.js index 1cf1b65..797be67 100644 --- a/dist/activity.js +++ b/dist/activity.js @@ -255,14 +255,23 @@ Object.freeze(Activity.State); - // Method that helps with setting up the user monitor. - // Forwards all user monitoring events to the window scope. - Activity.detect = function detect(options) { - if (Activity._instance) { - return false; + var instance = null; + function resolveInstance(options) { + return !instance ? instance = new Activity(options) : instance; + } + + Activity.configure = function configure(options) { + return resolveInstance(options); + }; + + Activity.detect = function detect() { + var instance = resolveInstance(); + + if (instance.initialized) { + return true; } - var instance = Activity._instance = new Activity(options); + instance.initialized = true; instance.on('inactive', function () { window.dispatchEvent(new CustomEvent('user_inactive')); @@ -277,26 +286,12 @@ return true; }; - // Listen to a event. Activity.on = function on(name, listener) { - var instance = Activity._instance; - - if (!instance) { - throw new Error('Activity.detect() hasn\'t been called.'); - } - - return instance.on(event, handler); + return resolveInstance().on(name, listener); }; - // Get activity state. Activity.state = function state() { - var instance = Activity._instance; - - if (!instance) { - throw new Error('Activity.detect() hasn\'t been called.'); - } - - return instance.state(); + return resolveInstance().state(); }; // Replays the current user state to all event listeners. diff --git a/dist/activity.js.map b/dist/activity.js.map index 36b309a..941719c 100644 --- a/dist/activity.js.map +++ b/dist/activity.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///webpack/bootstrap 063d83ed6d159f287fc4","webpack:///./src/activity.js"],"names":[],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA,uBAAe;AACf;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;;;;;;ACtCA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,wBAAuB,4BAA4B;AACnD;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA,QAAO;AACP;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA,QAAO;AACP;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;;AAEA;AACA,QAAO;;AAEP;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AAEA;AACA;AACA,MAAK;;AAEL;AACA;AACA,MAAK;;AAEL;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA,EAAC,I","file":"activity.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 063d83ed6d159f287fc4\n **/","// Activity.js\n// Know when your users are using your site.\n// Author: Robin Orheden\n// License: MIT\n\n(function () {\n function Activity(options) {\n var State = Activity.State;\n\n var listeners = {};\n var started = false;\n\n var currentState = State.Unknown;\n\n var inactiveSince = 0;\n var inactiveSinceTimeoutId = null;\n var flagInactiveTimeoutId = null;\n\n // Setup options\n if (!options) {\n options = {};\n }\n\n var defaultOptions = {\n inactiveAfter: 60,\n inactiveOnLostFocus: true,\n inactiveOnNoVisibility: true,\n inactiveOnMouseInactivity: true,\n inactiveOnKeyboardInactivity: true\n };\n\n // Iterate default options and fill in the gaps\n // where there are options missing.\n for (var key in defaultOptions) {\n var value = defaultOptions[key];\n if (!(key in options)) {\n options[key] = value;\n }\n }\n\n function emit(name, args) {\n if (!args) {\n args = [];\n }\n if (name in listeners) {\n for (var i = 0; i < listeners[name].length; i++) {\n listeners[name][i].apply(null, args);\n }\n }\n }\n\n function transitionState(newState, force) {\n if (currentState !== newState || force === true) {\n currentState = newState;\n switch (currentState) {\n case State.Inactive:\n emit('inactive');\n break;\n case State.Active:\n emit('active');\n break;\n }\n }\n }\n\n function inactiveSinceTick() {\n var newState = null;\n\n if (++inactiveSince >= options.inactiveAfter) {\n newState = State.Inactive;\n } else {\n newState = State.Active;\n }\n\n transitionState(newState);\n }\n\n function flagUserAsActive() {\n inactiveSince = 0;\n clearTimeout(flagInactiveTimeoutId);\n transitionState(State.Active);\n }\n\n function flagUserAsInactive() {\n clearTimeout(flagInactiveTimeoutId);\n flagInactiveTimeoutId = setTimeout(function () {\n transitionState(State.Inactive);\n }, 50);\n }\n\n function onVisibilityChanged() {\n if (document.hidden) {\n flagUserAsActive();\n } else {\n transitionState(State.Inactive);\n }\n }\n\n function addElementListener(element, name, listener) {\n if (element.addEventListener) {\n element.addEventListener(name, listener);\n } else if (element.attachEvent) {\n element.attachEvent(name, listener);\n }\n }\n\n function removeElementListener(element, name, listener) {\n if (element.removeEventListener) {\n element.removeEventListener(name, listener);\n } else if (element.detachEvent) {\n element.detachEvent(name, listener);\n }\n }\n\n return {\n // Start monitoring user activity.\n start: function start() {\n if (started) {\n return false;\n }\n\n started = true;\n inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n \n if (options.inactiveOnMouseInactivity) {\n addElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n addElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n addElementListener(window, 'blur', flagUserAsInactive);\n addElementListener(window, 'focus', flagUserAsActive);\n }\n \n if (options.inactiveOnNoVisibility) {\n addElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('started');\n\n return true;\n },\n\n // Get the current user state (see State for exactly what is available).\n state: function state() {\n return currentState;\n },\n\n // Listen to a event. Events currently supported:\n // started: The service has started.\n // stopped: The service has stopped.\n // inactive: When a user is inactive.\n // active: When a user is active.\n on: function on(name, listener) {\n if (!(name in listeners)) {\n listeners[name] = [];\n }\n listeners[name].push(listener);\n },\n\n // Replays the current user state to all event listeners.\n replayState: function replayState() {\n transitionState(currentState, true);\n },\n\n // Stop monitoring user activity.\n stop: function stop() {\n if (!started) {\n return false;\n }\n\n started = false;\n currentState = State.Unknown;\n clearTimeout(inactiveSinceTimeoutId);\n\n if (options.inactiveOnMouseInactivity) {\n removeElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n removeElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n removeElementListener(window, 'blur', flagUserAsInactive);\n removeElementListener(window, 'focus', flagUserAsActive);\n };\n\n if (options.inactiveOnNoVisibility) {\n removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('stopped');\n\n return true;\n }\n };\n };\n\n // Expose state enum.\n Activity.State = {\n Unknown: 0,\n Inactive: 1,\n Active: 2\n };\n\n Object.freeze(Activity.State);\n\n // Method that helps with setting up the user monitor.\n // Forwards all user monitoring events to the window scope.\n Activity.detect = function detect(options) {\n if (Activity._instance) {\n return false;\n }\n\n var instance = Activity._instance = new Activity(options);\n\n instance.on('inactive', function () {\n window.dispatchEvent(new CustomEvent('user_inactive'));\n });\n\n instance.on('active', function () {\n window.dispatchEvent(new CustomEvent('user_active'));\n });\n\n instance.start();\n\n return true;\n };\n\n // Listen to a event.\n Activity.on = function on(name, listener) {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.on(event, handler);\n };\n\n // Get activity state.\n Activity.state = function state() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.state();\n };\n\n // Replays the current user state to all event listeners.\n Activity.replayState = function replayState() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.replayState();\n };\n\n window.Activity = Activity;\n})();\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./src/activity.js\n ** module id = 0\n ** module chunks = 0 1\n **/"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/bootstrap 30f574ce8486a0dfa50a","webpack:///./src/activity.js"],"names":[],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA,uBAAe;AACf;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;;;;;;ACtCA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,wBAAuB,4BAA4B;AACnD;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA,QAAO;AACP;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA,QAAO;AACP;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA,QAAO;AACP;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;;AAEA;AACA,QAAO;;AAEP;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA,QAAO;;AAEP;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;;AAEA;AACA;AACA,MAAK;;AAEL;AACA;AACA,MAAK;;AAEL;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA,EAAC,I","file":"activity.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 30f574ce8486a0dfa50a\n **/","// Activity.js\n// Know when your users are using your site.\n// Author: Robin Orheden\n// License: MIT\n\n(function () {\n function Activity(options) {\n var State = Activity.State;\n\n var listeners = {};\n var started = false;\n\n var currentState = State.Unknown;\n\n var inactiveSince = 0;\n var inactiveSinceTimeoutId = null;\n var flagInactiveTimeoutId = null;\n\n // Setup options\n if (!options) {\n options = {};\n }\n\n var defaultOptions = {\n inactiveAfter: 60,\n inactiveOnLostFocus: true,\n inactiveOnNoVisibility: true,\n inactiveOnMouseInactivity: true,\n inactiveOnKeyboardInactivity: true\n };\n\n // Iterate default options and fill in the gaps\n // where there are options missing.\n for (var key in defaultOptions) {\n var value = defaultOptions[key];\n if (!(key in options)) {\n options[key] = value;\n }\n }\n\n function emit(name, args) {\n if (!args) {\n args = [];\n }\n if (name in listeners) {\n for (var i = 0; i < listeners[name].length; i++) {\n listeners[name][i].apply(null, args);\n }\n }\n }\n\n function transitionState(newState, force) {\n if (currentState !== newState || force === true) {\n currentState = newState;\n switch (currentState) {\n case State.Inactive:\n emit('inactive');\n break;\n case State.Active:\n emit('active');\n break;\n }\n }\n }\n\n function inactiveSinceTick() {\n var newState = null;\n\n if (++inactiveSince >= options.inactiveAfter) {\n newState = State.Inactive;\n } else {\n newState = State.Active;\n }\n\n transitionState(newState);\n }\n\n function flagUserAsActive() {\n inactiveSince = 0;\n clearTimeout(flagInactiveTimeoutId);\n transitionState(State.Active);\n }\n\n function flagUserAsInactive() {\n clearTimeout(flagInactiveTimeoutId);\n flagInactiveTimeoutId = setTimeout(function () {\n transitionState(State.Inactive);\n }, 50);\n }\n\n function onVisibilityChanged() {\n if (document.hidden) {\n flagUserAsActive();\n } else {\n transitionState(State.Inactive);\n }\n }\n\n function addElementListener(element, name, listener) {\n if (element.addEventListener) {\n element.addEventListener(name, listener);\n } else if (element.attachEvent) {\n element.attachEvent(name, listener);\n }\n }\n\n function removeElementListener(element, name, listener) {\n if (element.removeEventListener) {\n element.removeEventListener(name, listener);\n } else if (element.detachEvent) {\n element.detachEvent(name, listener);\n }\n }\n\n return {\n // Start monitoring user activity.\n start: function start() {\n if (started) {\n return false;\n }\n\n started = true;\n inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n \n if (options.inactiveOnMouseInactivity) {\n addElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n addElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n addElementListener(window, 'blur', flagUserAsInactive);\n addElementListener(window, 'focus', flagUserAsActive);\n }\n \n if (options.inactiveOnNoVisibility) {\n addElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('started');\n\n return true;\n },\n\n // Get the current user state (see State for exactly what is available).\n state: function state() {\n return currentState;\n },\n\n // Listen to a event. Events currently supported:\n // started: The service has started.\n // stopped: The service has stopped.\n // inactive: When a user is inactive.\n // active: When a user is active.\n on: function on(name, listener) {\n if (!(name in listeners)) {\n listeners[name] = [];\n }\n listeners[name].push(listener);\n },\n\n // Replays the current user state to all event listeners.\n replayState: function replayState() {\n transitionState(currentState, true);\n },\n\n // Stop monitoring user activity.\n stop: function stop() {\n if (!started) {\n return false;\n }\n\n started = false;\n currentState = State.Unknown;\n clearTimeout(inactiveSinceTimeoutId);\n\n if (options.inactiveOnMouseInactivity) {\n removeElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n removeElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n removeElementListener(window, 'blur', flagUserAsInactive);\n removeElementListener(window, 'focus', flagUserAsActive);\n };\n\n if (options.inactiveOnNoVisibility) {\n removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('stopped');\n\n return true;\n }\n };\n };\n\n // Expose state enum.\n Activity.State = {\n Unknown: 0,\n Inactive: 1,\n Active: 2\n };\n\n Object.freeze(Activity.State);\n\n var instance = null;\n function resolveInstance(options) {\n return !instance ? instance = new Activity(options) : instance;\n }\n\n Activity.configure = function configure(options) {\n return resolveInstance(options);\n };\n\n Activity.detect = function detect() {\n var instance = resolveInstance();\n\n if (instance.initialized) {\n return true;\n }\n\n instance.initialized = true;\n\n instance.on('inactive', function () {\n window.dispatchEvent(new CustomEvent('user_inactive'));\n });\n\n instance.on('active', function () {\n window.dispatchEvent(new CustomEvent('user_active'));\n });\n\n instance.start();\n\n return true;\n };\n\n Activity.on = function on(name, listener) {\n return resolveInstance().on(name, listener);\n };\n\n Activity.state = function state() {\n return resolveInstance().state();\n };\n\n // Replays the current user state to all event listeners.\n Activity.replayState = function replayState() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.replayState();\n };\n\n window.Activity = Activity;\n})();\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./src/activity.js\n ** module id = 0\n ** module chunks = 0 1\n **/"],"sourceRoot":""} \ No newline at end of file diff --git a/dist/activity.min.js b/dist/activity.min.js index 1791b96..acbd7d1 100644 --- a/dist/activity.min.js +++ b/dist/activity.min.js @@ -1,2 +1,2 @@ -!function(t){function n(i){if(e[i])return e[i].exports;var c=e[i]={exports:{},id:i,loaded:!1};return t[i].call(c.exports,c,c.exports,n),c.loaded=!0,c.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n){!function(){function t(n){function e(t,n){if(n||(n=[]),t in d)for(var e=0;e=n.inactiveAfter?s.Inactive:s.Active,i(t)}function a(){w=0,clearTimeout(h),i(s.Active)}function o(){clearTimeout(h),h=setTimeout(function(){i(s.Inactive)},50)}function r(){document.hidden?a():i(s.Inactive)}function v(t,n,e){t.addEventListener?t.addEventListener(n,e):t.attachEvent&&t.attachEvent(n,e)}function u(t,n,e){t.removeEventListener?t.removeEventListener(n,e):t.detachEvent&&t.detachEvent(n,e)}var s=t.State,d={},f=!1,l=s.Unknown,w=0,y=null,h=null;n||(n={});var p={inactiveAfter:60,inactiveOnLostFocus:!0,inactiveOnNoVisibility:!0,inactiveOnMouseInactivity:!0,inactiveOnKeyboardInactivity:!0};for(var m in p){var b=p[m];m in n||(n[m]=b)}return{start:function(){return f?!1:(f=!0,y=setInterval(c,1e3),n.inactiveOnMouseInactivity&&v(window,"mousemove",a),n.inactiveOnKeyboardInactivity&&v(window,"keypress",a),n.inactiveOnLostFocus&&(v(window,"blur",o),v(window,"focus",a)),n.inactiveOnNoVisibility&&v(document,"visibilitychange",r),e("started"),!0)},state:function(){return l},on:function(t,n){t in d||(d[t]=[]),d[t].push(n)},replayState:function(){i(l,!0)},stop:function(){return f?(f=!1,l=s.Unknown,clearTimeout(y),n.inactiveOnMouseInactivity&&u(window,"mousemove",a),n.inactiveOnKeyboardInactivity&&u(window,"keypress",a),n.inactiveOnLostFocus&&(u(window,"blur",o),u(window,"focus",a)),n.inactiveOnNoVisibility&&u(document,"visibilitychange",r),e("stopped"),!0):!1}}}t.State={Unknown:0,Inactive:1,Active:2},Object.freeze(t.State),t.detect=function(n){if(t._instance)return!1;var e=t._instance=new t(n);return e.on("inactive",function(){window.dispatchEvent(new CustomEvent("user_inactive"))}),e.on("active",function(){window.dispatchEvent(new CustomEvent("user_active"))}),e.start(),!0},t.on=function(n,e){var i=t._instance;if(!i)throw new Error("Activity.detect() hasn't been called.");return i.on(event,handler)},t.state=function(){var n=t._instance;if(!n)throw new Error("Activity.detect() hasn't been called.");return n.state()},t.replayState=function(){var n=t._instance;if(!n)throw new Error("Activity.detect() hasn't been called.");return n.replayState()},window.Activity=t}()}]); +!function(n){function t(e){if(i[e])return i[e].exports;var c=i[e]={exports:{},id:e,loaded:!1};return n[e].call(c.exports,c,c.exports,t),c.loaded=!0,c.exports}var i={};return t.m=n,t.c=i,t.p="",t(0)}([function(n,t){!function(){function n(t){function i(n,t){if(t||(t=[]),n in d)for(var i=0;i=t.inactiveAfter?s.Inactive:s.Active,e(n)}function o(){w=0,clearTimeout(p),e(s.Active)}function a(){clearTimeout(p),p=setTimeout(function(){e(s.Inactive)},50)}function r(){document.hidden?o():e(s.Inactive)}function u(n,t,i){n.addEventListener?n.addEventListener(t,i):n.attachEvent&&n.attachEvent(t,i)}function v(n,t,i){n.removeEventListener?n.removeEventListener(t,i):n.detachEvent&&n.detachEvent(t,i)}var s=n.State,d={},f=!1,l=s.Unknown,w=0,y=null,p=null;t||(t={});var m={inactiveAfter:60,inactiveOnLostFocus:!0,inactiveOnNoVisibility:!0,inactiveOnMouseInactivity:!0,inactiveOnKeyboardInactivity:!0};for(var h in m){var b=m[h];h in t||(t[h]=b)}return{start:function(){return f?!1:(f=!0,y=setInterval(c,1e3),t.inactiveOnMouseInactivity&&u(window,"mousemove",o),t.inactiveOnKeyboardInactivity&&u(window,"keypress",o),t.inactiveOnLostFocus&&(u(window,"blur",a),u(window,"focus",o)),t.inactiveOnNoVisibility&&u(document,"visibilitychange",r),i("started"),!0)},state:function(){return l},on:function(n,t){n in d||(d[n]=[]),d[n].push(t)},replayState:function(){e(l,!0)},stop:function(){return f?(f=!1,l=s.Unknown,clearTimeout(y),t.inactiveOnMouseInactivity&&v(window,"mousemove",o),t.inactiveOnKeyboardInactivity&&v(window,"keypress",o),t.inactiveOnLostFocus&&(v(window,"blur",a),v(window,"focus",o)),t.inactiveOnNoVisibility&&v(document,"visibilitychange",r),i("stopped"),!0):!1}}}function t(t){return i?i:i=new n(t)}n.State={Unknown:0,Inactive:1,Active:2},Object.freeze(n.State);var i=null;n.configure=function(n){return t(n)},n.detect=function(){var n=t();return n.initialized?!0:(n.initialized=!0,n.on("inactive",function(){window.dispatchEvent(new CustomEvent("user_inactive"))}),n.on("active",function(){window.dispatchEvent(new CustomEvent("user_active"))}),n.start(),!0)},n.on=function(n,i){return t().on(n,i)},n.state=function(){return t().state()},n.replayState=function(){var t=n._instance;if(!t)throw new Error("Activity.detect() hasn't been called.");return t.replayState()},window.Activity=n}()}]); //# sourceMappingURL=activity.min.js.map \ No newline at end of file diff --git a/dist/activity.min.js.map b/dist/activity.min.js.map index 1ed51e4..c05f999 100644 --- a/dist/activity.min.js.map +++ b/dist/activity.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///activity.min.js","webpack:///webpack/bootstrap 063d83ed6d159f287fc4?ad17","webpack:///./src/activity.js?9fea"],"names":["modules","__webpack_require__","moduleId","installedModules","exports","module","id","loaded","call","m","c","p","Activity","options","emit","name","args","listeners","i","length","apply","transitionState","newState","force","currentState","State","Inactive","Active","inactiveSinceTick","inactiveSince","inactiveAfter","flagUserAsActive","clearTimeout","flagInactiveTimeoutId","flagUserAsInactive","setTimeout","onVisibilityChanged","document","hidden","addElementListener","element","listener","addEventListener","attachEvent","removeElementListener","removeEventListener","detachEvent","started","Unknown","inactiveSinceTimeoutId","defaultOptions","inactiveOnLostFocus","inactiveOnNoVisibility","inactiveOnMouseInactivity","inactiveOnKeyboardInactivity","key","value","start","setInterval","window","state","on","push","replayState","stop","Object","freeze","detect","_instance","instance","dispatchEvent","CustomEvent","Error","event","handler"],"mappings":"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","file":"activity.min.js","sourcesContent":["/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports) {\n\n\t// Activity.js\n\t// Know when your users are using your site.\n\t// Author: Robin Orheden\n\t// License: MIT\n\t\n\t(function () {\n\t function Activity(options) {\n\t var State = Activity.State;\n\t\n\t var listeners = {};\n\t var started = false;\n\t\n\t var currentState = State.Unknown;\n\t\n\t var inactiveSince = 0;\n\t var inactiveSinceTimeoutId = null;\n\t var flagInactiveTimeoutId = null;\n\t\n\t // Setup options\n\t if (!options) {\n\t options = {};\n\t }\n\t\n\t var defaultOptions = {\n\t inactiveAfter: 60,\n\t inactiveOnLostFocus: true,\n\t inactiveOnNoVisibility: true,\n\t inactiveOnMouseInactivity: true,\n\t inactiveOnKeyboardInactivity: true\n\t };\n\t\n\t // Iterate default options and fill in the gaps\n\t // where there are options missing.\n\t for (var key in defaultOptions) {\n\t var value = defaultOptions[key];\n\t if (!(key in options)) {\n\t options[key] = value;\n\t }\n\t }\n\t\n\t function emit(name, args) {\n\t if (!args) {\n\t args = [];\n\t }\n\t if (name in listeners) {\n\t for (var i = 0; i < listeners[name].length; i++) {\n\t listeners[name][i].apply(null, args);\n\t }\n\t }\n\t }\n\t\n\t function transitionState(newState, force) {\n\t if (currentState !== newState || force === true) {\n\t currentState = newState;\n\t switch (currentState) {\n\t case State.Inactive:\n\t emit('inactive');\n\t break;\n\t case State.Active:\n\t emit('active');\n\t break;\n\t }\n\t }\n\t }\n\t\n\t function inactiveSinceTick() {\n\t var newState = null;\n\t\n\t if (++inactiveSince >= options.inactiveAfter) {\n\t newState = State.Inactive;\n\t } else {\n\t newState = State.Active;\n\t }\n\t\n\t transitionState(newState);\n\t }\n\t\n\t function flagUserAsActive() {\n\t inactiveSince = 0;\n\t clearTimeout(flagInactiveTimeoutId);\n\t transitionState(State.Active);\n\t }\n\t\n\t function flagUserAsInactive() {\n\t clearTimeout(flagInactiveTimeoutId);\n\t flagInactiveTimeoutId = setTimeout(function () {\n\t transitionState(State.Inactive);\n\t }, 50);\n\t }\n\t\n\t function onVisibilityChanged() {\n\t if (document.hidden) {\n\t flagUserAsActive();\n\t } else {\n\t transitionState(State.Inactive);\n\t }\n\t }\n\t\n\t function addElementListener(element, name, listener) {\n\t if (element.addEventListener) {\n\t element.addEventListener(name, listener);\n\t } else if (element.attachEvent) {\n\t element.attachEvent(name, listener);\n\t }\n\t }\n\t\n\t function removeElementListener(element, name, listener) {\n\t if (element.removeEventListener) {\n\t element.removeEventListener(name, listener);\n\t } else if (element.detachEvent) {\n\t element.detachEvent(name, listener);\n\t }\n\t }\n\t\n\t return {\n\t // Start monitoring user activity.\n\t start: function start() {\n\t if (started) {\n\t return false;\n\t }\n\t\n\t started = true;\n\t inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n\t \n\t if (options.inactiveOnMouseInactivity) {\n\t addElementListener(window, 'mousemove', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnKeyboardInactivity) {\n\t addElementListener(window, 'keypress', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnLostFocus) {\n\t addElementListener(window, 'blur', flagUserAsInactive);\n\t addElementListener(window, 'focus', flagUserAsActive);\n\t }\n\t \n\t if (options.inactiveOnNoVisibility) {\n\t addElementListener(document, 'visibilitychange', onVisibilityChanged);\n\t }\n\t\n\t emit('started');\n\t\n\t return true;\n\t },\n\t\n\t // Get the current user state (see State for exactly what is available).\n\t state: function state() {\n\t return currentState;\n\t },\n\t\n\t // Listen to a event. Events currently supported:\n\t // started: The service has started.\n\t // stopped: The service has stopped.\n\t // inactive: When a user is inactive.\n\t // active: When a user is active.\n\t on: function on(name, listener) {\n\t if (!(name in listeners)) {\n\t listeners[name] = [];\n\t }\n\t listeners[name].push(listener);\n\t },\n\t\n\t // Replays the current user state to all event listeners.\n\t replayState: function replayState() {\n\t transitionState(currentState, true);\n\t },\n\t\n\t // Stop monitoring user activity.\n\t stop: function stop() {\n\t if (!started) {\n\t return false;\n\t }\n\t\n\t started = false;\n\t currentState = State.Unknown;\n\t clearTimeout(inactiveSinceTimeoutId);\n\t\n\t if (options.inactiveOnMouseInactivity) {\n\t removeElementListener(window, 'mousemove', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnKeyboardInactivity) {\n\t removeElementListener(window, 'keypress', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnLostFocus) {\n\t removeElementListener(window, 'blur', flagUserAsInactive);\n\t removeElementListener(window, 'focus', flagUserAsActive);\n\t };\n\t\n\t if (options.inactiveOnNoVisibility) {\n\t removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n\t }\n\t\n\t emit('stopped');\n\t\n\t return true;\n\t }\n\t };\n\t };\n\t\n\t // Expose state enum.\n\t Activity.State = {\n\t Unknown: 0,\n\t Inactive: 1,\n\t Active: 2\n\t };\n\t\n\t Object.freeze(Activity.State);\n\t\n\t // Method that helps with setting up the user monitor.\n\t // Forwards all user monitoring events to the window scope.\n\t Activity.detect = function detect(options) {\n\t if (Activity._instance) {\n\t return false;\n\t }\n\t\n\t var instance = Activity._instance = new Activity(options);\n\t\n\t instance.on('inactive', function () {\n\t window.dispatchEvent(new CustomEvent('user_inactive'));\n\t });\n\t\n\t instance.on('active', function () {\n\t window.dispatchEvent(new CustomEvent('user_active'));\n\t });\n\t\n\t instance.start();\n\t\n\t return true;\n\t };\n\t\n\t // Listen to a event.\n\t Activity.on = function on(name, listener) {\n\t var instance = Activity._instance;\n\t\n\t if (!instance) {\n\t throw new Error('Activity.detect() hasn\\'t been called.');\n\t }\n\t\n\t return instance.on(event, handler);\n\t };\n\t\n\t // Get activity state.\n\t Activity.state = function state() {\n\t var instance = Activity._instance;\n\t\n\t if (!instance) {\n\t throw new Error('Activity.detect() hasn\\'t been called.');\n\t }\n\t\n\t return instance.state();\n\t };\n\t\n\t // Replays the current user state to all event listeners.\n\t Activity.replayState = function replayState() {\n\t var instance = Activity._instance;\n\t\n\t if (!instance) {\n\t throw new Error('Activity.detect() hasn\\'t been called.');\n\t }\n\t\n\t return instance.replayState();\n\t };\n\t\n\t window.Activity = Activity;\n\t})();\n\n/***/ }\n/******/ ]);\n\n\n/** WEBPACK FOOTER **\n ** activity.min.js\n **/"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 063d83ed6d159f287fc4\n **/","// Activity.js\n// Know when your users are using your site.\n// Author: Robin Orheden\n// License: MIT\n\n(function () {\n function Activity(options) {\n var State = Activity.State;\n\n var listeners = {};\n var started = false;\n\n var currentState = State.Unknown;\n\n var inactiveSince = 0;\n var inactiveSinceTimeoutId = null;\n var flagInactiveTimeoutId = null;\n\n // Setup options\n if (!options) {\n options = {};\n }\n\n var defaultOptions = {\n inactiveAfter: 60,\n inactiveOnLostFocus: true,\n inactiveOnNoVisibility: true,\n inactiveOnMouseInactivity: true,\n inactiveOnKeyboardInactivity: true\n };\n\n // Iterate default options and fill in the gaps\n // where there are options missing.\n for (var key in defaultOptions) {\n var value = defaultOptions[key];\n if (!(key in options)) {\n options[key] = value;\n }\n }\n\n function emit(name, args) {\n if (!args) {\n args = [];\n }\n if (name in listeners) {\n for (var i = 0; i < listeners[name].length; i++) {\n listeners[name][i].apply(null, args);\n }\n }\n }\n\n function transitionState(newState, force) {\n if (currentState !== newState || force === true) {\n currentState = newState;\n switch (currentState) {\n case State.Inactive:\n emit('inactive');\n break;\n case State.Active:\n emit('active');\n break;\n }\n }\n }\n\n function inactiveSinceTick() {\n var newState = null;\n\n if (++inactiveSince >= options.inactiveAfter) {\n newState = State.Inactive;\n } else {\n newState = State.Active;\n }\n\n transitionState(newState);\n }\n\n function flagUserAsActive() {\n inactiveSince = 0;\n clearTimeout(flagInactiveTimeoutId);\n transitionState(State.Active);\n }\n\n function flagUserAsInactive() {\n clearTimeout(flagInactiveTimeoutId);\n flagInactiveTimeoutId = setTimeout(function () {\n transitionState(State.Inactive);\n }, 50);\n }\n\n function onVisibilityChanged() {\n if (document.hidden) {\n flagUserAsActive();\n } else {\n transitionState(State.Inactive);\n }\n }\n\n function addElementListener(element, name, listener) {\n if (element.addEventListener) {\n element.addEventListener(name, listener);\n } else if (element.attachEvent) {\n element.attachEvent(name, listener);\n }\n }\n\n function removeElementListener(element, name, listener) {\n if (element.removeEventListener) {\n element.removeEventListener(name, listener);\n } else if (element.detachEvent) {\n element.detachEvent(name, listener);\n }\n }\n\n return {\n // Start monitoring user activity.\n start: function start() {\n if (started) {\n return false;\n }\n\n started = true;\n inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n \n if (options.inactiveOnMouseInactivity) {\n addElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n addElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n addElementListener(window, 'blur', flagUserAsInactive);\n addElementListener(window, 'focus', flagUserAsActive);\n }\n \n if (options.inactiveOnNoVisibility) {\n addElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('started');\n\n return true;\n },\n\n // Get the current user state (see State for exactly what is available).\n state: function state() {\n return currentState;\n },\n\n // Listen to a event. Events currently supported:\n // started: The service has started.\n // stopped: The service has stopped.\n // inactive: When a user is inactive.\n // active: When a user is active.\n on: function on(name, listener) {\n if (!(name in listeners)) {\n listeners[name] = [];\n }\n listeners[name].push(listener);\n },\n\n // Replays the current user state to all event listeners.\n replayState: function replayState() {\n transitionState(currentState, true);\n },\n\n // Stop monitoring user activity.\n stop: function stop() {\n if (!started) {\n return false;\n }\n\n started = false;\n currentState = State.Unknown;\n clearTimeout(inactiveSinceTimeoutId);\n\n if (options.inactiveOnMouseInactivity) {\n removeElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n removeElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n removeElementListener(window, 'blur', flagUserAsInactive);\n removeElementListener(window, 'focus', flagUserAsActive);\n };\n\n if (options.inactiveOnNoVisibility) {\n removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('stopped');\n\n return true;\n }\n };\n };\n\n // Expose state enum.\n Activity.State = {\n Unknown: 0,\n Inactive: 1,\n Active: 2\n };\n\n Object.freeze(Activity.State);\n\n // Method that helps with setting up the user monitor.\n // Forwards all user monitoring events to the window scope.\n Activity.detect = function detect(options) {\n if (Activity._instance) {\n return false;\n }\n\n var instance = Activity._instance = new Activity(options);\n\n instance.on('inactive', function () {\n window.dispatchEvent(new CustomEvent('user_inactive'));\n });\n\n instance.on('active', function () {\n window.dispatchEvent(new CustomEvent('user_active'));\n });\n\n instance.start();\n\n return true;\n };\n\n // Listen to a event.\n Activity.on = function on(name, listener) {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.on(event, handler);\n };\n\n // Get activity state.\n Activity.state = function state() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.state();\n };\n\n // Replays the current user state to all event listeners.\n Activity.replayState = function replayState() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.replayState();\n };\n\n window.Activity = Activity;\n})();\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./src/activity.js\n ** module id = 0\n ** module chunks = 0 1\n **/"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///activity.min.js","webpack:///webpack/bootstrap 30f574ce8486a0dfa50a?99e9","webpack:///./src/activity.js?9fea"],"names":["modules","__webpack_require__","moduleId","installedModules","exports","module","id","loaded","call","m","c","p","Activity","options","emit","name","args","listeners","i","length","apply","transitionState","newState","force","currentState","State","Inactive","Active","inactiveSinceTick","inactiveSince","inactiveAfter","flagUserAsActive","clearTimeout","flagInactiveTimeoutId","flagUserAsInactive","setTimeout","onVisibilityChanged","document","hidden","addElementListener","element","listener","addEventListener","attachEvent","removeElementListener","removeEventListener","detachEvent","started","Unknown","inactiveSinceTimeoutId","defaultOptions","inactiveOnLostFocus","inactiveOnNoVisibility","inactiveOnMouseInactivity","inactiveOnKeyboardInactivity","key","value","start","setInterval","window","state","on","push","replayState","stop","resolveInstance","instance","Object","freeze","configure","detect","initialized","dispatchEvent","CustomEvent","_instance","Error"],"mappings":"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","file":"activity.min.js","sourcesContent":["/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports) {\n\n\t// Activity.js\n\t// Know when your users are using your site.\n\t// Author: Robin Orheden\n\t// License: MIT\n\t\n\t(function () {\n\t function Activity(options) {\n\t var State = Activity.State;\n\t\n\t var listeners = {};\n\t var started = false;\n\t\n\t var currentState = State.Unknown;\n\t\n\t var inactiveSince = 0;\n\t var inactiveSinceTimeoutId = null;\n\t var flagInactiveTimeoutId = null;\n\t\n\t // Setup options\n\t if (!options) {\n\t options = {};\n\t }\n\t\n\t var defaultOptions = {\n\t inactiveAfter: 60,\n\t inactiveOnLostFocus: true,\n\t inactiveOnNoVisibility: true,\n\t inactiveOnMouseInactivity: true,\n\t inactiveOnKeyboardInactivity: true\n\t };\n\t\n\t // Iterate default options and fill in the gaps\n\t // where there are options missing.\n\t for (var key in defaultOptions) {\n\t var value = defaultOptions[key];\n\t if (!(key in options)) {\n\t options[key] = value;\n\t }\n\t }\n\t\n\t function emit(name, args) {\n\t if (!args) {\n\t args = [];\n\t }\n\t if (name in listeners) {\n\t for (var i = 0; i < listeners[name].length; i++) {\n\t listeners[name][i].apply(null, args);\n\t }\n\t }\n\t }\n\t\n\t function transitionState(newState, force) {\n\t if (currentState !== newState || force === true) {\n\t currentState = newState;\n\t switch (currentState) {\n\t case State.Inactive:\n\t emit('inactive');\n\t break;\n\t case State.Active:\n\t emit('active');\n\t break;\n\t }\n\t }\n\t }\n\t\n\t function inactiveSinceTick() {\n\t var newState = null;\n\t\n\t if (++inactiveSince >= options.inactiveAfter) {\n\t newState = State.Inactive;\n\t } else {\n\t newState = State.Active;\n\t }\n\t\n\t transitionState(newState);\n\t }\n\t\n\t function flagUserAsActive() {\n\t inactiveSince = 0;\n\t clearTimeout(flagInactiveTimeoutId);\n\t transitionState(State.Active);\n\t }\n\t\n\t function flagUserAsInactive() {\n\t clearTimeout(flagInactiveTimeoutId);\n\t flagInactiveTimeoutId = setTimeout(function () {\n\t transitionState(State.Inactive);\n\t }, 50);\n\t }\n\t\n\t function onVisibilityChanged() {\n\t if (document.hidden) {\n\t flagUserAsActive();\n\t } else {\n\t transitionState(State.Inactive);\n\t }\n\t }\n\t\n\t function addElementListener(element, name, listener) {\n\t if (element.addEventListener) {\n\t element.addEventListener(name, listener);\n\t } else if (element.attachEvent) {\n\t element.attachEvent(name, listener);\n\t }\n\t }\n\t\n\t function removeElementListener(element, name, listener) {\n\t if (element.removeEventListener) {\n\t element.removeEventListener(name, listener);\n\t } else if (element.detachEvent) {\n\t element.detachEvent(name, listener);\n\t }\n\t }\n\t\n\t return {\n\t // Start monitoring user activity.\n\t start: function start() {\n\t if (started) {\n\t return false;\n\t }\n\t\n\t started = true;\n\t inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n\t \n\t if (options.inactiveOnMouseInactivity) {\n\t addElementListener(window, 'mousemove', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnKeyboardInactivity) {\n\t addElementListener(window, 'keypress', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnLostFocus) {\n\t addElementListener(window, 'blur', flagUserAsInactive);\n\t addElementListener(window, 'focus', flagUserAsActive);\n\t }\n\t \n\t if (options.inactiveOnNoVisibility) {\n\t addElementListener(document, 'visibilitychange', onVisibilityChanged);\n\t }\n\t\n\t emit('started');\n\t\n\t return true;\n\t },\n\t\n\t // Get the current user state (see State for exactly what is available).\n\t state: function state() {\n\t return currentState;\n\t },\n\t\n\t // Listen to a event. Events currently supported:\n\t // started: The service has started.\n\t // stopped: The service has stopped.\n\t // inactive: When a user is inactive.\n\t // active: When a user is active.\n\t on: function on(name, listener) {\n\t if (!(name in listeners)) {\n\t listeners[name] = [];\n\t }\n\t listeners[name].push(listener);\n\t },\n\t\n\t // Replays the current user state to all event listeners.\n\t replayState: function replayState() {\n\t transitionState(currentState, true);\n\t },\n\t\n\t // Stop monitoring user activity.\n\t stop: function stop() {\n\t if (!started) {\n\t return false;\n\t }\n\t\n\t started = false;\n\t currentState = State.Unknown;\n\t clearTimeout(inactiveSinceTimeoutId);\n\t\n\t if (options.inactiveOnMouseInactivity) {\n\t removeElementListener(window, 'mousemove', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnKeyboardInactivity) {\n\t removeElementListener(window, 'keypress', flagUserAsActive);\n\t }\n\t\n\t if (options.inactiveOnLostFocus) {\n\t removeElementListener(window, 'blur', flagUserAsInactive);\n\t removeElementListener(window, 'focus', flagUserAsActive);\n\t };\n\t\n\t if (options.inactiveOnNoVisibility) {\n\t removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n\t }\n\t\n\t emit('stopped');\n\t\n\t return true;\n\t }\n\t };\n\t };\n\t\n\t // Expose state enum.\n\t Activity.State = {\n\t Unknown: 0,\n\t Inactive: 1,\n\t Active: 2\n\t };\n\t\n\t Object.freeze(Activity.State);\n\t\n\t var instance = null;\n\t function resolveInstance(options) {\n\t return !instance ? instance = new Activity(options) : instance;\n\t }\n\t\n\t Activity.configure = function configure(options) {\n\t return resolveInstance(options);\n\t };\n\t\n\t Activity.detect = function detect() {\n\t var instance = resolveInstance();\n\t\n\t if (instance.initialized) {\n\t return true;\n\t }\n\t\n\t instance.initialized = true;\n\t\n\t instance.on('inactive', function () {\n\t window.dispatchEvent(new CustomEvent('user_inactive'));\n\t });\n\t\n\t instance.on('active', function () {\n\t window.dispatchEvent(new CustomEvent('user_active'));\n\t });\n\t\n\t instance.start();\n\t\n\t return true;\n\t };\n\t\n\t Activity.on = function on(name, listener) {\n\t return resolveInstance().on(name, listener);\n\t };\n\t\n\t Activity.state = function state() {\n\t return resolveInstance().state();\n\t };\n\t\n\t // Replays the current user state to all event listeners.\n\t Activity.replayState = function replayState() {\n\t var instance = Activity._instance;\n\t\n\t if (!instance) {\n\t throw new Error('Activity.detect() hasn\\'t been called.');\n\t }\n\t\n\t return instance.replayState();\n\t };\n\t\n\t window.Activity = Activity;\n\t})();\n\n/***/ }\n/******/ ]);\n\n\n/** WEBPACK FOOTER **\n ** activity.min.js\n **/"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 30f574ce8486a0dfa50a\n **/","// Activity.js\n// Know when your users are using your site.\n// Author: Robin Orheden\n// License: MIT\n\n(function () {\n function Activity(options) {\n var State = Activity.State;\n\n var listeners = {};\n var started = false;\n\n var currentState = State.Unknown;\n\n var inactiveSince = 0;\n var inactiveSinceTimeoutId = null;\n var flagInactiveTimeoutId = null;\n\n // Setup options\n if (!options) {\n options = {};\n }\n\n var defaultOptions = {\n inactiveAfter: 60,\n inactiveOnLostFocus: true,\n inactiveOnNoVisibility: true,\n inactiveOnMouseInactivity: true,\n inactiveOnKeyboardInactivity: true\n };\n\n // Iterate default options and fill in the gaps\n // where there are options missing.\n for (var key in defaultOptions) {\n var value = defaultOptions[key];\n if (!(key in options)) {\n options[key] = value;\n }\n }\n\n function emit(name, args) {\n if (!args) {\n args = [];\n }\n if (name in listeners) {\n for (var i = 0; i < listeners[name].length; i++) {\n listeners[name][i].apply(null, args);\n }\n }\n }\n\n function transitionState(newState, force) {\n if (currentState !== newState || force === true) {\n currentState = newState;\n switch (currentState) {\n case State.Inactive:\n emit('inactive');\n break;\n case State.Active:\n emit('active');\n break;\n }\n }\n }\n\n function inactiveSinceTick() {\n var newState = null;\n\n if (++inactiveSince >= options.inactiveAfter) {\n newState = State.Inactive;\n } else {\n newState = State.Active;\n }\n\n transitionState(newState);\n }\n\n function flagUserAsActive() {\n inactiveSince = 0;\n clearTimeout(flagInactiveTimeoutId);\n transitionState(State.Active);\n }\n\n function flagUserAsInactive() {\n clearTimeout(flagInactiveTimeoutId);\n flagInactiveTimeoutId = setTimeout(function () {\n transitionState(State.Inactive);\n }, 50);\n }\n\n function onVisibilityChanged() {\n if (document.hidden) {\n flagUserAsActive();\n } else {\n transitionState(State.Inactive);\n }\n }\n\n function addElementListener(element, name, listener) {\n if (element.addEventListener) {\n element.addEventListener(name, listener);\n } else if (element.attachEvent) {\n element.attachEvent(name, listener);\n }\n }\n\n function removeElementListener(element, name, listener) {\n if (element.removeEventListener) {\n element.removeEventListener(name, listener);\n } else if (element.detachEvent) {\n element.detachEvent(name, listener);\n }\n }\n\n return {\n // Start monitoring user activity.\n start: function start() {\n if (started) {\n return false;\n }\n\n started = true;\n inactiveSinceTimeoutId = setInterval(inactiveSinceTick, 1 * 1000);\n \n if (options.inactiveOnMouseInactivity) {\n addElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n addElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n addElementListener(window, 'blur', flagUserAsInactive);\n addElementListener(window, 'focus', flagUserAsActive);\n }\n \n if (options.inactiveOnNoVisibility) {\n addElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('started');\n\n return true;\n },\n\n // Get the current user state (see State for exactly what is available).\n state: function state() {\n return currentState;\n },\n\n // Listen to a event. Events currently supported:\n // started: The service has started.\n // stopped: The service has stopped.\n // inactive: When a user is inactive.\n // active: When a user is active.\n on: function on(name, listener) {\n if (!(name in listeners)) {\n listeners[name] = [];\n }\n listeners[name].push(listener);\n },\n\n // Replays the current user state to all event listeners.\n replayState: function replayState() {\n transitionState(currentState, true);\n },\n\n // Stop monitoring user activity.\n stop: function stop() {\n if (!started) {\n return false;\n }\n\n started = false;\n currentState = State.Unknown;\n clearTimeout(inactiveSinceTimeoutId);\n\n if (options.inactiveOnMouseInactivity) {\n removeElementListener(window, 'mousemove', flagUserAsActive);\n }\n\n if (options.inactiveOnKeyboardInactivity) {\n removeElementListener(window, 'keypress', flagUserAsActive);\n }\n\n if (options.inactiveOnLostFocus) {\n removeElementListener(window, 'blur', flagUserAsInactive);\n removeElementListener(window, 'focus', flagUserAsActive);\n };\n\n if (options.inactiveOnNoVisibility) {\n removeElementListener(document, 'visibilitychange', onVisibilityChanged);\n }\n\n emit('stopped');\n\n return true;\n }\n };\n };\n\n // Expose state enum.\n Activity.State = {\n Unknown: 0,\n Inactive: 1,\n Active: 2\n };\n\n Object.freeze(Activity.State);\n\n var instance = null;\n function resolveInstance(options) {\n return !instance ? instance = new Activity(options) : instance;\n }\n\n Activity.configure = function configure(options) {\n return resolveInstance(options);\n };\n\n Activity.detect = function detect() {\n var instance = resolveInstance();\n\n if (instance.initialized) {\n return true;\n }\n\n instance.initialized = true;\n\n instance.on('inactive', function () {\n window.dispatchEvent(new CustomEvent('user_inactive'));\n });\n\n instance.on('active', function () {\n window.dispatchEvent(new CustomEvent('user_active'));\n });\n\n instance.start();\n\n return true;\n };\n\n Activity.on = function on(name, listener) {\n return resolveInstance().on(name, listener);\n };\n\n Activity.state = function state() {\n return resolveInstance().state();\n };\n\n // Replays the current user state to all event listeners.\n Activity.replayState = function replayState() {\n var instance = Activity._instance;\n\n if (!instance) {\n throw new Error('Activity.detect() hasn\\'t been called.');\n }\n\n return instance.replayState();\n };\n\n window.Activity = Activity;\n})();\n\n\n/*****************\n ** WEBPACK FOOTER\n ** ./src/activity.js\n ** module id = 0\n ** module chunks = 0 1\n **/"],"sourceRoot":""} \ No newline at end of file diff --git a/example/console.html b/example/console.html new file mode 100644 index 0000000..2d7a374 --- /dev/null +++ b/example/console.html @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/package.json b/package.json index 0b7c457..5a143af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "activityjs", - "version": "0.6.0", + "version": "0.7.0", "description": "Know when your users are using your site", "homepage": "https://github.com/typerandom/activity.js#readme", "author": "Robin Orheden", diff --git a/src/activity.js b/src/activity.js index 0177feb..5fe1eae 100644 --- a/src/activity.js +++ b/src/activity.js @@ -209,14 +209,23 @@ Object.freeze(Activity.State); - // Method that helps with setting up the user monitor. - // Forwards all user monitoring events to the window scope. - Activity.detect = function detect(options) { - if (Activity._instance) { - return false; + var instance = null; + function resolveInstance(options) { + return !instance ? instance = new Activity(options) : instance; + } + + Activity.configure = function configure(options) { + return resolveInstance(options); + }; + + Activity.detect = function detect() { + var instance = resolveInstance(); + + if (instance.initialized) { + return true; } - var instance = Activity._instance = new Activity(options); + instance.initialized = true; instance.on('inactive', function () { window.dispatchEvent(new CustomEvent('user_inactive')); @@ -231,26 +240,12 @@ return true; }; - // Listen to a event. Activity.on = function on(name, listener) { - var instance = Activity._instance; - - if (!instance) { - throw new Error('Activity.detect() hasn\'t been called.'); - } - - return instance.on(event, handler); + return resolveInstance().on(name, listener); }; - // Get activity state. Activity.state = function state() { - var instance = Activity._instance; - - if (!instance) { - throw new Error('Activity.detect() hasn\'t been called.'); - } - - return instance.state(); + return resolveInstance().state(); }; // Replays the current user state to all event listeners.