forked from pothonprogramming/pothonprogramming.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (61 loc) · 2.55 KB
/
index.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 filter_query = document.getElementById("main-filter-query");
const project_container = document.getElementById("main-project-container");
const projects_container = document.getElementById("main-projects-container");
const logs_container = document.getElementById("main-logs-container");
const projects = [];
const logs = [];
function clickOrTouchStart(event) {
if (event.target.id == "main-filter-button") {
event.preventDefault();
filter(filter_query.innerText);
}
}
function emptyContainer(container) {
while(container.firstChild) container.removeChild(container.firstChild);
}
function fillContainer(container, elements) {
while(elements.length != 0) container.appendChild(elements.shift());
}
function filter(query) {
query = query.replace(/[\n\r\v]/g, ''); // filter newline and carriage return
var elements = [];
for (var index = 1; index < projects.length; index ++) {
var project = projects[index];
var regexp = new RegExp(query, 'i');
if (regexp.test(project.data.name) || regexp.test(project.data.tags) || regexp.test(project.data.note) || regexp.test(project.data.date)) elements.push(project.element);
}
emptyContainer(projects_container);
fillContainer(projects_container, elements);
}
function keyDown(event) {
if (event.keyCode == 13) {
event.preventDefault();
filter(filter_query.innerText);
}
}
// Open all links in new tab
(() => {
const links = document.querySelectorAll('a');
for (var index = links.length - 1; index > -1; -- index) {
var link = links[index];
if (link.target === '') link.setAttribute('target', '_blank');
}
})();
fetch("data/logs.json").then(response => {
return response.json();
}).then(data => {
for (var index = 0; index < data.length; index ++) logs[index] = new Log(data[index]);
logs_container.appendChild(logs[0].element);
});
fetch("data/projects.json").then(response => {
return response.json();
}).then(data => {
for (var index = 0; index < data.length; index ++) projects[index] = new Project(data[index]);
project_container.appendChild(projects[0].element);
filter("");
});
window.addEventListener("touchstart", clickOrTouchStart);
window.addEventListener("click", clickOrTouchStart);
window.addEventListener("keydown", keyDown);
})();