-
Notifications
You must be signed in to change notification settings - Fork 0
/
location-lite.js
293 lines (262 loc) · 8.91 KB
/
location-lite.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/// <reference path="typings-project/global.d.ts"/>
import { resolveUrl } from './lib/resolve-url.js';
/**
* # LocationLite
* `<location-lite>` Location path getter system for making a single-page-application.
* It hijacks the global clicking of any `<a>` tag and pushes the anchor's `href` url to `history`
* without loading the page from the server.
*
* This is a copied version of without using Polymer `https://github.com/PolymerElements/iron-location/blob/__auto_generated_3.0_preview/iron-location.js`
*
* @customElement
*
*/
export class LocationLite extends window.HTMLElement {
static get is () { return 'location-lite'; }
constructor () {
super();
this.__data = {};
this.__dataInvalid = false;
this._boundHashChanged = this._hashChanged.bind(this);
this._boundUrlChanged = this._urlChanged.bind(this);
this._boundGlobalOnClick = this._globalOnClick.bind(this);
this._urlSpaceRegExp = null;
this.dwellTime = 2000;
}
connectedCallback () {
window.addEventListener('hashchange', this._boundHashChanged);
window.addEventListener('location-change', this._boundUrlChanged);
window.addEventListener('popstate', this._boundUrlChanged);
document.body.addEventListener('click', this._boundGlobalOnClick, true);
if (window.performance) {
this._lastChangedAt = window.performance.now() - (this.dwellTime - 200);
}
this._initialized = true;
this._urlChanged();
}
disconnectedCallback () {
window.removeEventListener('hashchange', this._boundHashChanged);
window.removeEventListener('location-change', this._boundUrlChanged);
window.removeEventListener('popstate', this._boundUrlChanged);
document.body.removeEventListener('click', this._boundGlobalOnClick);
this._initialized = false;
}
set urlSpaceRegex (urlSpaceRegex) {
this.__data.urlSpaceRegex = urlSpaceRegex;
this._urlSpaceRegExp = RegExp(urlSpaceRegex);
}
get urlSpaceRegex () {
return this.__data.urlSpaceRegex;
}
set path (path) {
if (this.__data.path !== path) {
this.__data.path = path;
this._invalidateProperties();
}
}
get path () {
return this.__data.path;
}
set query (query) {
if (this.__data.query !== query) {
this.__data.query = query;
this._invalidateProperties();
}
}
get query () {
return this.__data.query;
}
set hash (hash) {
if (this.__data.hash !== hash) {
this.__data.hash = hash;
this._invalidateProperties();
}
}
get hash () {
return this.__data.hash;
}
_invalidateProperties () {
if (!this.__dataInvalid) {
this.__dataInvalid = true;
Promise.resolve().then(() => {
if (this.__dataInvalid) {
this.__dataInvalid = false;
this._updateUrl();
}
});
}
}
_hashChanged () {
this.hash = window.decodeURIComponent(window.location.hash.slice(1));
Promise.resolve().then(() => {
this.dispatchEvent(new window.CustomEvent('hash-change', { detail: this.hash }));
});
}
_urlChanged () {
// We want to extract all info out of the updated URL before we
// try to write anything back into it.
//
// i.e. without _dontUpdateUrl we'd overwrite the new path with the old
// one when we set this.hash. Likewise for query.
this._dontUpdateUrl = true;
this._hashChanged();
this.path = window.decodeURIComponent(window.location.pathname);
this.query = window.location.search.slice(1);
Promise.resolve().then(() => {
this.dispatchEvent(new window.CustomEvent('path-change', { detail: this.path }));
this.dispatchEvent(new window.CustomEvent('query-change', { detail: this.query }));
});
this._dontUpdateUrl = false;
// this._updateUrl();
}
_getUrl () {
var partiallyEncodedPath = window.encodeURI(this.path).replace(/\#/g, '%23').replace(/\?/g, '%3F'); // eslint-disable-line no-useless-escape
var partiallyEncodedQuery = '';
if (this.query) {
partiallyEncodedQuery = '?' + this.query.replace(/\#/g, '%23'); // eslint-disable-line no-useless-escape
}
var partiallyEncodedHash = '';
if (this.hash) {
partiallyEncodedHash = '#' + window.encodeURI(this.hash);
}
return (partiallyEncodedPath + partiallyEncodedQuery + partiallyEncodedHash);
}
_updateUrl () {
if (this._dontUpdateUrl || !this._initialized) {
return;
}
if (this.path === window.decodeURIComponent(window.location.pathname) &&
this.query === window.location.search.substring(1) &&
this.hash === window.decodeURIComponent(window.location.hash.substring(1))) {
// Nothing to do, the current URL is a representation of our properties.
return;
}
var newUrl = this._getUrl();
// Need to use a full URL in case the containing page has a base URI.
var fullNewUrl = resolveUrl(newUrl, window.location.protocol + '//' + window.location.host).href;
var now = window.performance ? window.performance.now() : null;
var shouldReplace = this._lastChangedAt + this.dwellTime > now;
this._lastChangedAt = now;
if (shouldReplace) {
window.history.replaceState({}, '', fullNewUrl);
} else {
window.history.pushState({}, '', fullNewUrl);
}
window.dispatchEvent(new window.CustomEvent('location-change'));
}
/**
* A necessary evil so that links work as expected. Does its best to
* bail out early if possible.
*
* @param {MouseEvent} event .
*/
_globalOnClick (event) {
// If another event handler has stopped this event then there's nothing
// for us to do. This can happen e.g. when there are multiple
// iron-location elements in a page.
if (event.defaultPrevented) {
return;
}
var href = this._getSameOriginLinkHref(event);
if (!href) {
return;
}
event.preventDefault();
// If the navigation is to the current page we shouldn't add a history
// entry or fire a change event.
if (href === window.location.href) {
return;
}
window.history.pushState({}, '', href);
window.dispatchEvent(new window.CustomEvent('location-change'));
}
/**
* Returns the absolute URL of the link (if any) that this click event
* is clicking on, if we can and should override the resulting full
* page navigation. Returns null otherwise.
*
* @param {MouseEvent} event .
* @return {string?} .
*/
_getSameOriginLinkHref (event) {
// We only care about left-clicks.
if (event.button !== 0) {
return null;
}
// We don't want modified clicks, where the intent is to open the page
// in a new tab.
if (event.metaKey || event.ctrlKey) {
return null;
}
const eventPath = event.composedPath();
let anchor = null;
for (var i = 0; i < eventPath.length; i++) {
var element = eventPath[i];
if (element.tagName === 'A' && element.href) {
anchor = element;
break;
}
}
// If there's no link there's nothing to do.
if (!anchor) {
return null;
}
// Target blank is a new tab, don't intercept.
if (anchor.target === '_blank') {
// capture link click
if (anchor.href && window.ga) {
window.ga('send', 'event', 'Link', 'Click', anchor.href, 1);
}
return null;
}
// If the link is for an existing parent frame, don't intercept.
if ((anchor.target === '_top' ||
anchor.target === '_parent') &&
window.top !== window) {
return null;
}
const href = anchor.href;
// It only makes sense for us to intercept same-origin navigations.
// pushState/replaceState don't work with cross-origin links.
let url;
if (document.baseURI != null) {
url = resolveUrl(href, /** @type {string} */(document.baseURI));
} else {
url = resolveUrl(href);
}
let origin;
// IE Polyfill
if (window.location.origin) {
origin = window.location.origin;
} else {
origin = window.location.protocol + '//' + window.location.host;
}
let urlOrigin;
if (url.origin) {
urlOrigin = url.origin;
} else {
urlOrigin = url.protocol + '//' + url.host;
}
if (urlOrigin !== origin) {
return null;
}
var normalizedHref = url.pathname + url.search + url.hash;
// pathname should start with '/', but may not if `new URL` is not supported
if (normalizedHref[0] !== '/') {
normalizedHref = '/' + normalizedHref;
}
// If we've been configured not to handle this url... don't handle it!
if (this._urlSpaceRegExp &&
!this._urlSpaceRegExp.test(normalizedHref)) {
return null;
}
// Need to use a full URL in case the containing page has a base URI.
var fullNormalizedHref = resolveUrl(normalizedHref, window.location.href).href;
return fullNormalizedHref;
}
}
if (!window.customElements.get(LocationLite.is)) {
window.customElements.define(LocationLite.is, LocationLite);
} else {
console.warn(`${LocationLite.is} is already defined somewhere. Please check your code.`);
}