{"version":3,"file":"ng-push-notification.mjs","sources":["../../../projects/ng-push-notification/src/lib/ng-push-notification.config.ts","../../../projects/ng-push-notification/src/lib/ng-push-notification.service.ts","../../../projects/ng-push-notification/src/lib/ng-push-notification.module.ts","../../../projects/ng-push-notification/src/ng-push-notification.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\n\nimport { PushNotificationSettings } from './ng-push-notification.settings';\n\nexport const DEFAULT_NOTIFICATION_SETTINGS = new InjectionToken<PushNotificationSettings>('DEFAULT_NOTIFICATION_SETTINGS');\n","import { Injectable, Injector } from '@angular/core';\nimport { Subject } from 'rxjs';\n\nimport { PushNotificationSettings } from './ng-push-notification.settings';\nimport { DEFAULT_NOTIFICATION_SETTINGS } from './ng-push-notification.config';\n\n@Injectable()\nexport class PushNotificationService {\n  shown$ = new Subject<Notification>();\n  closed$ = new Subject<Notification>();\n  error$ = new Subject<Notification>();\n  click$ = new Subject<{event: any, notification: Notification}>();\n\n  private defaultCloseDelay: number;\n  private defaultSettings: any;\n  private instances: Notification[] = [];\n\n  constructor(injector: Injector) {\n    const defaultSettings = injector.get<PushNotificationSettings>(DEFAULT_NOTIFICATION_SETTINGS);\n    this.defaultSettings = Object.assign({\n      dir: 'auto',\n      lang: 'en-US',\n      renotify: false,\n      sticky: false,\n      noscreen: false,\n      silent: true,\n    }, defaultSettings || {});\n    this.defaultCloseDelay = this.defaultSettings.closeDelay === undefined ? 6000 : this.defaultSettings.closeDelay;\n  }\n\n  checkCompatibility() {\n    return !!('Notification' in window);\n  }\n\n  async requestPermission(): Promise<boolean> {\n    const permission = await Notification.requestPermission();\n    return (permission === 'granted');\n  }\n\n  async show(title: string, settings = {}, closeDelay = this.defaultCloseDelay): Promise<Notification> {\n    if (!this.checkCompatibility()) {\n      throw new Error('Notification API not available in this browser.');\n    }\n\n    const permission = await this.requestPermission();\n\n    if (!permission) {\n      throw new Error('Permission was not granted.');\n    }\n\n    const notificationSettings = {...this.defaultSettings, ...settings};\n\n    return this.create(title, notificationSettings, closeDelay);\n  }\n\n  closeAll(): void {\n    this.instances.forEach(notification => notification.close());\n    this.instances = [];\n  }\n\n  private create(title: string, settings: PushNotificationSettings, closeDelay = 0) {\n    let notification = new Notification(title, settings);\n\n    this.instances.push(notification);\n    this.attachEventHandlers(notification);\n\n    if (closeDelay) {\n      setTimeout(() => notification.close(), closeDelay);\n    }\n    return notification;\n  }\n\n  private attachEventHandlers(notification: Notification): void {\n    notification.onshow = () => {\n      this.shown$.next(notification);\n    };\n    notification.onclick = (event: any) => {\n      this.click$.next({event, notification});\n    };\n    notification.onerror = () => {\n      this.error$.next(notification);\n    };\n    notification.onclose = () => {\n      this.closed$.next(notification);\n    };\n  }\n}\n","import { ModuleWithProviders, NgModule } from '@angular/core';\n\nimport { PushNotificationService } from './ng-push-notification.service';\nimport { PushNotificationSettings } from './ng-push-notification.settings';\nimport { DEFAULT_NOTIFICATION_SETTINGS } from './ng-push-notification.config';\n\n@NgModule({})\nexport class PushNotificationModule {\n  static forRoot(config?: PushNotificationSettings): ModuleWithProviders<PushNotificationModule> {\n    return {\n      ngModule: PushNotificationModule,\n      providers: [\n          {provide: DEFAULT_NOTIFICATION_SETTINGS, useValue: config},\n          PushNotificationService,\n      ],\n    };\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAIO,MAAM,6BAA6B,GAAG,IAAI,cAAc,CAA2B,+BAA+B,CAAC;;MCG7G,uBAAuB;IAUlC,YAAY,QAAkB;QAT9B,WAAM,GAAG,IAAI,OAAO,EAAgB,CAAC;QACrC,YAAO,GAAG,IAAI,OAAO,EAAgB,CAAC;QACtC,WAAM,GAAG,IAAI,OAAO,EAAgB,CAAC;QACrC,WAAM,GAAG,IAAI,OAAO,EAA4C,CAAC;QAIzD,cAAS,GAAmB,EAAE,CAAC;QAGrC,MAAM,eAAe,GAAG,QAAQ,CAAC,GAAG,CAA2B,6BAA6B,CAAC,CAAC;QAC9F,IAAI,CAAC,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