forked from cerebral/addressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (168 loc) · 5.14 KB
/
index.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
198
199
200
201
202
203
204
205
206
/* global history */
var URL = require('url-parse')
var EventEmitter = require('events').EventEmitter
var instance = null
var location = window.location
module.exports = (function () {
if (instance) {
return instance
}
var eventEmitter = new EventEmitter()
eventEmitter.addEventListener = eventEmitter.addListener
eventEmitter.removeEventListener = eventEmitter.removeListener
var initialUrl = location.href
var uri = URL(initialUrl)
var origin = uri.protocol + '//' + uri.host
var isPreventingDefault = false
var doReplace = false
var prevUrl = ''
// var linkClicked = false
var isEmitting = false
var setSyncUrl = false
var emitChange = function (url, event) {
eventEmitter.emit('change', {
preventDefault: function () {
event && event.preventDefault()
isPreventingDefault = true
},
target: {
value: url ? origin + url : location.href
}
})
}
var onUrlChange = function (type) {
return function (event) {
if (!event.state || location.href === prevUrl) {
return
}
// Fixes bug where trailing slash is converted to normal url
if (location.href[location.href.length - 1] === '/') {
doReplace = true
}
isEmitting = true
emitChange()
isEmitting = false
if (!setSyncUrl && isPreventingDefault) {
history.replaceState({}, '', (prevUrl || initialUrl).replace(origin, ''))
}
prevUrl = location.href
isPreventingDefault = false
setSyncUrl = false
doReplace = false
}
}
global.addEventListener('popstate', onUrlChange('pop'))
Object.defineProperty(eventEmitter, 'value', {
get: function () {
return location.href
},
set: function (value) {
if (typeof value !== 'string') {
doReplace = Boolean(value.replace)
value = value.value
}
// If emitting a change we flag that we are setting
// a url based on the event being emitted
if (isEmitting) {
setSyncUrl = true
}
// Ensure full url
if (value.indexOf(origin) === -1) {
value = origin + value
}
// If it is same url, forget about it
if (value === location.href) {
return
}
// We might need to replace the url if we are fixing
// for example trailing slash issue
if (doReplace) {
history.replaceState({}, '', value.replace(origin, ''))
doReplace = false
} else {
history.pushState({}, '', value.replace(origin, ''))
}
prevUrl = location.href
isPreventingDefault = false
}
})
// expose URLUtils like API https://developer.mozilla.org/en-US/docs/Web/API/URLUtils
// thanks https://github.com/cofounders/urlutils for reference
Object.defineProperty(eventEmitter, 'origin', {
get: function () {
var uri = URL(location.href)
return uri.protocol + '//' + uri.host
}
})
Object.defineProperty(eventEmitter, 'protocol', {
get: function () {
return URL(location.href).protocol
}
})
Object.defineProperty(eventEmitter, 'port', {
get: function () {
return URL(location.href).port
}
})
Object.defineProperty(eventEmitter, 'hostname', {
get: function () {
return URL(location.href).hostname
}
})
Object.defineProperty(eventEmitter, 'pathname', {
get: function () {
return URL(location.href).pathname
}
})
Object.defineProperty(eventEmitter, 'hash', {
get: function () {
return URL(location.href).hash
}
})
/*
This code is from the Page JS source code. Amazing work on handling all
kinds of scenarios with hyperlinks, thanks!
*/
var isSameOrigin = function (href) {
return (href && (href.indexOf(origin) === 0))
}
var getClickedHref = function (event) {
// check which button
if ((event.which === null ? event.button : event.which) !== 1) { return false }
// check for modifiers
if (event.metaKey || event.ctrlKey || event.shiftKey) { return false }
if (event.defaultPrevented) { return false }
// ensure link
var element = event.target
while (element && element.nodeName !== 'A') { element = element.parentNode }
if (!element || element.nodeName !== 'A') { return false }
// Ignore if tag has
// 1. "download" attribute
// 2. rel="external" attribute
if (element.hasAttribute('download') || element.getAttribute('rel') === 'external') { return false }
// Check for mailto: in the href
var href = element.getAttribute('href')
if (href && href.indexOf('mailto:') > -1) { return false }
// check target
if (element.target) { return false }
// x-origin
if (!isSameOrigin(element.href)) { return false }
return href
}
global.addEventListener(document.ontouchstart ? 'touchstart' : 'click', function (event) {
var href = getClickedHref(event)
if (href) {
// linkClicked = true
isEmitting = true
emitChange(href, event)
isEmitting = false
if (isPreventingDefault) {
// linkClicked = false
}
prevUrl = href
isPreventingDefault = false
}
})
instance = eventEmitter
return eventEmitter
}())