-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·170 lines (139 loc) · 5.31 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
/**
* The main js file has links the page and the components.
*
*/
//Search component
import { SearchContent } from './SearchUI/Search.js';
import { SuggestContent } from './SearchUI/Suggest.js';
const searchBtn = document.getElementById('search-btn');
const searchInput = document.getElementById('search-input');
const searchSuggestions = document.getElementById('search-suggestions');
var tablesJsonPath = 'jsonFiles/students.json';
searchBtn.addEventListener('click', () => {
let keyword = searchInput.value;
if (!keyword) {
alert("Please enter a keyword");
} else {
SearchContent(keyword, tablesJsonPath);
searchSuggestions.innerHTML = '';
}
});
// Add an event listener for the Enter key press on the input field
searchInput.addEventListener('keyup', (event) => {
if (event.key === "Enter") {
// Prevent the default form submission
event.preventDefault();
// Trigger the search when the Enter key is pressed
let keyword = searchInput.value;
if (!keyword) {
alert("Please enter a keyword");
} else {
SearchContent(keyword, tablesJsonPath);
searchSuggestions.innerHTML = '';
}
}
});
searchInput.addEventListener('input', function() {
const keyword = this.value.trim();
SuggestContent(keyword, tablesJsonPath);
});
// Create links to all the seach funtions on the website
const searchGrades = document.getElementById('search-grades');
const searchStudents = document.getElementById('search-students');
const searchCourses = document.getElementById('search-courses');
const searchTeachers = document.getElementById('search-teachers');
const searchClasses = document.getElementById('search-classes');
const searchTitle = document.getElementById('search-title');
const searcView = document.getElementById('search-view');
var LoadTitle = document.getElementById('load-title');
const Load = document.getElementById('loading-container');
function showLoadingSpinner(title, jsonPath) {
Load.classList.add('showLoad');
LoadTitle.innerHTML = title;
searchTitle.innerHTML = title;
searchInput.placeholder = title;
setTimeout(function() {
Load.classList.remove('showLoad');
}, 2000);
tablesJsonPath = jsonPath;
searcView.innerHTML = '';
const keyword = searchInput.value.trim();
if(keyword){
SuggestContent(keyword, tablesJsonPath);
}else {
searchSuggestions.innerHTML = '';
}
CreateFilters();
}
// Add event listeners for the remaining search links
searchGrades.addEventListener('click', function(event) {
event.preventDefault();
const jsonPath = 'jsonFiles/grades.json';
showLoadingSpinner(this.title, jsonPath);
});
searchStudents.addEventListener('click', function(event) {
event.preventDefault();
const jsonPath = 'jsonFiles/students.json';
showLoadingSpinner(this.title, jsonPath);
});
searchCourses.addEventListener('click', function(event) {
event.preventDefault();
const jsonPath = 'jsonFiles/courses.json';
showLoadingSpinner(this.title, jsonPath);
});
searchTeachers.addEventListener('click', function(event) {
event.preventDefault();
const jsonPath = 'jsonFiles/teachers.json';
showLoadingSpinner(this.title, jsonPath);
});
searchClasses.addEventListener('click', function(event) {
event.preventDefault();
const jsonPath = 'jsonFiles/classes.json';
showLoadingSpinner(this.title, jsonPath);
});
// Add search filters:
const filterButton = document.getElementById('filter-btn');
const searchFilters = document.getElementById('search-filters');
// Handle focus events to show the "search-filters" div
filterButton.addEventListener('click', function() {
searchFilters.classList.toggle('show-filters');
});
// Handle blur events to hide the "search-filters" div
searchFilters.addEventListener(!'focus', function() {
searchFilters.classList.remove('show-filters');
console.log("Ignored filters")
});
document.addEventListener('DOMContentLoaded', CreateFilters());
function CreateFilters() {
fetch(tablesJsonPath)
.then(response => response.json())
.then(data => {
const tables = data.tables;
const allColumns = Object.keys(tables).reduce((columns, table) => {
return columns.concat(tables[table]);
}, []);
searchFilters.innerHTML = '';
// Create filters inside div elements
allColumns.forEach(column => {
// Create a div for each filter
const filterDiv = document.createElement('div');
filterDiv.className = 'filter'; // Add a CSS class for styling
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = 'columns';
checkbox.value = column;
// Set the 'checked' attribute to true to make the checkbox checked by default
checkbox.checked = true;
const label = document.createElement('label');
label.textContent = column;
// Append the checkbox and label to the filter div
filterDiv.appendChild(checkbox);
filterDiv.appendChild(label);
// Append the filter div to the container
searchFilters.appendChild(filterDiv);
});
})
.catch(error => {
console.error('Error reading JSON file:', error);
});
}