UNPKG

@dooboostore/dom-render

Version:
73 lines 3.23 kB
import { Router } from './Router'; export class PathRouter extends Router { constructor(config) { super(config); } getSearchParams(data) { var _a, _b; const searchParams = (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 = '') { // console.log('pppppp?') super.pushState(data, title, this.toUrl(path)); } replace(path, data, title = '') { super.replaceState(data, title, this.toUrl(path)); } pushDeleteSearchParam(name, data, title) { const s = this.getSearchParams({ delete: [name] }); this.push({ searchParams: s }, data, title); } pushDeleteHashSearchParam(name, data, title) { const s = this.getHashSearchParams({ delete: [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); } pushAddSearchParam(params, data, title) { const s = this.getSearchParams({ append: params }); this.push({ searchParams: s }, data, title); } replaceDeleteSearchParam(name, data, title) { const s = this.getSearchParams({ delete: Array.isArray(name) ? name : [name] }); this.replace({ 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.replaceState(data, title, href); } replaceAddSearchParam(params, data, title) { const s = this.getSearchParams({ append: params }); this.replace({ searchParams: s }, data, title); } getUrl() { const url = new URL(this.config.window.document.location.href); return url.pathname + url.search; } getPath() { return this.config.window.location.pathname; } getHashSearchParams(data) { var _a, _b; // http://local.com/#wow=222&wow=bb&z=2 const searchParams = new URLSearchParams(this.config.window.location.hash.slice(1)); (_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; // return undefined; } } //# sourceMappingURL=PathRouter.js.map