-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
502 lines (430 loc) · 14.6 KB
/
script.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//a cache of the downloaded episode list, for filtering
let allEpisodes;
let allShows = getAllShows();
let selectedShow;
function setup() {
document
.getElementById("searchInput")
.addEventListener("input", triggerEpisodeSearchFromGUI);
document.getElementById("showsSearchInput").addEventListener("input", triggerShowSearchFromGUI);
document.getElementById("listShowsLink").addEventListener("click", switchToShowsListing);
sortShowsByName(allShows);
switchToShowsListing();
registerSpeechRecognitionListener(processCommand);
console.log("started recognition tool");
}
function selectShowByName(showNameMaybe) {
const foundShow = allShows.find(candidate => candidate.name.toLowerCase() === showNameMaybe.toLowerCase());
if (foundShow) {
handleChosenShow(foundShow.id, foundShow.name);
}
}
function pick(arr) {
if (arr.length < 1) {
return undefined;
}
return arr[Math.floor(Math.random() * arr.length - 1)];
}
function selectRandomShow() {
const foundShow = pick(allShows);
handleChosenShow(foundShow.id, foundShow.name);
}
function selectEpisodeByNumber(seasonNumber, episodeNumber) {
const foundEpisode = allEpisodes.find(candidate => candidate.season === seasonNumber && candidate.number === episodeNumber);
if (foundEpisode) {
selectChosenEpisode(foundEpisode);
} else {
console.log("can't find episode s ${seasonNumber} e ${episodeNumber}")
}
}
function registerSpeechRecognitionListener(processPhraseCallback) {
//based on code demoed at this wes bos talk
//https://www.youtube.com/watch?v=pws4qzGn5ak
//Create the speech recognition object
window.SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.interimResults = true;
recognition.lang = "en-GB";
let p = document.createElement("p");
const words = document.querySelector(".words");
words.appendChild(p);
recognition.addEventListener("result", (e) => {
const transcript = Array.from(e.results)
.map((result) => result[0]) //just the highest confidence guess
.map((result) => result.transcript)
.join("");
p.textContent = transcript;
if (e.results[0].isFinal) {
processPhraseCallback(transcript);
//when exactly is isFinal set?
}
});
//only if we want to keep going after first phrase end is detected
recognition.addEventListener("end", recognition.start);
recognition.start();
}
const voiceCommands = [
{
title: "show everything",
regex: /show(?: me)? everything|show all shows/gi,
fn: (matches) => {
//show all shows (remove any filter)
console.log("Showing all shows...");
switchToShowsListing();
}
},
{
title: "show all episodes",
regex: /show(?: me)? all episodes/gi,
fn: (matches) => {
//show all episodes (remove any filter)
console.log("Would show all episodes");
showAllEpisodes();
}
},
{
title: "select a random show",
regex: /select a random show|show me anything/gi,
fn: (matches) => {
console.log("Showing a random show...");
selectRandomShow();
}
},
{
title: "Show season _ episode _",
regex: /season (\d+) episode (\d+)/gi,
fn: (matches) =>
//DO an episode change
selectEpisodeByNumber(Number(matches[1]), Number(matches[2]))
},
{
title: "Show season _",
regex: /show(?: me)? season (\d+)/gi,
fn: (matches) => {
//DO a filtering of episodes
console.log("Present all episodes in season number: " + matches[1]);
showSeason(Number(matches[1]));
},
},
{
title: "search episodes for _",
regex: /search episodes for (.*)/gi,
fn: (matches) =>
//DO a search
runEpisodeSearchForText(matches[1])
},
{
title: "search shows for _",
regex: /search shows for (.*)/gi,
fn: (matches) => {
//DO a search
switchToShowsListing();
runShowSearchForText(matches[1])
}
},
{
title: "Show _",
regex: /show(?: me)? (.*)/gi,
fn: (matches) => {
//pick that show if we can find it
console.log("Present the show called: " + matches[1]);
//TODO:
selectShowByName(matches[1]);
},
},
{
title: "Help|What can i say?",
regex: /what can i say|help/gi,
//display help dialog of what can be said
fn: (matches) =>
displayVoiceHelpDialog()
},
{
title: "dismiss|Hide the help",
regex: /dismiss|hide the help/gi,
//hide help dialog of what can be said
fn: (matches) =>
hideVoiceHelpDialog()
},
];
function displayVoiceHelpDialog() {
const dialog = document.getElementById("voiceCommandsHelpDialog");
dialog.hidden = false;
const listEl = dialog.querySelector("ul")
listEl.textContent = "";
voiceCommands.map((c) => {
const el = listEl.appendChild(document.createElement("li"))
el.textContent = c.title;
});
}
function hideVoiceHelpDialog() {
console.log("hiding voice command help dialog")
document.getElementById("voiceCommandsHelpDialog").hidden = true;
}
function processCommand(transcript) {
let els = document.querySelectorAll(".words");
els.forEach(el => el.textContent = "Cmd: " + transcript);
const matchedCmd = voiceCommands.find(
(cmd) => [...transcript.matchAll(cmd.regex)].length > 0
);
if (matchedCmd) {
let matches = [...transcript.matchAll(matchedCmd.regex)];
if (matches.length > 0) {
console.log("got a match. matches are: ", matches[0]);
console.log(matchedCmd.fn(matches[0]));
} else {
console.error(
"cmd was selected but then doesn't match. suspect code error."
);
}
} else {
("no command matches that");
}
}
function showAllEpisodes() {
//TODO: don't do this if we haven't selected a show
makePageForEpisodes(allEpisodes);
}
function switchToShowsListing() {
document.getElementById("showsPage").hidden = false;
document.getElementById("episodesPage").hidden = true;
makePageForShows(allShows);
}
function sortShowsByName(allShows) {
//assumes all shows have a name.
allShows.sort((a, b) => a.name.localeCompare(b.name));
}
function fetchEpisodesForShow(showId) {
fetch(`https://api.tvmaze.com/shows/${showId}/episodes`)
.then(resp => resp.json())
.then(handleEpisodesJSONResponse);
}
function handleEpisodesJSONResponse(json) {
//we cache the episode list locally for further filtering
allEpisodes = json;
makePageForEpisodes(allEpisodes);
}
function triggerEpisodeSearchFromGUI(event) {
const query = document.getElementById("searchInput").value;
runEpisodeSearchForText(query);
}
function runEpisodeSearchForText(soughtText) {
const filtered = allEpisodes.filter(episode =>
episodeMatchesQuery(episode, soughtText)
);
makePageForEpisodes(filtered);
}
function runShowSearchForText(soughtText) {
const filtered = allShows.filter(show =>
tvShowMatchesQuery(show, soughtText)
);
makePageForShows(filtered);
}
function showSeason(seasonNumber) {
//don't do this if we haven't a show selected.
const filtered = allEpisodes.filter(episode =>
episode.season === seasonNumber
);
makePageForEpisodes(filtered);
}
function triggerShowSearchFromGUI(event) {
const query = document.getElementById("showsSearchInput").value;
const filtered = allShows.filter(show =>
tvShowMatchesQuery(show, query)
);
makePageForShows(filtered);
}
function contains(inspectStr, targetStr) {
return inspectStr && targetStr && -1 !== inspectStr.toLowerCase().indexOf(targetStr.toLowerCase());
}
function episodeMatchesQuery(episode, query) {
return !query || contains(episode.name, query) || contains(episode.summary, query);
}
function tvShowMatchesQuery(show, query) {
return (
!query
|| contains(show.name, query)
|| show.genres.some(genre => contains(genre, query))
|| contains(show.summary, query)
);
}
function handleChosenEpisode(event) {
let opts = event.target.selectedOptions;
if (opts.length !== 1) {
return;
}
let id = opts[0].value;
selectChosenEpisodeById(Number(id));
}
function selectChosenEpisodeById(id) {
if (allEpisodes) {
const ep = allEpisodes.find(e => e.id === id);
if (ep) {
selectChosenEpisode(ep);
}
}
}
function selectChosenEpisode(episode) {
makePageForEpisodes([episode]);
}
function handleChosenShowFromSelect(event) {
let opts = event.target.selectedOptions;
if (opts.length !== 1) {
return;
}
let id = opts[0].value;
let showName = opts[0].dataset.title;
handleChosenShow(id, showName);
}
function handleChosenShow(id, showName) {
fetchEpisodesForShow(Number(id));
document.getElementById("showTitleHeader").textContent = showName;
document.getElementById("episodesPage").hidden = false;
document.getElementById("showsPage").hidden = true;
}
function makeShowSelector(shows) {
const selectElem = document.getElementById("showSelect");
selectElem.textContent = ""; //empty it
selectElem.onchange = handleChosenShowFromSelect;
shows.forEach(show => {
//e.g. <option value="82">Game Of Thrones</option>;
const optionElem = document.createElement("option");
optionElem.setAttribute("value", show.id);
optionElem.setAttribute("data-title", show.name);
optionElem.textContent = show.name;
selectElem.appendChild(optionElem);
});
}
function makeEpisodeSelector(episodes) {
const selectElem = document.getElementById("episodeSelect");
selectElem.textContent = ""; //empty it
selectElem.onchange = handleChosenEpisode;
episodes.forEach(episode => {
//<option value="S01E01">S01E01 Winter is Coming</option>;
const optionElem = document.createElement("option");
const code = makeEpisodeCode(episode);
optionElem.setAttribute("value", episode.id);
optionElem.textContent = `${code} - ${episode.name}`;
selectElem.appendChild(optionElem);
});
}
function pad(num) {
return num.toString().padStart(2, "0");
}
function makeEpisodeCode(episode) {
return `S${pad(episode.season)}E${pad(episode.number)}`;
}
function makePageForEpisodes(json) {
makeEpisodeSelector(json);
document.getElementById(
"countDisplay"
).textContent = `Displaying ${json.length}/${allEpisodes.length} episodes.`;
const container = document.getElementById("episodes");
container.textContent = ""; //wipe previous content
json.forEach(episode => {
const card = makeCardForEpisode(episode);
container.appendChild(card);
});
scrollToTop();
}
function makePageForShows(json) {
console.log("making page for shows");
makeShowSelector(json);
document.getElementById("filterSummary").textContent = json.length.toString();
const showsListElem = document.getElementById("showsList");
showsListElem.textContent = ""; //wipe previous content
json.forEach(show => {
const card = makeCardForShow(show);
showsListElem.appendChild(card);
});
scrollToTop();
}
function scrollToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
function makeCardForShow(show) {
//--- Containing... ---
//Title - linking to episodes view
//Image
//Summary
// (and in info panel)...
//Genre
//Rating
//status
const li = document.createElement("li");
li.classList.add("show");
const aWithTitle = appendNewChild(li, "a");
//aWithTitle.setAttribute("href", `https://www.tvmaze.com/shows/${show.id}`)
aWithTitle.addEventListener("click", () => handleChosenShow(show.id, show.name));
appendNewChild(aWithTitle, "h1").textContent = show.name;
const div3 = appendNewChild(li, "div");
div3.classList.add("three-panels");
const figure = appendNewChild(div3, "figure");
figure.classList.add("panel", "panel-one");
const img = appendNewChild(figure, "img");
img.setAttribute(
"src",
show.image ? show.image.medium : "https://placekitten.com/300/200"
);
const divMiddle = appendNewChild(div3, "div");
divMiddle.classList.add("panel", "panel-two");
divMiddle.textContent = stripTags(show.summary);
const divRight = appendNewChild(div3, "div");
divRight.classList.add("panel", "panel-three");
[{ title: "Rated", fn: show => show.rating.average },
{ title: "Genres", fn: show => show.genres.join(" | ") },
{ title: "Status", fn: show => show.status },
{ title: "Runtime", fn: show => show.runtime }
].forEach(info => {
const p = appendNewChild(divRight, "p");
const span = appendNewChild(p, "span");
span.classList.add("info-key");
span.textContent = info.title + ": ";
p.appendChild(document.createTextNode(info.fn(show)))
});
return li;
}
function appendNewChild(parent, elementType) {
return parent.appendChild(document.createElement(elementType));
}
function makeCardForEpisode(episode) {
//--- Containing... ---
//Title,
//Image
//Code (S02E07) and scroll-to-top link
//Summary (cleaned)
const code = makeEpisodeCode(episode);
const card = document.createElement("div");
card.classList.add("card");
card.setAttribute("id", code);
const h1 = document.createElement("h1");
h1.classList.add("episodeTitle");
h1.textContent = `${episode.name} - ${code}`; //` `;
const img = document.createElement("img");
img.setAttribute(
"src",
episode.image ? episode.image.medium : "https://placekitten.com/300/200"
);
const p = document.createElement("p");
p.textContent = stripTags(episode.summary);
card.appendChild(h1);
card.appendChild(img);
card.appendChild(p);
return card;
}
//Remove tags by replacing the matched expression with an empty string.
//This function uses a regular expression. It is NOT important to learn these on the course.
function stripTags(str) {
if (!str) {
return str;
}
//regex components:
// <
// / (optional)
// a sequence of at least one alphabet character (case insensitive)
// >
return str.replace(/<\/?[a-z]+>/gi, "");
}
window.onload = setup;