UNPKG

meetup-web-mocks

Version:
98 lines (72 loc) 3.16 kB
'use strict'; exports.__esModule = true; exports.go = exports.replaceLocation = exports.pushLocation = exports.startListener = exports.getUserConfirmation = exports.getCurrentLocation = undefined; var _LocationUtils = require('./LocationUtils'); var _DOMUtils = require('./DOMUtils'); var _DOMStateStorage = require('./DOMStateStorage'); var _PathUtils = require('./PathUtils'); var _ExecutionEnvironment = require('./ExecutionEnvironment'); var PopStateEvent = 'popstate'; var HashChangeEvent = 'hashchange'; var needsHashchangeListener = _ExecutionEnvironment.canUseDOM && !(0, _DOMUtils.supportsPopstateOnHashchange)(); var _createLocation = function _createLocation(historyState) { var key = historyState && historyState.key; return (0, _LocationUtils.createLocation)({ pathname: window.location.pathname, search: window.location.search, hash: window.location.hash, state: key ? (0, _DOMStateStorage.readState)(key) : undefined }, undefined, key); }; var getCurrentLocation = exports.getCurrentLocation = function getCurrentLocation() { var historyState = void 0; try { historyState = window.history.state || {}; } catch (error) { // IE 11 sometimes throws when accessing window.history.state // See https://github.com/ReactTraining/history/pull/289 historyState = {}; } return _createLocation(historyState); }; var getUserConfirmation = exports.getUserConfirmation = function getUserConfirmation(message, callback) { return callback(window.confirm(message)); }; // eslint-disable-line no-alert var startListener = exports.startListener = function startListener(listener) { var handlePopState = function handlePopState(event) { if (event.state !== undefined) // Ignore extraneous popstate events in WebKit listener(_createLocation(event.state)); }; (0, _DOMUtils.addEventListener)(window, PopStateEvent, handlePopState); var handleUnpoppedHashChange = function handleUnpoppedHashChange() { return listener(getCurrentLocation()); }; if (needsHashchangeListener) { (0, _DOMUtils.addEventListener)(window, HashChangeEvent, handleUnpoppedHashChange); } return function () { (0, _DOMUtils.removeEventListener)(window, PopStateEvent, handlePopState); if (needsHashchangeListener) { (0, _DOMUtils.removeEventListener)(window, HashChangeEvent, handleUnpoppedHashChange); } }; }; var updateLocation = function updateLocation(location, updateState) { var state = location.state; var key = location.key; if (state !== undefined) (0, _DOMStateStorage.saveState)(key, state); updateState({ key: key }, (0, _PathUtils.createPath)(location)); }; var pushLocation = exports.pushLocation = function pushLocation(location) { return updateLocation(location, function (state, path) { return window.history.pushState(state, null, path); }); }; var replaceLocation = exports.replaceLocation = function replaceLocation(location) { return updateLocation(location, function (state, path) { return window.history.replaceState(state, null, path); }); }; var go = exports.go = function go(n) { if (n) window.history.go(n); };