Skip to content

Commit

Permalink
Style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Feb 25, 2015
1 parent 9565ffa commit 0bf536e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 53 deletions.
58 changes: 25 additions & 33 deletions modules/locations/HashLocation.js
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -61,7 +47,7 @@ function onHashChange() {
var HashLocation = {

addChangeListener(listener) {
_changeListeners.push(listener);
_listeners.push(listener);

// Do this BEFORE listening for hashchange.
ensureSlash();
Expand All @@ -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 {
Expand Down Expand Up @@ -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 '<HashLocation>';
Expand Down
32 changes: 13 additions & 19 deletions modules/locations/HistoryLocation.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -38,7 +28,7 @@ function onPopState(event) {
var HistoryLocation = {

addChangeListener(listener) {
_changeListeners.push(listener);
_listeners.push(listener);

if (!_isListening) {
if (window.addEventListener) {
Expand All @@ -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 {
Expand All @@ -80,7 +70,11 @@ var HistoryLocation = {

pop: History.back,

getCurrentPath: getWindowPath,
getCurrentPath() {
return decodeURI(
window.location.pathname + window.location.search
);
},

toString() {
return '<HistoryLocation>';
Expand Down
7 changes: 6 additions & 1 deletion modules/locations/TestLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0bf536e

Please sign in to comment.