-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
315 lines (256 loc) · 10.8 KB
/
index.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
window.addEventListener('load', async () => {
const cinemasDiv = document.getElementById('cinemasDiv');
const moviesDiv = document.getElementById('moviesDiv');
const popupDiv = document.getElementById('popupDiv');
const posterImg = document.querySelector('#popupDiv img');
const nameH2 = document.querySelector('#popupDiv h2');
const contentDiv = document.getElementById('contentDiv');
const trailerIframe = document.getElementById('trailerIframe');
const tagDiv = document.getElementById('tagDiv');
const screeningsDiv = document.getElementById('screeningsDiv');
const response = await fetch('data/index.json');
const data = await response.json();
// TODO: Iterate local storage and delete keys which do not have a corresponding value in `data.movies`
// to clear data for movies that are no longer in cinemas
let cinemas = localStorage.getItem('cinemas')
? JSON.parse(localStorage.getItem('cinemas')).map(c => data.cinemas.indexOf(c))
: data.cinemas.map((_, i) => i);
data.movies = data.movies.sort((a, b) => b.screenings - a.screenings);
function renderCinemas() {
const fragment = document.createDocumentFragment();
for (const cinema of data.cinemas) {
const selected = cinemas.includes(cinema);
const cinemaInput = document.createElement('input');
cinemaInput.type = 'checkbox';
cinemaInput.checked = selected;
cinemaInput.id = cinema;
cinemaInput.dataset.cinema = cinema;
cinemaInput.addEventListener('change', handleCinemaInputChange);
const cinemaLabel = document.createElement('label');
cinemaLabel.textContent = cinema;
cinemaLabel.htmlFor = cinema;
fragment.append(cinemaInput, cinemaLabel);
}
// TODO: Add a select all button and a unselect all button depending on state
cinemasDiv.innerHTML = '';
cinemasDiv.append(fragment);
}
function renderMovies() {
const fragment = document.createDocumentFragment();
// TODO: Hook up "show watched/deleted" filter when exists between maybies and untagged
const probablies = data.movies.filter(m => localStorage.getItem(m.id) === 'probably');
const maybies = data.movies.filter(m => localStorage.getItem(m.id) === 'maybe');
const untagged = data.movies.filter(m => localStorage.getItem(m.id) === null);
for (const movie of [...probablies, ...maybies, ...untagged]) {
// Skip movies with no screenings for the selected cinemas
if (!movie.cinemas || !movie.cinemas.find(c => cinemas.includes(c))) {
continue;
}
const movieDiv = document.createElement('div');
const tag = localStorage.getItem(movie.id);
if (tag) {
const badgeDiv = document.createElement('div');
badgeDiv.className = 'badge';
switch (tag) {
case 'probably': {
badgeDiv.textContent = 'Will watch';
break;
}
case 'maybe': {
badgeDiv.textContent = 'Might watch';
break;
}
case 'watched': {
badgeDiv.textContent = 'Already watched';
break;
}
case 'deleted': {
badgeDiv.textContent = 'Won\'t watch';
break;
}
default: {
throw new Error('Invalid tag');
}
}
movieDiv.append(badgeDiv);
}
const posterA = document.createElement('a');
posterA.href = '#' + movie.id;
const posterImg = document.createElement('img');
posterImg.dataset.src = movie.posterUrl + '?h360';
posterImg.dataset.id = movie.id;
posterImg.addEventListener('error', handlePosterImgError);
posterA.append(posterImg);
const nameA = document.createElement('a');
nameA.textContent = movie.name;
nameA.href = '#' + movie.id;
const yearSpan = document.createElement('span');
yearSpan.textContent = movie.year;
movieDiv.append(posterA, nameA, yearSpan);
fragment.append(movieDiv);
}
moviesDiv.innerHTML = '';
moviesDiv.append(fragment);
loadImages();
}
function renderMovie() {
const popup = location.hash && location.hash !== '#_';
popupDiv.classList.toggle('popup', popup);
if (!popup) {
// Stop any playback if any
trailerIframe.src = '';
return;
}
const id = location.hash.slice(1);
const movie = data.movies.find(m => m.id === id);
const tag = localStorage.getItem(movie.id);
posterImg.src = movie.posterUrl + '?h180';
nameH2.textContent = movie.name;
const paragraphs = movie.content.split('\n').map(paragraph => {
const paragraphP = document.createElement('p');
paragraphP.textContent = paragraph;
return paragraphP;
});
contentDiv.innerHTML = '';
contentDiv.append(...paragraphs);
trailerIframe.src = movie.trailerUrl.replace('watch?v=', 'embed/');
tagDiv.innerHTML = '';
switch (tag) {
case 'probably': {
const tagSpan = document.createElement('span');
tagSpan.textContent = `You said you will watch ${movie.name}. Changed your mind?`;
const untagButton = document.createElement('button');
untagButton.dataset.id = id;
untagButton.textContent = 'Reset';
untagButton.addEventListener('click', handleUntagButtonClick);
tagDiv.append(tagSpan, untagButton);
break;
}
case 'maybe': {
const tagSpan = document.createElement('span');
tagSpan.textContent = `You said you might watch ${movie.name}. Changed your mind?`;
const untagButton = document.createElement('button');
untagButton.dataset.id = id;
untagButton.textContent = 'Reset';
untagButton.addEventListener('click', handleUntagButtonClick);
tagDiv.append(tagSpan, untagButton);
break;
}
case 'watched': {
const tagSpan = document.createElement('span');
tagSpan.textContent = `You already watched ${movie.name}. Is this a mistake?`;
const untagButton = document.createElement('button');
untagButton.dataset.id = id;
untagButton.textContent = 'Reset';
untagButton.addEventListener('click', handleUntagButtonClick);
tagDiv.append(tagSpan, untagButton);
break;
}
case 'deleted': {
const tagSpan = document.createElement('span');
tagSpan.textContent = `You said you weren't gonna watch ${movie.name}. Changed your mind?`;
const untagButton = document.createElement('button');
untagButton.dataset.id = id;
untagButton.textContent = 'Reset';
untagButton.addEventListener('click', handleUntagButtonClick);
tagDiv.append(tagSpan, untagButton);
break;
}
default: {
const questionP = document.createElement('p');
questionP.textContent = `Are you gonna watch ${movie.name}?`;
const tagAsProbablyButton = document.createElement('button');
tagAsProbablyButton.dataset.tag = 'probably';
tagAsProbablyButton.dataset.id = id;
tagAsProbablyButton.textContent = 'I will';
tagAsProbablyButton.addEventListener('click', handleTagButtonClick);
const tagAsMaybeButton = document.createElement('button');
tagAsMaybeButton.dataset.tag = 'maybe';
tagAsMaybeButton.dataset.id = id;
tagAsMaybeButton.textContent = 'I might';
tagAsMaybeButton.addEventListener('click', handleTagButtonClick);
const tagAsWatchedButton = document.createElement('button');
tagAsWatchedButton.dataset.tag = 'watched';
tagAsWatchedButton.dataset.id = id;
tagAsWatchedButton.textContent = 'I already did';
tagAsWatchedButton.addEventListener('click', handleTagButtonClick);
const tagAsDeletedButton = document.createElement('button');
tagAsDeletedButton.dataset.tag = 'deleted';
tagAsDeletedButton.dataset.id = id;
tagAsDeletedButton.textContent = 'I won\'t';
tagAsDeletedButton.addEventListener('click', handleTagButtonClick);
tagDiv.append(questionP, tagAsProbablyButton, tagAsMaybeButton, tagAsWatchedButton, tagAsDeletedButton);
}
}
const fragment = document.createDocumentFragment();
for (const screening of Object.keys(movie.screenings)) {
if (!cinemas.includes(screening)) {
continue;
}
const screeningH3 = document.createElement('h3');
screeningH3.textContent = screening;
fragment.append(screeningH3);
for (const dateAndTime of movie.screenings[screening]) {
const dateAndTimeSpan = document.createElement('span');
dateAndTimeSpan.textContent = new Date(dateAndTime).toLocaleString();
dateAndTimeSpan.className = 'screening';
fragment.append(dateAndTimeSpan);
}
}
screeningsDiv.innerHTML = '';
screeningsDiv.append(fragment);
// Attach this this way so that the initial scroll done by the browser on refresh doesn't trigger it
window.setTimeout(window.addEventListener, 0, 'scroll', dismissPopup, { passive: true });
}
renderCinemas();
renderMovies();
renderMovie();
function handleCinemaInputChange(event) {
const cinema = event.currentTarget.dataset.cinema;
if (event.currentTarget.checked) {
cinemas.push(cinema);
} else {
cinemas = cinemas.filter(c => c !== cinema);
}
localStorage.setItem('cinemas', JSON.stringify(cinemas));
renderCinemas();
renderMovies();
}
function handlePosterImgError(event) {
event.currentTarget.src = 'no-poster.png';
}
function handleTagButtonClick(event) {
const id = event.currentTarget.dataset.id;
const tag = event.currentTarget.dataset.tag;
localStorage.setItem(id, tag);
renderMovies();
location.hash = '#_';
}
function handleUntagButtonClick(event) {
const id = event.currentTarget.dataset.id;
localStorage.removeItem(id);
renderMovies();
renderMovie();
}
function dismissPopup() {
if (event.target !== document && event.target !== popupDiv) {
// Ignore clicks within the popup
return;
}
// Prevent scrolling up by not using an empty hash here
location.hash = '_';
window.removeEventListener('scroll', dismissPopup, { passive: true });
}
popupDiv.addEventListener('click', dismissPopup);
window.addEventListener('hashchange', renderMovie);
function loadImages() {
for (const img of document.querySelectorAll('img')) {
const { bottom, top } = img.getBoundingClientRect();
if (bottom >= 0 && top <= window.innerHeight && !img.src) {
img.src = img.dataset.src;
}
}
}
window.addEventListener('scroll', loadImages, { passive: true });
loadImages();
});