forked from subinamehta/training-material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search2.html
182 lines (159 loc) · 5.43 KB
/
search2.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
---
layout: page
title: Search GTN Materials
---
<p>
We are testing a new search experience. It does not do semantic ranking of results, but it's maybe a nicer and faster interface. In the future we might bring back the ranking of results. If you wish to access the old search interface, <a href="{{ site.baseurl}}/search">it will remain available here</a>.
</p>
<p>Tell us what you think!
</p>
<div class="row" id="search-form">
<div class="col-md-5">
<div class="form-group">
<label for="title">Search</label>
<input type="text" class="form-control" id="title" placeholder="Search Materials" oninput="onInputDebounced()">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="type">Type</label>
<select class="form-control" id="type">
<option value="Any">Any</option>
<option value="Hands-on">Hands-on</option>
<option value="Slides">Slides</option>
<option value="FAQs">FAQs</option>
<option value="News">News</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="level">Level</label>
<select class="form-control" id="level">
<option value="Any">Any</option>
<option value="Introductory">Introductory</option>
<option value="Intermediate">Intermediate</option>
<option value="Advanced">Advanced</option>
</select>
</div>
</div>
<div class="col-md-1">
<button class="btn btn-primary" onclick="search()">Search</button>
</div>
</div>
<table class="table" id="results">
<thead>
<tr>
<th scope="col">Identifier</th>
<th scope="col">Type</th>
<th scope="col">Title</th>
<th scope="col">Topic</th>
<th scope="col">Level</th>
<th scope="col">Length</th>
</tr>
</thead>
<tbody>
{%- for post in site.posts -%}
<tr id="{{ post.short_id }}">
<td><a class="badge badge-info" href="https://gxy.io/GTN:{{ post.short_id }}">{%- icon purl -%} GTN:{{ post.short_id }}</a></td>
<td>{{ post.layout | layout_to_human }}</td>
<td><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a><br/>
{%- for tag in post.tags -%}
<span class="label label-default tutorial_tag" style="{{ tag | colour_tag }}">{{ tag }}</span>
{%- endfor -%}
</td>
<td></td>
<td>{{ post.level }}</td>
<td>{{ post.time_estimation }}</td>
</tr>
{%- endfor -%}
{%- for page in site.pages -%}
{%- if page.short_id -%}
{%- if page.short_id != 'F00001' -%}
<tr id="{{ page.short_id }}">
<td><a class="badge badge-info" href="https://gxy.io/GTN:{{ page.short_id }}">{%- icon purl -%} GTN:{{ page.short_id }}</a></td>
<td>{{ page.layout | layout_to_human }}</td>
<td><a href="{{ site.baseurl }}{{ page.url }}">{{ page.title }}</a><br/>
{%- for tag in page.tags -%}
<span class="label label-default tutorial_tag" style="{{ tag | colour_tag }}">{{ tag }}</span>
{%- endfor -%}
</td>
<td>{{ page | topic_name_from_page: site }}</td>
<td>{{ page.level }}</td>
<td>{{ page.time_estimation }}</td>
</tr>
{%- endif -%}
{%- endif -%}
{%- endfor -%}
</tbody>
</table>
<script>
HTMLCollection.prototype.forEach = Array.prototype.forEach;
NodeList.prototype.forEach = Array.prototype.forEach;
HTMLCollection.prototype.filter = Array.prototype.filter;
NodeList.prototype.filter = Array.prototype.filter;
HTMLCollection.prototype.map = Array.prototype.map;
NodeList.prototype.map = Array.prototype.map;
const textInput = document.getElementById("title")
const otherInputs = document.querySelectorAll("#search-form select")
const table = document.getElementById('results'),
rows = table.tBodies[0].rows;
const DEBOUNCE_DURATION = 300; // in milliseconds
var previousSearch = "";
function search(){
const textQuery = document.getElementById("title").value
const typeFilter = document.getElementById("type").value
const levelFilter = document.getElementById("level").value
if (previousSearch == `${textQuery} ${typeFilter} ${levelFilter}`) {
console.log("Skipping search for the same query")
return
}
console.log(`Searching! ${textQuery} ${typeFilter} ${levelFilter}`)
// Which should be hidden
var to_hide = rows.filter(row => {
if (typeFilter != "Any" && row.children[1].innerText != typeFilter) {
return true;
}
if (levelFilter != "Any" && row.children[4].innerText != levelFilter) {
return true;
}
if (textQuery != "" && (row.children[2].innerText.toLowerCase() + " " + row.children[2].innerText.toLowerCase()).indexOf(textQuery.toLowerCase()) == -1) {
return true;
}
return false;
});
// Display all
document.querySelectorAll("#results tr").forEach(r => r.style.display = "");
// Hide the rest
to_hide.map(r => r.style.display = "none");
previousSearch = `${textQuery} ${typeFilter} ${levelFilter}`
}
const onInputDebounced = debounce(_ => {
console.log('DEBOUNCED');
search();
}, DEBOUNCE_DURATION);
// Attribution: https://gist.github.com/beaucharman/1f93fdd7c72860736643d1ab274fee1a
function debounce(callback, wait, context = this) {
let timeout = null;
let callbackArgs = null;
const later = () => callback.apply(context, callbackArgs);
return function() {
callbackArgs = arguments;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
otherInputs.forEach((input) => {
addEventListener("change", (event) => {
console.log(`Change! ${event}`)
search()
})
});
// Get the q parameter from URL
const params = (new URL(document.location)).searchParams;
paramSearch = params.get('query')
if (paramSearch) {
textInput.value = paramSearch
search()
}
</script>