-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve query parameters when rebasing location path #67
Open
braxnu
wants to merge
7
commits into
zambezi:master
Choose a base branch
from
braxnu:preserve-query-params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf6242d
Preserve query parameters when rebasing location path
braxnu 0470195
convert location module into factory to allow testing
braxnu f65a135
test query params preserving when "upgrading" from hash path
braxnu 324cef0
make sure there is only one instance of location module
braxnu 1456402
revert the changes to be minimally intrusive
braxnu 5ca13b2
allow force reinstantiation for testing
braxnu 540b06e
preserve consolidated query params only during module load
braxnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,197 +1,200 @@ | ||
define(function(require) { | ||
var findClosest = require('./find-closest') | ||
, rebind = require('./rebind') | ||
, dispatcher = require('d3-dispatch').dispatch('statechange') | ||
, history = window.history | ||
, location = window.location | ||
, on = require('./on') | ||
, base = '' | ||
|
||
on.call(window, 'popstate.location', handleStateChange) | ||
on.call(document, 'click.location', handleClick) | ||
|
||
if (isHashPath(location.hash)) { | ||
// Redirect current hash fragment location to "real" path | ||
history.replaceState(null, null, rebase(fullPath(location))) | ||
} | ||
|
||
var api = | ||
{ getState: getState | ||
, setState: setState | ||
, pushState: deprecatedPushState | ||
, replaceState: replaceState | ||
, openNewWindow: openNewWindow | ||
, basePath: basePath | ||
} | ||
var rebind = require('./rebind') | ||
var dispatcher = require('d3-dispatch').dispatch('statechange') | ||
var on = require('./on') | ||
|
||
return rebind(api, dispatcher, 'on') | ||
return function () { | ||
var history = window.history | ||
var location = window.location | ||
var base = '' | ||
|
||
function getState() { | ||
return unbase(fullPath(location)) | ||
} | ||
on.call(window, 'popstate.location', handleStateChange) | ||
on.call(document, 'click.location', handleClick) | ||
|
||
function setState(path) { | ||
var actual = pushState(path) | ||
|
||
if (actual) { | ||
dispatcher.statechange(actual) | ||
return actual | ||
} else { | ||
return false | ||
if (isHashPath(location.hash)) { | ||
// Redirect current hash fragment location to "real" path | ||
history.replaceState(null, null, rebase(fullPath(location)) + location.search) | ||
} | ||
} | ||
|
||
function trimPath(path) { | ||
return '/' + trimSlashes(~path.indexOf('#/')? path.split('#/')[1] : path) | ||
} | ||
var api = | ||
{ getState: getState | ||
, setState: setState | ||
, pushState: deprecatedPushState | ||
, replaceState: replaceState | ||
, openNewWindow: openNewWindow | ||
, basePath: basePath | ||
} | ||
|
||
function updateState(path, method) { | ||
path = unbase(trimPath(path)) | ||
return rebind(api, dispatcher, 'on') | ||
|
||
if (path === getState()) { | ||
return false | ||
} else { | ||
method({ base: base, path: path }, null, rebase(path)) | ||
return path | ||
function getState() { | ||
return unbase(fullPath(location)) | ||
} | ||
} | ||
|
||
function deprecatedPushState(path) { | ||
console.warn('deprecated : location.pushState, to be removed in v.4.0.0.') | ||
return pushState(path) | ||
} | ||
function setState(path) { | ||
var actual = pushState(path) | ||
|
||
function pushState(path) { | ||
return updateState(path, history.pushState.bind(history)) | ||
} | ||
if (actual) { | ||
dispatcher.statechange(actual) | ||
return actual | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
function replaceState(path) { | ||
return updateState(path, history.replaceState.bind(history)) | ||
} | ||
function trimPath(path) { | ||
return '/' + trimSlashes(~path.indexOf('#/')? path.split('#/')[1] : path) | ||
} | ||
|
||
function openNewWindow(path, target) { | ||
return window.open(rebase(path), target, '') | ||
} | ||
function updateState(path, method) { | ||
path = unbase(trimPath(path)) | ||
|
||
function basePath(path) { | ||
if (arguments.length === 0) return base | ||
if (path === getState()) { | ||
return false | ||
} else { | ||
method({ base: base, path: path }, null, rebase(path) + location.search) | ||
return path | ||
} | ||
} | ||
|
||
var cwd = unbase(fullPath(location)) | ||
function deprecatedPushState(path) { | ||
console.warn('deprecated : location.pushState, to be removed in v.4.0.0.') | ||
return pushState(path) | ||
} | ||
|
||
path = trimSlashes(path) | ||
base = path? '/' + path : '' | ||
function pushState(path) { | ||
return updateState(path, history.pushState.bind(history)) | ||
} | ||
|
||
history.replaceState(null, null, rebase(cwd)) | ||
} | ||
function replaceState(path) { | ||
return updateState(path, history.replaceState.bind(history)) | ||
} | ||
|
||
function handleClick(event) { | ||
var a | ||
, target = event.target | ||
, path | ||
function openNewWindow(path, target) { | ||
return window.open(rebase(path), target, '') | ||
} | ||
|
||
if (event.ctrlKey) return // Ignore ctrl+click | ||
if (event.button !== 0) return // Ignore clicks by buttons other than primary (usually left button) | ||
function basePath(path) { | ||
if (arguments.length === 0) return base | ||
|
||
a = findClosest.anchor(target) | ||
var cwd = unbase(fullPath(location)) | ||
|
||
if ( !a // non-anchor clicks | ||
|| !!a.target // anchors with specified targets | ||
|| a.hasAttribute('download') // anchors with download attribute | ||
|| !isSameOrigin(a, location) // links to different origins | ||
) { | ||
/* If any of the above conditions are true, we ignore the click and | ||
* let the browser deal with the navigation as it sees fit | ||
*/ | ||
return | ||
path = trimSlashes(path) | ||
base = path? '/' + path : '' | ||
|
||
history.replaceState(null, null, rebase(cwd)) | ||
} | ||
|
||
var path | ||
function handleClick(event) { | ||
var a | ||
, target = event.target | ||
, path | ||
|
||
if (event.ctrlKey) return // Ignore ctrl+click | ||
if (event.button !== 0) return // Ignore clicks by buttons other than primary (usually left button) | ||
|
||
a = findClosest.anchor(target) | ||
|
||
if ( !a // non-anchor clicks | ||
|| !!a.target // anchors with specified targets | ||
|| a.hasAttribute('download') // anchors with download attribute | ||
|| !isSameOrigin(a, location) // links to different origins | ||
) { | ||
/* If any of the above conditions are true, we ignore the click and | ||
* let the browser deal with the navigation as it sees fit | ||
*/ | ||
return | ||
} | ||
|
||
if (isHashPath(a.hash)) { | ||
path = rebase(a.hash.slice(1)) | ||
} else if (a.hash || a.href.slice(location.href.length) === '#') { | ||
// Ignore links with a non-path hash, and empty hashes (e.g.: `<a href="#"></a>`) | ||
return | ||
} else { | ||
path = rebase(fullPath(a)) | ||
} | ||
var path | ||
|
||
if (path) { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
var actual = pushState(path) | ||
if (isHashPath(a.hash)) { | ||
path = rebase(a.hash.slice(1)) | ||
} else if (a.hash || a.href.slice(location.href.length) === '#') { | ||
// Ignore links with a non-path hash, and empty hashes (e.g.: `<a href="#"></a>`) | ||
return | ||
} else { | ||
path = rebase(fullPath(a)) | ||
} | ||
|
||
if (actual) { | ||
dispatcher.statechange(actual) | ||
if (path) { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
var actual = pushState(path) | ||
|
||
if (actual) { | ||
dispatcher.statechange(actual) | ||
} | ||
} | ||
} | ||
} | ||
|
||
function handleStateChange(event) { | ||
var path, base = (event.state && event.state.base) || '' | ||
function handleStateChange(event) { | ||
var path, base = (event.state && event.state.base) || '' | ||
|
||
if (isHashPath(location.hash)) { | ||
// "Redirect" current location to a proper path | ||
path = location.hash.slice(1) | ||
if (isHashPath(location.hash)) { | ||
// "Redirect" current location to a proper path | ||
path = location.hash.slice(1) | ||
|
||
if (path) { | ||
var state = { base: base, path: path } | ||
history.replaceState(state, null, rebase(path)) | ||
if (path) { | ||
var state = { base: base, path: path } | ||
history.replaceState(state, null, rebase(path)) | ||
} | ||
} else { | ||
path = fullPath(location) | ||
} | ||
} else { | ||
path = fullPath(location) | ||
|
||
dispatcher.statechange(unbase(path)) | ||
} | ||
|
||
dispatcher.statechange(unbase(path)) | ||
} | ||
function isHashPath(hash) { | ||
return (hash || '').slice(0, 2) === '#/' | ||
} | ||
|
||
function isHashPath(hash) { | ||
return (hash || '').slice(0, 2) === '#/' | ||
} | ||
function isSameOrigin(a, x) { | ||
var o = origin(x) | ||
return a.href.slice(0, o.length) === o | ||
} | ||
|
||
function isSameOrigin(a, x) { | ||
var o = origin(x) | ||
return a.href.slice(0, o.length) === o | ||
} | ||
function origin(url) { | ||
if (url.origin) { | ||
return url.origin | ||
} else { | ||
var port | ||
|
||
function origin(url) { | ||
if (url.origin) { | ||
return url.origin | ||
} else { | ||
var port | ||
if (url.port && !~url.href.indexOf(':' + url.port)) { | ||
// IE defaults port values based on protocol, which messes things up | ||
port = '' | ||
} else { | ||
port = ':' + url.port | ||
} | ||
|
||
if (url.port && !~url.href.indexOf(':' + url.port)) { | ||
// IE defaults port values based on protocol, which messes things up | ||
port = '' | ||
} else { | ||
port = ':' + url.port | ||
return url.protocol + "//" + url.hostname + port | ||
} | ||
|
||
return url.protocol + "//" + url.hostname + port | ||
} | ||
} | ||
|
||
function fullPath(url) { | ||
if (isHashPath(url.hash)) { | ||
return url.hash.slice(1) | ||
} else { | ||
return url.href.slice(origin(url).length) | ||
function fullPath(url) { | ||
if (isHashPath(url.hash)) { | ||
return url.hash.slice(1) | ||
} else { | ||
return url.href.slice(origin(url).length) | ||
} | ||
} | ||
} | ||
|
||
function rebase(path) { | ||
return base + '/' + trimSlashes(unbase(path)) | ||
} | ||
function rebase(path) { | ||
return base + '/' + trimSlashes(unbase(path)) | ||
} | ||
|
||
function unbase(path) { | ||
if (path.slice(0, base.length) === base) { | ||
return path.slice(base.length) | ||
} else { | ||
return path | ||
function unbase(path) { | ||
if (path.slice(0, base.length) === base) { | ||
return path.slice(base.length) | ||
} else { | ||
return path | ||
} | ||
} | ||
} | ||
|
||
function trimSlashes(path) { | ||
return (path || '').replace(/^\/+|\/+$/g, '') | ||
function trimSlashes(path) { | ||
return (path || '').replace(/^\/+|\/+$/g, '') | ||
} | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The location module is effectively a singleton prior to these changes. I can understand how this makes testing more difficult, however it seems to me that by changing this to be an instantiated module with local state, this also breaks the API. Anything now depending on
address.location
can no longer rely on the state of this module to be the same as what's used byaddress
internally. At least, that's how I read this – it's a bit difficult to follow the changes.