Skip to content

Commit 0bf536e

Browse files
committed
Style tweaks
1 parent 9565ffa commit 0bf536e

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

modules/locations/HashLocation.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
11
var LocationActions = require('../actions/LocationActions');
22
var History = require('../History');
33

4-
/**
5-
* Returns the current URL path from the `hash` portion of the URL, including
6-
* query string.
7-
*/
8-
function getHashPath() {
9-
return decodeURI(
10-
// We can't use window.location.hash here because it's not
11-
// consistent across browsers - Firefox will pre-decode it!
12-
window.location.href.split('#')[1] || ''
13-
);
14-
}
15-
4+
var _listeners = [];
5+
var _isListening = false;
166
var _actionType;
177

18-
function ensureSlash() {
19-
var path = getHashPath();
20-
21-
if (path.charAt(0) === '/')
22-
return true;
23-
24-
HashLocation.replace('/' + path);
25-
26-
return false;
27-
}
28-
29-
var _changeListeners = [];
30-
318
function notifyChange(type) {
329
if (type === LocationActions.PUSH)
3310
History.length += 1;
3411

3512
var change = {
36-
path: getHashPath(),
13+
path: HashLocation.getCurrentPath(),
3714
type: type
3815
};
3916

40-
_changeListeners.forEach(function (listener) {
41-
listener(change);
17+
_listeners.forEach(function (listener) {
18+
listener.call(HashLocation, change);
4219
});
4320
}
4421

45-
var _isListening = false;
22+
function ensureSlash() {
23+
var path = HashLocation.getCurrentPath();
24+
25+
if (path.charAt(0) === '/')
26+
return true;
27+
28+
HashLocation.replace('/' + path);
29+
30+
return false;
31+
}
4632

4733
function onHashChange() {
4834
if (ensureSlash()) {
@@ -61,7 +47,7 @@ function onHashChange() {
6147
var HashLocation = {
6248

6349
addChangeListener(listener) {
64-
_changeListeners.push(listener);
50+
_listeners.push(listener);
6551

6652
// Do this BEFORE listening for hashchange.
6753
ensureSlash();
@@ -78,11 +64,11 @@ var HashLocation = {
7864
},
7965

8066
removeChangeListener(listener) {
81-
_changeListeners = _changeListeners.filter(function (l) {
67+
_listeners = _listeners.filter(function (l) {
8268
return l !== listener;
8369
});
8470

85-
if (_changeListeners.length === 0) {
71+
if (_listeners.length === 0) {
8672
if (window.removeEventListener) {
8773
window.removeEventListener('hashchange', onHashChange, false);
8874
} else {
@@ -110,7 +96,13 @@ var HashLocation = {
11096
History.back();
11197
},
11298

113-
getCurrentPath: getHashPath,
99+
getCurrentPath() {
100+
return decodeURI(
101+
// We can't use window.location.hash here because it's not
102+
// consistent across browsers - Firefox will pre-decode it!
103+
window.location.href.split('#')[1] || ''
104+
);
105+
},
114106

115107
toString() {
116108
return '<HashLocation>';

modules/locations/HistoryLocation.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
var LocationActions = require('../actions/LocationActions');
22
var History = require('../History');
33

4-
/**
5-
* Returns the current URL path from `window.location`, including query string.
6-
*/
7-
function getWindowPath() {
8-
return decodeURI(
9-
window.location.pathname + window.location.search
10-
);
11-
}
12-
13-
var _changeListeners = [];
4+
var _listeners = [];
5+
var _isListening = false;
146

157
function notifyChange(type) {
168
var change = {
17-
path: getWindowPath(),
9+
path: HistoryLocation.getCurrentPath(),
1810
type: type
1911
};
2012

21-
_changeListeners.forEach(function (listener) {
22-
listener(change);
13+
_listeners.forEach(function (listener) {
14+
listener.call(HistoryLocation, change);
2315
});
2416
}
2517

26-
var _isListening = false;
27-
2818
function onPopState(event) {
2919
if (event.state === undefined)
3020
return; // Ignore extraneous popstate events in WebKit.
@@ -38,7 +28,7 @@ function onPopState(event) {
3828
var HistoryLocation = {
3929

4030
addChangeListener(listener) {
41-
_changeListeners.push(listener);
31+
_listeners.push(listener);
4232

4333
if (!_isListening) {
4434
if (window.addEventListener) {
@@ -52,11 +42,11 @@ var HistoryLocation = {
5242
},
5343

5444
removeChangeListener(listener) {
55-
_changeListeners = _changeListeners.filter(function (l) {
45+
_listeners = _listeners.filter(function (l) {
5646
return l !== listener;
5747
});
5848

59-
if (_changeListeners.length === 0) {
49+
if (_listeners.length === 0) {
6050
if (window.addEventListener) {
6151
window.removeEventListener('popstate', onPopState, false);
6252
} else {
@@ -80,7 +70,11 @@ var HistoryLocation = {
8070

8171
pop: History.back,
8272

83-
getCurrentPath: getWindowPath,
73+
getCurrentPath() {
74+
return decodeURI(
75+
window.location.pathname + window.location.search
76+
);
77+
},
8478

8579
toString() {
8680
return '<HistoryLocation>';

modules/locations/TestLocation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ class TestLocation {
2222
}
2323

2424
_notifyChange(type) {
25+
var change = {
26+
path: this.getCurrentPath(),
27+
type: type
28+
};
29+
2530
for (var i = 0, len = this.listeners.length; i < len; ++i)
26-
this.listeners[i].call(this, { path: this.getCurrentPath(), type: type });
31+
this.listeners[i].call(this, change);
2732
}
2833

2934
addChangeListener(listener) {

0 commit comments

Comments
 (0)