-
Notifications
You must be signed in to change notification settings - Fork 3
/
blog.html
218 lines (182 loc) · 6.93 KB
/
blog.html
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
---
layout: default
title: navigation_headlines.blog
---
<section class="section is-fullheight">
{% include title-h2.html text="Blog & News" %}
<!-- Search bar -->
<div class="columns">
<div class="column">
<!-- Category labels - css class "category-button" is required for highlighting active categories -->
<div class="buttons is-fullwidth">
<button
class="button is-info category-button"
onclick="filterUsingCategory('Alle Artikel')"
>Alle Artikel</button>
{% for category in site.categories %}
<button
class="button is-info is-light category-button"
onclick="filterUsingCategory('{{ category | first }}')"
>{{ category | first }}</button>
{% endfor %}
</div>
</div>
<div class="column">
<div id="blog-search-field" class="field is-pulled-right">
<p class="control has-icons-left has-icons-right">
<input
class="input"
id="searchInput"
type="text"
placeholder="Suche"
oninput="filterPosts(this.value)"
>
<img class="icon is-left search-icon ml-3" src="/assets/css/fontawesome-free-svgs/solid/search.svg">
<span class="icon is-right pointer" onclick="clearSearch()">
<img class="pointer search-icon ml-3" src="/assets/css/fontawesome-free-svgs/solid/times.svg">
</span>
</p>
</div>
</div>
</div>
<!-- Post-grid -->
<div class="columns is-multiline my-3 mb-6 padding-left-blog-list">
{% assign id = 0 %}
{% for post in site.posts %}
{% assign id = id | plus:1 %}
<div
id="postId-{{id}}"
class="column is-half-tablet is-one-third-widescreen blog-post"
name="post-{{id}}"
>
{% if post.hidden != null and post.hidden != true %}
{% include blog-post-tile.html %}
{% endif %}
</div>
{% endfor %}
</div>
</section>
<!-- Can be improved even more (probably):
https://esthermakes.tech/blog/2015/03/18/filtering-posts-by-tags-with-jekyll/
-->
<!-- Shameless copy+pasting from
https://langrsoft.com/2020/03/26/filtering-blog-posts-by-category-with-jekyll/
-->
<script>
function filterUsingCategory(selectedCategory) {
const unmatchedPosts = document.getElementsByClassName("hidden");
for (let index = 0; index < unmatchedPosts.length; index++) {
unmatchedPosts[index].classList.remove("hidden");
}
const categoryButtons = document.getElementsByClassName("category-button");
for(let i = 0; i < categoryButtons.length; i++) {
// console.log(categoryButtons[i].textContent);
if(selectedCategory && selectedCategory === categoryButtons[i].textContent) {
categoryButtons[i].classList.remove('is-light');
} else {
categoryButtons[i].classList.add('is-light');
}
}
// Changing the 'var's to 'let' or 'const' breaks the function
var id = 0;
{% for post in site.posts %}
var categories = {{ post.categories | jsonify }};
var newId = ++id;
var blogPost = document.getElementById("postId-" + newId);
if(selectedCategory == 'Alle Artikel' || categories.includes(selectedCategory)) {
blogPost.classList.remove('hidden');
} else {
blogPost.classList.add('hidden');
}
{% endfor %}}
// Related to
// https://www.impressivewebs.com/animate-display-block-none/
// It would be nice to have fade-in/ -out animations for the categories
let box = document.getElementById('box'),
btn = document.querySelector('button');
btn.addEventListener('click', function () {
if(box) {
if (box.classList.contains('hidden')) {
box.classList.remove('hidden');
setTimeout(function () {
box.classList.remove('visuallyhidden');
}, 20);
} else {
box.classList.add('visuallyhidden');
box.addEventListener('transitionend', function(e) {
box.classList.add('hidden');
}, {
capture: false,
once: true,
passive: false
});
}
}
}, false);
</script>
<script>
function filterPosts(rawSearchQuery) {
markFirstCategoryAsActive();
const unmatchedPosts = document.getElementsByClassName("hidden");
for (let index = 0; index < unmatchedPosts.length; index++) {
unmatchedPosts[index].classList.remove("hidden");
}
const searchQuery = rawSearchQuery.toLowerCase().trim();
// console.log(searchQuery);
displayAllPostsIfQueryIsEmpty(searchQuery);
const allPosts = document.getElementsByClassName("blog-post");
for (let index = 0; index < allPosts.length; index++) {
if(allPosts[index].firstElementChild) {
const titleOfPost = allPosts[index].firstElementChild.innerText.toLowerCase();
if(!titleOfPost.includes(searchQuery)) {
allPosts[index].classList.add("hidden");
}
}
}
displayAllPostsIfQueryIsEmpty(searchQuery);
}
function markFirstCategoryAsActive() {
const categoryButtons = document.getElementsByClassName("category-button");
for(let i = 1; i < categoryButtons.length; i++) {
categoryButtons[i].classList.add('is-light');
}
categoryButtons[0].classList.remove('is-light');
}
function displayAllPostsIfQueryIsEmpty(searchQuery) {
if(searchQuery === "") {
const unmatchedPosts = document.getElementsByClassName("hidden");
for (let index = 0; index < unmatchedPosts.length; index++) {
unmatchedPosts[index].classList.remove("hidden");
}
}
}
function clearSearch() {
const searchInput = document.getElementById("searchInput");
searchInput.value = "";
const hiddenPosts = document.querySelectorAll(".hidden");
hiddenPosts.forEach(el => {
el.classList.remove("hidden");
});
}
</script>
<style>
.hidden {
display: none;
}
.search-icon {
width: 1rem !important;
border: none;
/* The color attribute doesn't seem to apply in the case of svg.
Opacity seems to be easier than using the filter attribute
https://stackoverflow.com/questions/22252472/
*/
opacity: 0.65;
}
.is-fullwidth {
width: 100%;
}
.pointer {
cursor: pointer;
pointer-events: auto;
}
</style>