UNPKG

react-view-router

Version:
295 lines (292 loc) 9.86 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import "core-js/modules/web.dom.iterable.js"; import { Action, HistoryType } from './types'; import { createPath, freeze, createEvents, parsePath, createKey, allowTx, allowTxWithParams, copyOwnProperties } from './utils'; // const BeforeUnloadEventType = 'beforeunload'; export const HashChangeEventType = 'hashchange'; export const PopStateEventType = 'popstate'; export function createHistory(options) { const { window: _window, type, getLocationPath, createHref } = options; let globalHistory = _window.history; function getIndexAndLocation() { let { pathname = '/', search = '', hash = '' } = getLocationPath(); let state = globalHistory.state || {}; return [state.idx, freeze({ pathname, search, hash, state: state.usr || null, key: state.key || 'default' })]; } let action = Action.Push; let [index, location] = getIndexAndLocation(); let listeners = createEvents(); let blockers = createEvents(); function getIndex(delta) { let ret = index; if (delta) ret = Math.max(index + delta, 0); return ret; } function getNextLocation(to, state = null) { return freeze(copyOwnProperties(copyOwnProperties(_objectSpread({}, location), typeof to === 'string' ? parsePath(to) : to, true), { state, key: createKey() }, true)); } function getHistoryState(nextLocation, index) { return { usr: nextLocation.state, key: nextLocation.key, idx: index }; } function getHistoryStateAndUrl(nextLocation, index) { return [getHistoryState(nextLocation, index), createHref(nextLocation)]; } function applyTx(nextAction, payload) { action = nextAction; let prevIndex = index; [index, location] = getIndexAndLocation(); if (index == null && prevIndex != null) { index = nextAction === Action.Push ? prevIndex + 1 : nextAction === Action.Replace ? prevIndex : prevIndex - 1; } listeners.call({ action, index, location }, payload); } function pushHistoryState(location, index) { let [historyState, url] = getHistoryStateAndUrl(location, index); // TODO: Support forced reloading // try...catch because iOS limits us to 100 pushState calls :/ try { globalHistory.pushState(historyState, '', url); } catch (error) { // They are going to lose state here, but there is no real // way to warn them about it since the page will refresh... _window.location.assign(url); } return index; } function replaceHistoryState(location, index) { let [historyState, url] = getHistoryStateAndUrl(location, index); // TODO: Support forced reloading globalHistory.replaceState(historyState, '', url); return index; } let blockedPopTx = null; let blockedPopAp = null; let blockedPopDc = null; function go(delta) { blockedPopTx = null; globalHistory.go(delta); } function handlePop(e) { let index = getIndex(); let [nextIndex, nextLocation] = getIndexAndLocation(); if (nextIndex == null) { if (index == null || isNaN(index)) index = 0; nextIndex = createPath(nextLocation) === createPath(location) ? index : index + 1; replaceHistoryState(nextLocation, nextIndex); if (!blockedPopAp && !blockedPopTx && nextIndex === index) return; } let delta = index - nextIndex; let nextAction = !delta ? Action.Replace : delta > 0 ? Action.Pop : Action.Push; if (blockedPopDc) { blockedPopDc({ index: nextIndex, location: nextLocation }); blockedPopDc = null; } else if (blockedPopAp) { if (nextIndex === blockedPopAp.prevIndex) { go(blockedPopAp.delta); return; } blockedPopAp.cb && blockedPopAp.cb(); blockedPopAp = null; } else if (blockers.length && delta) { let seed = 0; const callback = (ok, payload) => { if (!blockedPopTx) return; if (ok) { blockedPopTx = null; applyTx(nextAction, payload); return; } blockedPopTx.backCallback(seed); }; nextLocation.fromEvent = true; blockedPopTx = { seed, index, nextIndex, action: nextAction, location: nextLocation, callback, backCallback(seed) { if (this.seed !== seed) return; blockedPopTx = null; blockedPopAp = { action, index, location, prevIndex: nextIndex, delta }; go(blockedPopAp.delta); } }; allowTxWithParams(blockers, blockedPopTx); } else { applyTx(nextAction); } } _window.addEventListener(PopStateEventType, handlePop); if (type === HistoryType.hash) { // popstate does not fire on hashchange in IE 11 and old (trident) Edge // https://developer.mozilla.org/de/docs/Web/API/Window/popstate_event _window.addEventListener(HashChangeEventType, e => { if (blockedPopTx || blockedPopAp) return; let [, nextLocation] = getIndexAndLocation(); // Ignore extraneous hashchange events. if (createPath(nextLocation) !== createPath(location)) { handlePop(e); } }); } if (index == null) { index = 0; globalHistory.replaceState(_objectSpread(_objectSpread({}, globalHistory.state), {}, { idx: index }), ''); } function push(to, state) { let nextAction = Action.Push; let nextLocation = getNextLocation(to, state); const seed = blockedPopTx ? ++blockedPopTx.seed : -1; const callback = (ok, payload) => { if (!ok) { if (blockedPopTx) blockedPopTx.backCallback(seed); return; } if (blockedPopTx && blockedPopTx.seed === seed) { blockedPopTx = null; } const _cb = index => { pushHistoryState(nextLocation, index + 1); applyTx(nextAction, payload); }; if (typeof to !== 'string' && to.delta) { blockedPopDc = ({ index }) => _cb(index); go(to.delta); return; } if (blockedPopAp) blockedPopAp.cb = () => _cb(index);else _cb(index); }; if (allowTx(blockers, nextAction, nextLocation, index, getIndex(typeof to !== 'string' ? to.delta : undefined) + 1, callback)) { callback(true, to); } } function replace(to, state) { let nextAction = Action.Replace; let nextLocation = getNextLocation(to, state); const seed = blockedPopTx ? ++blockedPopTx.seed : -1; const callback = (ok, payload) => { if (!ok) { if (blockedPopTx) blockedPopTx.backCallback(seed); return; } if (blockedPopTx && blockedPopTx.seed === seed) { blockedPopTx = null; } const _cb = index => { replaceHistoryState(nextLocation, index); applyTx(nextAction, payload); }; if (typeof to !== 'string' && to.delta) { blockedPopDc = ({ index }) => _cb(index); go(to.delta); return; } if (blockedPopAp) blockedPopAp.cb = () => _cb(index);else _cb(index); }; if (allowTx(blockers, nextAction, nextLocation, index, getIndex(typeof to !== 'string' ? to.delta : undefined), callback)) { callback(true); } } let history = { get extra() { return options.extra; }, get type() { return type; }, get action() { return action; }, get location() { return location; }, get index() { return index; }, get length() { return globalHistory.length; }, get state() { return getHistoryState(location, index).usr; }, get realtimeLocation() { const [, current] = getIndexAndLocation(); return current.pathname === location.pathname && current.search === location.search && current.hash === location.hash ? location : current; }, createHref, getIndexAndLocation, push, replace, replaceState(state) { const historyState = getHistoryState(location, index); historyState.usr = state; globalHistory.replaceState(historyState, ''); return state; }, refresh() { [index, location] = getIndexAndLocation(); return [index, location]; }, go, back() { go(-1); }, forward() { go(1); }, listen(listener) { return listeners.push(listener); }, block(blocker) { return blockers.push(blocker); } }; return history; }