-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
86 lines (75 loc) · 2.7 KB
/
search.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
function findNow(search, sitemap, appendIn, limit) {
let sectionList = fastn_utils.getStaticValue(sitemap.get("sections"));
let searchValue = fastn_utils.getStaticValue(search).toLowerCase();
appendIn.clearAll();
if (searchValue.length === 0) {
return;
}
findInSections(sectionList, searchValue, appendIn, limit);
}
function findInSections(sectionList, search, appendIn, limit) {
if (appendIn.getList().length >= limit) {
return;
}
for(let item of sectionList) {
let tocItem = item.item;
let title = fastn_utils.getStaticValue(tocItem.get("title"));
let description = fastn_utils.getStaticValue(tocItem.get("description"));
let url = fastn_utils.getStaticValue(tocItem.get("url"));
if (fastn_utils.isNull(url) || url == "") {
let children = fastn_utils.getStaticValue(tocItem.get("children"));
findInSections(children, search, appendIn, limit);
continue;
}
let alreadyInList = appendIn.getList().some(
existingItem =>
fastn_utils.getStaticValue(existingItem.item.get("url")) === url
);
if (
(!fastn_utils.isNull(title) && title.toLowerCase().includes(search))
|| (!fastn_utils.isNull(description) && description.toLowerCase().includes(search))
|| url.toLowerCase().includes(search)
&& !alreadyInList
) {
if (appendIn.getList().length >= limit) {
return;
}
appendIn.push(
fastn.recordInstance({
title: title,
description: description,
url: url
}));
}
let children = fastn_utils.getStaticValue(tocItem.get("children"));
findInSections(children, search, appendIn, limit);
}
}
function goBack() {
const currentURL = new URL(window.location.href);
let nextPage = currentURL.searchParams.get("next");
if (nextPage !== null) {
window.location.href = nextPage;
} else {
window.location.href = "/";
}
}
function openSearch() {
const currentURL = document.location.pathname + document.location.search;
window.location.href = `/search/?next=${encodeURIComponent(currentURL)}`
}
function goToUrl(a, l) {
let index = fastn_utils.getStaticValue(a);
let list = fastn_utils.getStaticValue(l);
if (list.length === 0 || index >= list.length) {
return;
}
window.location.href = fastn_utils.getStaticValue(list[index].item.get("url"));
}
function clampDecrement(a,n) {
let newValue = (a.get() - 1) ;
if (newValue < 0) {
newValue = n.get() - 1;
}
a.set(newValue);
}