-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-monitor.js
76 lines (68 loc) · 2.4 KB
/
ng-monitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
angular.module('ngMonitor', []).provider('ngMonitor', function(){
this.idle = 20; // senconds
this.warning = 5; // senconds
this.interval = 1; // senconds
this.timeout = 5; //seconds
this.events = ['mousemove','keydown','DOMMouseScroll','mousewheel','mousedown','touchstart'];
this.$get = function($rootScope, $window, $interval, cookies){
var _this = this;
var _intervalPromise = null;
var _time = Math.trunc(new Date().getTime() / 1000);
cookies.put('ngMonitorExpiryTime', parseInt(_time) + parseInt(_this.idle));
return {
check: function(){
var expiryTime = cookies.get('ngMonitorExpiryTime');
var currentTime = Math.trunc(new Date().getTime() / 1000);
var warning = parseInt(expiryTime) - parseInt(_this.warning);
var timeout = parseInt(expiryTime) + parseInt(_this.timeout);
if(warning == currentTime){
$rootScope.$broadcast('warning');
}
if(expiryTime == currentTime){
$rootScope.$broadcast('idle');
}
if(timeout == currentTime){
$rootScope.$broadcast('timeout');
}
},
watchDom: function(){
angular.element($window).on(_this.events.join(' '), this.resetMonitor);
},
resetMonitor: function(){
_time = Math.trunc(new Date().getTime() / 1000);
cookies.put('ngMonitorExpiryTime', parseInt(_time) + parseInt(_this.idle));
$rootScope.$broadcast('domTouched');
},
stop: function(){
$interval.cancel(_intervalPromise);
},
start: function(){
cookies.put('ngMonitorExpiryTime', (parseInt(_time) + parseInt(_this.idle)));
this.watchDom();
_intervalPromise = $interval(this.check, _this.interval * 1000);
}
};
};
}).service('cookies', function(){
this.put = function(name, value, expires){
var d = new Date();
expires = expires || 999999;
d.setTime(d.getTime() + (expires*24*60*60*1000));
expires = "expires="+ d.toUTCString();
document.cookie = name + "=" + value + "; " + expires;
};
this.get = function(name){
var name = name + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return null;
};
});