blob: aab60d919609f9cf78bf36c0ac65d28b60d22f66 [file] [log] [blame]
avm999631ca663e2020-12-17 19:34:22 +01001var bookletNames = [
Adrià Vilanova Martínez5d5742a2022-03-26 18:13:20 +01002 'cant2022',
avm99963c26d16b2021-02-28 00:58:56 +01003 'ciba2021',
avm999631ca663e2020-12-17 19:34:22 +01004 'nadal2020nens',
avm99963c26d16b2021-02-28 00:58:56 +01005 'nadal2020joves',
avm999631ca663e2020-12-17 19:34:22 +01006];
7
avm9996304c97542020-12-20 19:11:16 +01008var interval = null;
avm999631ca663e2020-12-17 19:34:22 +01009var isBookletShown = false;
avm99963d6027c32021-02-28 00:54:49 +010010var showAllConcerts = false;
avm9996374028bc2021-03-06 22:30:04 +010011var forceConcert = null;
avm999631ca663e2020-12-17 19:34:22 +010012
13function showSection(section) {
14 document.querySelectorAll('section').forEach(el => {
15 if (el.id === section)
16 el.removeAttribute('hidden');
17 else
18 el.setAttribute('hidden', '');
19 });
20}
21
avm99963b2a3fa92020-12-21 16:07:29 +010022function convertTime(time) {
23 var hour = Math.floor(time);
24 var totalMinutes = time*60;
25 var minutes = totalMinutes - hour*60;
26
27 return hour + ':' + minutes;
28}
29
30function songElement(song, hasTimes) {
avm999631ca663e2020-12-17 19:34:22 +010031 var div = document.createElement('div');
32 div.classList.add('song');
33
avm99963b2a3fa92020-12-21 16:07:29 +010034 if (hasTimes) {
35 var time = document.createElement('div');
36 time.classList.add('song-time');
avm99963b2a3fa92020-12-21 16:07:29 +010037 time.textContent = convertTime(song.begins);
38
39 div.append(time);
40 }
41
avm999631ca663e2020-12-17 19:34:22 +010042 var title = document.createElement('h3');
43 title.classList.add('song-title');
44 title.textContent = song.title;
45
46 var author = document.createElement('div');
47 author.classList.add('song-author');
48 author.textContent = song.author;
49
50 var performers = document.createElement('div');
51 performers.classList.add('performers');
52
53 song.performers.forEach(performer => {
54 var item = document.createElement('div');
55 item.classList.add('performers-item');
56
57 var span = document.createElement('span');
58 span.classList.add('instrument');
59 span.textContent = performer.instrument;
60
61 var people = document.createTextNode(': ' + performer.names.join(', '));
62
63 item.append(span, people);
64 performers.append(item);
65 });
66
67 div.append(title, author, performers);
68 return div;
69}
70
avm9996304c97542020-12-20 19:11:16 +010071function loadBooklet(booklet, showBackButton = false) {
avm999631ca663e2020-12-17 19:34:22 +010072 console.info('Booklet: ', booklet);
73
avm99963b2a3fa92020-12-21 16:07:29 +010074 var hasTimes = booklet.hasTimes;
75 if (hasTimes) {
76 booklet.songs.sort((a, b) => {
77 return a.begins - b.begins;
78 });
79 }
80
avm999631ca663e2020-12-17 19:34:22 +010081 document.getElementById('concert-title').textContent = booklet.title;
82 document.getElementById('concert-subtitle').textContent = booklet.subtitle;
83
avm99963a8c89e82021-03-01 22:41:39 +010084 if ('customHeader' in booklet)
85 document.getElementById('booklet-custom-header').innerHTML =
86 booklet.customHeader;
Adrià Vilanova Martínez466fd112022-03-26 18:32:03 +010087 else
88 document.getElementById('booklet-custom-header').textContent = '';
avm99963a8c89e82021-03-01 22:41:39 +010089 if ('customFooter' in booklet)
90 document.getElementById('booklet-custom-footer').innerHTML =
91 booklet.customFooter;
Adrià Vilanova Martínez466fd112022-03-26 18:32:03 +010092 else
93 document.getElementById('booklet-custom-footer').textContent = '';
avm99963a8c89e82021-03-01 22:41:39 +010094
avm999631ca663e2020-12-17 19:34:22 +010095 var bookletContent = document.getElementById('booklet-content');
avm9996304c97542020-12-20 19:11:16 +010096 bookletContent.textContent = '';
avm999631ca663e2020-12-17 19:34:22 +010097 booklet.songs.forEach(song => {
avm99963b2a3fa92020-12-21 16:07:29 +010098 var songEl = songElement(song, hasTimes);
avm999631ca663e2020-12-17 19:34:22 +010099 bookletContent.append(songEl);
100 });
101
102 showSection('booklet');
avm9996304c97542020-12-20 19:11:16 +0100103 if (showBackButton)
104 document.getElementById('previous-concerts-btn').removeAttribute('hidden');
105}
106
107function loadPreviousBookletsList(booklets) {
108 var now = Date.now();
109 var list = document.getElementById('previous-concerts-list');
110
111 var existsPreviousConcert = false;
112 booklets.forEach(booklet => {
avm99963d6027c32021-02-28 00:54:49 +0100113 if (showAllConcerts || booklet.ends * 1000 <= now) {
avm9996304c97542020-12-20 19:11:16 +0100114 existsPreviousConcert = true;
115
116 var el = document.createElement('div');
117 el.classList.add('concert');
118
119 var title = document.createElement('div');
120 title.classList.add('concert-title');
121 title.textContent = booklet.title;
122
123 var subtitle = document.createElement('div');
124 subtitle.classList.add('concert-subtitle');
125 subtitle.textContent = booklet.subtitle;
126
127 var view = document.createElement('button');
128 view.classList.add('concert-btn', 'linkify');
129 view.textContent = 'Obre el programa';
130
131 el.append(title, subtitle, view);
132
133 el.addEventListener('click', _ => {
134 loadBooklet(booklet, true);
135 });
136
137 list.append(el);
138 }
139 });
140
141 if (existsPreviousConcert)
142 document.getElementById('previous-concerts-btn').removeAttribute('hidden');
avm999631ca663e2020-12-17 19:34:22 +0100143}
144
145function checkBooklets(booklets) {
146 if (isBookletShown)
147 return;
148
149 var now = Date.now();
150 var latest = null;
avm9996374028bc2021-03-06 22:30:04 +0100151 for (var booklet of booklets) {
152 if (forceConcert !== null && ('codename' in booklet) &&
153 booklet['codename'] == forceConcert) {
avm999631ca663e2020-12-17 19:34:22 +0100154 latest = booklet;
avm9996374028bc2021-03-06 22:30:04 +0100155 break;
156 }
157
158 if (booklet['begins'] * 1000 <= now && booklet['ends'] * 1000 >= now &&
159 (latest === null || latest['begins'] < booklet['begins']))
160 latest = booklet;
161 }
avm999631ca663e2020-12-17 19:34:22 +0100162
avm9996304c97542020-12-20 19:11:16 +0100163 if (latest !== null) {
164 document.getElementById('previous-concerts-btn').setAttribute('hidden', '');
avm999631ca663e2020-12-17 19:34:22 +0100165 loadBooklet(latest);
avm9996304c97542020-12-20 19:11:16 +0100166 window.clearInterval(interval);
avm999631ca663e2020-12-17 19:34:22 +0100167 isBookletShown = true;
168 }
169}
170
avm9996320fdf2a2020-12-20 13:08:16 +0100171function showUpdateFooter() {
172 document.getElementById('update-available').removeAttribute('hidden');
173}
174
avm999632d213272021-03-06 23:46:12 +0100175function initBooklets() {
176 var promises = [];
177 bookletNames.forEach(name => {
178 var p = fetch('concerts/' + name + '.json').then(res => res.json());
179 promises.push(p);
180 });
181
182 Promise.all(promises).then(booklets => {
183 showSection('wait-screen');
184 loadPreviousBookletsList(booklets);
185 checkBooklets(booklets);
186 interval = window.setInterval(_ => {
187 checkBooklets(booklets);
188 }, 15 * 1000);
189 });
190}
191
avm999631ca663e2020-12-17 19:34:22 +0100192window.addEventListener('load', _ => {
avm9996304c97542020-12-20 19:11:16 +0100193 document.getElementById('previous-concerts-btn')
194 .addEventListener('click', e => {
195 showSection('previous-concerts');
196 document.getElementById('previous-concerts-btn')
197 .setAttribute('hidden', '');
198 });
199
avm99963d6027c32021-02-28 00:54:49 +0100200 var searchParams = new URLSearchParams(location.search);
201 if (searchParams.has('showAllConcerts'))
202 showAllConcerts = true;
avm9996374028bc2021-03-06 22:30:04 +0100203 if (searchParams.has('concert'))
204 forceConcert = searchParams.get('concert');
avm99963d6027c32021-02-28 00:54:49 +0100205
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000206 if ('serviceWorker' in navigator) {
207 navigator.serviceWorker.register('/sw.js')
208 .then(reg => {
209 reg.addEventListener('updatefound', _ => {
210 showUpdateFooter();
avm999631ca663e2020-12-17 19:34:22 +0100211 });
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000212
avm999632d213272021-03-06 23:46:12 +0100213 initBooklets();
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000214 })
215 .catch(err => {
avm999632d213272021-03-06 23:46:12 +0100216 if (searchParams.has('optionalSW')) {
217 initBooklets();
218 } else {
219 showSection('cant-install');
220 console.error('The service worker failed to be registered.');
221 }
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000222 });
223 } else {
224 showSection('no-service-worker');
avm999631ca663e2020-12-17 19:34:22 +0100225 }
226});