1
1
/*** Directives and services for responding to idle users in AngularJS
2
2
* @author Mike Grabski <me@mikegrabski.com>
3
- * @version v1.0.4
3
+ * @version v1.1.0
4
4
* @link https://github.com/HackedByChinese/ng-idle.git
5
5
* @license MIT
6
6
*/
@@ -42,7 +42,6 @@ angular.module('ngIdle.keepalive', [])
42
42
ping : null
43
43
} ;
44
44
45
-
46
45
function handleResponse ( data , status ) {
47
46
$rootScope . $broadcast ( 'KeepaliveResponse' , data , status ) ;
48
47
}
@@ -200,7 +199,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
200
199
function getExpiry ( ) {
201
200
var obj = LocalStorage . get ( 'expiry' ) ;
202
201
203
- return new Date ( obj . time ) ;
202
+ return obj && obj . time ? new Date ( obj . time ) : null ;
204
203
}
205
204
206
205
function setExpiry ( date ) {
@@ -215,6 +214,12 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
215
214
_getNow : function ( ) {
216
215
return new Date ( ) ;
217
216
} ,
217
+ getIdle : function ( ) {
218
+ return options . idle ;
219
+ } ,
220
+ getTimeout : function ( ) {
221
+ return options . timeout ;
222
+ } ,
218
223
setIdle : function ( seconds ) {
219
224
changeOption ( this , setIdle , seconds ) ;
220
225
} ,
@@ -223,7 +228,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
223
228
} ,
224
229
isExpired : function ( ) {
225
230
var expiry = getExpiry ( ) ;
226
- return expiry && expiry <= this . _getNow ( ) ;
231
+ return expiry !== null && expiry <= this . _getNow ( ) ;
227
232
} ,
228
233
running : function ( ) {
229
234
return state . running ;
@@ -290,34 +295,37 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
290
295
] ;
291
296
} ) ;
292
297
293
- angular . module ( 'ngIdle.countdown' , [ ] )
294
- . directive ( 'idleCountdown' , function ( ) {
298
+ angular . module ( 'ngIdle.countdown' , [ 'ngIdle.idle' ] )
299
+ . directive ( 'idleCountdown' , [ 'Idle' , function ( Idle ) {
295
300
return {
296
301
restrict : 'A' ,
297
302
scope : {
298
303
value : '=idleCountdown'
299
304
} ,
300
305
link : function ( $scope ) {
306
+ // Initialize the scope's value to the configured timeout.
307
+ $scope . value = Idle . getTimeout ( ) ;
308
+
301
309
$scope . $on ( 'IdleWarn' , function ( e , countdown ) {
302
- $scope . $apply ( function ( ) {
310
+ $scope . $evalAsync ( function ( ) {
303
311
$scope . value = countdown ;
304
312
} ) ;
305
313
} ) ;
306
314
307
315
$scope . $on ( 'IdleTimeout' , function ( ) {
308
- $scope . $apply ( function ( ) {
316
+ $scope . $evalAsync ( function ( ) {
309
317
$scope . value = 0 ;
310
318
} ) ;
311
319
} ) ;
312
320
}
313
321
} ;
314
- } ) ;
322
+ } ] ) ;
315
323
316
324
angular . module ( 'ngIdle.title' , [ ] )
317
325
. factory ( 'Title' , [ '$document' , '$interpolate' , function ( $document , $interpolate ) {
318
326
319
327
function padLeft ( nr , n , str ) {
320
- return Array ( n - String ( nr ) . length + 1 ) . join ( str || '0' ) + nr ;
328
+ return new Array ( n - String ( nr ) . length + 1 ) . join ( str || '0' ) + nr ;
321
329
}
322
330
323
331
var state = {
@@ -377,6 +385,10 @@ angular.module('ngIdle.title', [])
377
385
378
386
Title . store ( true ) ;
379
387
388
+ $scope . $on ( 'IdleStart' , function ( ) {
389
+ Title . original ( $element [ 0 ] . innerText ) ;
390
+ } ) ;
391
+
380
392
$scope . $on ( 'IdleWarn' , function ( e , countdown ) {
381
393
Title . setAsIdle ( countdown ) ;
382
394
} ) ;
0 commit comments