-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
191 lines (146 loc) · 4.37 KB
/
main.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
let inpWords = [];
let tags = [];
let main_divs = [];
let page_count = 0;
const click_counts = {};
window.onload = () =>
{
render_content();
assemble_tags(base);
btn_event();
document.getElementById("apps-quantity").innerHTML = `Apps: ${base.length}`;
document.getElementById("tags-quantity").innerHTML = `Tags: ${tags.length}`;
}
function dark(item) {
item.style.border = "2px solid var(--medium-dark-purple)";
item.style.backgroundColor = "var(--medium-dark-purple)";
item.style.color = "var(--light-purple)";
}
function light(item) {
item.style.border = "2px solid var(--lightest-purple)";
item.style.backgroundColor = "var(--lightest-purple)";
item.style.color = "var(--medium-dark-purple)";
}
function set_theme(item, int) {
const darktheme = window.matchMedia("(prefers-color-scheme: dark)");
if (int == 1) (darktheme.matches) ? dark(item) : light(item);
else if (int == 2) (darktheme.matches) ? light(item) : dark(item);
}
function assemble_tags(x)
{
const test = [];
base.forEach((item) => { for (const value of item['type']) test.push(value); });
tags = [...new Set(test)];
tags.sort();
for (const i of tags)
{
let tag_btn = document.createElement('button');
tag_btn.innerHTML = i;
document.getElementById("tags-block").append(tag_btn);
}
}
const get_tags = (x) =>
{
let main_ul = document.createElement('ul');
for (let item of x['type']) {
main_ul.append((function() {
let main_li = document.createElement('li');
main_li.innerHTML = item;
return main_li;
})());
}
return main_ul;
};
const get_links = (x) =>
{
let links_div = document.createElement("div");
links_div.setAttribute("id", "app-links");
const keys = Object.entries(x["url"]);
for (let i of keys)
{
let link = document.createElement("a");
link.setAttribute("href", i[1]);
link.innerHTML = i[0];
links_div.append(link);
}
return links_div;
};
const create_block = (divs) =>
{
for (let elm of divs)
{
let main_div = document.createElement("div");
let title = document.createElement("div");
let descr = document.createElement("p");
let links = document.createElement("div");
main_div.setAttribute("id", `post-${elm['id']}`);
title.innerHTML = elm["name"];
title.setAttribute("id", "app-title");
descr.innerHTML = elm["description"];
const tags_list = get_tags(elm);
const links_list = get_links(elm);
main_div.append(title);
main_div.append(descr);
main_div.append(links_list);
main_div.append(document.createElement("hr"));
main_div.append(tags_list);
document.getElementById("main-content-block").append(main_div);
}
}
function filter_tags(btn_txt)
{
let posts_have = [];
for (let item of base) { if (item['type'].includes(btn_txt) == true) posts_have.push(item); }
document.getElementById("main-content-block").innerHTML = "";
document.getElementById("load_more").style.visibility = "hidden";
create_block(posts_have);
let x = document.getElementById("main-content-block").children;
for (let i = 0; i < x.length; i++)
{
for (let item of x[i].children[4].children)
{
if (btn_txt == item.innerHTML) set_theme(item, 2);
else set_theme(item, 1);
}
}
}
function render_content()
{
const next_idx = page_count + 5;
const divs = base.slice(page_count, next_idx);
page_count = next_idx;
//main_divs = divs;
main_divs = main_divs.concat(divs);
create_block(divs);
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
document.getElementById("load_more").addEventListener("click", () => render_content());
const btn_event = () =>
{
const change_color = (curr_box) =>
{
document.querySelectorAll("#tags-block > button").forEach(box =>
{
if (curr_box == box) set_theme(box, 2);
else set_theme(box, 1);
});
};
document.querySelectorAll("#tags-block > button").forEach((box, idx) => {
box.addEventListener('click', () =>
{
if (!click_counts[idx]) click_counts[idx] = 0;
click_counts[idx]++;
if (click_counts[idx] === 2)
{
click_counts[idx] = 0;
set_theme(box, 1);
document.getElementById("main-content-block").innerHTML = "";
create_block(main_divs);
document.getElementById("load_more").style.visibility = 'visible';
return;
}
change_color(box);
filter_tags(box.innerHTML);
});
});
};