UNPKG

@dooboostore/dom-render

Version:
86 lines 3.76 kB
import { Router } from './Router'; import { LocationUtils } from '@dooboostore/core-web/location/LocationUtils'; export class HashRouter extends Router { constructor(config) { super(config); } getSearchParams(data) { var _a, _b; const hashSearch = LocationUtils.hashSearch(this.config.window); const searchParams = hashSearch ? new URLSearchParams(hashSearch) : (new URL(this.config.window.document.location.href)).searchParams; (_a = data === null || data === void 0 ? void 0 : data.delete) === null || _a === void 0 ? void 0 : _a.forEach(it => { searchParams.delete(it); }); (_b = data === null || data === void 0 ? void 0 : data.append) === null || _b === void 0 ? void 0 : _b.forEach(it => { searchParams.append(it[0], it[1]); }); return searchParams; } push(path, data, title = '') { if (path === '/') { super.pushState(data, title, '/'); } else { super.pushState(data, title, `#${this.toUrl(path)}`); } } replace(path, data, title) { if (path === '/') { super.replaceState(data, title, '/'); } else { super.replaceState(data, title, `#${this.toUrl(path)}`); } } setDeleteHashSearchParam(name, data, title) { } pushDeleteHashSearchParam(name, data, title) { const s = this.getHashSearchParams({ delete: Array.isArray(name) ? name : [name] }); const size = Array.from(s.entries()).length; const href = `${this.config.window.location.pathname}${this.config.window.location.search}${size > 0 ? '#' + s.toString() : ''}`; super.pushState(data, title, href); } pushDeleteSearchParam(name, data, title) { const s = this.getSearchParams({ delete: Array.isArray(name) ? name : [name] }); this.push({ searchParams: s }, data, title); } pushAddSearchParam(params, data, title) { const s = this.getSearchParams({ append: params }); this.push({ searchParams: s }, data, title); } replaceDeleteHashSearchParam(name, data, title) { const s = this.getHashSearchParams({ delete: Array.isArray(name) ? name : [name] }); const size = Array.from(s.entries()).length; const href = `${this.config.window.location.pathname}${this.config.window.location.search}${size > 0 ? '#' + s.toString() : ''}`; super.pushState(data, title, href); } replaceDeleteSearchParam(name, data, title) { const s = this.getSearchParams({ delete: Array.isArray(name) ? name : [name] }); this.push({ searchParams: s }, data, title); } replaceAddSearchParam(params, data, title) { const s = this.getSearchParams({ append: params }); this.push({ searchParams: s }, data, title); } getUrl() { return LocationUtils.hash(this.config.window) || '/'; } getPath() { return LocationUtils.hashPath(this.config.window) || '/'; } getHashSearchParams(data) { var _a, _b; const hash = this.config.window.location.hash; const queryIndex = hash.indexOf('?'); const searchParams = (queryIndex !== -1) ? new URLSearchParams(hash.slice(queryIndex + 1)) : new URLSearchParams(); ; (_a = data === null || data === void 0 ? void 0 : data.delete) === null || _a === void 0 ? void 0 : _a.forEach(it => { searchParams.delete(it); }); (_b = data === null || data === void 0 ? void 0 : data.append) === null || _b === void 0 ? void 0 : _b.forEach(it => { searchParams.append(it[0], it[1]); }); return searchParams; } } //# sourceMappingURL=HashRouter.js.map