ngx-back-button
Version:
A library for handling a proper angular back button capability
1 lines • 13.1 kB
Source Map (JSON)
{"version":3,"file":"ngx-back-button.mjs","sources":["../../../projects/ngx-back-button/src/lib/ngx-back-button.const.ts","../../../projects/ngx-back-button/src/lib/ngx-back-button.service.ts","../../../projects/ngx-back-button/src/lib/ngx-back-button.directive.ts","../../../projects/ngx-back-button/src/lib/ngx-back-button.providers.ts","../../../projects/ngx-back-button/src/public-api.ts","../../../projects/ngx-back-button/src/ngx-back-button.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core'\nimport { NgxBackButtonServiceConfig } from './ngx-back-button.interface'\n\nexport const NgxBackButtonServiceProvider = new InjectionToken<NgxBackButtonServiceConfig>(\n 'NgxBackButtonServiceConfig',\n)\n","import { Location } from '@angular/common'\nimport { inject, Injectable, Signal, signal } from '@angular/core'\nimport { NavigationEnd, Router } from '@angular/router'\nimport { filter, skip } from 'rxjs'\nimport { NgxBackButtonServiceProvider } from './ngx-back-button.const'\nimport { NgxBackButtonServiceConfig } from './ngx-back-button.interface'\n\n@Injectable({\n providedIn: 'root',\n})\nexport class NgxBackButtonService {\n readonly #router = inject(Router)\n readonly #location = inject(Location)\n readonly #rootConfig = inject(NgxBackButtonServiceProvider, { optional: true })\n\n private _history: string[] = []\n private _navigatingBack = false\n private _$nextBackNavigationPath = signal(this._getFallBackNavigationPath())\n\n readonly $nextBackNavigationPath: Signal<string> = this._$nextBackNavigationPath.asReadonly()\n\n constructor() {\n this.#router.events\n .pipe(\n filter((e): e is NavigationEnd => e instanceof NavigationEnd),\n skip(1), // Skip the first event (initial load)\n )\n .subscribe((event) => {\n if (!this._navigatingBack) this._history.push(event.urlAfterRedirects)\n\n this._updateNextBackNavigationPath()\n this._navigatingBack = false\n })\n }\n\n getHistory(): string[] {\n return this._history\n }\n\n /**\n * @param fallback\n * @param config Optional configuration to override the root configuration (typically from child routes)\n * @return Boolean: True === Had an history to go back to\n */\n back(fallback?: string, config?: NgxBackButtonServiceConfig | null): boolean {\n this._navigatingBack = true\n\n const record = this._history.pop()\n this._updateNextBackNavigationPath(fallback, config)\n\n if (this._history.length > 0) {\n this.#location.back()\n return true\n } else {\n this._navigatingBack = false // Give an element to go back to on next navigation\n\n try {\n window.history.replaceState(null, '', this._getFallBackNavigationPath(fallback, config))\n } catch (error) {\n console.error('NgxBackButton: ' + error)\n }\n\n window.history.pushState(null, '', record ?? this.#router.url)\n this.#location.back()\n return false\n }\n }\n\n private _getFallBackNavigationPath(\n fallback?: string,\n config?: NgxBackButtonServiceConfig | null,\n ): string {\n const effectiveConfig = config || this.#rootConfig\n const rootUrl = effectiveConfig?.rootUrl || ''\n const fallbackPrefix = effectiveConfig?.fallbackPrefix || ''\n const fallbackPath = fallback || rootUrl\n\n if (fallbackPath.startsWith('../')) {\n return this._resolveRelativeFallbackNavigationPath(fallbackPath)\n }\n\n return fallbackPrefix + fallbackPath\n }\n\n private _resolveRelativeFallbackNavigationPath(fallback: string): string {\n const suffixIndex = fallback.search(/[?#]/)\n const path = suffixIndex === -1 ? fallback : fallback.slice(0, suffixIndex)\n const suffix = suffixIndex === -1 ? '' : fallback.slice(suffixIndex)\n const fallbackSegments = path.split('/')\n let parentSegmentCount = 0\n\n while (fallbackSegments[0] === '..') {\n fallbackSegments.shift()\n parentSegmentCount++\n }\n\n const currentPath = this.#router.url.split(/[?#]/, 1)[0]\n const currentSegments = currentPath.split('/').filter(Boolean)\n const retainedSegments = currentSegments.slice(\n 0,\n Math.max(0, currentSegments.length - parentSegmentCount),\n )\n const appendedSegments = fallbackSegments.filter((segment) => segment && segment !== '.')\n\n return `/${[...retainedSegments, ...appendedSegments].join('/')}${suffix}`\n }\n\n private _updateNextBackNavigationPath(\n fallback?: string,\n config?: NgxBackButtonServiceConfig | null,\n ): void {\n const previousHistoryEntry = this._history[this._history.length - 2]\n\n this._$nextBackNavigationPath.set(\n previousHistoryEntry ?? this._getFallBackNavigationPath(fallback, config),\n )\n }\n}\n","import { Directive, HostListener, inject, Input } from '@angular/core'\nimport { NgxBackButtonServiceProvider } from './ngx-back-button.const'\nimport { NgxBackButtonService } from './ngx-back-button.service'\n\n@Directive({\n selector: '[ngxBackButton]',\n})\nexport class NgxBackButtonDirective {\n @Input() ngxBackButton?: string\n\n readonly #ngxBackButtonService = inject(NgxBackButtonService)\n readonly #config = inject(NgxBackButtonServiceProvider, { optional: true })\n\n @HostListener('click')\n onClick(): void {\n this.#ngxBackButtonService.back(this.ngxBackButton, this.#config)\n }\n}\n","import {\n EnvironmentProviders,\n inject,\n makeEnvironmentProviders,\n provideEnvironmentInitializer,\n} from '@angular/core'\nimport { NgxBackButtonServiceProvider } from './ngx-back-button.const'\nimport { NgxBackButtonServiceConfig } from './ngx-back-button.interface'\nimport { NgxBackButtonService } from './ngx-back-button.service'\n\n/**\n * Internal function to create environment providers for NgxBackButton configuration.\n * @internal\n */\nfunction createNgxBackButtonProviders(config?: NgxBackButtonServiceConfig): EnvironmentProviders {\n return makeEnvironmentProviders([\n {\n provide: NgxBackButtonServiceProvider,\n useValue: config || {},\n },\n provideEnvironmentInitializer(() => inject(NgxBackButtonService)),\n ])\n}\n\n/**\n * Provides the NgxBackButton service with the given configuration.\n * Use this in your root application providers.\n *\n * When both root and child configurations are provided, the child configuration\n * will override the root configuration for routes under that path, following\n * Angular's hierarchical dependency injection.\n *\n * @param config Configuration for the NgxBackButton service\n * @returns Environment providers for NgxBackButton\n *\n * @example\n * ```typescript\n * bootstrapApplication(AppComponent, {\n * providers: [\n * provideNgxBackButton({\n * rootUrl: '/home',\n * fallbackPrefix: '/tabs'\n * })\n * ]\n * })\n * ```\n */\nexport function provideNgxBackButton(config?: NgxBackButtonServiceConfig): EnvironmentProviders {\n return createNgxBackButtonProviders(config)\n}\n\n/**\n * Provides child-level configuration for NgxBackButton service.\n * Use this in lazy-loaded route providers to override the root configuration.\n *\n * This configuration will override the root configuration for all components\n * and directives within the route's component tree, following Angular's\n * hierarchical dependency injection.\n *\n * @param config Configuration for the NgxBackButton service at child level\n * @returns Environment providers for NgxBackButton child configuration\n *\n * @example\n * ```typescript\n * export const routes: Routes = [\n * {\n * path: 'child',\n * providers: [\n * provideNgxBackButtonChild({\n * rootUrl: '/login'\n * })\n * ],\n * loadComponent: () => import('./child.component')\n * }\n * ]\n * ```\n */\nexport function provideNgxBackButtonChild(\n config?: NgxBackButtonServiceConfig,\n): EnvironmentProviders {\n return createNgxBackButtonProviders(config)\n}\n","/*\n * Public API Surface of ngx-back-button\n */\n\nexport * from './lib/ngx-back-button.const'\nexport * from './lib/ngx-back-button.directive'\nexport * from './lib/ngx-back-button.service'\nexport * from './lib/ngx-back-button.providers'\nexport * from './lib/ngx-back-button.interface'\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;MAGa,4BAA4B,GAAG,IAAI,cAAc,CAC5D,4BAA4B;;MCMjB,oBAAoB,CAAA;AACtB,IAAA,OAAO;AACP,IAAA,SAAS;AACT,IAAA,WAAW;AAQpB,IAAA,WAAA,GAAA;AAVS,QAAA,IAAA,CAAA,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;AACxB,QAAA,IAAA,CAAA,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC;QAC5B,IAAA,CAAA,WAAW,GAAG,MAAM,CAAC,4BAA4B,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QAEvE,IAAA,CAAA,QAAQ,GAAa,EAAE;QACvB,IAAA,CAAA,eAAe,GAAG,KAAK;AACvB,QAAA,IAAA,CAAA,wBAAwB,GAAG,MAAM,CAAC,IAAI,CAAC,0BAA0B,EAAE;qGAAC;AAEnE,QAAA,IAAA,CAAA,uBAAuB,GAAmB,IAAI,CAAC,wBAAwB,CAAC,UAAU,EAAE;QAG3F,IAAI,CAAC,OAAO,CAAC;AACV,aAAA,IAAI,CACH,MAAM,CAAC,CAAC,CAAC,KAAyB,CAAC,YAAY,aAAa,CAAC,EAC7D,IAAI,CAAC,CAAC,CAAC;AAER,aAAA,SAA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