UNPKG

@kovalenko/has-unsaved-data

Version:

Provides a router guard preventing route deactivation, if the component\`s method decorated with `@CheckUnsavedData()` returns `true`. Also it prevents the window to be unloaded.

90 lines (81 loc) 4.34 kB
import * as i0 from '@angular/core'; import { InjectionToken, NgModule, inject } from '@angular/core'; const UNSAVED_DATA_CONFIG = new InjectionToken('UNSAVED_DATA_CONFIG'); class HasUnsavedDataModule { static config(config) { var _a, _b, _c; return { ngModule: HasUnsavedDataModule, providers: [ config.confirmService, { provide: UNSAVED_DATA_CONFIG, useValue: { message: (_a = config.message) !== null && _a !== void 0 ? _a : 'There is unsaved data', title: config.title, ok: (_b = config.ok) !== null && _b !== void 0 ? _b : 'Ok', cancel: (_c = config.cancel) !== null && _c !== void 0 ? _c : 'Cancel', } }, ], }; } } HasUnsavedDataModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: HasUnsavedDataModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); HasUnsavedDataModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.0", ngImport: i0, type: HasUnsavedDataModule }); HasUnsavedDataModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: HasUnsavedDataModule }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.0", ngImport: i0, type: HasUnsavedDataModule, decorators: [{ type: NgModule }] }); class HasUnsavedDataConfirmService { } const hasUnsavedDataGuard = component => { var _a, _b, _c, _d, _e; const config = inject(UNSAVED_DATA_CONFIG); const confirm = inject(HasUnsavedDataConfirmService); const methodName = component.constructor.prototype.____UnsavedDataChecker____; console.log('called', methodName); if ((component === null || component === void 0 ? void 0 : component[methodName]) && component[methodName]()) { const params = component.____UnsavedDataOptionsHandler____ ? component.____UnsavedDataOptionsHandler____.call(component, component) : null; return confirm.confirm((_b = (_a = params === null || params === void 0 ? void 0 : params.message) !== null && _a !== void 0 ? _a : config.message) !== null && _b !== void 0 ? _b : '', (_c = params === null || params === void 0 ? void 0 : params.title) !== null && _c !== void 0 ? _c : config.title, (_d = params === null || params === void 0 ? void 0 : params.ok) !== null && _d !== void 0 ? _d : config.ok, (_e = params === null || params === void 0 ? void 0 : params.cancel) !== null && _e !== void 0 ? _e : config.cancel).then(v => !!v); } else { return true; } }; const UnsavedDataChecker = (optionsHandler) => { return (target, methodName, descriptor) => { if (target.constructor.prototype.____UnsavedDataChecker____) { throw new Error('There can be only one @UnsavedDataChecker() in a class'); } target.constructor.prototype.____UnsavedDataChecker____ = methodName; target.constructor.prototype.____UnsavedDataOptionsHandler____ = optionsHandler; function beforeUnload(e) { // @ts-ignore if (this[methodName]()) { e.returnValue = true; } } let binded; const originalNgOnInit = target.constructor.prototype.ngOnInit; target.constructor.prototype.ngOnInit = function () { originalNgOnInit && originalNgOnInit.call(this); binded = beforeUnload.bind(this); window.addEventListener('beforeunload', binded); }; const originalNgOnDestroy = target.constructor.prototype.ngOnDestroy; target.constructor.prototype.ngOnDestroy = function () { originalNgOnDestroy && originalNgOnDestroy.call(this); window.removeEventListener('beforeunload', binded); }; return descriptor; }; }; /* * Public API Surface of has-unsaved-data */ /** * Generated bundle index. Do not edit. */ export { HasUnsavedDataConfirmService, HasUnsavedDataModule, UNSAVED_DATA_CONFIG, UnsavedDataChecker, hasUnsavedDataGuard }; //# sourceMappingURL=kovalenko-has-unsaved-data.mjs.map