UNPKG

cuz

Version:

Front-end modular development kit.

129 lines (95 loc) 4.04 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } var _ExecutionEnvironment = require('./ExecutionEnvironment'); var _runTransitionHook = require('./runTransitionHook'); var _runTransitionHook2 = _interopRequireDefault(_runTransitionHook); var _extractPath = require('./extractPath'); var _extractPath2 = _interopRequireDefault(_extractPath); var _parsePath = require('./parsePath'); var _parsePath2 = _interopRequireDefault(_parsePath); function useBasename(createHistory) { return function () { var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var basename = options.basename; var historyOptions = _objectWithoutProperties(options, ['basename']); var history = createHistory(historyOptions); // Automatically use the value of <base href> in HTML // documents as basename if it's not explicitly given. if (basename == null && _ExecutionEnvironment.canUseDOM) { var base = document.getElementsByTagName('base')[0]; if (base) basename = _extractPath2['default'](base.href); } function addBasename(location) { if (basename && location.basename == null) { if (location.pathname.indexOf(basename) === 0) { location.pathname = location.pathname.substring(basename.length); location.basename = basename; if (location.pathname === '') location.pathname = '/'; } else { location.basename = ''; } } return location; } function prependBasename(path) { if (!basename) return path; if (typeof path === 'string') path = _parsePath2['default'](path); var pname = path.pathname; var normalizedBasename = basename.slice(-1) === '/' ? basename : basename + '/'; var normalizedPathname = pname.charAt(0) === '/' ? pname.slice(1) : pname; var pathname = normalizedBasename + normalizedPathname; return _extends({}, path, { pathname: pathname }); } // Override all read methods with basename-aware versions. function listenBefore(hook) { return history.listenBefore(function (location, callback) { _runTransitionHook2['default'](hook, addBasename(location), callback); }); } function listen(listener) { return history.listen(function (location) { listener(addBasename(location)); }); } // Override all write methods with basename-aware versions. function pushState(state, path) { history.pushState(state, prependBasename(path)); } function push(path) { pushState(null, path); } function replaceState(state, path) { history.replaceState(state, prependBasename(path)); } function replace(path) { replaceState(null, path); } function createPath(path) { return history.createPath(prependBasename(path)); } function createHref(path) { return history.createHref(prependBasename(path)); } function createLocation() { return addBasename(history.createLocation.apply(history, arguments)); } return _extends({}, history, { listenBefore: listenBefore, listen: listen, pushState: pushState, push: push, replaceState: replaceState, replace: replace, createPath: createPath, createHref: createHref, createLocation: createLocation }); }; } exports['default'] = useBasename; module.exports = exports['default'];