react-crossroads
Version:
Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.
74 lines (66 loc) • 1.93 kB
JavaScript
var ExecutionEnvironment, HashLocation, getWindowPath, invariant, _ensureChangeFires, _onChange;
invariant = require('react/lib/invariant');
getWindowPath = require('./getWindowPath');
ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
_onChange = null;
_ensureChangeFires = function(func) {
var original;
original = HashLocation.getCurrentPath();
func();
if (original === HashLocation.getCurrentPath()) {
return _onChange();
}
};
HashLocation = {
setup: function(onChange) {
invariant(ExecutionEnvironment.canUseDOM, 'You cannot use HashLocation in an environment with no DOM');
_onChange = onChange;
if (window.location.hash === '') {
window.location.replace("" + (getWindowPath()) + "#/");
}
if (window.addEventListener) {
return window.addEventListener('hashchange', _onChange, false);
} else {
return window.attachEvent('onhashchange', _onChange);
}
},
teardown: function() {
if (window.removeEventListener) {
return window.removeEventListener('hashchange', _onChange, false);
} else {
return window.detachEvent('onhashchange', _onChange);
}
},
push: function(path) {
return _ensureChangeFires(function() {
return window.location.hash = path;
});
},
replace: function(path) {
return _ensureChangeFires(function() {
return window.location.replace("" + (getWindowPath()) + "#" + path);
});
},
pop: function() {
return _ensureChangeFires(function() {
return window.history.back();
});
},
getCurrentPath: function() {
return window.location.hash.substr(1);
},
isSupportedOrFallback: function() {
if (ExecutionEnvironment.canUseDOM) {
return true;
} else {
return 'memory';
}
},
pathToHref: function(path) {
return "#" + path;
},
toString: function() {
return '<HashLocation>';
}
};
module.exports = HashLocation;