Skip to content

Commit d8e4428

Browse files
committed
basic searchbar functionality (it can't search dates); some documentation in the postSys.js file
1 parent 9e79c15 commit d8e4428

File tree

9 files changed

+130
-33
lines changed

9 files changed

+130
-33
lines changed

blog.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
<h2>Blog</h2>
1717
<ul id="blog-feed"></ul>
1818
</section>
19-
<script src="./scripts/postSys.js"></script>
20-
<script src="./scripts/blog.js"></script>
19+
<!--Loader Script for the Header -->
20+
<script src="scripts/load-header.js" defer></script>
21+
22+
<script src="./scripts/postSys.js"></script>
23+
<script src="./scripts/blog.js"></script>
24+
<script>
25+
document.addEventListener("DOMContentLoaded", function() {
26+
debugTestBlog(1); // or whatever number matches your file
27+
});
28+
</script>
2129

22-
<script>
23-
document.addEventListener("DOMContentLoaded", function() {
24-
debugTestBlog(1); // or whatever number matches your file
25-
});
26-
</script>
27-
<!--Loader Script for the Header -->
28-
<script src="scripts/load-header.js" defer></script>
2930
</body>
3031
</html>

events.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
<h2>Events</h2>
1717
<ul id="events-feed"></ul>
1818
</section>
19-
<script src="./scripts/postSys.js"></script>
20-
<script src="./scripts/events.js"></script>
21-
<script>
22-
document.addEventListener("DOMContentLoaded", function() {
23-
debugTestEventsPost(1); // or whatever file number exists
24-
});
25-
</script>
26-
<!--Loader Script for the Header -->
27-
<script src="scripts/load-header.js" defer></script>
19+
20+
<!--Loader Script for the Header -->
21+
<script src="scripts/load-header.js" defer></script>
22+
<script src="./scripts/postSys.js"></script>
23+
<script src="./scripts/events.js"></script>
24+
<script>
25+
document.addEventListener("DOMContentLoaded", function() {
26+
debugTestEventsPost(1); // or whatever file number exists
27+
});
28+
</script>
29+
2830
</body>
2931
</html>

header.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
</div>
1818

1919
<!-- This form submits to the backend, replace with /search -->
20-
<form onSubmit=search($event)>
21-
<input type="text" name="q" placeholder="Search..." required>
22-
<button type="submit">Go</button>
20+
<form id="searchForm">
21+
<input type="text" id="searchbar" name="q" placeholder="Search..." required>
22+
<button id="searchSubmit" type="submit">Go</button>
2323
</form>
2424
</nav>
2525
<hr>

news.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ <h1>Announcements</h1>
2222
<h1>News</h1>
2323
<ul id="news-feed"></ul>
2424
</section>
25+
<!--Loader Script for the Header -->
26+
<script src="scripts/load-header.js"></script>
2527

26-
<script src="./scripts/postSys.js"></script>
27-
<script src="./scripts/announcements.js"></script>
28-
<script>
29-
document.addEventListener("DOMContentLoaded", function() {
30-
debugTestAnnouncements(2); // or 2, 3, etc., depending on your file
31-
});
32-
</script>
33-
<!--Loader Script for the Header -->
34-
<script src="scripts/load-header.js" defer></script>
28+
<script src="./scripts/postSys.js"></script>
29+
<script src="./scripts/announcements.js"></script>
30+
<script>
31+
document.addEventListener("DOMContentLoaded", function() {
32+
debugTestAnnouncements(2); // or 2, 3, etc., depending on your file
33+
});
34+
</script>
3535
</body>
3636
</html>

scripts/announcements.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,26 @@ function debugTestAnnouncements(idx){
3434
.catch(error => {
3535
console.error('Error fetching JSON:', error);
3636
});
37-
}
37+
}
38+
39+
40+
window.GO_HeaderLoaded.pushSubscribers([
41+
() => {
42+
let searchForm = document.getElementById('searchForm');
43+
let searchbar = document.getElementById('searchbar');
44+
searchForm.addEventListener('submit', (event) => {
45+
event.preventDefault();
46+
47+
let searchIn = searchbar.value;
48+
let searchQuery = new GO_SearchQuery();
49+
searchQuery.titleRegex = new RegExp(searchIn, 'g');
50+
searchQuery.contentRegex = new RegExp(searchIn, 'g');
51+
52+
announcementSys.searchQuery = searchQuery;
53+
announcementSys.refreshPostsOntoHTML();
54+
55+
newsSys.searchQuery = searchQuery;
56+
newsSys.refreshPostsOntoHTML();
57+
})
58+
}
59+
]);

scripts/events.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ function debugTestEventsPost(idx){
5353
console.error('Error fetching JSON:', error);
5454
});
5555
}
56+
57+
window.GO_HeaderLoaded.pushSubscribers([
58+
() => {
59+
let searchForm = document.getElementById('searchForm');
60+
let searchbar = document.getElementById('searchbar');
61+
searchForm.addEventListener('submit', (event) => {
62+
event.preventDefault();
63+
64+
let searchIn = searchbar.value;
65+
let searchQuery = new GO_SearchQuery();
66+
searchQuery.titleRegex = new RegExp(searchIn, 'g');
67+
searchQuery.contentRegex = new RegExp(searchIn, 'g');
68+
69+
eventSys.searchQuery = searchQuery;
70+
eventSys.refreshPostsOntoHTML();
71+
})
72+
}
73+
]);

scripts/hiking.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ function debugTestHikingTrails(idx){
5252
console.error('Error fetching JSON:', error);
5353
});
5454
}
55+
window.GO_HeaderLoaded.pushSubscribers([
56+
() => {
57+
let searchForm = document.getElementById('searchForm');
58+
let searchbar = document.getElementById('searchbar');
59+
searchForm.addEventListener('submit', (event) => {
60+
event.preventDefault();
61+
62+
let searchIn = searchbar.value;
63+
let searchQuery = new GO_SearchQuery();
64+
searchQuery.titleRegex = new RegExp(searchIn, 'g');
65+
searchQuery.contentRegex = new RegExp(searchIn, 'g');
66+
67+
eventSys.searchQuery = searchQuery;
68+
eventSys.refreshPostsOntoHTML();
69+
})
70+
}
71+
]);

scripts/load-header.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
class GO_HeaderLoaded{
2+
subscribers = [];
3+
constructor(){
4+
this.subscribers = [];
5+
}
6+
7+
pushSubscribers(subscribers){
8+
for(let subscriber of subscribers){
9+
this.subscribers.push(subscriber);
10+
}
11+
}
12+
13+
notifySubscribers(){
14+
for(let subscriber of this.subscribers){
15+
subscriber();
16+
}
17+
}
18+
}
19+
20+
let headerLoadedEventManager = new GO_HeaderLoaded();
21+
window.GO_HeaderLoaded = headerLoadedEventManager;
22+
123
// js/load-header.js
224
fetch('./header.txt')
325
.then(r => r.text())
426
.then(txt => {
527
let header = document.getElementById('site-header');
628
header.innerHTML = txt;
7-
});
29+
headerLoadedEventManager.notifySubscribers();
30+
});

scripts/postSys.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ class GO_PostContent {
5757
}
5858
}
5959

60+
// builder interprets a json and builds a model of the post
6061
class GO_PostBuilder {
62+
// accepts a json obj and parses a post (main method to override for a custom builder with custom json)
6163
fromJsonObject(jsonObject) {
6264
return new GO_PostContent(jsonObject.title, jsonObject.timestamp, jsonObject.content);
6365
}
6466

67+
// accepts a json string and parses a post
6568
fromJsonString(jsonString) {
6669
return this.fromJsonObject(JSON.parse(jsonString));
6770
}
6871
}
6972

73+
// a class to represent all the loaded posts and what the class should look for when parsing a json object to get the array of posts
7074
class GO_PostModel {
7175
loadedPosts = []
7276
jsonArrayVarName = "";
@@ -100,6 +104,7 @@ class GO_PostModel {
100104
}
101105
}
102106

107+
// a class to control where the post is shown in the HTML
103108
class GO_PostView {
104109
htmlIDToLookup = "";
105110

@@ -117,6 +122,7 @@ class GO_PostView {
117122
}
118123
}
119124

125+
// a class to store a date interval
120126
class GO_DateRange {
121127
_beginDate;
122128
_endDate;
@@ -162,6 +168,7 @@ class GO_DateRange {
162168
}
163169
}
164170

171+
// a class to store the regex required to complete a search
165172
class GO_SearchQuery {
166173
titleRegex = new RegExp('', 'g');
167174
timestampFilter = new GO_DateRange(new Date(), new Date());
@@ -180,6 +187,7 @@ class GO_SearchQuery {
180187
this.titleRegex = regex;
181188
}
182189

190+
// scoring function attributes a value to each post that is based on the number of matches within the post
183191
scoringFunc(post){
184192
// get title matches based on regex
185193
let titleRegexResult = post.title.matchAll(this.titleRegex);
@@ -206,6 +214,7 @@ class GO_SearchQuery {
206214
return [titleScore, contentScore, timestampScore[0], timestampScore[1]];
207215
}
208216

217+
// filter out posts that do and don't match the criterion
209218
filterFunc(post){
210219
let scores = this.scoringFunc(post);
211220
let titleResult = this.targetTitle ? scores[0] > 0 : true;
@@ -214,6 +223,7 @@ class GO_SearchQuery {
214223
return titleResult && contentResult && timestampResult;
215224
}
216225

226+
// sorting function to dictate order of which posts are shown in via score value
217227
compareFunc(postA, postB){
218228
let scoreA = this.scoringFunc(postA);
219229
let scoreB = this.scoringFunc(postB);
@@ -226,6 +236,7 @@ class GO_SearchQuery {
226236
}
227237
}
228238

239+
// a class that acts as the controller for the model and view components (resulting in the MVC pattern)
229240
class GO_PostSystem {
230241
postModel = new GO_PostModel();
231242
postBuilder = new GO_PostBuilder();
@@ -253,23 +264,26 @@ class GO_PostSystem {
253264
this.postModel.removePoseOnIndices(indices);
254265
}
255266

267+
// filters and then sorts the intermediate posts (that should be rendered)
256268
filterPosts(searchQuery){
257269
this.intermediatePosts = this.postModel.loadedPosts.filter(post => searchQuery.filterFunc(post));
258270
this.intermediatePosts.sort((a, b) => this.searchQuery.compareFunc(a, b));
259271
}
260272

273+
// render intermediate posts unto the page dictated by the view
261274
refreshPostsOntoHTML(){
262275
this.filterPosts(this.searchQuery)
263276
this.postView.pasteOntoHTML(this.intermediatePosts);
264277
}
265278

279+
// wrapper method for loading the model from a json obj
266280
loadFromJson(jsonObj){
267281
this.postModel.loadFromJson(jsonObj);
268282
}
269283
}
270284

271285

272-
//export {PostContent, PostBuilder, PostModel, PostView, PostSystem};
286+
// bind the classes declared here to the window so they're in global scope instead of being file-specific
273287
window.GO_PostContent = GO_PostContent;
274288
window.GO_PostBuilder = GO_PostBuilder;
275289

0 commit comments

Comments
 (0)