UNPKG

nice-ui

Version:

React design system, components, and utilities

56 lines (55 loc) 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NiceUiNavService = void 0; const location_1 = require("rx-use/lib/location$"); const pathname_1 = require("rx-use/lib/pathname$"); const rxjs_1 = require("rxjs"); const router_1 = require("../../misc/router"); const mapPath = (pathname) => { if (pathname[0] === '/') pathname = pathname.slice(1); return pathname.split('/'); }; class NiceUiNavService { constructor(opts = {}) { this.location$ = location_1.location$; this.pathname$ = pathname_1.pathname$; /** * ID of asset which is displayed on the page. Empty string if the currently * rendered page is not related to an asset. */ this.pageAsset$ = new rxjs_1.BehaviorSubject(''); this.go = (route, replace, state) => { if (route[0] !== '/') route = '/' + route; this.navigate(route, { replace: !!replace, state }); }; this.updateQuery = (params, replace) => { const urlSearchParams = new URLSearchParams(location.search); for (const [name, value] of Object.entries(params)) { if (typeof value !== 'string') { if (!value) { urlSearchParams.delete(name); } else { urlSearchParams.set(name, String(value)); } } else { urlSearchParams.set(name, value); } } const search = urlSearchParams.toString(); const route = location.pathname + (search ? '?' + search : ''); this.go(route, replace); }; this.navigate = opts.go ?? router_1.go; const steps$ = new rxjs_1.BehaviorSubject(mapPath(this.pathname$.getValue())); this.steps$ = steps$; this.pathname$.subscribe((pathname) => { window.scrollTo(0, 0); steps$.next(mapPath(pathname)); }); } } exports.NiceUiNavService = NiceUiNavService;