-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
109 lines (98 loc) · 2.83 KB
/
scripts.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
$(document).ready(function() {
$("#search").on("keyup", function() {
search();
});
});
var tags = new Set();
var years = new Set();
const options = {
includeScore: true,
minMatchCharLength: 2,
threshold: 0.0,
useExtendedSearch: true,
ignoreLocation: true,
keys: [
"title",
"description",
"year",
"tags"
]
};
var fuse = null;
$.getJSON( "talks.json", function(talks) {
fuse = new Fuse(talks, options);
for (let i = 0; i < talks.length; i++) {
let talk = talks[i];
years.add(talk.year);
for (let j = 0; j < talk.tags.length; j++) {
let tag = talk.tags[j];
tags.add(tag);
}
}
$("#years").html(selectors(years));
$("#tags").html(selectors(tags));
});
function selectors(items) {
var out = "";
items.forEach(item => {
out += "<input type='checkbox' id='" + item + "' name='" + item + "' onchange='search()'><label for='" + item + "'>" + item + "</label>";
$("input[name=" + item + "]").change(function() { search(); });
});
return out;
}
function formatResults(talks) {
if (talks.length === 0) {
return "";
}
var out = "<span>Found " + talks.length + " talks!</span>";
out += "<ul>";
for (let i = 0; i < talks.length; i++) {
out += formatTalk(talks[i].item);
}
out += "</ul>";
return out;
}
function formatTalk(talk) {
return `<li class=\"talk\">
<h3><a href="${talk.link}" target="_blank">${talk.title}</a></h3>
<div>${talk.description}</div>
<div><b>Event:</b> ${talk.event} - ${talk.year}</div>
<div><b>Speakers:</b> ${formatSpeakers(talk.speakers)}</div>
<div><b>Tags:</b> ${talk.tags.join(", ")}</div>
</li>`;
}
function formatSpeakers(speakers) {
var list = [];
for (let i = 0; i < speakers.length; i++) {
speaker = speakers[i];
list.push(speaker.name + " - " + speaker.company);
}
return list.join(", ");
}
function search() {
var conditions = [];
let value = $("#search").val();
if (value.length !== 0) {
conditions.push({ $or: [ { title: value }, { description: value } ] });
}
let selectedYears = []
$("#years>input").each(function() {
if ($(this).is(':checked')) {
selectedYears.push({ year: "'" + $(this).attr("id")})
}
});
if (selectedYears.length !== 0) {
conditions.push({ $or: selectedYears });
}
let selectedTags = []
$("#tags>input").each(function() {
if ($(this).is(':checked')) {
selectedTags.push({ tags: "'" + $(this).attr("id")})
}
});
if (selectedTags.length !== 0) {
conditions.push({ $and: selectedTags });
}
let results = fuse.search({ $and: conditions });
$("#results").html(formatResults(results));
}