-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.js
250 lines (181 loc) · 6.28 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
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
function sanatise (unsanatisedInput) {
const tempEl = document.createElement('div')
tempEl.innerText = unsanatisedInput
const sanatisedOutput = tempEl.innerHTML
return sanatisedOutput
}
class Card {
constructor (data, id) {
this._id = id + 1
this._title = sanatise(data.title)
this._description = sanatise(data.description)
this._link = sanatise(data.link)
this._image = sanatise(data.image)
}
render (outputContainer) {
const template = `
<div id="theme-${this._id}" class="card">
<header>
<h3 class="theme-title">${this._title}</h3>
<a href="${this._link}">
<i class="fas fa-chevron-circle-down"></i>
</a>
</header>
<div class="meta">
<a href="${this._link}" tabindex="-1">
<img src="${this._image}">
<p class="description">${this._description}</p>
</a>
</div>
<div class="button-wrapper">
<button class="btn btn-lightbox" type="button" onClick="createLightbox(${this._id})"><i class="fas fa-search-plus"></i> Enlarge</button>
<a href="${this._link}" class="btn btn-download"><i class="fas fa-file-download"></i> Download</a>
</div>
</div>
`
outputContainer.insertAdjacentHTML('beforeend', template)
}
}
const removeLightbox = () => document.body.getElementsById('lightbox').remove()
function createLightbox (id) {
const card = document.getElementById(`theme-${id}`)
const themeTitle = card.querySelector('h3')
const img = card.querySelector('img')
const template = `
<div id="lightbox" onclick="this.remove()">
<h2>${themeTitle.innerText}</h2>
<img src="${img.src}">
<button type="button" class="btn btn-close-lightbox" onClick="removeLightbox"><i class="fas fa-times-circle"></i> Close</button>
</div>
`
card.insertAdjacentHTML('afterend', template)
}
(() => { // IIFE to avoid globals
/* SEARCH Parameter Handling
* ======================
*/
const search = /** @type {HTMLInputElement} */ (document.getElementById('searchInput'))
search.addEventListener('keydown', e => {
if (e.key === "Enter")
sort(localStorage.sort, search.value)
})
const search_button = /** @type {HTMLInputElement} */ (document.getElementById('searchButton'))
search_button.addEventListener('click', () => sort(localStorage.sort, search.value))
/* Load Content
* ============
*/
/*
* If sorting is not set yet in `localStorage`,
* then use as default `latest` kind.
*/
if (!localStorage.sort)
localStorage.sort = 'latest'
/*
* Add event to sort when an option is chosen..
*/
const sort_menu = /** @type {HTMLSelectElement} */ (document.getElementById('js-sort-menu'))
sort_menu.addEventListener('change', () => {
const name = /** @type {string} */ (sort_menu.selectedOptions[0].getAttribute('name'))
sort(name)
})
sort(localStorage.sort)
const current_option = sort_menu.options.namedItem(localStorage.sort)
if (current_option)
current_option.selected = true
/**
* Toggle the sorting type of the themes.
*
* @param {string} kind How to sort the themes.
* @param {string=} filter Term to filter the themes.
**/
function sort (kind, filter) {
localStorage.sort = kind
// Remove all themes cards from the page.
const cards_container = document.getElementById('themes_container')
if (cards_container)
cards_container.innerHTML = ''
fetch('themes.json')
.then(data => data.json())
.then(async data => {
data = Object.entries(data)
if (filter) {
/**
* Match any substring (partial) from a string (text).
* @param {string} text
* @param {string} partial
*/
function matches (text, partial) {
return text.toLowerCase().indexOf(partial.toLowerCase()) > -1
}
data = data.filter(element => matches(`${element[1].title}, ${element[1].tags}`, search.value))
}
switch (localStorage.sort) {
/*
* Sort from the most recent theme added.
*/
case 'latest':
data.reverse()
break
/*
* Ascending sorting of stars from repositories.
*/
case 'updated':
// item1.attr.localeCompare(item2.attr);
data.sort((a, b) => b[1].pushed_at.localeCompare(a[1].pushed_at))
break
/*
* Ascending sorting of stars from repositories.
*/
case 'stars':
data.sort((a, b) => b[1].stargazers_count - a[1].stargazers_count)
break
/*
* Randomly sorting of themes.
*/
case 'random':
for (let i = data.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[data[i], data[j]] = [data[j], data[i]]
}
break
/*
* Sort from the least recent theme added (oldest).
* Since it's sorted like this by default from the file, do nothing.
*/
default:
}
for (const [index, entry] of data)
{
const card = new Card(entry, index)
card.render(outputContainer)
await new Promise(r => setTimeout(r, 666));
}
})
}
// add themes
const outputContainer = document.getElementById('themes_container')
/* Theme Handling
* ==============
*/
const systemPref = window.matchMedia("(prefers-color-scheme: dark)").matches ? 'night' : 'day',
themeTrigger = document.getElementById('js-themeSwitcher'),
themeTriggerIcon = themeTrigger.querySelector('i')
// when local storage is not populated set the system preferrence as value
if (!localStorage['theme']) localStorage['theme'] = systemPref === 'day' ? 'day' : 'night'
// set nightmode when according to local storage
if (localStorage['theme'] === 'night') {
themeTriggerIcon.classList.toggle('fa-sun')
themeTriggerIcon.classList.toggle('fa-moon')
document.documentElement.classList.add('nightmode')
} else { document.documentElement.classList.add('daymode') }
function toggleTheme () {
document.documentElement.classList.toggle('nightmode')
document.documentElement.classList.toggle('daymode')
themeTriggerIcon.classList.toggle('fa-sun')
themeTriggerIcon.classList.toggle('fa-moon')
// update local storage
if (localStorage['theme'] === 'night') localStorage['theme'] = 'day'
else localStorage['theme'] = 'night'
}
themeTrigger.addEventListener('click', () => toggleTheme())
})()