Skip to content

Commit 9599606

Browse files
committed
Store UI filter changes into the URL in compare page
1 parent 9bdb9d6 commit 9599606

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

site/static/compare/script.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function findQueryParam(name) {
2-
let params = new URLSearchParams(window.location.search.slice(1));
2+
const params = new URLSearchParams(window.location.search.slice(1));
33
return params.get(name);
44
}
55

@@ -28,10 +28,11 @@ function createDefaultFilter() {
2828

2929
/**
3030
* Loads the initial state of UI filters from URL parameters.
31+
* Keep in sync with `storeFilterToUrl` and `createDefaultFilter`!
3132
*/
3233
function initializeFilterFromUrl() {
3334
const defaultFilter = createDefaultFilter();
34-
let params = new URLSearchParams(window.location.search.slice(1));
35+
const params = new URLSearchParams(window.location.search.slice(1));
3536

3637
function getBoolOrDefault(name, defaultValue) {
3738
const urlValue = params.get(name);
@@ -63,6 +64,43 @@ function initializeFilterFromUrl() {
6364
};
6465
}
6566

67+
/**
68+
* Stores the given filter parameters into URL, so that the current "view" can be shared with
69+
* others easily.
70+
*/
71+
function storeFilterToUrl(filter) {
72+
const defaultFilter = createDefaultFilter();
73+
const params = new URLSearchParams(window.location.search);
74+
75+
function storeOrReset(name, value, defaultValue) {
76+
if (value === defaultValue) {
77+
if (params.has(name)) {
78+
params.delete(name);
79+
}
80+
} else {
81+
params.set(name, value);
82+
}
83+
}
84+
85+
storeOrReset("name", filter.name || null, defaultFilter.name);
86+
storeOrReset("nonRelevant", filter.nonRelevant, defaultFilter.nonRelevant);
87+
storeOrReset("check", filter.profile.check, defaultFilter.profile.check);
88+
storeOrReset("debug", filter.profile.debug, defaultFilter.profile.debug);
89+
storeOrReset("opt", filter.profile.opt, defaultFilter.profile.opt);
90+
storeOrReset("doc", filter.profile.doc, defaultFilter.profile.doc);
91+
storeOrReset("full", filter.scenario.full, defaultFilter.scenario.full);
92+
storeOrReset("incrFull", filter.scenario.incrFull, defaultFilter.scenario.incrFull);
93+
storeOrReset("incrUnchanged", filter.scenario.incrUnchanged, defaultFilter.scenario.incrUnchanged);
94+
storeOrReset("incrPatched", filter.scenario.incrPatched, defaultFilter.scenario.incrPatched);
95+
storeOrReset("primary", filter.category.primary, defaultFilter.category.primary);
96+
storeOrReset("secondary", filter.category.secondary, defaultFilter.category.secondary);
97+
98+
// Change URL without creating a history entry
99+
if (history.replaceState) {
100+
history.replaceState({}, null, createUrlFromParams(params));
101+
}
102+
}
103+
66104
const app = Vue.createApp({
67105
mounted() {
68106
const app = this;
@@ -86,6 +124,15 @@ const app = Vue.createApp({
86124
dataLoading: false
87125
}
88126
},
127+
watch: {
128+
// Every time the filter changes, update URL
129+
filter: {
130+
handler(newValue, oldValue) {
131+
storeFilterToUrl(newValue);
132+
},
133+
deep: true
134+
}
135+
},
89136
computed: {
90137
notContinuous() {
91138
return !this.data.is_contiguous;
@@ -735,7 +782,9 @@ function createSearchParamsForMetric(stat, start, end) {
735782
}
736783

737784
function createUrlFromParams(params) {
738-
return window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + params;
785+
const url = new URL(window.location);
786+
url.search = params;
787+
return url.toString();
739788
}
740789

741790
function submitSettings() {

0 commit comments

Comments
 (0)