ng2-timeout-dialog
Version:
Timeout dialog for Angular
69 lines • 2.78 kB
JavaScript
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
import { TimeoutService } from './timeout.service';
var TimeoutDirective = (function () {
function TimeoutDirective(timeoutService) {
this.timeoutService = timeoutService;
this.timeout = new EventEmitter();
this.warningIsActive = false;
}
TimeoutDirective.prototype.ngOnDestroy = function () {
this.clearTimeout();
this.setWarningState(false);
};
TimeoutDirective.prototype.ngOnInit = function () {
if (!this.warningAfter) {
this.warningAfter = this.timeoutAfter;
}
if (parseInt(this.warningAfter, 10) > parseInt(this.timeoutAfter, 10)) {
throw new Error('warningAfter value can not be higher then timeoutAfter');
}
this.startTimeoutCounting(parseInt(this.warningAfter, 10));
};
TimeoutDirective.prototype.resetTimeout = function () {
this.clearTimeout();
this.setWarningState(false);
this.startTimeoutCounting(parseInt(this.warningAfter, 10));
};
TimeoutDirective.prototype.clearTimeout = function () {
window.clearTimeout(this.timeoutId);
};
TimeoutDirective.prototype.startTimeoutCounting = function (seconds) {
this.timeoutId = window.setTimeout(this.handleTimeout(), seconds * 1000);
};
TimeoutDirective.prototype.handleTimeout = function () {
var _this = this;
return function () {
_this.clearTimeout();
if (parseInt(_this.timeoutAfter, 10) === parseInt(_this.warningAfter, 10) || _this.warningIsActive) {
_this.setWarningState(false);
_this.timeout.emit();
}
else {
_this.setWarningState(true);
_this.startTimeoutCounting(parseInt(_this.timeoutAfter, 10) - parseInt(_this.warningAfter, 10));
}
};
};
TimeoutDirective.prototype.setWarningState = function (val) {
this.warningIsActive = val;
this.timeoutService.setState(val);
};
TimeoutDirective.decorators = [
{ type: Directive, args: [{
selector: 'sh-timeout'
},] },
];
/** @nocollapse */
TimeoutDirective.ctorParameters = function () { return [
{ type: TimeoutService, },
]; };
TimeoutDirective.propDecorators = {
'timeout': [{ type: Output },],
'timeoutAfter': [{ type: Input },],
'warningAfter': [{ type: Input },],
'resetTimeout': [{ type: HostListener, args: ['window:mousedown',] }, { type: HostListener, args: ['window:keydown',] },],
};
return TimeoutDirective;
}());
export { TimeoutDirective };
//# sourceMappingURL=timeout.directive.js.map