history-extra
Advanced tools
Comparing version
### Changelog | ||
#### [v4.0.4](https://github.com/w33ble/history-extra/compare/v4.0.3...v4.0.4) (3 June 2019) | ||
### [v5.0.0](https://github.com/w33ble/history-extra/compare/v4.0.2...v5.0.0) (3 June 2019) | ||
- fix: duplicate listeners [`cf7ca45`](https://github.com/w33ble/history-extra/commit/cf7ca45988e4f8069b405c8cff538e6fe55e79f2) | ||
#### [v4.0.3](https://github.com/w33ble/history-extra/compare/v4.0.2...v4.0.3) (3 June 2019) | ||
- fix: duplicate listeners [`d27402a`](https://github.com/w33ble/history-extra/commit/d27402aad94fd57fa34ef50d5ca1a1994af8a111) | ||
#### [v4.0.2](https://github.com/w33ble/history-extra/compare/v4.0.1...v4.0.2) (16 November 2018) | ||
@@ -9,0 +7,0 @@ - Fix: Allow push/replace on same path [`#1`](https://github.com/w33ble/history-extra/pull/1) |
/* | ||
* history-extra version 4.0.4 | ||
* history-extra version 5.0.0 | ||
* | ||
@@ -28,30 +28,7 @@ * The MIT License (MIT) | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('history/LocationUtils'), require('history/PathUtils'), require('history/createTransitionManager'), require('history/DOMUtils')) : | ||
typeof define === 'function' && define.amd ? define(['history/LocationUtils', 'history/PathUtils', 'history/createTransitionManager', 'history/DOMUtils'], factory) : | ||
(global.history = factory(global.LocationUtils,global.PathUtils,global.createTransitionManager,global.DOMUtils)); | ||
}(this, (function (locationUtils,PathUtils,_createTransitionManager,DOMUtils) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('history')) : | ||
typeof define === 'function' && define.amd ? define(['history'], factory) : | ||
(global.history = factory(global.history)); | ||
}(this, (function (history) { 'use strict'; | ||
locationUtils = locationUtils && locationUtils.hasOwnProperty('default') ? locationUtils['default'] : locationUtils; | ||
PathUtils = PathUtils && PathUtils.hasOwnProperty('default') ? PathUtils['default'] : PathUtils; | ||
_createTransitionManager = _createTransitionManager && _createTransitionManager.hasOwnProperty('default') ? _createTransitionManager['default'] : _createTransitionManager; | ||
DOMUtils = DOMUtils && DOMUtils.hasOwnProperty('default') ? DOMUtils['default'] : DOMUtils; | ||
/* eslint no-use-before-define: 0 */ | ||
var createLocation = locationUtils.createLocation, | ||
locationsAreEqual = locationUtils.locationsAreEqual; | ||
var addLeadingSlash = PathUtils.addLeadingSlash, | ||
stripLeadingSlash = PathUtils.stripLeadingSlash, | ||
stripTrailingSlash = PathUtils.stripTrailingSlash, | ||
hasBasename = PathUtils.hasBasename, | ||
stripBasename = PathUtils.stripBasename, | ||
createPath = PathUtils.createPath; | ||
var canUseDOM = DOMUtils.canUseDOM, | ||
addEventListener = DOMUtils.addEventListener, | ||
removeEventListener = DOMUtils.removeEventListener, | ||
getConfirmation = DOMUtils.getConfirmation, | ||
supportsGoWithoutReloadUsingHash = DOMUtils.supportsGoWithoutReloadUsingHash, | ||
supportsHistory = DOMUtils.supportsHistory; // goofy hack to handle cjs and esm differences in build | ||
var createTransitionManager = Object.hasOwnProperty.call(_createTransitionManager, 'default') ? _createTransitionManager.default : _createTransitionManager; | ||
function warning(condition, message) { | ||
@@ -62,3 +39,2 @@ if (condition) return; // eslint-disable-next-line no-console | ||
} | ||
function invariant(condition, message) { | ||
@@ -69,2 +45,123 @@ if (condition) return; | ||
function createTransitionManager() { | ||
var prompt = null; | ||
function setPrompt(nextPrompt) { | ||
warning(prompt == null, 'A history supports only one prompt at a time'); | ||
prompt = nextPrompt; | ||
return function () { | ||
if (prompt === nextPrompt) prompt = null; | ||
}; | ||
} | ||
function confirmTransitionTo(location, action, getUserConfirmation, callback) { | ||
// TODO: If another transition starts while we're still confirming | ||
// the previous one, we may end up in a weird state. Figure out the | ||
// best way to handle this. | ||
if (prompt != null) { | ||
var result = typeof prompt === 'function' ? prompt(location, action) : prompt; | ||
if (typeof result === 'string') { | ||
if (typeof getUserConfirmation === 'function') { | ||
getUserConfirmation(result, callback); | ||
} else { | ||
warning(false, 'A history needs a getUserConfirmation function in order to use a prompt message'); | ||
callback(true); | ||
} | ||
} else { | ||
// Return false from a transition hook to cancel the transition. | ||
callback(result !== false); | ||
} | ||
} else { | ||
callback(true); | ||
} | ||
} | ||
var listeners = []; | ||
function appendListener(fn) { | ||
var isActive = true; | ||
function listener() { | ||
if (isActive) fn.apply(void 0, arguments); | ||
} | ||
listeners.push(listener); | ||
return function () { | ||
isActive = false; | ||
listeners = listeners.filter(function (item) { | ||
return item !== listener; | ||
}); | ||
}; | ||
} | ||
function notifyListeners() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
listeners.forEach(function (listener) { | ||
return listener.apply(void 0, args); | ||
}); | ||
} | ||
return { | ||
setPrompt: setPrompt, | ||
confirmTransitionTo: confirmTransitionTo, | ||
appendListener: appendListener, | ||
notifyListeners: notifyListeners | ||
}; | ||
} | ||
var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); | ||
function getConfirmation(message, callback) { | ||
callback(window.confirm(message)); // eslint-disable-line no-alert | ||
} | ||
/** | ||
* Returns true if the HTML5 history API is supported. Taken from Modernizr. | ||
* | ||
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE | ||
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js | ||
* changed to avoid false negatives for Windows Phones: https://github.com/reactjs/react-router/issues/586 | ||
*/ | ||
function supportsHistory() { | ||
var ua = window.navigator.userAgent; | ||
if ((ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) && ua.indexOf('Mobile Safari') !== -1 && ua.indexOf('Chrome') === -1 && ua.indexOf('Windows Phone') === -1) return false; | ||
return window.history && 'pushState' in window.history; | ||
} | ||
/** | ||
* Returns false if using go(n) with hash history causes a full page reload. | ||
*/ | ||
function supportsGoWithoutReloadUsingHash() { | ||
return window.navigator.userAgent.indexOf('Firefox') === -1; | ||
} | ||
function addLeadingSlash(path) { | ||
return path.charAt(0) === '/' ? path : "/".concat(path); | ||
} | ||
function stripLeadingSlash(path) { | ||
return path.charAt(0) === '/' ? path.substr(1) : path; | ||
} | ||
function hasBasename(path, prefix) { | ||
return path.toLowerCase().indexOf(prefix.toLowerCase()) === 0 && '/?#'.indexOf(path.charAt(prefix.length)) !== -1; | ||
} | ||
function stripBasename(path, prefix) { | ||
return hasBasename(path, prefix) ? path.substr(prefix.length) : path; | ||
} | ||
function stripTrailingSlash(path) { | ||
return path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path; | ||
} | ||
function createPath(location) { | ||
var pathname = location.pathname, | ||
search = location.search, | ||
hash = location.hash; | ||
var path = pathname || '/'; | ||
if (search && search !== '?') path += search.charAt(0) === '?' ? search : "?".concat(search); | ||
if (hash && hash !== '#') path += hash.charAt(0) === '#' ? hash : "#".concat(hash); | ||
return path; | ||
} | ||
/* eslint no-use-before-define: 0 */ | ||
var PopStateEvent = 'popstate'; | ||
@@ -144,3 +241,3 @@ var HashChangeEvent = 'hashchange'; | ||
if (basename) path = stripBasename(path, basename); | ||
return createLocation(path, state, key); | ||
return history.createLocation(path, state, key); | ||
}; | ||
@@ -155,5 +252,5 @@ | ||
var setState = function setState(nextState) { | ||
Object.assign(history, nextState); | ||
history.length = globalHistory.length; | ||
transitionManager.notifyListeners(history.location, history.action); | ||
Object.assign(history$$1, nextState); | ||
history$$1.length = globalHistory.length; | ||
transitionManager.notifyListeners(history$$1.location, history$$1.action); | ||
}; | ||
@@ -173,4 +270,4 @@ | ||
var location = getDOMLocation(); | ||
var prevLocation = history.location; | ||
if (!forceNextPop && locationsAreEqual(prevLocation, location)) return; // A hashchange doesn't always == location change. | ||
var prevLocation = history$$1.location; | ||
if (!forceNextPop && history.locationsAreEqual(prevLocation, location)) return; // A hashchange doesn't always == location change. | ||
@@ -204,3 +301,3 @@ if (ignorePath === createPath(location)) return; // Ignore this change; we already setState in push/replace. | ||
var revertPop = function revertPop(fromLocation) { | ||
var toLocation = history.location; // TODO: We could probably make this more reliable by | ||
var toLocation = history$$1.location; // TODO: We could probably make this more reliable by | ||
// keeping a list of paths we've seen in sessionStorage. | ||
@@ -235,3 +332,3 @@ // Instead, we just default to 0 for paths we don't know. | ||
var action = 'PUSH'; | ||
var location = createLocation(path, state, createKey(), history.location); | ||
var location = history.createLocation(path, state, createKey(), history$$1.location); | ||
transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) { | ||
@@ -271,3 +368,3 @@ if (!ok) return; // eslint-disable-next-line no-shadow | ||
var prevIndex = allPaths.lastIndexOf(createPath(history.location)); | ||
var prevIndex = allPaths.lastIndexOf(createPath(history$$1.location)); | ||
var nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1); | ||
@@ -286,3 +383,3 @@ nextPaths.push(path); | ||
var action = 'REPLACE'; | ||
var location = createLocation(path, state, createKey(), history.location); | ||
var location = history.createLocation(path, state, createKey(), history$$1.location); | ||
transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) { | ||
@@ -318,3 +415,3 @@ if (!ok) return; // eslint-disable-next-line no-shadow | ||
var prevIndex = allPaths.indexOf(createPath(history.location)); | ||
var prevIndex = allPaths.indexOf(createPath(history$$1.location)); | ||
if (prevIndex !== -1) allPaths[prevIndex] = path; | ||
@@ -348,5 +445,5 @@ setState({ | ||
if (listenerCount === 1 && delta === 1) { | ||
addEventListener(window, eventType, handleHashChange); | ||
window.addEventListener(eventType, handleHashChange); | ||
} else if (listenerCount === 0) { | ||
removeEventListener(window, eventType, handleHashChange); | ||
window.removeEventListener(eventType, handleHashChange); | ||
} | ||
@@ -385,3 +482,3 @@ }; | ||
var history = { | ||
var history$$1 = { | ||
length: globalHistory.length, | ||
@@ -399,3 +496,3 @@ action: 'POP', | ||
}; | ||
return history; | ||
return history$$1; | ||
}; | ||
@@ -402,0 +499,0 @@ |
{ | ||
"name": "history-extra", | ||
"version": "4.0.4", | ||
"version": "5.0.0", | ||
"description": "Extra functionality for the history module", | ||
@@ -5,0 +5,0 @@ "main": "dist/createHashStateHistory.js", |
@@ -21,5 +21,8 @@ # history-extra | ||
The major version of `history-extra` should be compatible with the upstream `history` module with the same major version. | ||
The version of the `history` module you're using will determine which version of `history-extra` you should use. See the table below to find the version you need. | ||
So far, it's only been tested with `4.7.0` and newer. | ||
`history` | `history-extra` | ||
--- | --- | ||
4.7.x | 4.x | ||
4.8.0+ | 5.x | ||
@@ -34,3 +37,3 @@ ## Methods | ||
Works the same way that [createHashHistory in history](https://github.com/ReactTraining/history/blob/master/README.md#usage) works, except that it supports state. | ||
Works the same way that [createHashHistory in history](https://github.com/ReactTraining/history/blob/master/README.md#usage) works, except that it supports browser history and pushState. | ||
@@ -37,0 +40,0 @@ ## Development |
/* eslint no-use-before-define: 0 */ | ||
import locationUtils from 'history/LocationUtils'; | ||
import PathUtils from 'history/PathUtils'; | ||
import _createTransitionManager from 'history/createTransitionManager'; | ||
import DOMUtils from 'history/DOMUtils'; | ||
const { createLocation, locationsAreEqual } = locationUtils; | ||
const { | ||
import { createLocation, locationsAreEqual } from 'history'; | ||
import { warning, invariant } from './utils/helpers'; | ||
import createTransitionManager from './utils/createTransitionManager'; | ||
import { | ||
canUseDOM, | ||
getConfirmation, | ||
supportsGoWithoutReloadUsingHash, | ||
supportsHistory, | ||
} from './utils/dom'; | ||
import { | ||
addLeadingSlash, | ||
@@ -15,28 +18,4 @@ stripLeadingSlash, | ||
createPath, | ||
} = PathUtils; | ||
const { | ||
canUseDOM, | ||
addEventListener, | ||
removeEventListener, | ||
getConfirmation, | ||
supportsGoWithoutReloadUsingHash, | ||
supportsHistory, | ||
} = DOMUtils; | ||
} from './utils/path'; | ||
// goofy hack to handle cjs and esm differences in build | ||
const createTransitionManager = Object.hasOwnProperty.call(_createTransitionManager, 'default') | ||
? _createTransitionManager.default | ||
: _createTransitionManager; | ||
function warning(condition, message) { | ||
if (condition) return; | ||
// eslint-disable-next-line no-console | ||
console.warn(message); | ||
} | ||
function invariant(condition, message) { | ||
if (condition) return; | ||
throw new Error(`Invariant failed: ${message || ''}`); | ||
} | ||
const PopStateEvent = 'popstate'; | ||
@@ -326,5 +305,5 @@ const HashChangeEvent = 'hashchange'; | ||
if (listenerCount === 1 && delta === 1) { | ||
addEventListener(window, eventType, handleHashChange); | ||
window.addEventListener(eventType, handleHashChange); | ||
} else if (listenerCount === 0) { | ||
removeEventListener(window, eventType, handleHashChange); | ||
window.removeEventListener(eventType, handleHashChange); | ||
} | ||
@@ -331,0 +310,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112663
25.02%14
40%1228
29.4%69
4.55%