diff --git a/README.md b/README.md index ec62556..054ea62 100644 --- a/README.md +++ b/README.md @@ -13,59 +13,63 @@ Authored by Mike Grabski @HackedByChinese Licensed under [MIT](http://www.opensource.org/licenses/mit-license.php) ## Requirements -* Angular 1.2.0 or later (earlier might be possible but not tested). +* Angular 1.2.0 or later. ## What NgIdle Does Check out the Overview in the wiki. +## Getting Help / "How do I..." + +I know a lot of GH projects give you the basics on the README and don't bother with a wiki. I assure you [our wiki is fully operational](https://github.com/HackedByChinese/ng-idle/wiki) and documents the full API. Before opening an issue asking me how to do something, please stop by the wiki first; I'll probably just end up linking you to your answer in the wiki anyways :wink:. + ## Getting Started Include `angular-idle.js` after `angular.js`. You can install using Bower with this command: `bower install --save ng-idle`. Bare bones example: - // include the `ngIdle` module - var app = angular.module('demo', ['ngIdle']); - - app - .controller('EventsCtrl', function($scope, Idle) { - $scope.events = []; - - $scope.$on('IdleStart', function() { - // the user appears to have gone idle - }); - - $scope.$on('IdleWarn', function(e, countdown) { - // follows after the IdleStart event, but includes a countdown until the user is considered timed out - // the countdown arg is the number of seconds remaining until then. - // you can change the title or display a warning dialog from here. - // you can let them resume their session by calling Idle.watch() - }); - - $scope.$on('IdleTimeout', function() { - // the user has timed out (meaning idleDuration + timeout has passed without any activity) - // this is where you'd log them - }); - - $scope.$on('IdleEnd', function() { - // the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog - }); - - $scope.$on('Keepalive', function() { - // do something to keep the user's session alive - }); - - }) - .config(function(IdleProvider, KeepaliveProvider) { - // configure Idle settings - IdleProvider.idle(5); // in seconds - IdleProvider.timeout(5); // in seconds - KeepaliveProvider.interval(2); // in seconds - }) - .run(function(Idle){ - // start watching when the app runs. also starts the Keepalive service by default. - Idle.watch(); - }); + // include the `ngIdle` module + var app = angular.module('demo', ['ngIdle']); + + app + .controller('EventsCtrl', function($scope, Idle) { + $scope.events = []; + + $scope.$on('IdleStart', function() { + // the user appears to have gone idle + }); + + $scope.$on('IdleWarn', function(e, countdown) { + // follows after the IdleStart event, but includes a countdown until the user is considered timed out + // the countdown arg is the number of seconds remaining until then. + // you can change the title or display a warning dialog from here. + // you can let them resume their session by calling Idle.watch() + }); + + $scope.$on('IdleTimeout', function() { + // the user has timed out (meaning idleDuration + timeout has passed without any activity) + // this is where you'd log them + }); + + $scope.$on('IdleEnd', function() { + // the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog + }); + + $scope.$on('Keepalive', function() { + // do something to keep the user's session alive + }); + + }) + .config(function(IdleProvider, KeepaliveProvider) { + // configure Idle settings + IdleProvider.idle(5); // in seconds + IdleProvider.timeout(5); // in seconds + KeepaliveProvider.interval(2); // in seconds + }) + .run(function(Idle){ + // start watching when the app runs. also starts the Keepalive service by default. + Idle.watch(); + }); You may use `Keepalive` and `Idle` independently if you desire, but they are contained in the same script. @@ -83,7 +87,7 @@ Contributors are welcome. I use the `git-flow` lifecyle, so `master` is the stab ## Developing -You will need Node/NPM, Grunt, and Bower. Once you checkout from git, run `npm install`. This will install all dev and bower dependencies so you can immediately build and test your working copy. +You will need Node/NPM and Grunt (don't forget `grunt-cli`). Once you checkout from git, run `npm install`. This will install all dev and bower dependencies so you can immediately build and test your working copy. ### Building You can build the module by running `grunt build`. diff --git a/angular-idle.js b/angular-idle.js index 57e41a3..f8e55ba 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.2 +* @version v1.0.3 * @link https://github.com/HackedByChinese/ng-idle.git * @license MIT */ @@ -200,7 +200,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) function getExpiry() { var obj = LocalStorage.get('expiry'); - return obj.time; + return new Date(obj.time); } function setExpiry(date) { @@ -276,7 +276,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) var wrap = function(event) { if (event.key === 'ngIdle.expiry' && event.newValue !== event.oldValue) { - var val = LocalStorage.parseJson(event.newValue); + var val = angular.fromJson(event.newValue); if (val.id === id) return; svc.interrupt(true); } @@ -393,36 +393,18 @@ angular.module('ngIdle.title', []) }]); angular.module('ngIdle.localStorage', []) - .factory('IdleLocalStorage', ['$window', function($window) { + .service('IdleLocalStorage', ['$window', function($window) { var storage = $window.localStorage; - - function tryParseJson(value) { - try { - return JSON.parse(value, function(key, value) { - var match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); - if (match) return new Date(value); - - return value; - }); - } - catch(e) { - return value; - } - } - + return { set: function(key, value) { - storage.setItem('ngIdle.'+key, JSON.stringify(value)); + storage.setItem('ngIdle.'+key, angular.toJson(value)); }, get: function(key) { - var raw = storage.getItem('ngIdle.'+key); - return tryParseJson(raw); + return angular.fromJson(storage.getItem('ngIdle.'+key)); }, remove: function(key) { storage.removeItem('ngIdle.'+key); - }, - parseJson: function(raw) { - return tryParseJson(raw); } }; }]); diff --git a/angular-idle.map b/angular-idle.map index e780a7e..46fd522 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","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","setExpiry","date","set","id","remove","Date","getTime","svc","_getNow","isExpired","expiry","noExpiryUpdate","find","on","wrap","event","key","newValue","oldValue","val","parseJson","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","tryParseJson","JSON","parse","match","exec","storage","localStorage","setItem","stringify","raw","getItem","removeItem"],"mappings":";;;;;;;CAMA,SAAUA,EAAQC,GAClB,YACAA,GAAQC,OAAO,UAAW,mBAAoB,cAAe,mBAAoB,eAAgB,wBACjGD,EAAQC,OAAO,uBACZC,SAAS,YAAa,WACrB,GAAIC,IACFC,KAAM,KACNC,SAAU,IAGZC,MAAKF,KAAO,SAASG,GACnB,IAAKA,EAAO,KAAM,IAAIC,OAAM,sGACxBR,GAAQS,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,GAOpC,QAASC,GAAeC,EAAMC,GAC5BN,EAAWO,WAAW,oBAAqBF,EAAMC,GAGnD,QAASE,KACPR,EAAWO,WAAW,aAElBzB,EAAQ2B,SAASxB,EAAQC,OAC3BiB,EAAMlB,EAAQC,MACXwB,QAAQN,GACRO,MAAMP,GAfb,GAAIQ,IACFJ,KAAM,KAkBR,QACEK,SAAU,WACR,MAAO5B,IAETU,YAAaA,EACbmB,MAAO,WAIL,MAHAZ,GAAUa,OAAOH,EAAMJ,MAEvBI,EAAMJ,KAAON,EAAUM,EAAyB,IAAnBvB,EAAQE,UAC9ByB,EAAMJ,MAEfQ,KAAM,WACJd,EAAUa,OAAOH,EAAMJ,OAEzBA,KAAM,WACJA,UAOZ1B,EAAQC,OAAO,eAAgB,mBAAoB,wBAChDC,SAAS,OAAQ,WAChB,GAAIC,IACFgC,KAAM,KACNC,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,GAAI3D,MAAKhE,GAEpBA,IAGX,MAAMwF,GACJ,MAAOxF,IAZX,GAAI6H,GAAUpF,EAAQqF,YAgBtB,QACEjE,IAAK,SAASc,EAAK3E,GACjB6H,EAAQE,QAAQ,UAAUpD,EAAK8C,KAAKO,UAAUhI,KAEhDyD,IAAK,SAASkB,GACZ,GAAIsD,GAAMJ,EAAQK,QAAQ,UAAUvD,EACpC,OAAO6C,GAAaS,IAEtBlE,OAAQ,SAASY,GACfkD,EAAQM,WAAW,UAAUxD,IAE/BI,UAAW,SAASkD,GAClB,MAAOT,GAAaS,SAKzBzI,OAAQA,OAAOC"} \ No newline at end of file +{"version":3,"file":"angular-idle.min.js","sources":["angular-idle.js"],"names":["window","angular","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,GAClB,YACAA,GAAQC,OAAO,UAAW,mBAAoB,cAAe,mBAAoB,eAAgB,wBACjGD,EAAQC,OAAO,uBACZC,SAAS,YAAa,WACrB,GAAIC,IACFC,KAAM,KACNC,SAAU,IAGZC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AEf6B,gBAAiB,SAAS7B,GACxB,MAAIrF,GAAQ6G,YAAYxB,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,wBAMlBzH,EAAQC,OAAO,0BACZ8H,QAAQ,oBAAqB,UAAW,SAAS/E,GAChD,GAAIgF,GAAUhF,EAAQiF,YAEtB,QACE5D,IAAK,SAASa,EAAK3E,GACjByH,EAAQE,QAAQ,UAAUhD,EAAKlF,EAAQmI,OAAO5H,KAEhDyD,IAAK,SAASkB,GACZ,MAAOlF,GAAQsF,SAAS0C,EAAQI,QAAQ,UAAUlD,KAEpDX,OAAQ,SAASW,GACf8C,EAAQK,WAAW,UAAUnD,SAKlCnF,OAAQA,OAAOC"} \ No newline at end of file diff --git a/angular-idle.min.js b/angular-idle.min.js index 41a6503..865fd84 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.2 +* @version v1.0.3 * @link https://github.com/HackedByChinese/ng-idle.git * @license MIT */ -!function(a,b){"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(b,e,f,g,h,i,j){function k(){a.keepalive&&(s.running&&h.ping(),h.start())}function l(){a.keepalive&&h.stop()}function m(){s.idling=!s.idling;var c=s.idling?"Start":"End";f.$broadcast("Idle"+c),s.idling?(l(),a.timeout&&(s.countdown=a.timeout,n(),s.timeout=b(n,1e3,a.timeout,!1))):k(),b.cancel(s.idle)}function n(){return s.countdown<=0?void o():(f.$broadcast("IdleWarn",s.countdown),void s.countdown--)}function o(){l(),b.cancel(s.idle),b.cancel(s.timeout),s.idling=!0,s.running=!1,s.countdown=0,f.$broadcast("IdleTimeout")}function p(a,b,c){var d=a.running();a.unwatch(),b(c),d&&a.watch()}function q(){var a=i.get("expiry");return a.time}function r(a){a?i.set("expiry",{id:t,time:a}):i.remove("expiry")}var s={idle:null,timeout:null,idling:!1,running:!1,countdown:null},t=(new Date).getTime(),u={_options:function(){return a},_getNow:function(){return new Date},setIdle:function(a){p(this,d,a)},setTimeout:function(a){p(this,c,a)},isExpired:function(){var a=q();return a&&a<=this._getNow()},running:function(){return s.running},idling:function(){return s.idling},watch:function(c){b.cancel(s.idle),b.cancel(s.timeout);var d=a.timeout?a.timeout:0;c||r(new Date((new Date).getTime()+1e3*(a.idle+d))),s.idling?m():s.running||k(),s.running=!0,s.idle=b(m,1e3*a.idle,0,!1)},unwatch:function(){b.cancel(s.idle),b.cancel(s.timeout),s.idling=!1,s.running=!1,r(null),l()},interrupt:function(b){return s.running?a.timeout&&this.isExpired()?void o():void(("idle"===a.autoResume||"notIdle"===a.autoResume&&!s.idling)&&this.watch(b)):void 0}};g.find("body").on(a.interrupt,function(){u.interrupt()});var v=function(a){if("ngIdle.expiry"===a.key&&a.newValue!==a.oldValue){var b=i.parseJson(a.newValue);if(b.id===t)return;u.interrupt(!0)}};return j.addEventListener?j.addEventListener("storage",v,!1):j.attachEvent("onstorage",v),u}]}),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",[]).factory("IdleLocalStorage",["$window",function(a){function b(a){try{return JSON.parse(a,function(a,b){var c=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(b);return c?new Date(b):b})}catch(b){return a}}var c=a.localStorage;return{set:function(a,b){c.setItem("ngIdle."+a,JSON.stringify(b))},get:function(a){var d=c.getItem("ngIdle."+a);return b(d)},remove:function(a){c.removeItem("ngIdle."+a)},parseJson:function(a){return b(a)}}}])}(window,window.angular); +!function(a,b){"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); //# sourceMappingURL=angular-idle.map \ No newline at end of file diff --git a/bower.json b/bower.json index 9ff0cd8..e61799b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ng-idle", - "version": "1.0.2", + "version": "1.0.3", "homepage": "https://github.com/HackedByChinese/ng-idle", "description": "Responding to idle users in AngularJS", "main": "angular-idle.js", diff --git a/karma.conf.js b/karma.conf.js index 70cbe18..44cab70 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -14,9 +14,9 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - 'bower_components/jquery/jquery.js', - 'bower_components/angular/angular.js', - 'bower_components/angular-mocks/angular-mocks.js', + '+(bower_components|node_modules)/jquery/?(dist)/jquery.js', + '+(bower_components|node_modules)/angular/angular.js', + '+(bower_components|node_modules)/angular-mocks/angular-mocks.js', 'src/**/*.js' ], diff --git a/package.json b/package.json index c05af7f..8b34870 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng-idle", - "version": "1.0.2", + "version": "1.0.3", "description": "Directives and services for responding to idle users in AngularJS", "main": [ "angular-idle.js", @@ -8,7 +8,6 @@ "angular-idle.map" ], "scripts": { - "postinstall": "node_modules/.bin/bower install", "test": "grunt test" }, "repository": { @@ -21,7 +20,6 @@ "url": "https://github.com/HackedByChinese/ng-idle/issues" }, "devDependencies": { - "bower": "^1.3.9", "grunt": "~0.4.2", "grunt-bump": "0.0.13", "grunt-contrib-clean": "~0.5.0", @@ -40,6 +38,9 @@ "karma-requirejs": "~0.2.1", "karma-script-launcher": "~0.1.0", "matchdep": "~0.3.0", - "requirejs": "~2.1.10" + "requirejs": "~2.1.10", + "angular": "~1.3.15", + "angular-mocks": "~1.3.15", + "jquery": "~2.1.3" } } diff --git a/src/idle/idle.js b/src/idle/idle.js index 946082a..826607b 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 obj.time; + return new Date(obj.time); } function setExpiry(date) { @@ -195,7 +195,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage']) var wrap = function(event) { if (event.key === 'ngIdle.expiry' && event.newValue !== event.oldValue) { - var val = LocalStorage.parseJson(event.newValue); + var val = angular.fromJson(event.newValue); if (val.id === id) return; svc.interrupt(true); } diff --git a/src/localStorage/localStorage.js b/src/localStorage/localStorage.js index 9de4c5e..c711c8f 100644 --- a/src/localStorage/localStorage.js +++ b/src/localStorage/localStorage.js @@ -1,34 +1,16 @@ angular.module('ngIdle.localStorage', []) - .factory('IdleLocalStorage', ['$window', function($window) { + .service('IdleLocalStorage', ['$window', function($window) { var storage = $window.localStorage; - - function tryParseJson(value) { - try { - return JSON.parse(value, function(key, value) { - var match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); - if (match) return new Date(value); - - return value; - }); - } - catch(e) { - return value; - } - } - + return { set: function(key, value) { - storage.setItem('ngIdle.'+key, JSON.stringify(value)); + storage.setItem('ngIdle.'+key, angular.toJson(value)); }, get: function(key) { - var raw = storage.getItem('ngIdle.'+key); - return tryParseJson(raw); + return angular.fromJson(storage.getItem('ngIdle.'+key)); }, remove: function(key) { storage.removeItem('ngIdle.'+key); - }, - parseJson: function(raw) { - return tryParseJson(raw); } }; }]); diff --git a/src/localStorage/localStorage.spec.js b/src/localStorage/localStorage.spec.js index 8f5b870..4d20c28 100644 --- a/src/localStorage/localStorage.spec.js +++ b/src/localStorage/localStorage.spec.js @@ -36,21 +36,6 @@ describe('ngIdle', function() { expect(actual).toEqualData({value:1}); }); - it ('get() should retrieve value as string', function() { - spyOn($window.localStorage, 'getItem').andReturn('test'); - var actual = LocalStorage.get('key'); - expect(actual).toEqualData('test'); - }); - - it ('get() should retrieve value as date', function() { - var expected = new Date(); - var raw = JSON.stringify(expected); - spyOn($window.localStorage, 'getItem').andReturn(raw); - - var actual = LocalStorage.get('key'); - expect(actual).toEqual(expected); - }); - it ('remove() should remove key/value', function() { spyOn($window.localStorage, 'removeItem'); LocalStorage.remove('key');