react-view-router
Version:
react-view-router
263 lines (262 loc) • 11.4 kB
JavaScript
import "core-js/modules/es6.symbol.js";
import "core-js/modules/es6.array.filter.js";
import "core-js/modules/es7.object.get-own-property-descriptors.js";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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; }
import "core-js/modules/es6.regexp.search.js";
import "core-js/modules/es6.regexp.replace.js";
import "core-js/modules/es7.array.includes.js";
import { createHashHistory, createBrowserHistory, createMemoryHistory, getBaseHref, HistoryType, readonly } from './history';
import { innumerable, isHistory, once, getSessionStorage, setSessionStorage, isFunction, isString } from './util';
import { REACT_VIEW_ROUTER_GLOBAL } from './global';
import { parseQuery } from './config';
function eachInterceptor(interceptors, location, callback, index, nexts, payload) {
var item = interceptors[index];
if (!item) return callback(true, payload);
if (isFunction(item)) item = {
interceptor: item,
router: null
};
var cb = once((ok, payload) => {
item.payload = payload;
if (!ok) return callback(ok);
if (isFunction(ok)) nexts.push(ok);
eachInterceptor(interceptors, location, callback, index + 1, nexts, payload);
});
if (item.router && item.router.isRunning) item.interceptor(location, cb);else cb(true, payload);
}
function confirmInterceptors(interceptors, location, callback) {
if (isHistory(interceptors)) interceptors = interceptors.interceptors;
var nexts = [];
interceptors = [...interceptors];
return eachInterceptor(interceptors, location, ok => {
nexts.forEach(next => next(ok));
callback(ok, interceptors);
}, 0, nexts);
}
function createStackInfo(index, location) {
return {
pathname: location.pathname,
search: location.search,
index,
timestamp: Date.now(),
query: parseQuery(location.search)
};
}
function createHistory4(history, options = {}) {
var _options$basename = options.basename,
basename = _options$basename === void 0 ? '' : _options$basename;
if (basename && basename.endsWith('/')) basename = basename.substr(0, basename.length - 1);
var ensurceSlash = pathname => pathname && (pathname.startsWith('/') ? pathname : '/' + pathname);
var encodeLocation = location => {
if (!basename) return location;
if (isString(location)) return basename + ensurceSlash(location);
return _objectSpread(_objectSpread({}, location), {}, {
pathname: basename + ensurceSlash(location.pathname)
});
};
var decodeLocation = location => {
var _ret$pathname;
if (!basename) return location;
if (isString(location) && location.startsWith(basename)) return location.replace(basename, '');
var ret = _objectSpread({}, location);
if ((_ret$pathname = ret.pathname) !== null && _ret$pathname !== void 0 && _ret$pathname.startsWith(basename)) {
ret.pathname = ret.pathname.replace(basename, '');
}
return ret;
};
var history4 = {
isHistory4: true,
goBack: () => history.back(),
goForward: () => history.forward(),
listen: listener => history.listen(({
location,
action
}) => listener(decodeLocation(location), action)),
block: prompt => history.block(({
location,
action,
callback
}) => {
if (prompt != null) {
var result = typeof prompt === 'function' ? prompt(decodeLocation(location), action) : prompt;
if (isString(result)) {
if (typeof options.getUserConfirmation === 'function') {
options.getUserConfirmation(result, callback);
} else {
// if (process.env.NODE_ENV !== 'production') warn('A history needs a getUserConfirmation function in order to use a prompt message');
callback(true);
}
} else {
// Return false from a transition hook to cancel the transition.
callback(result !== false);
}
} else {
callback(true);
}
})
};
innumerable(history4, 'owner', history);
innumerable(history, 'locationCached', null);
readonly(history4, 'location', function () {
var newLocation = history.location;
var locationCached = this.locationCached;
if (!locationCached || locationCached.pathname !== newLocation.pathname || locationCached.search !== newLocation.search || locationCached.hash !== newLocation.hash) {
this.locationCached = encodeLocation(newLocation);
}
return this.locationCached;
});
history4.createHref = location => history.createHref(encodeLocation(location));
history4.push = (path, state) => history.push(encodeLocation(path), state);
history4.replace = (path, state) => history.replace(encodeLocation(path), state);
['length', 'type', 'action', 'go', 'state'].forEach(key => {
readonly(history4, key, () => history[key]);
});
return history4;
}
function isHistory4(history) {
return history && history.isHistory4;
}
function createHistory(options, fn, type) {
var interceptors = [];
var history = fn();
history.createHistory4 = (options = {}) => createHistory4(history, options);
history.isHistoryInstance = true;
history.interceptors = interceptors;
history.interceptorTransitionTo = function (interceptor, router) {
var idx = this.interceptors.findIndex(v => v.interceptor === interceptor);
var newRouter = null;
if (idx < 0) {
this.interceptors.push({
interceptor,
router
});
newRouter = router;
} else {
console.error(`[react-view-router][interceptorTransitionTo]interceptor was already exist in index: ${idx}!`, router);
if (router && router !== this.interceptors[idx].router) {
var oldRouter = this.interceptors[idx].router;
oldRouter && oldRouter.stop();
this.interceptors[idx].router = router;
newRouter = router;
console.error('[react-view-router][interceptorTransitionTo] router was replaced by same interceptor!', oldRouter, router);
}
}
if (newRouter && newRouter.basename) {
var basename = newRouter.basename;
var parentRouter = null;
this.interceptors.forEach(v => {
if (!v.router || v.router === newRouter || !basename.includes(v.router.basename)) return;
if (!parentRouter || parentRouter.basename < v.router.basename) parentRouter = v.router;
});
if (newRouter._updateParent) newRouter._updateParent(parentRouter);
}
return () => {
var idx = this.interceptors.findIndex(v => v.interceptor === interceptor);
if (~idx) {
this.interceptors.splice(idx, 1);
if (router.parent && router._updateParent) router._updateParent(null);
}
};
};
var needSession = type !== HistoryType.memory;
var SessionStacksKey = `_REACT_VIEW_ROUTER_${type.toUpperCase()}_STACKS_`;
var SessionStacksKeys = ['index', 'pathname', 'search', 'timestamp'];
history.stacks = needSession ? getSessionStorage(SessionStacksKey, true) || [] : [];
history.stacks.forEach(s => !s.query && (s.query = parseQuery(s.search)));
var lastStackInfo = history.stacks[history.stacks.length - 1];
if (!lastStackInfo || lastStackInfo.index !== history.index) {
history.stacks.push(createStackInfo(history.index, history.location));
if (needSession) setSessionStorage(SessionStacksKey, history.stacks, SessionStacksKeys);
}
innumerable(history, '_unlisten', history.listen(state => {
if (!state) return;
var location = state.location,
index = state.index;
var lastStackInfo = history.stacks[history.stacks.length - 1];
if (lastStackInfo && index > lastStackInfo.index) {
history.stacks.push(createStackInfo(index, location));
} else {
var idx = history.stacks.findIndex(v => v.index === index);
if (~idx) {
history.stacks.splice(idx, history.stacks.length, createStackInfo(index, location));
}
}
if (needSession) setSessionStorage(SessionStacksKey, history.stacks, SessionStacksKeys);
// console.log('[createHistory][listen]', location, action, index);
}));
innumerable(history, '_unblock', history.block(({
action,
location,
callback
}) => {
location = _objectSpread({
action
}, location);
confirmInterceptors(interceptors, location, callback);
}));
return history;
}
function createHashHistoryNew(options, router) {
if (options.history && options.history.type === HistoryType.hash) {
return isHistory4(options.history) ? options.history.owner : options.history;
}
if (REACT_VIEW_ROUTER_GLOBAL.historys.hash) {
router.isHistoryCreater = REACT_VIEW_ROUTER_GLOBAL.historys.hash.extra === router;
return REACT_VIEW_ROUTER_GLOBAL.historys.hash;
}
return REACT_VIEW_ROUTER_GLOBAL.historys.hash = createHistory(options, () => {
router.isHistoryCreater = true;
return createHashHistory(_objectSpread(_objectSpread({}, options), {}, {
extra: router
}));
}, HistoryType.hash);
}
function createBrowserHistoryNew(options, router) {
if (options.history && options.history.type === HistoryType.browser) {
return isHistory4(options.history) ? options.history.owner : options.history;
}
if (REACT_VIEW_ROUTER_GLOBAL.historys.browser) {
router.isHistoryCreater = REACT_VIEW_ROUTER_GLOBAL.historys.browser.extra === router;
return REACT_VIEW_ROUTER_GLOBAL.historys.browser;
}
return REACT_VIEW_ROUTER_GLOBAL.historys.browser = createHistory(options, () => {
router.isHistoryCreater = true;
return createBrowserHistory(_objectSpread(_objectSpread({}, options), {}, {
extra: router
}));
}, HistoryType.browser);
}
function createMemoryHistoryNew(options, router) {
if (options.history && options.history.type === HistoryType.memory) {
return isHistory4(options.history) ? options.history.owner : options.history;
}
return createHistory(options, () => {
router.isHistoryCreater = true;
return createMemoryHistory({
initialEntries: options.pathname ? [options.pathname] : ['/'],
extra: router
});
}, HistoryType.memory);
}
function getPossibleHistory(options) {
if (REACT_VIEW_ROUTER_GLOBAL.historys.hash) {
return REACT_VIEW_ROUTER_GLOBAL.historys.hash;
}
if (REACT_VIEW_ROUTER_GLOBAL.historys.browser) {
return REACT_VIEW_ROUTER_GLOBAL.historys.browser;
}
if (options && options.history) return options.history;
return null;
}
export { createHashHistoryNew as createHashHistory, createBrowserHistoryNew as createBrowserHistory, createMemoryHistoryNew as createMemoryHistory, getBaseHref, getPossibleHistory, confirmInterceptors, REACT_VIEW_ROUTER_GLOBAL, isHistory4 };
// export {
// createHashHistory,
// createBrowserHistory,
// createMemoryHistory,
// History,
// HistoryFix,
// LocationState
// };
//# sourceMappingURL=history-fix.js.map