-
Notifications
You must be signed in to change notification settings - Fork 414
/
location.js
164 lines (131 loc) · 4.11 KB
/
location.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var dep = new Deps.Dependency;
// XXX: we have to store the state internally (rather than just calling out
// to window.location) due to an android 2.3 bug. See:
// https://github.com/EventedMind/iron-router/issues/350
var currentState = {
path: location.pathname + location.search + location.hash
};
function onclick (e) {
var el = e.currentTarget;
var which = _.isUndefined(e.which) ? e.button : e.which;
var href = el.href;
var path = el.pathname + el.search + el.hash;
// we only want to handle clicks on links which:
// - are with the left mouse button with no meta key pressed
if (which !== 1)
return;
if (e.metaKey || e.ctrlKey || e.shiftKey)
return;
// - haven't been cancelled already
if (e.isDefaultPrevented())
return;
// - aren't in a new window
if (el.target)
return;
// - aren't external to the app
if (!IronLocation.isSameOrigin(href))
return;
// note that we _do_ handle links which point to the current URL
// and links which only change the hash.
e.preventDefault();
IronLocation.set(path);
}
function onpopstate (e) {
setState(e.state, null, location.pathname + location.search + location.hash);
}
IronLocation = {};
IronLocation.options = {
"linkSelector": 'a[href]'
};
IronLocation.configure = function(options){
if (this.isStarted){
IronLocation.unbindEvents();
}
_.extend(this.options, options);
if(this.isStarted){
IronLocation.bindEvents();
}
};
IronLocation.origin = function () {
return location.protocol + '//' + location.host;
};
IronLocation.isSameOrigin = function (href) {
var origin = IronLocation.origin();
return href.indexOf(origin) === 0;
};
IronLocation.get = function () {
dep.depend();
return currentState;
};
IronLocation.path = function () {
dep.depend();
return currentState.path;
};
IronLocation.set = function (url, options) {
options = options || {};
var state = options.state || {};
if (/^http/.test(url))
href = url;
else {
if (url.charAt(0) !== '/')
url = '/' + url;
href = IronLocation.origin() + url;
}
if (!IronLocation.isSameOrigin(href))
window.location = href;
else if (options.where === 'server')
window.location = href;
else if (options.replaceState)
IronLocation.replaceState(state, options.title, url, options.skipReactive);
else
IronLocation.pushState(state, options.title, url, options.skipReactive);
};
// store the state for later access
setState = function(newState, title, url, skipReactive) {
if (!skipReactive && (currentState.path !== url))
dep.changed();
currentState = newState || {};
currentState.path = url;
currentState.title = title;
}
IronLocation.pushState = function (state, title, url, skipReactive) {
setState(state, title, url, skipReactive);
if (history.pushState)
history.pushState(state, title, url);
else
window.location = url;
};
IronLocation.replaceState = function (state, title, url, skipReactive) {
setState(state, title, url, skipReactive);
if (history.replaceState)
history.replaceState(state, title, url);
else
window.location = url;
};
IronLocation.bindEvents = function(){
$(window).on('popstate', onpopstate);
$(document).on('click', this.options.linkSelector, onclick);
};
IronLocation.unbindEvents = function(){
$(window).off('popstate', onpopstate);
$(document).off('click', this.options.linkSelector, onclick);
};
IronLocation.start = function () {
if (this.isStarted)
return;
IronLocation.bindEvents();
this.isStarted = true;
// store the fact that this is the first route we hit.
// this serves two purposes
// 1. We can tell when we've reached an unhandled route and need to show a
// 404 (rather than bailing out to let the server handle it)
// 2. Users can look at the state to tell if the history.back() will stay
// inside the app (this is important for mobile apps).
if (history.replaceState)
history.replaceState({initial: true}, null, location.pathname + location.search + location.hash);
};
IronLocation.stop = function () {
IronLocation.unbindEvents();
this.isStarted = false;
};
IronLocation.start();