react-view-router
Version:
react-view-router
183 lines (178 loc) • 7 kB
JavaScript
const _excluded = ["configurable", "enumerable"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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); }
const CAN_USE_DOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
// eslint-disable-next-line no-constant-condition
const freeze = false /** __DEV__* */ ? obj => Object.freeze(obj) : obj => obj;
function getPossibleHashType(_window = document.defaultView, hash = '') {
if (!hash || !hash.startsWith('#')) hash = _window.location.hash;
let locationHash = hash.substr(1, 1);
return !locationHash || locationHash === '/' ? 'slash' : 'noslash';
}
function clamp(n, lowerBound, upperBound) {
return Math.min(Math.max(n, lowerBound), upperBound);
}
// function promptBeforeUnload(event: BeforeUnloadEvent) {
// // Cancel the event.
// event.preventDefault();
// // Chrome (and legacy IE) requires returnValue to be set.
// event.returnValue = '';
// }
function createEvents() {
let handlers = [];
return {
get length() {
return handlers.length;
},
push(fn) {
handlers.push(fn);
return function () {
handlers = handlers.filter(handler => handler !== fn);
};
},
call(arg, payload) {
handlers.forEach(fn => fn && fn(arg, payload));
}
};
}
function createKey() {
return Math.random().toString(36).substr(2, 8);
}
function getBaseHref() {
if (!CAN_USE_DOM) return '';
let base = globalThis.document.querySelector('base');
let href = '';
if (base && base.getAttribute('href')) {
let url = globalThis.location.href;
let hashIndex = url.indexOf('#');
href = hashIndex === -1 ? url : url.slice(0, hashIndex);
}
return href;
}
/**
* Creates a string URL path from the given pathname, search, and hash components.
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#createpath
*/
function createPath({
pathname = '/',
search = '',
hash = ''
}) {
return pathname + search + hash;
}
/**
* Parses a string URL path into its separate pathname, search, and hash components.
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#parsepath
*/
function parsePath(path) {
let partialPath = {};
if (path) {
let hashIndex = path.indexOf('#');
if (hashIndex >= 0) {
partialPath.hash = path.substr(hashIndex);
path = path.substr(0, hashIndex);
if (path && !path.startsWith('/')) path = '/' + path;
}
let searchIndex = path.indexOf('?');
if (searchIndex >= 0) {
partialPath.search = path.substr(searchIndex);
path = path.substr(0, searchIndex);
if (path && !path.startsWith('/')) path = '/' + path;
}
if (path) {
partialPath.pathname = path;
}
}
return partialPath;
}
function createHref(to, hashType, _window = globalThis) {
let path = typeof to === 'string' ? to : createPath(to);
if (!hashType) hashType = getPossibleHashType(_window, path);
if (hashType != null) {
let pathPrefix = '';
if (path.startsWith('#')) {
pathPrefix = '#';
path = path.substr(1, path.length);
}
let slashChar = path.substr(0, 1);
if (hashType === 'slash') {
if (slashChar !== '/') path = '/' + path;
} else if (hashType === 'noslash') {
if (slashChar === '/') path = path.substr(1);
}
path = pathPrefix + path;
}
return path;
}
function allowTxWithParams(blockers, params) {
let resultPayload;
let count = blockers.length;
let cb = params.callback;
function callback(ok, payload) {
if (!cb) return;
if (!ok) {
cb(ok);
cb = null;
return;
}
if (arguments.length > 1) resultPayload = payload;
if (! --count) {
cb(ok, resultPayload);
cb = null;
}
}
return !blockers.length || (blockers.call(_objectSpread(_objectSpread({}, params), {}, {
callback
})), false);
}
function allowTx(blockers, action, location, index, nextIndex, callback) {
return allowTxWithParams(blockers, {
action,
location,
index,
nextIndex,
callback
});
}
function readonly(obj, key, get, options) {
const _ref = options || {},
{
configurable = true,
enumerable = true
} = _ref,
restOptions = _objectWithoutProperties(_ref, _excluded);
Object.defineProperty(obj, key, _objectSpread({
get,
configurable,
enumerable
}, restOptions));
return obj;
}
const _hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwnProp(obj, key) {
return Boolean(obj) && _hasOwnProperty.call(obj, key);
}
function copyOwnProperty(target, key, source) {
if (!target || !source) return;
const d = Object.getOwnPropertyDescriptor(source, key);
d && Object.defineProperty(target, key, d);
return d;
}
function copyOwnProperties(target, source, overwrite) {
if (!target || !source) {
return target;
}
Object.getOwnPropertyNames(source).forEach(key => {
if (!overwrite && hasOwnProp(target, key)) return;
copyOwnProperty(target, key, source);
});
return target;
}
export { CAN_USE_DOM, freeze, getPossibleHashType, clamp, createEvents, createKey, getBaseHref, createPath, parsePath, createHref, allowTxWithParams, allowTx, readonly, hasOwnProp, copyOwnProperty, copyOwnProperties };