forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[added] pluggable history implementations
closes remix-run#166
- Loading branch information
1 parent
f51a7c6
commit 0e7a182
Showing
11 changed files
with
318 additions
and
140 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./modules/helpers/Location'); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var getWindowPath = require('./getWindowPath'); | ||
|
||
/** | ||
* Location handler that doesn't actually do any location handling. Instead, requests | ||
* are sent to the server as normal. | ||
*/ | ||
var DisabledLocation = { | ||
|
||
type: 'disabled', | ||
|
||
init: function() { }, | ||
|
||
destroy: function() { }, | ||
|
||
getCurrentPath: function() { | ||
return getWindowPath(); | ||
}, | ||
|
||
push: function(path) { | ||
window.location = path; | ||
}, | ||
|
||
replace: function(path) { | ||
window.location.replace(path); | ||
}, | ||
|
||
back: function() { | ||
window.history.back(); | ||
} | ||
|
||
}; | ||
|
||
module.exports = DisabledLocation; | ||
|
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var getWindowPath = require('./getWindowPath'); | ||
|
||
function getWindowChangeEvent() { | ||
return window.addEventListener ? 'hashchange' : 'onhashchange'; | ||
} | ||
|
||
/** | ||
* Location handler which uses the `window.location.hash` to push and replace URLs | ||
*/ | ||
var HashLocation = { | ||
|
||
type: 'hash', | ||
onChange: null, | ||
|
||
init: function(onChange) { | ||
var changeEvent = getWindowChangeEvent(); | ||
|
||
if (window.location.hash === '') { | ||
this.replace('/'); | ||
} | ||
|
||
if (window.addEventListener) { | ||
window.addEventListener(changeEvent, onChange, false); | ||
} else { | ||
window.attachEvent(changeEvent, onChange); | ||
} | ||
|
||
this.onChange = onChange; | ||
onChange(); | ||
}, | ||
|
||
destroy: function() { | ||
var changeEvent = getWindowChangeEvent(); | ||
if (window.removeEventListener) { | ||
window.removeEventListener(changeEvent, this.onChange, false); | ||
} else { | ||
window.detachEvent(changeEvent, this.onChange); | ||
} | ||
}, | ||
|
||
getCurrentPath: function() { | ||
return window.location.hash.substr(1); | ||
}, | ||
|
||
push: function(path) { | ||
window.location.hash = path; | ||
}, | ||
|
||
replace: function(path) { | ||
window.location.replace(getWindowPath() + '#' + path); | ||
}, | ||
|
||
back: function() { | ||
window.history.back(); | ||
} | ||
|
||
}; | ||
|
||
module.exports = HashLocation; | ||
|
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var getWindowPath = require('./getWindowPath'); | ||
|
||
/** | ||
* Location handler which uses the HTML5 History API to push and replace URLs | ||
*/ | ||
var HistoryLocation = { | ||
|
||
type: 'history', | ||
onChange: null, | ||
|
||
init: function(onChange) { | ||
if (window.addEventListener) { | ||
window.addEventListener('popstate', onChange, false); | ||
} else { | ||
window.attachEvent('popstate', onChange); | ||
} | ||
this.onChange = onChange; | ||
onChange(); | ||
}, | ||
|
||
destroy: function() { | ||
if (window.removeEventListener) { | ||
window.removeEventListener('popstate', this.onChange, false); | ||
} else { | ||
window.detachEvent('popstate', this.onChange); | ||
} | ||
}, | ||
|
||
getCurrentPath: function() { | ||
return getWindowPath(); | ||
}, | ||
|
||
push: function(path) { | ||
window.history.pushState({ path: path }, '', path); | ||
this.onChange(); | ||
}, | ||
|
||
replace: function(path) { | ||
window.history.replaceState({ path: path }, '', path); | ||
this.onChange(); | ||
}, | ||
|
||
back: function() { | ||
window.history.back(); | ||
} | ||
|
||
}; | ||
|
||
module.exports = HistoryLocation; | ||
|
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Map of location type to handler. | ||
* @see Routes#location | ||
*/ | ||
module.exports = { | ||
hash: require('./HashLocation'), | ||
history: require('./HistoryLocation'), | ||
disabled: require('./DisabledLocation'), | ||
memory: require('./MemoryLocation') | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var invariant = require('react/lib/invariant'); | ||
|
||
var _lastPath; | ||
var _currentPath = '/'; | ||
|
||
/** | ||
* Fake location handler that can be used outside the scope of the browser. It | ||
* tracks the current and previous path, as given to #push() and #replace(). | ||
*/ | ||
var MemoryLocation = { | ||
|
||
type: 'memory', | ||
onChange: null, | ||
|
||
init: function(onChange) { | ||
this.onChange = onChange; | ||
}, | ||
|
||
destroy: function() { | ||
this.onChange = null; | ||
_lastPath = null; | ||
_currentPath = '/'; | ||
}, | ||
|
||
getCurrentPath: function() { | ||
return _currentPath; | ||
}, | ||
|
||
push: function(path) { | ||
_lastPath = _currentPath; | ||
_currentPath = path; | ||
this.onChange(); | ||
}, | ||
|
||
replace: function(path) { | ||
_currentPath = path; | ||
this.onChange(); | ||
}, | ||
|
||
back: function() { | ||
invariant( | ||
_lastPath, | ||
'You cannot make the URL store go back more than once when it does not use the DOM' | ||
); | ||
|
||
_currentPath = _lastPath; | ||
_lastPath = null; | ||
this.onChange(); | ||
} | ||
}; | ||
|
||
module.exports = MemoryLocation; | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns the current URL path from `window.location`, including query string | ||
*/ | ||
function getWindowPath() { | ||
return window.location.pathname + window.location.search; | ||
} | ||
|
||
module.exports = getWindowPath; | ||
|
Oops, something went wrong.