@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.
88 lines (79 loc) • 3.75 kB
JavaScript
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) {
return {
ngModule: HasUnsavedDataModule,
providers: [
config.confirmService,
{
provide: UNSAVED_DATA_CONFIG,
useValue: {
message: config.message ?? 'There is unsaved data',
title: config.title,
ok: config.ok ?? 'Ok',
cancel: config.cancel ?? '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 => {
const config = inject(UNSAVED_DATA_CONFIG);
const confirm = inject(HasUnsavedDataConfirmService);
const methodName = component.constructor.prototype.____UnsavedDataChecker____;
console.log('called', methodName);
if (component?.[methodName] && component[methodName]()) {
const params = component.____UnsavedDataOptionsHandler____ ? component.____UnsavedDataOptionsHandler____.call(component, component) : null;
return confirm.confirm(params?.message ?? config.message ?? '', params?.title ?? config.title, params?.ok ?? config.ok, params?.cancel ?? 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