diff --git a/modules/locations/HashLocation.js b/modules/locations/HashLocation.js index bf4d57908c..7d67a81b51 100644 --- a/modules/locations/HashLocation.js +++ b/modules/locations/HashLocation.js @@ -1,48 +1,34 @@ var LocationActions = require('../actions/LocationActions'); var History = require('../History'); -/** - * Returns the current URL path from the `hash` portion of the URL, including - * query string. - */ -function getHashPath() { - return decodeURI( - // We can't use window.location.hash here because it's not - // consistent across browsers - Firefox will pre-decode it! - window.location.href.split('#')[1] || '' - ); -} - +var _listeners = []; +var _isListening = false; var _actionType; -function ensureSlash() { - var path = getHashPath(); - - if (path.charAt(0) === '/') - return true; - - HashLocation.replace('/' + path); - - return false; -} - -var _changeListeners = []; - function notifyChange(type) { if (type === LocationActions.PUSH) History.length += 1; var change = { - path: getHashPath(), + path: HashLocation.getCurrentPath(), type: type }; - _changeListeners.forEach(function (listener) { - listener(change); + _listeners.forEach(function (listener) { + listener.call(HashLocation, change); }); } -var _isListening = false; +function ensureSlash() { + var path = HashLocation.getCurrentPath(); + + if (path.charAt(0) === '/') + return true; + + HashLocation.replace('/' + path); + + return false; +} function onHashChange() { if (ensureSlash()) { @@ -61,7 +47,7 @@ function onHashChange() { var HashLocation = { addChangeListener(listener) { - _changeListeners.push(listener); + _listeners.push(listener); // Do this BEFORE listening for hashchange. ensureSlash(); @@ -78,11 +64,11 @@ var HashLocation = { }, removeChangeListener(listener) { - _changeListeners = _changeListeners.filter(function (l) { + _listeners = _listeners.filter(function (l) { return l !== listener; }); - if (_changeListeners.length === 0) { + if (_listeners.length === 0) { if (window.removeEventListener) { window.removeEventListener('hashchange', onHashChange, false); } else { @@ -110,7 +96,13 @@ var HashLocation = { History.back(); }, - getCurrentPath: getHashPath, + getCurrentPath() { + return decodeURI( + // We can't use window.location.hash here because it's not + // consistent across browsers - Firefox will pre-decode it! + window.location.href.split('#')[1] || '' + ); + }, toString() { return ''; diff --git a/modules/locations/HistoryLocation.js b/modules/locations/HistoryLocation.js index b4b1a22b63..d08dfa34ed 100644 --- a/modules/locations/HistoryLocation.js +++ b/modules/locations/HistoryLocation.js @@ -1,30 +1,20 @@ var LocationActions = require('../actions/LocationActions'); var History = require('../History'); -/** - * Returns the current URL path from `window.location`, including query string. - */ -function getWindowPath() { - return decodeURI( - window.location.pathname + window.location.search - ); -} - -var _changeListeners = []; +var _listeners = []; +var _isListening = false; function notifyChange(type) { var change = { - path: getWindowPath(), + path: HistoryLocation.getCurrentPath(), type: type }; - _changeListeners.forEach(function (listener) { - listener(change); + _listeners.forEach(function (listener) { + listener.call(HistoryLocation, change); }); } -var _isListening = false; - function onPopState(event) { if (event.state === undefined) return; // Ignore extraneous popstate events in WebKit. @@ -38,7 +28,7 @@ function onPopState(event) { var HistoryLocation = { addChangeListener(listener) { - _changeListeners.push(listener); + _listeners.push(listener); if (!_isListening) { if (window.addEventListener) { @@ -52,11 +42,11 @@ var HistoryLocation = { }, removeChangeListener(listener) { - _changeListeners = _changeListeners.filter(function (l) { + _listeners = _listeners.filter(function (l) { return l !== listener; }); - if (_changeListeners.length === 0) { + if (_listeners.length === 0) { if (window.addEventListener) { window.removeEventListener('popstate', onPopState, false); } else { @@ -80,7 +70,11 @@ var HistoryLocation = { pop: History.back, - getCurrentPath: getWindowPath, + getCurrentPath() { + return decodeURI( + window.location.pathname + window.location.search + ); + }, toString() { return ''; diff --git a/modules/locations/TestLocation.js b/modules/locations/TestLocation.js index c01bcdee12..76cab8ad65 100644 --- a/modules/locations/TestLocation.js +++ b/modules/locations/TestLocation.js @@ -22,8 +22,13 @@ class TestLocation { } _notifyChange(type) { + var change = { + path: this.getCurrentPath(), + type: type + }; + for (var i = 0, len = this.listeners.length; i < len; ++i) - this.listeners[i].call(this, { path: this.getCurrentPath(), type: type }); + this.listeners[i].call(this, change); } addChangeListener(listener) {