forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-navigator.js
165 lines (145 loc) · 4.52 KB
/
self-navigator.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
/**
* Copyright 2018 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
import '../node_modules/@polymer/polymer/polymer-element.js';
import { PathInfo } from './path.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<dom-module id="self-navigator">
</dom-module>`;
document.head.appendChild($_documentContainer.content);
// eslint-disable-next-line no-unused-vars
const SelfNavigation = (superClass) => class SelfNavigation extends PathInfo(superClass) {
static get properties() {
return {
path: {
type: String,
value: '/',
observer: 'pathUpdated',
},
onLocationUpdated: Function,
};
}
ready() {
super.ready();
if (this.path === SelfNavigation.properties.path.value) {
this.path = this.urlToPath(window.location);
}
window.onpopstate = () => {
this.path = this.urlToPath(window.location);
this.onLocationUpdated && this.onLocationUpdated(this.path, history.state);
// Do an extra 'back' for the first (artificially stacked) query state
// when we pop off of the stack completely.
if (!history.state) {
window.history.back();
}
};
// Push initial state into the stack.
const params = this.navigationQueryParams();
const url = this.getLocation(params, window.location);
window.history.pushState(params, '', url);
}
urlToPath(location) {
let path = location.pathname;
if (this.navigationPathPrefix() !== '') {
// Strip prefix
let prefixRe = RegExp(`^${this.navigationPathPrefix()}/(.+)?$`);
path = path.replace(prefixRe, '/$1');
}
path = path.replace(/.+\/$/, ''); // Strip trailing slash
return this.decodeTestPath(path);
}
// These are two helper functions to encode/decode the LAST component of
// the test path, which may contain query strings (because of test
// variants), e.g. "/dom/interfaces.html?exclude=Node" <-->
// "/dom/interfaces.html%3Fexclude%3DNode".
encodeTestPath(path) {
console.assert(path.startsWith('/'));
let parts = path.split('/').slice(1);
parts.push(encodeURIComponent(parts.pop()));
return '/' + parts.join('/');
}
decodeTestPath(path) {
console.assert(path.startsWith('/'));
let parts = path.split('/').slice(1);
parts.push(decodeURIComponent(parts.pop()));
return '/' + parts.join('/');
}
pathUpdated(path) {
if (this.onLocationUpdated) {
this.onLocationUpdated(
path, history.state || this.navigationQueryParams());
}
}
/**
* Get the path prefix when creating history.
*/
navigationPathPrefix() {
return '';
}
/**
* Get query params to persist when creating history.
* Defaults to the queryParams property.
*/
navigationQueryParams() {
return this.queryParams && JSON.parse(JSON.stringify(this.queryParams));
}
bindNavigate() {
return this.navigate.bind(this);
}
navigate(event) {
// Don't intercept Ctrl+click or Meta(Win/Command)+click (open new tabs).
if (event.ctrlKey || event.metaKey) {
return;
}
event.preventDefault();
this.navigateToLocation(event.target);
}
/**
* Navigate to the path + query of the given Location object.
*/
navigateToLocation(location) {
const params = this.navigationQueryParams();
const url = this.getLocation(params, location);
if (url.toString() === window.location.toString()) {
return;
}
const path = this.urlToPath(location);
if (path !== this.path) {
this.path = path;
}
url.search = url.search
.replace(/=true/g, '')
.replace(/%3A00.000Z/g, '');
window.history.pushState(params, '', url);
// Send Google Analytics page_view event
if ('gtag' in window) {
window.gtag('event', 'page_view', {
page_path: path
});
}
}
navigateToPath(testPath) {
const url = new URL(window.location);
url.pathname = this.navigationPathPrefix() + testPath;
this.navigateToLocation(url);
}
getLocation(params, location) {
const url = new URL(location);
url.search = '';
if (params) {
for (const [k, v] of Object.entries(params)) {
const list = (v instanceof Array) ? v : [v];
for (const item of list) {
url.searchParams.append(k, item);
}
}
}
url.search = url.search
.replace(/=true/g, '')
.replace(/%3A00.000Z/g, '');
return url;
}
};
export { SelfNavigation };