{"version":3,"file":"kovalenko-has-unsaved-data.mjs","sources":["../../../projects/has-unsaved-data/src/lib/unsaved-data.config.ts","../../../projects/has-unsaved-data/src/lib/has-unsaved-data.module.ts","../../../projects/has-unsaved-data/src/lib/confirm-service.ts","../../../projects/has-unsaved-data/src/lib/has-unsaved-data.guard.ts","../../../projects/has-unsaved-data/src/lib/unsaved-data-checker.decorator.ts","../../../projects/has-unsaved-data/src/public-api.ts","../../../projects/has-unsaved-data/src/kovalenko-has-unsaved-data.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\nimport {UnsavedDataConfig} from './unsaved-data-config.interface';\n\nexport const UNSAVED_DATA_CONFIG: InjectionToken<UnsavedDataConfig> = new InjectionToken('UNSAVED_DATA_CONFIG');\n","import {ModuleWithProviders, NgModule} from '@angular/core';\nimport {ModuleConfig} from './module-config';\nimport {UNSAVED_DATA_CONFIG} from './unsaved-data.config';\n\n@NgModule()\nexport class HasUnsavedDataModule {\n  static config(config: ModuleConfig): ModuleWithProviders<HasUnsavedDataModule> {\n    return {\n      ngModule: HasUnsavedDataModule,\n      providers: [\n        config.confirmService,\n        {\n          provide: UNSAVED_DATA_CONFIG,\n          useValue: {\n            message: config.message ?? 'There is unsaved data',\n            title: config.title,\n            ok: config.ok ?? 'Ok',\n            cancel: config.cancel ?? 'Cancel',\n          }\n        },\n      ],\n    };\n  }\n}\n","export abstract class HasUnsavedDataConfirmService {\r\n\r\n  abstract confirm(message: string, title?: string, ok?: string, cancel?: string): Promise<boolean> | boolean;\r\n}\r\n","import {inject} from '@angular/core';\nimport {CanDeactivateFn} from '@angular/router';\nimport {HasUnsavedDataConfirmService} from './confirm-service';\nimport {UnsavedDataConfig} from './unsaved-data-config.interface';\nimport {UNSAVED_DATA_CONFIG} from './unsaved-data.config';\n\nexport const hasUnsavedDataGuard: CanDeactivateFn<any> = component => {\n  const config: UnsavedDataConfig = inject(UNSAVED_DATA_CONFIG);\n  const confirm = inject(HasUnsavedDataConfirmService);\n\n  const methodName = component.constructor.prototype.____UnsavedDataChecker____;\n\n  console.log('called', methodName);\n\n  if (component?.[methodName] && component[methodName]()) {\n    const params: UnsavedDataConfig = component.____UnsavedDataOptionsHandler____ ? component.____UnsavedDataOptionsHandler____.call(component, component) : null;\n    return (confirm.confirm(\n      params?.message ?? config.message ?? '',\n      params?.title ?? config.title,\n      params?.ok ?? config.ok,\n      params?.cancel ?? config.cancel,\n    ) as Promise<boolean>).then(v => !!v);\n  } else {\n    return true;\n  }\n};\n","import {UnsavedDataConfig} from './unsaved-data-config.interface';\r\n\r\nexport const UnsavedDataChecker = <T extends Record<string, any>>(optionsHandler?: (obj: T) => UnsavedDataConfig): MethodDecorator => {\r\n  return (target: Record<string, any>, methodName, descriptor) => {\r\n    if (target.constructor.prototype.____UnsavedDataChecker____) {\r\n      throw new Error('There can be only one @UnsavedDataChecker() in a class');\r\n    }\r\n\r\n    target.constructor.prototype.____UnsavedDataChecker____ = methodName;\r\n    target.constructor.prototype.____UnsavedDataOptionsHandler____ = optionsHandler;\r\n\r\n    function beforeUnload(e: Event): void {\r\n      // @ts-ignore\r\n      if (this[methodName]()) {\r\n        e.returnValue = true;\r\n      }\r\n    }\r\n\r\n    let binded: (e: Event) => void;\r\n    const originalNgOnInit = target.constructor.prototype.ngOnInit;\r\n    target.constructor.prototype.ngOnInit = function(this: any): void {\r\n      originalNgOnInit && originalNgOnInit.call(this);\r\n\r\n      binded = beforeUnload.bind(this);\r\n      window.addEventListener('beforeunload', binded);\r\n    };\r\n\r\n    const originalNgOnDestroy = target.constructor.prototype.ngOnDestroy;\r\n    target.constructor.prototype.ngOnDestroy = function(this: any): void {\r\n      originalNgOnDestroy && originalNgOnDestroy.call(this);\r\n\r\n      window.removeEventListener('beforeunload', binded);\r\n    };\r\n\r\n    return descriptor;\r\n  }\r\n}\r\n","/*\n * Public API Surface of has-unsaved-data\n */\n\nexport * from './lib/has-unsaved-data.module';\nexport * from './lib/has-unsaved-data.guard';\nexport * from './lib/unsaved-data-checker.decorator';\nexport * from './lib/unsaved-data.config';\nexport * from './lib/unsaved-data-config.interface';\nexport * from './lib/confirm-service';\nexport * from './lib/module-config';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAGa,mBAAmB,GAAsC,IAAI,cAAc,CAAC,qBAAqB;;MCEjG,oBAAoB,CAAA;IAC/B,OAAO,MAAM,CAAC,MAAoB,EAAA;QAChC,OAAO;AACL,YAAA,QAAQ,EAAE,oBAAoB;AAC9B,YAAA,SAAS,EAAE;AACT,gBAAA,MAAM,CAAC,cAAc;AACrB,gBAAA;AACE,oBAAA,OAAO,EAAE,mBAAmB;AAC5B,oBAAA,QAAQ,EAAE;AACR,wBAAA,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,uBAAuB;wBAClD,KAAK,EAAE,MAAM,CAAC,KAAK;AACnB,wBAAA,EAAE,EAAE,MAAM,CAAC,EAAE,IAAI,IAAI;AACrB,wBAAA,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,QAAQ;AAClC,qBAAA;AACF,iBAAA;AACF,aAAA;SACF,CAAC;KACH;;iHAjBU,oBAAoB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,QAAA,EAAA,CAAA,CAAA;kHAApB,oBAAoB,EAAA,CAAA,CAAA;kHAApB,oBAAoB,EAAA,CAAA,CAAA;2FAApB,oBAAoB,EAAA,UAAA,EAAA,CAAA;kBADhC,QAAQ;;;MCJa,4BAA4B,CAAA;AAGjD;;ACGY,MAAA,mBAAmB,GAAyB,SAAS,IAAG;AACnE,IAAA,MAAM,MAAM,GAAsB,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC9D,IAAA,MAAM,OAAO,GAAG,MAAM,CAAC,4BAA4B,CAAC,CAAC;IAErD,MAAM,UAAU,GAAG,SAAS,CAAC,WAAW,CAAC,SAAS,CAAC,0BAA0B,CAAC;AAE9E,IAAA,OAAO,CAAC,GAAG,CAAC,Q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