UNPKG

react-redux-express

Version:

React fullstack generator with express,redux, and some components.

136 lines (98 loc) 3.96 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceLocation = exports.pushLocation = exports.startListener = exports.getCurrentLocation = exports.go = exports.getUserConfirmation = undefined; var _BrowserProtocol = require('./BrowserProtocol'); Object.defineProperty(exports, 'getUserConfirmation', { enumerable: true, get: function get() { return _BrowserProtocol.getUserConfirmation; } }); Object.defineProperty(exports, 'go', { enumerable: true, get: function get() { return _BrowserProtocol.go; } }); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _LocationUtils = require('./LocationUtils'); var _DOMUtils = require('./DOMUtils'); var _DOMStateStorage = require('./DOMStateStorage'); var _PathUtils = require('./PathUtils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var HashChangeEvent = 'hashchange'; var getHashPath = function getHashPath() { // We can't use window.location.hash here because it's not // consistent across browsers - Firefox will pre-decode it! var href = window.location.href; var index = href.indexOf('#'); return index === -1 ? '' : href.substring(index + 1); }; var pushHashPath = function pushHashPath(path) { return window.location.hash = path; }; var replaceHashPath = function replaceHashPath(path) { var i = window.location.href.indexOf('#'); window.location.replace(window.location.href.slice(0, i >= 0 ? i : 0) + '#' + path); }; var ensureSlash = function ensureSlash() { var path = getHashPath(); if ((0, _PathUtils.isAbsolutePath)(path)) return true; replaceHashPath('/' + path); return false; }; var getCurrentLocation = exports.getCurrentLocation = function getCurrentLocation(queryKey) { var path = getHashPath(); var key = (0, _PathUtils.getQueryStringValueFromPath)(path, queryKey); var state = void 0; if (key) { path = (0, _PathUtils.stripQueryStringValueFromPath)(path, queryKey); state = (0, _DOMStateStorage.readState)(key); } var init = (0, _PathUtils.parsePath)(path); init.state = state; return (0, _LocationUtils.createLocation)(init, undefined, key); }; var prevLocation = void 0; var startListener = exports.startListener = function startListener(listener, queryKey) { var handleHashChange = function handleHashChange() { if (!ensureSlash()) return; // Hash path must always begin with a / var currentLocation = getCurrentLocation(queryKey); if (prevLocation && currentLocation.key && prevLocation.key === currentLocation.key) return; // Ignore extraneous hashchange events prevLocation = currentLocation; listener(currentLocation); }; ensureSlash(); (0, _DOMUtils.addEventListener)(window, HashChangeEvent, handleHashChange); return function () { return (0, _DOMUtils.removeEventListener)(window, HashChangeEvent, handleHashChange); }; }; var updateLocation = function updateLocation(location, queryKey, updateHash) { var state = location.state; var key = location.key; var path = (0, _PathUtils.createPath)(location); if (state !== undefined) { path = (0, _PathUtils.addQueryStringValueToPath)(path, queryKey, key); (0, _DOMStateStorage.saveState)(key, state); } prevLocation = location; updateHash(path); }; var pushLocation = exports.pushLocation = function pushLocation(location, queryKey) { return updateLocation(location, queryKey, function (path) { if (getHashPath() !== path) { pushHashPath(path); } else { process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'You cannot PUSH the same path using hash history') : void 0; } }); }; var replaceLocation = exports.replaceLocation = function replaceLocation(location, queryKey) { return updateLocation(location, queryKey, function (path) { if (getHashPath() !== path) replaceHashPath(path); }); };