diff --git a/angular-idle.js b/angular-idle.js index 33090e1..100a60b 100644 --- a/angular-idle.js +++ b/angular-idle.js @@ -1,6 +1,6 @@ /*** Directives and services for responding to idle users in AngularJS * @author Mike Grabski -* @version v1.0.4 +* @version v1.1.0 * @link https://github.com/HackedByChinese/ng-idle.git * @license MIT */ @@ -42,7 +42,6 @@ angular.module('ngIdle.keepalive', []) ping: null }; - function handleResponse(data, status) { $rootScope.$broadcast('KeepaliveResponse', data, status); } @@ -200,7 +199,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) function getExpiry() { var obj = LocalStorage.get('expiry'); - return new Date(obj.time); + return obj && obj.time ? new Date(obj.time) : null; } function setExpiry(date) { @@ -215,6 +214,12 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) _getNow: function() { return new Date(); }, + getIdle: function(){ + return options.idle; + }, + getTimeout: function(){ + return options.timeout; + }, setIdle: function(seconds) { changeOption(this, setIdle, seconds); }, @@ -223,7 +228,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) }, isExpired: function() { var expiry = getExpiry(); - return expiry && expiry <= this._getNow(); + return expiry !== null && expiry <= this._getNow(); }, running: function() { return state.running; @@ -290,34 +295,37 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) ]; }); -angular.module('ngIdle.countdown', []) - .directive('idleCountdown', function() { +angular.module('ngIdle.countdown', ['ngIdle.idle']) + .directive('idleCountdown', ['Idle', function(Idle) { return { restrict: 'A', scope: { value: '=idleCountdown' }, link: function($scope) { + // Initialize the scope's value to the configured timeout. + $scope.value = Idle.getTimeout(); + $scope.$on('IdleWarn', function(e, countdown) { - $scope.$apply(function() { + $scope.$evalAsync(function() { $scope.value = countdown; }); }); $scope.$on('IdleTimeout', function() { - $scope.$apply(function() { + $scope.$evalAsync(function() { $scope.value = 0; }); }); } }; - }); + }]); angular.module('ngIdle.title', []) .factory('Title', ['$document', '$interpolate', function($document, $interpolate) { function padLeft(nr, n, str){ - return Array(n-String(nr).length+1).join(str||'0')+nr; + return new Array(n-String(nr).length+1).join(str||'0')+nr; } var state = { @@ -377,6 +385,10 @@ angular.module('ngIdle.title', []) Title.store(true); + $scope.$on('IdleStart', function() { + Title.original($element[0].innerText); + }); + $scope.$on('IdleWarn', function(e, countdown) { Title.setAsIdle(countdown); }); diff --git a/angular-idle.map b/angular-idle.map index eecf9d3..43d962d 100644 --- a/angular-idle.map +++ b/angular-idle.map @@ -1 +1 @@ -{"version":3,"file":"angular-idle.min.js","sources":["angular-idle.js"],"names":["window","angular","undefined","module","provider","options","http","interval","this","value","Error","isString","url","method","cache","setInterval","seconds","parseInt","isNaN","$get","$rootScope","$log","$interval","$http","handleResponse","data","status","$broadcast","ping","isObject","success","error","state","_options","start","cancel","stop","idle","timeout","autoResume","interrupt","keepalive","setTimeout","isNumber","events","setIdle","enabled","$document","Keepalive","LocalStorage","$window","startKeepalive","running","stopKeepalive","toggleState","idling","name","countdown","changeOption","self","fn","reset","unwatch","watch","getExpiry","obj","get","Date","time","setExpiry","date","set","id","remove","getTime","svc","_getNow","isExpired","expiry","noExpiryUpdate","find","on","wrap","event","key","newValue","oldValue","val","fromJson","addEventListener","attachEvent","directive","restrict","scope","link","$scope","$on","e","$apply","factory","$interpolate","padLeft","nr","n","str","Array","String","length","join","original","timedout","isUndefined","store","overwrite","title","idleMessage","timedOutMessage","setAsIdle","remaining","totalSeconds","minutes","Math","floor","setAsTimedOut","restore","Title","$element","$attr","idleDisabled","service","storage","localStorage","setItem","toJson","getItem","removeItem"],"mappings":";;;;;;;CAMA,SAAUA,EAAQC,EAASC,GAC3B,YACAD,GAAQE,OAAO,UAAW,mBAAoB,cAAe,mBAAoB,eAAgB,wBACjGF,EAAQE,OAAO,uBACZC,SAAS,YAAa,WACrB,GAAIC,IACFC,KAAM,KACNC,SAAU,IAGZC,MAAKF,KAAO,SAASG,GACnB,IAAKA,EAAO,KAAM,IAAIC,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GAAavD,EAAM8E,cAE3C9E,EAAM8E,SAAWvB,IAEnB8B,UAAW,SAAS5D,GAClBjD,KAAKwG,OAEL,IAAIM,IAAcC,aAAc9D,EAChC6D,GAAUE,QAAUC,KAAKC,MAAMjE,EAAU,IACzC6D,EAAUtG,QAAUqF,EAAQ5C,EAAgC,GAApB6D,EAAUE,QAAc,GAEhEhH,KAAKC,MAAM2F,EAAa5F,KAAK2G,eAAeG,KAE9CK,cAAe,WACbnH,KAAKwG,QAELxG,KAAKC,MAAMD,KAAK4G,oBAElBQ,QAAS,WACHpH,KAAKqG,YAAYrG,KAAKC,MAAMD,KAAKqG,iBAI1ClB,UAAU,SAAU,QAAS,SAASkC,GACnC,OACEjC,SAAU,IACVE,KAAM,SAASC,EAAQ+B,EAAUC,GAC3BA,EAAMC,eAEVH,EAAMb,OAAM,GAEZjB,EAAOC,IAAI,WAAY,SAASC,EAAGxC,GACjCoE,EAAMR,UAAU5D,KAGlBsC,EAAOC,IAAI,UAAW,WACpB6B,EAAMD,YAGR7B,EAAOC,IAAI,cAAe,WACxB6B,EAAMF,wBAMlB1H,EAAQE,OAAO,0BACZ8H,QAAQ,oBAAqB,UAAW,SAAS/E,GAChD,GAAIgF,GAAUhF,EAAQiF,YAEtB,QACE5D,IAAK,SAASa,EAAK3E,GACjByH,EAAQE,QAAQ,UAAUhD,EAAKnF,EAAQoI,OAAO5H,KAEhDyD,IAAK,SAASkB,GACZ,MAAOnF,GAAQuF,SAAS0C,EAAQI,QAAQ,UAAUlD,KAEpDX,OAAQ,SAASW,GACf8C,EAAQK,WAAW,UAAUnD,SAKlCpF,OAAQA,OAAOC"} \ No newline at end of file +{"version":3,"file":"angular-idle.min.js","sources":["angular-idle.js"],"names":["window","angular","undefined","module","provider","options","http","interval","this","value","Error","isString","url","method","cache","setInterval","seconds","parseInt","isNaN","$get","$rootScope","$log","$interval","$http","handleResponse","data","status","$broadcast","ping","isObject","success","error","state","_options","start","cancel","stop","idle","timeout","autoResume","interrupt","keepalive","setTimeout","isNumber","events","setIdle","enabled","$document","Keepalive","LocalStorage","$window","startKeepalive","running","stopKeepalive","toggleState","idling","name","countdown","changeOption","self","fn","reset","unwatch","watch","getExpiry","obj","get","time","Date","setExpiry","date","set","id","remove","getTime","svc","_getNow","getIdle","getTimeout","isExpired","expiry","noExpiryUpdate","find","on","wrap","event","key","newValue","oldValue","val","fromJson","addEventListener","attachEvent","directive","Idle","restrict","scope","link","$scope","$on","e","$evalAsync","factory","$interpolate","padLeft","nr","n","str","Array","String","length","join","original","timedout","isUndefined","store","overwrite","title","idleMessage","timedOutMessage","setAsIdle","remaining","totalSeconds","minutes","Math","floor","setAsTimedOut","restore","Title","$element","$attr","idleDisabled","innerText","service","storage","localStorage","setItem","toJson","getItem","removeItem"],"mappings":";;;;;;;CAMA,SAAUA,EAAQC,EAASC,GAC3B,YACAD,GAAQE,OAAO,UAAW,mBAAoB,cAAe,mBAAoB,eAAgB,wBACjGF,EAAQE,OAAO,uBACZC,SAAS,YAAa,WACrB,GAAIC,IACFC,KAAM,KACNC,SAAU,IAGZC,MAAKF,KAAO,SAASG,GACnB,IAAKA,EAAO,KAAM,IAAIC,OAAM,sGACxBT,GAAQU,SAASF,KACnBA,GACEG,IAAKH,EACLI,OAAQ,QAIZJ,EAAMK,OAAQ,EAEdT,EAAQC,KAAOG,EAGjB,IAAIM,GAAcP,KAAKD,SAAW,SAASS,GAGzC,GAFAA,EAAUC,SAASD,GAEfE,MAAMF,IAAuB,GAAXA,EAAc,KAAM,IAAIN,OAAM,+DACpDL,GAAQE,SAAWS,EAGrBR,MAAKW,MAAQ,aAAc,OAAQ,YAAa,QAC9C,SAASC,EAAYC,EAAMC,EAAWC,GAMpC,QAASC,GAAeC,EAAMC,GAC5BN,EAAWO,WAAW,oBAAqBF,EAAMC,GAGnD,QAASE,KACPR,EAAWO,WAAW,aAElB1B,EAAQ4B,SAASxB,EAAQC,OAC3BiB,EAAMlB,EAAQC,MACXwB,QAAQN,GACRO,MAAMP,GAdb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EL3G,KAAKC,MAAMD,KAAK+G,oBAElBQ,QAAS,WACHvH,KAAKwG,YAAYxG,KAAKC,MAAMD,KAAKwG,iBAI1CnB,UAAU,SAAU,QAAS,SAASmC,GACnC,OACEjC,SAAU,IACVE,KAAM,SAASC,EAAQ+B,EAAUC,GAC3BA,EAAMC,eAEVH,EAAMb,OAAM,GAEZjB,EAAOC,IAAI,YAAa,WACtB6B,EAAMhB,SAASiB,EAAS,GAAGG,aAG7BlC,EAAOC,IAAI,WAAY,SAASC,EAAG3C,GACjCuE,EAAMR,UAAU/D,KAGlByC,EAAOC,IAAI,UAAW,WACpB6B,EAAMD,YAGR7B,EAAOC,IAAI,cAAe,WACxB6B,EAAMF,wBAMlB7H,EAAQE,OAAO,0BACZkI,QAAQ,oBAAqB,UAAW,SAASnF,GAChD,GAAIoF,GAAUpF,EAAQqF,YAEtB,QACEhE,IAAK,SAASe,EAAK7E,GACjB6H,EAAQE,QAAQ,UAAUlD,EAAKrF,EAAQwI,OAAOhI,KAEhDyD,IAAK,SAASoB,GACZ,MAAOrF,GAAQyF,SAAS4C,EAAQI,QAAQ,UAAUpD,KAEpDb,OAAQ,SAASa,GACfgD,EAAQK,WAAW,UAAUrD,SAKlCtF,OAAQA,OAAOC"} \ No newline at end of file diff --git a/angular-idle.min.js b/angular-idle.min.js index bd96bcf..479726c 100644 --- a/angular-idle.min.js +++ b/angular-idle.min.js @@ -1,9 +1,9 @@ /*** Directives and services for responding to idle users in AngularJS * @author Mike Grabski -* @version v1.0.4 +* @version v1.1.0 * @link https://github.com/HackedByChinese/ng-idle.git * @license MIT */ -!function(a,b,c){"use strict";b.module("ngIdle",["ngIdle.keepalive","ngIdle.idle","ngIdle.countdown","ngIdle.title","ngIdle.localStorage"]),b.module("ngIdle.keepalive",[]).provider("Keepalive",function(){var a={http:null,interval:600};this.http=function(c){if(!c)throw new Error("Argument must be a string containing a URL, or an object containing the HTTP request configuration.");b.isString(c)&&(c={url:c,method:"GET"}),c.cache=!1,a.http=c};var c=this.interval=function(b){if(b=parseInt(b),isNaN(b)||0>=b)throw new Error("Interval must be expressed in seconds and be greater than 0.");a.interval=b};this.$get=["$rootScope","$log","$interval","$http",function(d,e,f,g){function h(a,b){d.$broadcast("KeepaliveResponse",a,b)}function i(){d.$broadcast("Keepalive"),b.isObject(a.http)&&g(a.http).success(h).error(h)}var j={ping:null};return{_options:function(){return a},setInterval:c,start:function(){return f.cancel(j.ping),j.ping=f(i,1e3*a.interval),j.ping},stop:function(){f.cancel(j.ping)},ping:function(){i()}}}]}),b.module("ngIdle.idle",["ngIdle.keepalive","ngIdle.localStorage"]).provider("Idle",function(){var a={idle:1200,timeout:30,autoResume:"idle",interrupt:"mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove scroll",keepalive:!0},c=this.timeout=function(c){if(c===!1)a.timeout=0;else{if(!(b.isNumber(c)&&c>=0))throw new Error("Timeout must be zero or false to disable the feature, or a positive integer (in seconds) to enable it.");a.timeout=c}};this.interrupt=function(b){a.interrupt=b};var d=this.idle=function(b){if(0>=b)throw new Error("Idle must be a value in seconds, greater than 0.");a.idle=b};this.autoResume=function(b){a.autoResume=b===!0?"idle":b===!1?"off":b},this.keepalive=function(b){a.keepalive=b===!0},this.$get=["$interval","$log","$rootScope","$document","Keepalive","IdleLocalStorage","$window",function(e,f,g,h,i,j,k){function l(){a.keepalive&&(t.running&&i.ping(),i.start())}function m(){a.keepalive&&i.stop()}function n(){t.idling=!t.idling;var b=t.idling?"Start":"End";g.$broadcast("Idle"+b),t.idling?(m(),a.timeout&&(t.countdown=a.timeout,o(),t.timeout=e(o,1e3,a.timeout,!1))):l(),e.cancel(t.idle)}function o(){return t.countdown<=0?void p():(g.$broadcast("IdleWarn",t.countdown),void t.countdown--)}function p(){m(),e.cancel(t.idle),e.cancel(t.timeout),t.idling=!0,t.running=!1,t.countdown=0,g.$broadcast("IdleTimeout")}function q(a,b,c){var d=a.running();a.unwatch(),b(c),d&&a.watch()}function r(){var a=j.get("expiry");return new Date(a.time)}function s(a){a?j.set("expiry",{id:u,time:a}):j.remove("expiry")}var t={idle:null,timeout:null,idling:!1,running:!1,countdown:null},u=(new Date).getTime(),v={_options:function(){return a},_getNow:function(){return new Date},setIdle:function(a){q(this,d,a)},setTimeout:function(a){q(this,c,a)},isExpired:function(){var a=r();return a&&a<=this._getNow()},running:function(){return t.running},idling:function(){return t.idling},watch:function(b){e.cancel(t.idle),e.cancel(t.timeout);var c=a.timeout?a.timeout:0;b||s(new Date((new Date).getTime()+1e3*(a.idle+c))),t.idling?n():t.running||l(),t.running=!0,t.idle=e(n,1e3*a.idle,0,!1)},unwatch:function(){e.cancel(t.idle),e.cancel(t.timeout),t.idling=!1,t.running=!1,s(null),m()},interrupt:function(b){return t.running?a.timeout&&this.isExpired()?void p():void(("idle"===a.autoResume||"notIdle"===a.autoResume&&!t.idling)&&this.watch(b)):void 0}};h.find("body").on(a.interrupt,function(){v.interrupt()});var w=function(a){if("ngIdle.expiry"===a.key&&a.newValue!==a.oldValue){var c=b.fromJson(a.newValue);if(c.id===u)return;v.interrupt(!0)}};return k.addEventListener?k.addEventListener("storage",w,!1):k.attachEvent("onstorage",w),v}]}),b.module("ngIdle.countdown",[]).directive("idleCountdown",function(){return{restrict:"A",scope:{value:"=idleCountdown"},link:function(a){a.$on("IdleWarn",function(b,c){a.$apply(function(){a.value=c})}),a.$on("IdleTimeout",function(){a.$apply(function(){a.value=0})})}}}),b.module("ngIdle.title",[]).factory("Title",["$document","$interpolate",function(a,c){function d(a,b,c){return Array(b-String(a).length+1).join(c||"0")+a}var e={original:null,idle:"{{minutes}}:{{seconds}} until your session times out!",timedout:"Your session has expired."};return{original:function(a){return b.isUndefined(a)?e.original:void(e.original=a)},store:function(a){(a||!e.original)&&(e.original=this.value())},value:function(c){return b.isUndefined(c)?a[0].title:void(a[0].title=c)},idleMessage:function(a){return b.isUndefined(a)?e.idle:void(e.idle=a)},timedOutMessage:function(a){return b.isUndefined(a)?e.timedout:void(e.timedout=a)},setAsIdle:function(a){this.store();var b={totalSeconds:a};b.minutes=Math.floor(a/60),b.seconds=d(a-60*b.minutes,2),this.value(c(this.idleMessage())(b))},setAsTimedOut:function(){this.store(),this.value(this.timedOutMessage())},restore:function(){this.original()&&this.value(this.original())}}}]).directive("title",["Title",function(a){return{restrict:"E",link:function(b,c,d){d.idleDisabled||(a.store(!0),b.$on("IdleWarn",function(b,c){a.setAsIdle(c)}),b.$on("IdleEnd",function(){a.restore()}),b.$on("IdleTimeout",function(){a.setAsTimedOut()}))}}}]),b.module("ngIdle.localStorage",[]).service("IdleLocalStorage",["$window",function(a){var c=a.localStorage;return{set:function(a,d){c.setItem("ngIdle."+a,b.toJson(d))},get:function(a){return b.fromJson(c.getItem("ngIdle."+a))},remove:function(a){c.removeItem("ngIdle."+a)}}}])}(window,window.angular); +!function(a,b,c){"use strict";b.module("ngIdle",["ngIdle.keepalive","ngIdle.idle","ngIdle.countdown","ngIdle.title","ngIdle.localStorage"]),b.module("ngIdle.keepalive",[]).provider("Keepalive",function(){var a={http:null,interval:600};this.http=function(c){if(!c)throw new Error("Argument must be a string containing a URL, or an object containing the HTTP request configuration.");b.isString(c)&&(c={url:c,method:"GET"}),c.cache=!1,a.http=c};var c=this.interval=function(b){if(b=parseInt(b),isNaN(b)||0>=b)throw new Error("Interval must be expressed in seconds and be greater than 0.");a.interval=b};this.$get=["$rootScope","$log","$interval","$http",function(d,e,f,g){function h(a,b){d.$broadcast("KeepaliveResponse",a,b)}function i(){d.$broadcast("Keepalive"),b.isObject(a.http)&&g(a.http).success(h).error(h)}var j={ping:null};return{_options:function(){return a},setInterval:c,start:function(){return f.cancel(j.ping),j.ping=f(i,1e3*a.interval),j.ping},stop:function(){f.cancel(j.ping)},ping:function(){i()}}}]}),b.module("ngIdle.idle",["ngIdle.keepalive","ngIdle.localStorage"]).provider("Idle",function(){var a={idle:1200,timeout:30,autoResume:"idle",interrupt:"mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove scroll",keepalive:!0},c=this.timeout=function(c){if(c===!1)a.timeout=0;else{if(!(b.isNumber(c)&&c>=0))throw new Error("Timeout must be zero or false to disable the feature, or a positive integer (in seconds) to enable it.");a.timeout=c}};this.interrupt=function(b){a.interrupt=b};var d=this.idle=function(b){if(0>=b)throw new Error("Idle must be a value in seconds, greater than 0.");a.idle=b};this.autoResume=function(b){b===!0?a.autoResume="idle":b===!1?a.autoResume="off":a.autoResume=b},this.keepalive=function(b){a.keepalive=b===!0},this.$get=["$interval","$log","$rootScope","$document","Keepalive","IdleLocalStorage","$window",function(e,f,g,h,i,j,k){function l(){a.keepalive&&(t.running&&i.ping(),i.start())}function m(){a.keepalive&&i.stop()}function n(){t.idling=!t.idling;var b=t.idling?"Start":"End";g.$broadcast("Idle"+b),t.idling?(m(),a.timeout&&(t.countdown=a.timeout,o(),t.timeout=e(o,1e3,a.timeout,!1))):l(),e.cancel(t.idle)}function o(){return t.countdown<=0?void p():(g.$broadcast("IdleWarn",t.countdown),void t.countdown--)}function p(){m(),e.cancel(t.idle),e.cancel(t.timeout),t.idling=!0,t.running=!1,t.countdown=0,g.$broadcast("IdleTimeout")}function q(a,b,c){var d=a.running();a.unwatch(),b(c),d&&a.watch()}function r(){var a=j.get("expiry");return a&&a.time?new Date(a.time):null}function s(a){a?j.set("expiry",{id:u,time:a}):j.remove("expiry")}var t={idle:null,timeout:null,idling:!1,running:!1,countdown:null},u=(new Date).getTime(),v={_options:function(){return a},_getNow:function(){return new Date},getIdle:function(){return a.idle},getTimeout:function(){return a.timeout},setIdle:function(a){q(this,d,a)},setTimeout:function(a){q(this,c,a)},isExpired:function(){var a=r();return null!==a&&a<=this._getNow()},running:function(){return t.running},idling:function(){return t.idling},watch:function(b){e.cancel(t.idle),e.cancel(t.timeout);var c=a.timeout?a.timeout:0;b||s(new Date((new Date).getTime()+1e3*(a.idle+c))),t.idling?n():t.running||l(),t.running=!0,t.idle=e(n,1e3*a.idle,0,!1)},unwatch:function(){e.cancel(t.idle),e.cancel(t.timeout),t.idling=!1,t.running=!1,s(null),m()},interrupt:function(b){return t.running?a.timeout&&this.isExpired()?void p():void(("idle"===a.autoResume||"notIdle"===a.autoResume&&!t.idling)&&this.watch(b)):void 0}};h.find("body").on(a.interrupt,function(){v.interrupt()});var w=function(a){if("ngIdle.expiry"===a.key&&a.newValue!==a.oldValue){var c=b.fromJson(a.newValue);if(c.id===u)return;v.interrupt(!0)}};return k.addEventListener?k.addEventListener("storage",w,!1):k.attachEvent("onstorage",w),v}]}),b.module("ngIdle.countdown",["ngIdle.idle"]).directive("idleCountdown",["Idle",function(a){return{restrict:"A",scope:{value:"=idleCountdown"},link:function(b){b.value=a.getTimeout(),b.$on("IdleWarn",function(a,c){b.$evalAsync(function(){b.value=c})}),b.$on("IdleTimeout",function(){b.$evalAsync(function(){b.value=0})})}}}]),b.module("ngIdle.title",[]).factory("Title",["$document","$interpolate",function(a,c){function d(a,b,c){return new Array(b-String(a).length+1).join(c||"0")+a}var e={original:null,idle:"{{minutes}}:{{seconds}} until your session times out!",timedout:"Your session has expired."};return{original:function(a){return b.isUndefined(a)?e.original:void(e.original=a)},store:function(a){(a||!e.original)&&(e.original=this.value())},value:function(c){return b.isUndefined(c)?a[0].title:void(a[0].title=c)},idleMessage:function(a){return b.isUndefined(a)?e.idle:void(e.idle=a)},timedOutMessage:function(a){return b.isUndefined(a)?e.timedout:void(e.timedout=a)},setAsIdle:function(a){this.store();var b={totalSeconds:a};b.minutes=Math.floor(a/60),b.seconds=d(a-60*b.minutes,2),this.value(c(this.idleMessage())(b))},setAsTimedOut:function(){this.store(),this.value(this.timedOutMessage())},restore:function(){this.original()&&this.value(this.original())}}}]).directive("title",["Title",function(a){return{restrict:"E",link:function(b,c,d){d.idleDisabled||(a.store(!0),b.$on("IdleStart",function(){a.original(c[0].innerText)}),b.$on("IdleWarn",function(b,c){a.setAsIdle(c)}),b.$on("IdleEnd",function(){a.restore()}),b.$on("IdleTimeout",function(){a.setAsTimedOut()}))}}}]),b.module("ngIdle.localStorage",[]).service("IdleLocalStorage",["$window",function(a){var c=a.localStorage;return{set:function(a,d){c.setItem("ngIdle."+a,b.toJson(d))},get:function(a){return b.fromJson(c.getItem("ngIdle."+a))},remove:function(a){c.removeItem("ngIdle."+a)}}}])}(window,window.angular); //# sourceMappingURL=angular-idle.map \ No newline at end of file diff --git a/bower.json b/bower.json index 67e09b3..27d81b7 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ng-idle", - "version": "1.0.4", + "version": "1.1.0", "homepage": "https://github.com/HackedByChinese/ng-idle", "description": "Responding to idle users in AngularJS", "main": "angular-idle.js", diff --git a/package.json b/package.json index 1b2ef84..7dca919 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,17 @@ { "name": "ng-idle", - "version": "1.0.4", + "version": "1.1.0", "description": "Directives and services for responding to idle users in AngularJS", + "keywords": [ + "angularjs", + "idle", + "ng-idle", + "keepalive", + "ping", + "angular", + "user", + "session" + ], "main": [ "angular-idle.js", "angular-idle.min.js", @@ -19,6 +29,9 @@ "bugs": { "url": "https://github.com/HackedByChinese/ng-idle/issues" }, + "dependencies": { + "angular": "^1.2.0" + }, "devDependencies": { "grunt": "~0.4.2", "grunt-bump": "0.0.13", @@ -39,7 +52,6 @@ "karma-script-launcher": "~0.1.0", "matchdep": "~0.3.0", "requirejs": "~2.1.10", - "angular": "^1.2.0", "angular-mocks": "^1.2.0", "jquery": "^2.0.3" } diff --git a/sample/index.html b/sample/index.html new file mode 100644 index 0000000..670c299 --- /dev/null +++ b/sample/index.html @@ -0,0 +1,82 @@ + + + NgIdle Sample + + + + + +
+

Idle and Keepalive events

+ +
    +
  • {{event}}
  • +
+ +
+ Timeout in {{countdown}} seconds. +
+ +
+ Change idle value +
+
+ Change timeout value +
+
+ + diff --git a/src/countdown/countdown.js b/src/countdown/countdown.js index dfa9e13..a111ae0 100644 --- a/src/countdown/countdown.js +++ b/src/countdown/countdown.js @@ -1,22 +1,25 @@ -angular.module('ngIdle.countdown', []) - .directive('idleCountdown', function() { +angular.module('ngIdle.countdown', ['ngIdle.idle']) + .directive('idleCountdown', ['Idle', function(Idle) { return { restrict: 'A', scope: { value: '=idleCountdown' }, link: function($scope) { + // Initialize the scope's value to the configured timeout. + $scope.value = Idle.getTimeout(); + $scope.$on('IdleWarn', function(e, countdown) { - $scope.$apply(function() { + $scope.$evalAsync(function() { $scope.value = countdown; }); }); $scope.$on('IdleTimeout', function() { - $scope.$apply(function() { + $scope.$evalAsync(function() { $scope.value = 0; }); }); } }; - }); + }]); diff --git a/src/countdown/countdown.spec.js b/src/countdown/countdown.spec.js index 36c7526..2475568 100644 --- a/src/countdown/countdown.spec.js +++ b/src/countdown/countdown.spec.js @@ -13,11 +13,12 @@ describe('ngIdle', function() { describe('idle-countdown', function() { beforeEach(module('ngIdle.countdown')); - var $compile, $scope, create; + var $compile, $scope, create, Idle; - beforeEach(inject(function(_$rootScope_, _$compile_) { + beforeEach(inject(function(_$rootScope_, _$compile_, _Idle_) { $scope = _$rootScope_; $compile = _$compile_; + Idle = _Idle_; create = function() { var el = $compile(angular.element('
{{countdown}} seconds remaining.
'))($scope); @@ -26,6 +27,13 @@ describe('ngIdle', function() { }; })); + it('should initialize the countdown scope value to the Idle timeout', function() { + var randomTimeout = Math.floor(Math.random() * 100); + Idle.setTimeout(randomTimeout); + create(); + expect($scope.countdown).toBe(randomTimeout); + }); + it('should update countdown scope value when receiving new IdleWarning event', function() { create(); diff --git a/src/idle/idle.js b/src/idle/idle.js index 826607b..79b5ebd 100644 --- a/src/idle/idle.js +++ b/src/idle/idle.js @@ -119,7 +119,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) function getExpiry() { var obj = LocalStorage.get('expiry'); - return new Date(obj.time); + return obj && obj.time ? new Date(obj.time) : null; } function setExpiry(date) { @@ -134,6 +134,12 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) _getNow: function() { return new Date(); }, + getIdle: function(){ + return options.idle; + }, + getTimeout: function(){ + return options.timeout; + }, setIdle: function(seconds) { changeOption(this, setIdle, seconds); }, @@ -142,7 +148,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) }, isExpired: function() { var expiry = getExpiry(); - return expiry && expiry <= this._getNow(); + return expiry !== null && expiry <= this._getNow(); }, running: function() { return state.running; diff --git a/src/idle/idle.spec.js b/src/idle/idle.spec.js index b7120cb..4790456 100644 --- a/src/idle/idle.spec.js +++ b/src/idle/idle.spec.js @@ -158,6 +158,16 @@ describe('ngIdle', function() { expect(Idle.watch).toHaveBeenCalled(); }); + it ('getIdle() should return the current idle value', function(){ + Idle.setIdle(100); + expect(Idle.getIdle()).toBe(100); + }); + + it ('getTimeout() should return the current timeout value', function(){ + Idle.setTimeout(100); + expect(Idle.getTimeout()).toBe(100); + }); + it ('setTimeout() should update option.timeout and restart', function() { spyOn(Idle, 'watch'); spyOn(Idle, 'unwatch'); @@ -332,6 +342,10 @@ describe('ngIdle', function() { expect(Idle.isExpired()).toBe(true); }); + it ('isExpired() should return false if there is no expiry yet', function() { + expect(Idle.isExpired()).toBe(false); + }); + it ('interrupt() should broadcast $timeout if running and past expiry', function() { spyOn($rootScope, '$broadcast'); @@ -376,15 +390,15 @@ describe('ngIdle', function() { // in practice, the functionality works fine, but here the test always fails. dunno how to fix it right now. // it ('document event should interrupt idle timeout', function() { - // Idle.watch(); - // $timeout.flush(); + // Idle.watch(); + // $timeout.flush(); - // expect(Idle.idling()).toBe(true); + // expect(Idle.idling()).toBe(true); - // var e = $.Event('click'); - // $('body').trigger(e); + // var e = $.Event('click'); + // $('body').trigger(e); - // expect(Idle.idling()).toBe(false); + // expect(Idle.idling()).toBe(false); // }); }); diff --git a/src/keepalive/keepalive.js b/src/keepalive/keepalive.js index 9a4db08..5701c23 100644 --- a/src/keepalive/keepalive.js +++ b/src/keepalive/keepalive.js @@ -33,7 +33,6 @@ angular.module('ngIdle.keepalive', []) ping: null }; - function handleResponse(data, status) { $rootScope.$broadcast('KeepaliveResponse', data, status); } diff --git a/src/title/title.js b/src/title/title.js index 70cde2b..b624258 100644 --- a/src/title/title.js +++ b/src/title/title.js @@ -2,7 +2,7 @@ angular.module('ngIdle.title', []) .factory('Title', ['$document', '$interpolate', function($document, $interpolate) { function padLeft(nr, n, str){ - return Array(n-String(nr).length+1).join(str||'0')+nr; + return new Array(n-String(nr).length+1).join(str||'0')+nr; } var state = { @@ -62,6 +62,10 @@ angular.module('ngIdle.title', []) Title.store(true); + $scope.$on('IdleStart', function() { + Title.original($element[0].innerText); + }); + $scope.$on('IdleWarn', function(e, countdown) { Title.setAsIdle(countdown); }); diff --git a/src/title/title.spec.js b/src/title/title.spec.js index b3b887d..f61a298 100644 --- a/src/title/title.spec.js +++ b/src/title/title.spec.js @@ -115,6 +115,7 @@ describe('ngIdle', function() { spyOn(Title, 'setAsTimedOut'); spyOn(Title, 'restore'); spyOn(Title, 'store'); + spyOn(Title, 'original'); create = function(template) { var el = $compile(angular.element(template||'Hello World'))($scope); @@ -133,6 +134,14 @@ describe('ngIdle', function() { expect(Title.store).toHaveBeenCalledWith(true); }); + it('should set original title on IdleStart event', function() { + create('Dynamic Title'); + $scope.$broadcast('IdleStart'); + $scope.$apply(); + + expect(Title.original).toHaveBeenCalledWith('Dynamic Title'); + }); + it('should set title to idle message on IdleWarn event', function() { $scope.$broadcast('IdleWarn', 5); $scope.$apply(); diff --git a/test/index.html b/test/index.html deleted file mode 100644 index 42412c4..0000000 --- a/test/index.html +++ /dev/null @@ -1,74 +0,0 @@ - - - NgIdle Sample - - - - - -
-

Idle and Keepalive events

- -
    -
  • {{event}}
  • -
- - Timeout in {{countdown}} seconds. - -
- Change idle value -
-
- Change timeout value -
-
- -