UNPKG

@intuitionrobotics/thunderstorm

Version:
106 lines 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserHistoryModule = exports.BrowserHistoryModule_Class = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); const history_1 = require("history"); class BrowserHistoryModule_Class extends ts_common_1.Module { constructor() { super("BrowserHistoryModule"); this.getEncodedQueryParams = () => { const queryParams = {}; let queryAsString = window.location.search; if (!queryAsString || queryAsString.length === 0) return {}; while (true) { if (queryAsString.startsWith("?")) queryAsString = queryAsString.substring(1); else if (queryAsString.startsWith("/?")) queryAsString = queryAsString.substring(1); else break; } const query = queryAsString.split("&"); return query.map(param => { const parts = param.split("="); return { key: parts[0], value: parts[1] }; }).reduce((toRet, param) => { if (param.key && param.value) toRet[param.key] = param.value; return toRet; }, queryParams); }; this.history = (0, history_1.createBrowserHistory)(); } push(push) { this.history.push(push); } replace(push) { this.history.replace(push); } composeQuery(queryParams) { const queryAsString = (0, ts_common_1._keys)(queryParams).map((key) => `${key}=${queryParams[key]}`).join("&"); if (queryAsString.length === 0) return undefined; return queryAsString; } getQueryParams() { const params = this.getEncodedQueryParams(); (0, ts_common_1._keys)(params).forEach(key => { const value = params[key]; if (!value) { delete params[key]; return; } params[key] = decodeURIComponent(value); }); return params; } setQuery(queryParams) { const encodedQueryParams = Object.assign({}, queryParams); (0, ts_common_1._keys)(encodedQueryParams).forEach(key => { const value = encodedQueryParams[key]; if (!value) { delete encodedQueryParams[key]; return; } encodedQueryParams[key] = encodeURIComponent(value); }); this.updateQueryParams(encodedQueryParams); } addQueryParam(key, value) { const encodedQueryParams = this.getEncodedQueryParams(); encodedQueryParams[key] = encodeURIComponent(value); this.updateQueryParams(encodedQueryParams); } removeQueryParam(key) { const encodedQueryParams = this.getEncodedQueryParams(); delete encodedQueryParams[key]; const data = this.createHistoryDataFromQueryParams(encodedQueryParams); this.replace(data); } setUrl(url, queryParams) { this.push(this.createHistoryDataFromQueryParams(queryParams, url)); } createHistoryDataFromQueryParams(encodedQueryParams, pathname = window.location.pathname) { return { pathname: !pathname.endsWith("/") ? pathname : pathname.substring(0, pathname.length - 1), search: !encodedQueryParams ? "" : this.composeQuery(encodedQueryParams) }; } updateQueryParams(encodedQueryParams) { const data = this.createHistoryDataFromQueryParams(encodedQueryParams); this.push(data); } getOrigin() { return window.location.origin; } getCurrent() { return this.history.location; } getHistory() { return this.history; } } exports.BrowserHistoryModule_Class = BrowserHistoryModule_Class; exports.BrowserHistoryModule = new BrowserHistoryModule_Class(); //# sourceMappingURL=HistoryModule.js.map