UNPKG

meetup-web-mocks

Version:
147 lines (101 loc) 4.76 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; }; var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _ExecutionEnvironment = require('./ExecutionEnvironment'); var _DOMUtils = require('./DOMUtils'); var _HashProtocol = require('./HashProtocol'); var HashProtocol = _interopRequireWildcard(_HashProtocol); var _createHistory = require('./createHistory'); var _createHistory2 = _interopRequireDefault(_createHistory); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var DefaultQueryKey = '_k'; var addLeadingSlash = function addLeadingSlash(path) { return path.charAt(0) === '/' ? path : '/' + path; }; var HashPathCoders = { hashbang: { encodePath: function encodePath(path) { return path.charAt(0) === '!' ? path : '!' + path; }, decodePath: function decodePath(path) { return path.charAt(0) === '!' ? path.substring(1) : path; } }, noslash: { encodePath: function encodePath(path) { return path.charAt(0) === '/' ? path.substring(1) : path; }, decodePath: addLeadingSlash }, slash: { encodePath: addLeadingSlash, decodePath: addLeadingSlash } }; var createHashHistory = function createHashHistory() { var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; !_ExecutionEnvironment.canUseDOM ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'Hash history needs a DOM') : (0, _invariant2.default)(false) : void 0; var queryKey = options.queryKey; var hashType = options.hashType; process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(queryKey !== false, 'Using { queryKey: false } no longer works. Instead, just don\'t ' + 'use location state if you don\'t want a key in your URL query string') : void 0; if (typeof queryKey !== 'string') queryKey = DefaultQueryKey; if (hashType == null) hashType = 'slash'; if (!(hashType in HashPathCoders)) { process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'Invalid hash type: %s', hashType) : void 0; hashType = 'slash'; } var pathCoder = HashPathCoders[hashType]; var getUserConfirmation = HashProtocol.getUserConfirmation; var getCurrentLocation = function getCurrentLocation() { return HashProtocol.getCurrentLocation(pathCoder, queryKey); }; var pushLocation = function pushLocation(location) { return HashProtocol.pushLocation(location, pathCoder, queryKey); }; var replaceLocation = function replaceLocation(location) { return HashProtocol.replaceLocation(location, pathCoder, queryKey); }; var history = (0, _createHistory2.default)(_extends({ getUserConfirmation: getUserConfirmation }, options, { getCurrentLocation: getCurrentLocation, pushLocation: pushLocation, replaceLocation: replaceLocation, go: HashProtocol.go })); var listenerCount = 0, stopListener = void 0; var startListener = function startListener(listener, before) { if (++listenerCount === 1) stopListener = HashProtocol.startListener(history.transitionTo, pathCoder, queryKey); var unlisten = before ? history.listenBefore(listener) : history.listen(listener); return function () { unlisten(); if (--listenerCount === 0) stopListener(); }; }; var listenBefore = function listenBefore(listener) { return startListener(listener, true); }; var listen = function listen(listener) { return startListener(listener, false); }; var goIsSupportedWithoutReload = (0, _DOMUtils.supportsGoWithoutReloadUsingHash)(); var go = function go(n) { process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(goIsSupportedWithoutReload, 'Hash history go(n) causes a full page reload in this browser') : void 0; history.go(n); }; var createHref = function createHref(path) { return '#' + pathCoder.encodePath(history.createHref(path)); }; return _extends({}, history, { listenBefore: listenBefore, listen: listen, go: go, createHref: createHref }); }; exports.default = createHashHistory;