-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·85 lines (75 loc) · 2.14 KB
/
app.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
class FirstPWA {
constructor() {
this.speakersDiv = document.querySelector('.developers');
this.scheduleDiv = document.querySelector('.schedule');
this.init();
}
async init() {
this.loadSpeakers();
this.loadSchedule();
}
async loadSpeakers() {
this.speakers = await this.fetchJSON('./developers.json');
this.speakersDiv.innerHTML = this.speakers
.map(this.toSpeakerBlock)
.join('\n');
}
async loadSchedule() {
const rawSchedule = await this.fetchJSON('./schedule.json');
// Add speaker details to array
this.schedule = rawSchedule.map(this.addSpeakerDetails, this);
this.scheduleDiv.innerHTML = this.schedule
.map(this.toScheduleBlock)
.join('\n');
}
toSpeakerBlock(speaker) {
return `
<div class="developer">
<img src="${speaker.picture}" alt="${speaker.name}">
<div>${speaker.name}</div>
</div>`;
}
toScheduleBlock(scheduleItem) {
return `
<div class="schedule-item ${scheduleItem.category}">
<div class="title-and-time">
<div class="time">${scheduleItem.startTime}</div>
<div class="title-and-developer">
<div class="title">${scheduleItem.title}</div>
<div class="developer">${
scheduleItem.speaker ? scheduleItem.speaker.name : ' '
}</div>
</div>
</div>
<p class="description">${scheduleItem.description}</p>
</div>
`;
}
addSpeakerDetails(item) {
if (item.speakerId) {
return Object.assign({}, item, {
speaker: this.speakers.find(s => s.id === item.speakerId)
});
}
return Object.assign({}, item);
}
async fetchJSON(url) {
const res = await fetch(url);
return res.json();
}
}
async function registerSW() {
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('./sw.js');
} catch (e) {
alert('ServiceWorker registration failed. Sorry about that.');
}
} else {
document.querySelector('.alert').removeAttribute('hidden');
}
}
window.addEventListener('load', e => {
new FirstPWA();
registerSW();
});