forked from scheme-requests-for-implementation/srfi-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrfi.js
332 lines (278 loc) · 8.4 KB
/
srfi.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
function sortFunction(itemA, itemB, options) {
let column = options.valueName;
let sort = srfiList.utils.naturalSort;
let primary = sort(itemA.values()[column],
itemB.values()[column]);
if (primary == 0) {
if (column === "number") {
return 0;
}
return sort(itemA.values()["number"],
itemB.values()["number"]);
}
return primary;
};
let options = {
sortFunction,
valueNames: [ "abstract",
"authors",
"date",
"name",
"number",
{ name: "status", attr: "data-status" }
]};
let srfiList = new List("srfis", options);
let abstractsControl = document.querySelector("#abstracts-control");
let listElement = document.querySelector(".list");
let searchControl = document.querySelector("#search");
let sortControls = Array.from(document.querySelectorAll(".sort"));
let statusesControl = document.querySelector("#statuses");
let keywordsControl = document.querySelector("#keywords");
function assert(expression) {
if (! expression) {
throw new Error();
}
}
function assertMember(candidate, array) {
assert(array.find(x => x === candidate));
}
function assertValidColumn(column) {
assertMember(column,
["abstract", "authors", "date", "name", "number", "status"]);
}
function assertValidSortOrder(order) {
assertMember(order, ["asc", "desc"]);
}
function decodeSortParameter(sp) {
if (sp) {
let [column, order] = sp.split("-", 2);
assertValidColumn(column);
assertValidSortOrder(order);
return { column, order };
} else {
return { column: "number", order: "desc" };
}
}
function decodeMultiParameter(string) {
return string && string.split(",");
}
let Choice = {
YES: 1,
NO: 2
};
function choice(boolean) {
return boolean ? Choice.YES : Choice.NO;
}
function decodeURL(url) {
let parameters = url.searchParams;
let abstracts = parameters.has("abstracts") ? Choice.YES : Choice.NO;
let keywords = decodeMultiParameter(parameters.get("keywords"));
let query = parameters.get("q") || "";
let sort = decodeSortParameter(parameters.get("sort"));
let statuses = decodeMultiParameter(parameters.get("statuses"));
return { abstracts, keywords, query, sort, statuses };
}
function encodeWithCommas(name, values) {
return values && values.length > 0
? [`${name}=${values.join(",")}`]
: [];
}
function encodeURL(abstracts, keywords, query, sort, statuses) {
// Drop defaults.
let elements =
[].concat(
abstracts == Choice.YES ? ["abstracts"] : [],
query ? [`q=${encodeURIComponent(query)}`] : [],
(sort && (sort.column !== "number" || sort.order !== "desc"))
? [`sort=${sort.column}-${sort.order}`]
: [],
encodeWithCommas("keywords", keywords),
encodeWithCommas("statuses", statuses));
return elements.length == 0
? ""
: "?" + elements.join("&");
}
function updateURL(url, { abstracts=null,
keywords=null,
query=null,
sort=null,
statuses=null }) {
let { abstracts: oldAbstracts,
keywords: oldKeywords,
query: oldQuery,
sort: oldSort,
statuses: oldStatuses }
= decodeURL(url);
return url.protocol
+ "//"
+ url.host
+ url.pathname
+ encodeURL(abstracts || oldAbstracts,
keywords || oldKeywords,
query === null ? oldQuery : query,
sort || oldSort,
statuses || oldStatuses);
}
function obeyAbstracts(abstracts) {
abstractsControl.checked = abstracts == Choice.YES;
if (abstractsControl.checked) {
listElement.classList.remove("summary");
listElement.classList.add("detailed");
} else {
listElement.classList.remove("detailed");
listElement.classList.add("summary");
}
}
function updateMultiSelect(control, values) {
let options = Array.from(control.querySelectorAll("option"));
let set = new Set(values);
let names = [];
for (let o of options) {
o.selected = set.has(o.value);
if (o.selected) {
names.push(o.innerHTML);
}
};
control.querySelector(".chosen").innerHTML =
choiceEnglish(names);
if (set.size == 0) {
control.querySelector("[value=\"any\"]").selected = true;
}
}
function srfiCards() {
return Array.from(srfiList.items).map(i => i.elm);
}
function filterMulti(control, name, values) {
let invisibleClass = `invisible-${name}`;
for (let i of Array.from(document.querySelectorAll("." + invisibleClass))) {
i.classList.remove(invisibleClass);
}
if (values.length > 0) {
let set = new Set(values);
for (let c of srfiCards()) {
let cardValues = c.querySelector(`.${name}`)
.dataset[name].split(",");
if (! cardValues.find(v => set.has(v))) {
c.classList.add(invisibleClass);
}
}
}}
function obeyKeywords(keywords) {
updateMultiSelect(keywordsControl, keywords);
filterMulti(keywordsControl, "keywords", keywords);
}
function obeySearch(query) {
searchControl.value = query;
srfiList.search(query);
}
function obeySort(sort) {
srfiList.sort(sort.column, { order: sort.order });
}
function obeyStatuses(statuses) {
updateMultiSelect(statusesControl, statuses);
filterMulti(statusesControl, "status", statuses);
}
function obeyQueryParameters(_) {
let { abstracts, keywords, query, sort, statuses }
= decodeURL(new URL(document.location));
obeyAbstracts(abstracts);
obeyKeywords(keywords || []);
obeySearch(query);
obeySort(sort);
obeyStatuses(statuses || []);
}
window.onpopstate = obeyQueryParameters;
function changeURL(replace, components) {
let existing = new URL(document.location);
let newURL = updateURL(existing, components);
if (existing.toString() !== newURL) {
if (replace) {
history.replaceState(newURL, "home", newURL);
} else {
history.pushState(newURL, "home", newURL);
}
}
}
abstractsControl.addEventListener(
"change",
function(event) {
obeyAbstracts(choice(this.checked));
changeURL(false, { abstracts: choice(this.checked) });
event.preventDefault();
event.stopPropagation();
});
searchControl.addEventListener(
"input",
function(event) {
changeURL(true, { query: this.value });
});
function choiceEnglish(choices) {
if (choices.length == 0) {
return "any";
}
let size = choices.length;
let last = choices[size - 1];
if (size == 1) {
return last;
}
if (size == 2) {
return choices[0] + " or " + last;
}
return choices.slice(0, -1).join(", ") + ", or " + last;
}
function enableMultiSelect(control, update) {
let popper = control.querySelector("button");
let select = control.querySelector("select");
popper.addEventListener(
"click",
function(event) {
event.preventDefault();
event.stopPropagation();
for (let s of Array.from(document.querySelectorAll(".show-options"))) {
if (s != control) {
s.classList.remove("show-options");
}
}
control.classList.toggle("show-options");
}
);
select.addEventListener(
"change",
function(event) {
event.preventDefault();
event.stopPropagation();
let values = Array.from(select.options)
.filter(s => s.selected)
.map(s => s.value);
update(values.includes("any") ? [] : values);
});
}
enableMultiSelect(statusesControl,
function(choices) {
changeURL(false, { statuses: choices });
obeyStatuses(choices);
});
enableMultiSelect(keywordsControl,
function(choices) {
changeURL(false, { keywords: choices });
obeyKeywords(choices);
});
let observer = new MutationObserver(
function(mutationRecords, observer) {
for (let r of mutationRecords) {
let button = r.target;
let classes = button.classList;
let column = button.textContent;
assertValidColumn(column);
if (classes.contains("asc")) {
changeURL(false, { sort: { column: column, order: "asc" } });
} else if (classes.contains("desc")) {
changeURL(false, { sort: { column: column, order: "desc" } });
}
}
});
for (let button of sortControls) {
observer.observe(button, { attributes: true });
}
obeyQueryParameters(null);
document.querySelector(".invisible").classList.remove("invisible");