Skip to content

Commit e33119d

Browse files
committed
Merge pull request #2 from cwhenderson20/master
Update for use with webpack/browserify as well in a script tag
2 parents 175f134 + 5e07bf6 commit e33119d

File tree

7 files changed

+127
-37
lines changed

7 files changed

+127
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Gruntfile.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ This is a simple service that will emit a couple of events based on the users' D
55
```bash
66
$ bower install angular-activity-monitor
77
```
8+
or
89

9-
#### Usage:
10+
```bash
11+
$ npm install --save angular-activity-monitor
12+
```
1013

14+
#### Usage:
15+
**Bower:**
1116
```js
1217
angular.module('myModule', ['ActivityMonitor']);
1318
MyController.$inject = ['ActivityMonitor'];
@@ -18,11 +23,25 @@ function MyController(ActivityMonitor) {
1823
});
1924
}
2025
```
26+
27+
**npm (with Webpack or Bowserify)** :
28+
```js
29+
angular.module('myModule', [require('angular-activity-monitor')]);
30+
MyController.$inject = ['ActivityMonitor'];
31+
32+
function MyController(ActivityMonitor) {
33+
ActivityMonitor.on('inactive', function() {
34+
alert("y0, you're inactive!");
35+
});
36+
}
37+
```
38+
2139
##### `ActivityMonitor.options` (configuration):
2240
* `enabled`: whether to regularly check for inactivity (default: `false`) [bool]
2341
* `keepAlive`: background execution frequency (default: `800`) [seconds]
2442
* `inactive`: how long until user is considered inactive (default: `900`) [seconds]
2543
* `warning`: when user is nearing inactive state (deducted from inactive) (default: `60`) [seconds]
44+
* `DOMevents`: array of events on the DOM that count as user activity (default: `['mousemove', 'mousedown', 'mouseup', 'keypress', 'wheel', 'touchstart', 'scroll']`)
2645

2746
##### `ActivityMonitor.user` (information about the user):
2847
* `action`: timestamp of the users' last action (default: `Date.now()`) [milliseconds]

activity-monitor.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
/*jshint -W116, -W030, latedef: false */
22
'use strict';
33

4-
(function() {
5-
angular
6-
.module('ActivityMonitor', [])
7-
.service('ActivityMonitor', ActivityMonitor);
4+
(function (root, factory) {
5+
if (typeof module !== 'undefined' && module.exports) {
6+
// CommonJS
7+
if (typeof angular === 'undefined') {
8+
factory(require('angular'));
9+
} else {
10+
factory(angular);
11+
}
12+
module.exports = 'ActivityMonitor';
13+
} else if (typeof define === 'function' && define.amd) {
14+
// AMD
15+
define(['angular'], factory);
16+
} else {
17+
// Global variables
18+
factory(root.angular);
19+
}
20+
}(this, function (angular) {
21+
var m = angular
22+
.module('ActivityMonitor', [])
23+
.service('ActivityMonitor', ActivityMonitor);
824

925
var MILLISECOND = 1000;
1026
var EVENT_KEEPALIVE = 'keepAlive';
@@ -22,9 +38,12 @@
2238
keepAlive: 800, /* keepAlive ping invterval (seconds) */
2339
inactive: 900, /* how long until user is considered inactive? (seconds) */
2440
warning: 60, /* when to warn user when nearing inactive state (deducted from inactive in seconds) */
25-
monitor: 3 /* how frequently to check if the user is inactive (seconds) */
41+
monitor: 3, /* how frequently to check if the user is inactive (seconds) */
42+
DOMevents: ['mousemove', 'mousedown', 'mouseup', 'keypress', 'wheel', 'touchstart', 'scroll'] /* list of DOM events to determine user's activity */
2643
};
2744

45+
var DOMevents = service.options.DOMevents.join(' ');
46+
2847
/* user activity */
2948
service.user = {
3049
action: Date.now(), /* timestamp of the users' last action */
@@ -47,9 +66,6 @@
4766
keepAlive: null /* setInterval handle for ping handler (options.frequency) */
4867
};
4968

50-
/* list of DOM events to determine user's activity */
51-
var DOMevents = ['mousemove', 'mousedown', 'keypress', 'wheel', 'touchstart', 'scroll'].join(' ');
52-
5369
return service;
5470

5571
///////////////
@@ -68,14 +84,14 @@
6884
service.options.enabled = true;
6985
service.user.warning = false;
7086

71-
timer.keepAlive = setInterval(function() {
87+
timer.keepAlive = setInterval(function () {
7288
publish(EVENT_KEEPALIVE);
7389
}, service.options.keepAlive * MILLISECOND);
7490

75-
timer.inactivity = setInterval(function() {
91+
timer.inactivity = setInterval(function () {
7692
var now = Date.now();
77-
var warning = now - ((service.options.inactive - service.options.warning) * MILLISECOND);
78-
var inactive = now - (service.options.inactive * MILLISECOND);
93+
var warning = now - (service.options.inactive - service.options.warning) * MILLISECOND;
94+
var inactive = now - service.options.inactive * MILLISECOND;
7995

8096
/* should we display warning */
8197
if (!service.user.warning && service.user.action <= warning) {
@@ -109,7 +125,7 @@
109125
if (!service.options.enabled) return;
110126
var spaces = Object.keys(events[event]);
111127
if (!event || !spaces.length) return;
112-
spaces.forEach(function(space) {
128+
spaces.forEach(function (space) {
113129
events[event][space] && events[event][space]();
114130
});
115131
}
@@ -147,4 +163,6 @@
147163
};
148164
}
149165
}
150-
})();
166+
167+
return m;
168+
}));

activity-monitor.min.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.conf.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
module.exports = function(config) {
4+
config.set({
5+
6+
// base path that will be used to resolve all patterns (eg. files, exclude)
7+
basePath: '',
8+
9+
// frameworks to use
10+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
11+
frameworks: ['mocha', 'chai'],
12+
13+
// list of files / patterns to load in the browser
14+
files: [
15+
'node_modules/angular/angular.min.js',
16+
'node_modules/angular-mocks/angular-mocks.js',
17+
'activity-monitor.js',
18+
'test/*.js'
19+
],
20+
21+
// list of files to exclude
22+
exclude: [],
23+
24+
// preprocess matching files before serving them to the browser
25+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
26+
preprocessors: {},
27+
28+
// test results reporter to use
29+
// possible values: 'dots', 'progress'
30+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
31+
reporters: ['progress'],
32+
33+
// web server port
34+
port: 9876,
35+
36+
// enable / disable colors in the output (reporters and logs)
37+
colors: true,
38+
39+
// level of logging
40+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
41+
logLevel: config.LOG_INFO,
42+
43+
// enable / disable watching file and executing tests whenever any file changes
44+
autoWatch: false,
45+
46+
// start these browsers
47+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
48+
browsers: ['PhantomJS'],
49+
50+
// Continuous Integration mode
51+
// if true, Karma captures browsers, runs the tests and exits
52+
singleRun: true,
53+
54+
// Concurrency level
55+
// how many browser should be started simultaneous
56+
concurrency: Infinity
57+
})
58+
}

package.json

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"test": "test"
88
},
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "karma start",
11+
"build": "uglifyjs activity-monitor.js -o activity-monitor.min.js --screw-ie8 -c -m"
1112
},
1213
"repository": {
1314
"type": "git",
@@ -28,14 +29,18 @@
2829
},
2930
"homepage": "https://github.com/sean3z/angular-activity-monitor#readme",
3031
"devDependencies": {
31-
"chai": "^3.4.1",
32-
"grunt": "^0.4.5",
33-
"grunt-cli": "^0.1.13",
34-
"grunt-contrib-jshint": "^0.12.0",
35-
"grunt-jscs": "^2.6.0",
36-
"jit-grunt": "^0.9.1",
37-
"jscs": "^2.8.0",
38-
"jshint": "^2.9.1",
39-
"mocha": "^2.3.4"
32+
"angular": "~1.4.9",
33+
"angular-mocks": "~1.4.9",
34+
"chai": "~3.5.0",
35+
"karma": "~0.13.19",
36+
"karma-chai": "~0.1.0",
37+
"karma-mocha": "~0.2.1",
38+
"karma-phantomjs-launcher": "~1.0.0",
39+
"mocha": "^2.3.4",
40+
"phantomjs-prebuilt": "~2.1.3"
41+
},
42+
"dependencies": {
43+
"eslint": "~1.10.3",
44+
"uglify-js": "~2.6.1"
4045
}
4146
}

0 commit comments

Comments
 (0)