UNPKG

react-view-router

Version:
74 lines (71 loc) 3.39 kB
import "core-js/modules/es6.regexp.search.js"; import "core-js/modules/es6.regexp.split.js"; import { HistoryType } from './types'; import { getBaseHref, getPossibleHashType, createHref, parsePath, readonly } from './utils'; import { createHistory } from './history'; /** * A hash history stores the current location in the fragment identifier portion * of the URL in a web browser environment. * * This is ideal for apps that do not control the server for some reason * (because the fragment identifier is never sent to the server), including some * shared hosting environments that do not provide fine-grained controls over * which pages are served at which URLs. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#hashhistory */ export function createHashHref(to, hashType, _window = globalThis) { var _window$location; var path = createHref(to, hashType, _window); var searchIndex = path.indexOf('?'); if (searchIndex > 0 && _window !== null && _window !== void 0 && (_window$location = _window.location) !== null && _window$location !== void 0 && _window$location.search) { var _window$location2; var searches = path.substr(searchIndex + 1).split('&'); path = path.substr(0, searchIndex); (_window$location2 = _window.location) === null || _window$location2 === void 0 || _window$location2.search.substr(1).split('&').forEach(value => { var idx = searches.indexOf(value); if (~idx) searches.splice(idx, 1); }); if (searches.length) path += '?' + searches.join('&'); } if (!path.startsWith('#')) path = '#' + path; return getBaseHref() + path; } /** * Hash history stores the location in window.location.hash. This makes it ideal * for situations where you don't want to send the location to the server for * some reason, either because you do cannot configure it or the URL space is * reserved for something else. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#createhashhistory */ export function createHashHistory(options = {}) { var _globalThis$document; var _options$window = options.window, _window = _options$window === void 0 ? (_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.defaultView : _options$window; var _options$hashType = options.hashType, hashType = _options$hashType === void 0 ? getPossibleHashType(_window) : _options$hashType; var type = HistoryType.hash; var history = createHistory({ window: _window, type, getLocationPath: () => { var _window$location3; var path = _window.location.hash.substr(1); if (path && !path.startsWith('/')) path = '/' + path; if (_window !== null && _window !== void 0 && (_window$location3 = _window.location) !== null && _window$location3 !== void 0 && _window$location3.search) { var search = _window.location.search; var searchIndex = path.indexOf('?'); if (searchIndex >= 0) { path = path.substr(0, searchIndex) + search + '&' + path.substr(searchIndex + 1); } else path += search; } return parsePath(path); }, createHref: to => createHashHref(to, hashType, _window), extra: options.extra }); readonly(history, 'hashType', () => hashType); return history; } //# sourceMappingURL=hash.js.map