ng2-idle-core
Version:
ng-idle for Angular 2+ core module
2 lines (1 loc) • 13.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("rxjs"),require("rxjs/operators"),require("@angular/core")):"function"==typeof define&&define.amd?define(["exports","rxjs","rxjs/operators","@angular/core"],e):e((t.ngidle=t.ngidle||{},t.ngidle.core=t.ngidle.core||{}),t.rxjs,t.rxjs_operators,t.ng.core)}(this,function(t,e,n,r){"use strict";function i(t){return[new c("mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove scroll",t),new y]}var o=function(){function t(t,e,n){void 0===n&&(n=!1),this.source=t,this.innerArgs=e,this.force=n}return t}(),s=function(){function t(t,e){this.attachFn=t,this.detachFn=e,this.isAttached=!1,this.onInterrupt=new r.EventEmitter}return t.prototype.attach=function(){var t=this;if(!0===Zone.current.get("isAngularZone"))return void Zone.current.parent.run(function(){return t.attach()});!this.isAttached&&this.attachFn&&this.attachFn(this),this.isAttached=!0},t.prototype.detach=function(){this.isAttached&&this.detachFn&&this.detachFn(this),this.isAttached=!1},t}(),u=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),a=500,p=function(t){function r(r,i,s){var u=t.call(this,null,null)||this;u.target=r,u.events=i,u.eventSubscription=new e.Subscription,"number"==typeof s&&(s={throttleDelay:s,passive:!1}),s=s||{passive:!1,throttleDelay:a},void 0!==s.throttleDelay&&null!==s.throttleDelay||(s.throttleDelay=a),u.throttleDelay=s.throttleDelay,u.passive=!!s.passive;var p=u.passive?{passive:!0}:null,l=i.split(" ").map(function(t){return e.fromEvent(r,t,p)});u.eventSrc=e.merge.apply(void 0,l),u.eventSrc=u.eventSrc.pipe(n.filter(function(t){return!u.filterEvent(t)})),u.throttleDelay>0&&(u.eventSrc=u.eventSrc.pipe(n.throttleTime(u.throttleDelay)));var c=function(t){return u.onInterrupt.emit(new o(u,t))};return u.attachFn=function(){return u.eventSubscription=u.eventSrc.subscribe(c)},u.detachFn=function(){return u.eventSubscription.unsubscribe()},u}return u(r,t),r.prototype.filterEvent=function(t){return!1},Object.defineProperty(r.prototype,"options",{get:function(){return{throttleDelay:this.throttleDelay,passive:this.passive}},enumerable:!0,configurable:!0}),r}(s),l=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),c=function(t){function e(e,n){return t.call(this,document.documentElement,e,n)||this}return l(e,t),e.prototype.filterEvent=function(t){return!("mousemove"!==t.type||!(t.originalEvent&&0===t.originalEvent.movementX&&0===t.originalEvent.movementY||void 0!==t.movementX&&!t.movementX)&&t.movementY)},e}(p),h=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),f=function(t){function e(e,n){return t.call(this,window,e,n)||this}return h(e,t),e}(p),d=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),y=function(t){function e(e){return void 0===e&&(e=500),t.call(this,"storage",e)||this}return d(e,t),e.prototype.filterEvent=function(t){return!(t.key.indexOf("ng2Idle.")>=0&&t.key.indexOf(".expiry")>=0)},e}(f),v=function(){function t(){this.idValue=new Date,this.idlingValue=!1}return t.prototype.id=function(t){if(void 0!==t){if(!t)throw new Error("A value must be specified for the ID.");this.idValue=t}return this.idValue},t.prototype.idling=function(t){return void 0!==t&&(this.idlingValue=t),this.idlingValue},t.prototype.now=function(){return new Date},t.prototype.isExpired=function(){var t=this.last();return null!=t&&t<=this.now()},t}(),g=function(){function t(t){this.source=t}return t.prototype.subscribe=function(t){this.sub=this.source.onInterrupt.subscribe(t)},t.prototype.unsubscribe=function(){this.sub.unsubscribe(),this.sub=null},t.prototype.resume=function(){this.source.attach()},t.prototype.pause=function(){this.source.detach()},t}(),m=function(){function t(){}return t}(),I=function(){function t(){this.storageMap={}}return Object.defineProperty(t.prototype,"length",{get:function(){return Object.keys(this.storageMap).length},enumerable:!0,configurable:!0}),t.prototype.clear=function(){this.storageMap={}},t.prototype.getItem=function(t){return void 0!==this.storageMap[t]?this.storageMap[t]:null},t.prototype.key=function(t){return Object.keys(this.storageMap)[t]||null},t.prototype.removeItem=function(t){this.storageMap[t]=void 0},t.prototype.setItem=function(t,e){this.storageMap[t]=e},t}(),b=function(){function t(){this.storage=this.getStorage()}return t.prototype.getStorage=function(){try{var t=localStorage;return t.setItem("ng2IdleStorage",""),t.removeItem("ng2IdleStorage"),t}catch(t){return new I}},t.prototype.getItem=function(t){return this.storage.getItem("ng2Idle."+t)},t.prototype.removeItem=function(t){this.storage.removeItem("ng2Idle."+t)},t.prototype.setItem=function(t,e){this.storage.setItem("ng2Idle."+t,e)},t.prototype._wrapped=function(){return this.storage},t.decorators=[{type:r.Injectable}],t.ctorParameters=function(){return[]},t}(),w=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),E=function(t){function e(e){var n=t.call(this)||this;return n.localStorage=e,n.idleName="main",n}return w(e,t),e.prototype.last=function(t){return void 0!==t&&this.setExpiry(t),this.getExpiry()},e.prototype.idling=function(t){return void 0!==t&&this.setIdling(t),this.getIdling()},e.prototype.getIdleName=function(){return this.idleName},e.prototype.setIdleName=function(t){t&&(this.idleName=t)},e.prototype.getExpiry=function(){var t=this.localStorage.getItem(this.idleName+".expiry");return t?new Date(parseInt(t,10)):null},e.prototype.setExpiry=function(t){t?this.localStorage.setItem(this.idleName+".expiry",t.getTime().toString()):this.localStorage.removeItem(this.idleName+".expiry")},e.prototype.getIdling=function(){var t=this.localStorage.getItem(this.idleName+".idling");return!!t&&"true"===t},e.prototype.setIdling=function(t){t?this.localStorage.setItem(this.idleName+".idling",t.toString()):this.localStorage.setItem(this.idleName+".idling","false")},e.decorators=[{type:r.Injectable}],e.ctorParameters=function(){return[{type:b}]},e}(v);!function(t){t[t.disabled=0]="disabled",t[t.idle=1]="idle",t[t.notIdle=2]="notIdle"}(t.AutoResume||(t.AutoResume={}));var _=function(){function e(e,n,i){this.expiry=e,this.zone=n,this.idle=1200,this.timeoutVal=30,this.autoResume=t.AutoResume.idle,this.interrupts=new Array,this.running=!1,this.keepaliveEnabled=!1,this.onIdleStart=new r.EventEmitter,this.onIdleEnd=new r.EventEmitter,this.onTimeoutWarning=new r.EventEmitter,this.onTimeout=new r.EventEmitter,this.onInterrupt=new r.EventEmitter,i&&(this.keepaliveSvc=i,this.keepaliveEnabled=!0),this.setIdling(!1)}return e.prototype.setIdleName=function(t){if(!(this.expiry instanceof E))throw new Error("Cannot set expiry key name because no LocalStorageExpiry has been provided.");this.expiry.setIdleName(t)},e.prototype.getKeepaliveEnabled=function(){return this.keepaliveEnabled},e.prototype.setKeepaliveEnabled=function(t){if(!this.keepaliveSvc)throw new Error("Cannot enable keepalive integration because no KeepaliveSvc has been provided.");return this.keepaliveEnabled=t},e.prototype.getTimeout=function(){return this.timeoutVal},e.prototype.setTimeout=function(t){if(!1===t)this.timeoutVal=0;else{if(!("number"==typeof t&&t>=0))throw new Error("'seconds' can only be 'false' or a positive number.");this.timeoutVal=t}return this.timeoutVal},e.prototype.getIdle=function(){return this.idle},e.prototype.setIdle=function(t){if(t<=0)throw new Error("'seconds' must be greater zero");return this.idle=t},e.prototype.getAutoResume=function(){return this.autoResume},e.prototype.setAutoResume=function(t){return this.autoResume=t},e.prototype.setInterrupts=function(t){this.clearInterrupts();for(var e=this,n=0,r=t;n<r.length;n++){var i=r[n],o=new g(i);o.subscribe(function(t){e.interrupt(t.force,t.innerArgs)}),this.interrupts.push(o)}return this.interrupts},e.prototype.getInterrupts=function(){return this.interrupts},e.prototype.clearInterrupts=function(){for(var t=0,e=this.interrupts;t<e.length;t++){var n=e[t];n.pause(),n.unsubscribe()}this.interrupts.length=0},e.prototype.isRunning=function(){return this.running},e.prototype.isIdling=function(){return this.idling},e.prototype.watch=function(t){var e=this;this.safeClearInterval("idleHandle"),this.safeClearInterval("timeoutHandle");var n=this.timeoutVal?this.timeoutVal:0;if(!t){var r=new Date(this.expiry.now().getTime()+1e3*(this.idle+n));this.expiry.last(r)}this.idling&&this.toggleState(),this.running||(this.startKeepalive(),this.toggleInterrupts(!0)),this.running=!0;var i=function(){e.zone.run(function(){var t=e.getExpiryDiff(n);t>0?(e.safeClearInterval("idleHandle"),e.setIdleIntervalOutsideOfZone(i,t)):e.toggleState()})};this.setIdleIntervalOutsideOfZone(i,1e3*this.idle)},e.prototype.setIdleIntervalOutsideOfZone=function(t,e){var n=this;this.zone.runOutsideAngular(function(){n.idleHandle=setInterval(t,e)})},e.prototype.stop=function(){this.stopKeepalive(),this.toggleInterrupts(!1),this.safeClearInterval("idleHandle"),this.safeClearInterval("timeoutHandle"),this.setIdling(!1),this.running=!1,this.expiry.last(null)},e.prototype.timeout=function(){this.stopKeepalive(),this.toggleInterrupts(!1),this.safeClearInterval("idleHandle"),this.safeClearInterval("timeoutHandle"),this.setIdling(!0),this.running=!1,this.countdown=0,this.onTimeout.emit(null)},e.prototype.interrupt=function(e,n){if(this.running){if(this.timeoutVal&&this.expiry.isExpired())return void this.timeout();this.onInterrupt.emit(n),(!0===e||this.autoResume===t.AutoResume.idle||this.autoResume===t.AutoResume.notIdle&&!this.expiry.idling())&&this.watch(e)}},e.prototype.setIdling=function(t){this.idling=t,this.expiry.idling(t)},e.prototype.toggleState=function(){var t=this;this.setIdling(!this.idling),this.idling?(this.onIdleStart.emit(null),this.stopKeepalive(),this.timeoutVal>0&&(this.countdown=this.timeoutVal,this.doCountdown(),this.setTimoutIntervalOutsideZone(function(){t.doCountdownInZone()},1e3))):(this.toggleInterrupts(!0),this.onIdleEnd.emit(null),this.startKeepalive()),this.safeClearInterval("idleHandle")},e.prototype.setTimoutIntervalOutsideZone=function(t,e){var n=this;this.zone.runOutsideAngular(function(){n.timeoutHandle=setInterval(function(){t()},e)})},e.prototype.toggleInterrupts=function(t){for(var e=0,n=this.interrupts;e<n.length;e++){var r=n[e];t?r.resume():r.pause()}},e.prototype.getExpiryDiff=function(t){var e=this.expiry.now();return(this.expiry.last()||e).getTime()-e.getTime()-1e3*t},e.prototype.doCountdownInZone=function(){var t=this;this.zone.run(function(){t.doCountdown()})},e.prototype.doCountdown=function(){var t=this.timeoutVal?this.timeoutVal:0;if(this.getExpiryDiff(t)>0)return this.safeClearInterval("timeoutHandle"),void this.interrupt(!0);if(this.idling){if(this.countdown<=0)return void this.timeout();this.onTimeoutWarning.emit(this.countdown),this.countdown--}},e.prototype.safeClearInterval=function(t){var e=this[t];null!==e&&void 0!==e&&(clearInterval(this[t]),this[t]=null)},e.prototype.startKeepalive=function(){this.keepaliveSvc&&this.keepaliveEnabled&&(this.running&&this.keepaliveSvc.ping(),this.keepaliveSvc.start())},e.prototype.stopKeepalive=function(){this.keepaliveSvc&&this.keepaliveEnabled&&this.keepaliveSvc.stop()},e.prototype.ngOnDestroy=function(){this.stop(),this.clearInterrupts()},e.decorators=[{type:r.Injectable}],e.ctorParameters=function(){return[{type:v},{type:r.NgZone},{type:m,decorators:[{type:r.Optional}]}]},e}(),S=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),O=function(t){function e(){var e=t.call(this)||this;return e.lastValue=null,e}return S(e,t),e.prototype.last=function(t){return void 0!==t&&(this.lastValue=t),this.lastValue},e}(v),x=function(){function t(){}return t.forRoot=function(){return{ngModule:t,providers:[E,{provide:v,useExisting:E},_]}},t.decorators=[{type:r.NgModule,args:[{providers:[b]}]}],t}(),j=i();t.createDefaultInterruptSources=i,t.DEFAULT_INTERRUPTSOURCES=j,t.NgIdleModule=x,t.Idle=_,t.InterruptArgs=o,t.InterruptSource=s,t.EventTargetInterruptSource=p,t.DocumentInterruptSource=c,t.WindowInterruptSource=f,t.StorageInterruptSource=y,t.KeepaliveSvc=m,t.IdleExpiry=v,t.SimpleExpiry=O,t.LocalStorage=b,t.LocalStorageExpiry=E,Object.defineProperty(t,"__esModule",{value:!0})});