UNPKG

ngx-back-button

Version:

A library for handling a proper angular back button capability

99 lines (92 loc) 4.07 kB
import * as i0 from '@angular/core'; import { InjectionToken, inject, Injectable, HostListener, Input, Directive } from '@angular/core'; import { Location } from '@angular/common'; import { Router, NavigationEnd } from '@angular/router'; import { filter, skip } from 'rxjs'; const NgxBackButtonServiceProvider = new InjectionToken('NgxBackButtonServiceConfig'); class NgxBackButtonService { #router; #location; #config; constructor() { this.#router = inject(Router); this.#location = inject(Location); this.#config = inject(NgxBackButtonServiceProvider, { optional: true }); this._history = []; this._navigatingBack = false; this._rootUrl = this.#config?.rootUrl || ''; this._fallbackPrefix = this.#config?.fallbackPrefix || ''; this.#router.events .pipe(filter((e) => e instanceof NavigationEnd), skip(1)) .subscribe((event) => { if (!this._navigatingBack) this._history.push(event.urlAfterRedirects); this._navigatingBack = false; }); } getHistory() { return this._history; } /** * * @param fallback * @return Boolean: True === Had an history to go back to */ back(fallback) { this._navigatingBack = true; const record = this._history.pop(); if (this._history.length > 0) { this.#location.back(); return true; } else { this._navigatingBack = false; // Give an element to go back to on next navigation try { window.history.replaceState(null, '', this._fallbackPrefix + (fallback || this._rootUrl)); } catch (error) { console.error('NgxBackButton: ' + error); } window.history.pushState(null, '', record ?? this.#router.url); this.#location.back(); return false; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.2", ngImport: i0, type: NgxBackButtonService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.0.2", ngImport: i0, type: NgxBackButtonService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.2", ngImport: i0, type: NgxBackButtonService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [] }); class NgxBackButtonDirective { constructor(_ngxBackButtonService) { this._ngxBackButtonService = _ngxBackButtonService; } onClick() { this._ngxBackButtonService.back(this.ngxBackButton); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.2", ngImport: i0, type: NgxBackButtonDirective, deps: [{ token: NgxBackButtonService }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.0.2", type: NgxBackButtonDirective, isStandalone: true, selector: "[ngxBackButton]", inputs: { ngxBackButton: "ngxBackButton" }, host: { listeners: { "click": "onClick()" } }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.2", ngImport: i0, type: NgxBackButtonDirective, decorators: [{ type: Directive, args: [{ selector: '[ngxBackButton]', }] }], ctorParameters: () => [{ type: NgxBackButtonService }], propDecorators: { ngxBackButton: [{ type: Input }], onClick: [{ type: HostListener, args: ['click'] }] } }); /* * Public API Surface of ngx-back-button */ /** * Generated bundle index. Do not edit. */ export { NgxBackButtonDirective, NgxBackButtonService, NgxBackButtonServiceProvider }; //# sourceMappingURL=ngx-back-button.mjs.map