UNPKG

@nu-art/thunder

Version:

Thunder - React & Typescript based frontend framework

154 lines 6.57 kB
"use strict"; /* * Thunder is a typescript & react frontend foundation that natively * runs on firebase function and is a part of the Thunderstorm larger project * * Copyright (C) 2018 Adam van der Kruk aka TacB0sS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Created by tacb0ss on 27/07/2018. */ var ts_common_1 = require("@nu-art/ts-common"); var history_1 = require("history"); var BrowserHistoryModule_Class = /** @class */ (function (_super) { __extends(BrowserHistoryModule_Class, _super); function BrowserHistoryModule_Class() { var _this = _super.call(this) || this; _this.getEncodedQueryParams = function () { var queryParams = {}; var 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; } var query = queryAsString.split("&"); return query.map(function (param) { var parts = param.split("="); return { key: parts[0], value: parts[1] }; }).reduce(function (toRet, param) { if (param.key && param.value) toRet[param.key] = param.value; return toRet; }, queryParams); }; _this.history = history_1.createBrowserHistory(); return _this; } BrowserHistoryModule_Class.prototype.push = function (push) { this.history.push(push); }; BrowserHistoryModule_Class.prototype.replace = function (push) { this.history.replace(push); }; BrowserHistoryModule_Class.prototype.composeQuery = function (queryParams) { var queryAsString = ts_common_1._keys(queryParams).map(function (key) { return key + "=" + queryParams[key]; }).join("&"); if (queryAsString.length === 0) return undefined; return queryAsString; }; BrowserHistoryModule_Class.prototype.getQueryParams = function () { var params = this.getEncodedQueryParams(); ts_common_1._keys(params).forEach(function (key) { var value = params[key]; if (!value) { delete params[key]; return; } params[key] = decodeURIComponent(value); }); return params; }; BrowserHistoryModule_Class.prototype.setQuery = function (queryParams) { var encodedQueryParams = __assign({}, queryParams); ts_common_1._keys(encodedQueryParams).forEach(function (key) { var value = encodedQueryParams[key]; if (!value) { delete encodedQueryParams[key]; return; } encodedQueryParams[key] = encodeURIComponent(value); }); this.updateQueryParams(encodedQueryParams); }; BrowserHistoryModule_Class.prototype.addQueryParam = function (key, value) { var encodedQueryParams = this.getEncodedQueryParams(); encodedQueryParams[key] = encodeURIComponent(value); this.updateQueryParams(encodedQueryParams); }; BrowserHistoryModule_Class.prototype.removeQueryParam = function (key) { var encodedQueryParams = this.getEncodedQueryParams(); delete encodedQueryParams[key]; var data = this.createHistoryDataFromQueryParams(encodedQueryParams); this.replace(data); }; BrowserHistoryModule_Class.prototype.setUrl = function (url, queryParams) { this.push(this.createHistoryDataFromQueryParams(queryParams, url)); }; BrowserHistoryModule_Class.prototype.createHistoryDataFromQueryParams = function (encodedQueryParams, pathname) { if (pathname === void 0) { pathname = window.location.pathname; } return { pathname: !pathname.endsWith("/") ? pathname : pathname.substring(0, pathname.length - 1), search: !encodedQueryParams ? "" : this.composeQuery(encodedQueryParams) }; }; BrowserHistoryModule_Class.prototype.updateQueryParams = function (encodedQueryParams) { var data = this.createHistoryDataFromQueryParams(encodedQueryParams); this.push(data); }; BrowserHistoryModule_Class.prototype.getOrigin = function () { return window.location.origin; }; BrowserHistoryModule_Class.prototype.getCurrent = function () { return this.history.location; }; BrowserHistoryModule_Class.prototype.getHistory = function () { return this.history; }; return BrowserHistoryModule_Class; }(ts_common_1.Module)); exports.BrowserHistoryModule_Class = BrowserHistoryModule_Class; exports.BrowserHistoryModule = new BrowserHistoryModule_Class(); //# sourceMappingURL=history-module.js.map