-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation.js
197 lines (158 loc) · 4.64 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
define(function(require) {
var findClosest = require('./find-closest')
, rebind = require('./rebind')
, dispatcher = require('d3-dispatch').dispatch('statechange')
, history = window.history
, location = window.location
, on = require('./on')
, base = ''
on.call(window, 'popstate.location', handleStateChange)
on.call(document, 'click.location', handleClick)
if (isHashPath(location.hash)) {
// Redirect current hash fragment location to "real" path
history.replaceState(null, null, rebase(fullPath(location)))
}
var api =
{ getState: getState
, setState: setState
, pushState: deprecatedPushState
, replaceState: replaceState
, openNewWindow: openNewWindow
, basePath: basePath
}
return rebind(api, dispatcher, 'on')
function getState() {
return unbase(fullPath(location))
}
function setState(path) {
var actual = pushState(path)
if (actual) {
dispatcher.statechange(actual)
return actual
} else {
return false
}
}
function trimPath(path) {
return '/' + trimSlashes(~path.indexOf('#/')? path.split('#/')[1] : path)
}
function updateState(path, method) {
path = unbase(trimPath(path))
if (path === getState()) {
return false
} else {
method({ base: base, path: path }, null, rebase(path))
return path
}
}
function deprecatedPushState(path) {
console.warn('deprecated : location.pushState, to be removed in v.4.0.0.')
return pushState(path)
}
function pushState(path) {
return updateState(path, history.pushState.bind(history))
}
function replaceState(path) {
return updateState(path, history.replaceState.bind(history))
}
function openNewWindow(path, target) {
return window.open(rebase(path), target, '')
}
function basePath(path) {
if (arguments.length === 0) return base
var cwd = unbase(fullPath(location))
path = trimSlashes(path)
base = path? '/' + path : ''
history.replaceState(null, null, rebase(cwd))
}
function handleClick(event) {
var a
, target = event.target
, path
if (event.ctrlKey) return // Ignore ctrl+click
if (event.button !== 0) return // Ignore clicks by buttons other than primary (usually left button)
a = findClosest.anchor(target)
if ( !a // non-anchor clicks
|| !!a.target // anchors with specified targets
|| a.hasAttribute('download') // anchors with download attribute
|| !isSameOrigin(a, location) // links to different origins
) {
/* If any of the above conditions are true, we ignore the click and
* let the browser deal with the navigation as it sees fit
*/
return
}
var path
if (isHashPath(a.hash)) {
path = rebase(a.hash.slice(1))
} else if (a.hash || a.href.slice(location.href.length) === '#') {
// Ignore links with a non-path hash, and empty hashes (e.g.: `<a href="#"></a>`)
return
} else {
path = rebase(fullPath(a))
}
if (path) {
event.preventDefault()
event.stopPropagation()
var actual = pushState(path)
if (actual) {
dispatcher.statechange(actual)
}
}
}
function handleStateChange(event) {
var path, base = (event.state && event.state.base) || ''
if (isHashPath(location.hash)) {
// "Redirect" current location to a proper path
path = location.hash.slice(1)
if (path) {
var state = { base: base, path: path }
history.replaceState(state, null, rebase(path))
}
} else {
path = fullPath(location)
}
dispatcher.statechange(unbase(path))
}
function isHashPath(hash) {
return (hash || '').slice(0, 2) === '#/'
}
function isSameOrigin(a, x) {
var o = origin(x)
return a.href.slice(0, o.length) === o
}
function origin(url) {
if (url.origin) {
return url.origin
} else {
var port
if (url.port && !~url.href.indexOf(':' + url.port)) {
// IE defaults port values based on protocol, which messes things up
port = ''
} else {
port = ':' + url.port
}
return url.protocol + "//" + url.hostname + port
}
}
function fullPath(url) {
if (isHashPath(url.hash)) {
return url.hash.slice(1)
} else {
return url.href.slice(origin(url).length)
}
}
function rebase(path) {
return base + '/' + trimSlashes(unbase(path))
}
function unbase(path) {
if (path.slice(0, base.length) === base) {
return path.slice(base.length)
} else {
return path
}
}
function trimSlashes(path) {
return (path || '').replace(/^\/+|\/+$/g, '')
}
})