blob: 0daa39901e39bed4deb67c2fd2be711a157563b7 [file] [log] [blame]
avm999631ca663e2020-12-17 19:34:22 +01001var bookletNames = [
avm99963c26d16b2021-02-28 00:58:56 +01002 'ciba2021',
avm999631ca663e2020-12-17 19:34:22 +01003 'nadal2020nens',
avm99963c26d16b2021-02-28 00:58:56 +01004 'nadal2020joves',
avm999631ca663e2020-12-17 19:34:22 +01005];
6
avm9996304c97542020-12-20 19:11:16 +01007var interval = null;
avm999631ca663e2020-12-17 19:34:22 +01008var isBookletShown = false;
avm99963d6027c32021-02-28 00:54:49 +01009var showAllConcerts = false;
avm9996374028bc2021-03-06 22:30:04 +010010var forceConcert = null;
avm999631ca663e2020-12-17 19:34:22 +010011
12function showSection(section) {
13 document.querySelectorAll('section').forEach(el => {
14 if (el.id === section)
15 el.removeAttribute('hidden');
16 else
17 el.setAttribute('hidden', '');
18 });
19}
20
avm99963b2a3fa92020-12-21 16:07:29 +010021function convertTime(time) {
22 var hour = Math.floor(time);
23 var totalMinutes = time*60;
24 var minutes = totalMinutes - hour*60;
25
26 return hour + ':' + minutes;
27}
28
29function songElement(song, hasTimes) {
avm999631ca663e2020-12-17 19:34:22 +010030 var div = document.createElement('div');
31 div.classList.add('song');
32
avm99963b2a3fa92020-12-21 16:07:29 +010033 if (hasTimes) {
34 var time = document.createElement('div');
35 time.classList.add('song-time');
36 console.log(song.begins);
37 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;
87 if ('customFooter' in booklet)
88 document.getElementById('booklet-custom-footer').innerHTML =
89 booklet.customFooter;
90
avm999631ca663e2020-12-17 19:34:22 +010091 var bookletContent = document.getElementById('booklet-content');
avm9996304c97542020-12-20 19:11:16 +010092 bookletContent.textContent = '';
avm999631ca663e2020-12-17 19:34:22 +010093 booklet.songs.forEach(song => {
avm99963b2a3fa92020-12-21 16:07:29 +010094 var songEl = songElement(song, hasTimes);
avm999631ca663e2020-12-17 19:34:22 +010095 bookletContent.append(songEl);
96 });
97
98 showSection('booklet');
avm9996304c97542020-12-20 19:11:16 +010099 if (showBackButton)
100 document.getElementById('previous-concerts-btn').removeAttribute('hidden');
101}
102
103function loadPreviousBookletsList(booklets) {
104 var now = Date.now();
105 var list = document.getElementById('previous-concerts-list');
106
107 var existsPreviousConcert = false;
108 booklets.forEach(booklet => {
avm99963d6027c32021-02-28 00:54:49 +0100109 if (showAllConcerts || booklet.ends * 1000 <= now) {
avm9996304c97542020-12-20 19:11:16 +0100110 existsPreviousConcert = true;
111
112 var el = document.createElement('div');
113 el.classList.add('concert');
114
115 var title = document.createElement('div');
116 title.classList.add('concert-title');
117 title.textContent = booklet.title;
118
119 var subtitle = document.createElement('div');
120 subtitle.classList.add('concert-subtitle');
121 subtitle.textContent = booklet.subtitle;
122
123 var view = document.createElement('button');
124 view.classList.add('concert-btn', 'linkify');
125 view.textContent = 'Obre el programa';
126
127 el.append(title, subtitle, view);
128
129 el.addEventListener('click', _ => {
130 loadBooklet(booklet, true);
131 });
132
133 list.append(el);
134 }
135 });
136
137 if (existsPreviousConcert)
138 document.getElementById('previous-concerts-btn').removeAttribute('hidden');
avm999631ca663e2020-12-17 19:34:22 +0100139}
140
141function checkBooklets(booklets) {
142 if (isBookletShown)
143 return;
144
145 var now = Date.now();
146 var latest = null;
avm9996374028bc2021-03-06 22:30:04 +0100147 for (var booklet of booklets) {
148 if (forceConcert !== null && ('codename' in booklet) &&
149 booklet['codename'] == forceConcert) {
avm999631ca663e2020-12-17 19:34:22 +0100150 latest = booklet;
avm9996374028bc2021-03-06 22:30:04 +0100151 break;
152 }
153
154 if (booklet['begins'] * 1000 <= now && booklet['ends'] * 1000 >= now &&
155 (latest === null || latest['begins'] < booklet['begins']))
156 latest = booklet;
157 }
avm999631ca663e2020-12-17 19:34:22 +0100158
avm9996304c97542020-12-20 19:11:16 +0100159 if (latest !== null) {
160 document.getElementById('previous-concerts-btn').setAttribute('hidden', '');
avm999631ca663e2020-12-17 19:34:22 +0100161 loadBooklet(latest);
avm9996304c97542020-12-20 19:11:16 +0100162 window.clearInterval(interval);
avm999631ca663e2020-12-17 19:34:22 +0100163 isBookletShown = true;
164 }
165}
166
avm9996320fdf2a2020-12-20 13:08:16 +0100167function showUpdateFooter() {
168 document.getElementById('update-available').removeAttribute('hidden');
169}
170
avm999631ca663e2020-12-17 19:34:22 +0100171window.addEventListener('load', _ => {
avm9996304c97542020-12-20 19:11:16 +0100172 document.getElementById('previous-concerts-btn')
173 .addEventListener('click', e => {
174 showSection('previous-concerts');
175 document.getElementById('previous-concerts-btn')
176 .setAttribute('hidden', '');
177 });
178
avm99963d6027c32021-02-28 00:54:49 +0100179 var searchParams = new URLSearchParams(location.search);
180 if (searchParams.has('showAllConcerts'))
181 showAllConcerts = true;
avm9996374028bc2021-03-06 22:30:04 +0100182 if (searchParams.has('concert'))
183 forceConcert = searchParams.get('concert');
avm99963d6027c32021-02-28 00:54:49 +0100184
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000185 if ('serviceWorker' in navigator) {
186 navigator.serviceWorker.register('/sw.js')
187 .then(reg => {
188 reg.addEventListener('updatefound', _ => {
189 showUpdateFooter();
avm999631ca663e2020-12-17 19:34:22 +0100190 });
Adrià Vilanova Martínezb06de522021-03-06 22:15:19 +0000191
192 var promises = [];
193 bookletNames.forEach(name => {
194 var p = fetch('concerts/' + name + '.json').then(res => res.json());
195 promises.push(p);
196 });
197
198 Promise.all(promises).then(booklets => {
199 showSection('wait-screen');
200 loadPreviousBookletsList(booklets);
201 checkBooklets(booklets);
202 interval = window.setInterval(_ => {
203 checkBooklets(booklets);
204 }, 15 * 1000);
205 });
206 })
207 .catch(err => {
208 showSection('cant-install');
209 console.error('The service worker failed to be registered.');
210 });
211 } else {
212 showSection('no-service-worker');
avm999631ca663e2020-12-17 19:34:22 +0100213 }
214});