-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (84 loc) · 3.28 KB
/
script.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
const commands = [];
const search = document.querySelector("#search");
const btnSearch = document.querySelector(".btn-search");
getCommands();
function getCommands() {
fetch("./commands.json")
.then((res) => res.json())
.then((data) => {
commands.push(...data);
commands.sort((a, b) => {
if (a.command < b.command) {
return -1;
}
if (a.command > b.command) {
return 1;
}
return 0;
});
renderCommands(commands);
});
}
function renderCommands(data) {
const output = document.querySelector("#commands-list");
output.innerText = "";
data.forEach((command) => {
const container = document.createElement("li");
const header = document.createElement("header");
const commandTitle = document.createElement("h2");
commandTitle.innerText = command.command;
const commandMeaning = document.createElement("small");
commandMeaning.innerText = command.meaning;
const code = document.createElement("code");
code.classList.add("main-code");
code.innerText = command.code;
const description = document.createElement("p");
description.innerText = command.description;
const wrapper = document.createElement("section");
const exampleTitle = document.createElement("h3");
if (command.examples.length > 1) {
exampleTitle.innerText = "Beispiele:";
} else {
exampleTitle.innerText = "Beispiel:";
}
wrapper.append(exampleTitle);
command.examples.forEach((example) => {
const exampleWrapper = document.createElement("div");
exampleWrapper.classList.add("example-wrapper");
const exampleCode = document.createElement("code");
exampleCode.innerText = example.code;
const exampleDescription = document.createElement("p");
exampleDescription.innerText = example.description;
exampleWrapper.append(exampleCode, exampleDescription);
wrapper.append(exampleWrapper);
});
const spacer = document.createElement("div");
spacer.classList.add("spacer");
header.append(commandTitle, commandMeaning);
container.append(header, code, description, wrapper);
output.append(container, spacer);
});
setIndividualHeights();
}
window.addEventListener("resize", setIndividualHeights);
function setIndividualHeights() {
const list = document.querySelector("#commands-list");
const children = list.children;
for (let child of children) {
const height = child.offsetHeight;
child.style.setProperty("--rows", Math.floor(height));
}
}
btnSearch.addEventListener("click", filterCommands);
search.addEventListener("input", filterCommands);
function filterCommands(e) {
let searchTerm;
if (e.target.value) {
searchTerm = new RegExp("\\b" + e.target.value.trim(), "i");
} else {
searchTerm = new RegExp("\\b" + search.value.trim(), "i");
search.blur();
}
const searchResults = commands.filter((command) => searchTerm.test([...command.tags]));
renderCommands(searchResults);
}