avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 1 | var bookletNames = [ |
avm99963 | c26d16b | 2021-02-28 00:58:56 +0100 | [diff] [blame] | 2 | 'ciba2021', |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 3 | 'nadal2020nens', |
avm99963 | c26d16b | 2021-02-28 00:58:56 +0100 | [diff] [blame] | 4 | 'nadal2020joves', |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 5 | ]; |
| 6 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 7 | var interval = null; |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 8 | var isBookletShown = false; |
avm99963 | d6027c3 | 2021-02-28 00:54:49 +0100 | [diff] [blame] | 9 | var showAllConcerts = false; |
avm99963 | 74028bc | 2021-03-06 22:30:04 +0100 | [diff] [blame] | 10 | var forceConcert = null; |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 11 | |
| 12 | function 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 | |
avm99963 | b2a3fa9 | 2020-12-21 16:07:29 +0100 | [diff] [blame] | 21 | function 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 | |
| 29 | function songElement(song, hasTimes) { |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 30 | var div = document.createElement('div'); |
| 31 | div.classList.add('song'); |
| 32 | |
avm99963 | b2a3fa9 | 2020-12-21 16:07:29 +0100 | [diff] [blame] | 33 | 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 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 42 | 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 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 71 | function loadBooklet(booklet, showBackButton = false) { |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 72 | console.info('Booklet: ', booklet); |
| 73 | |
avm99963 | b2a3fa9 | 2020-12-21 16:07:29 +0100 | [diff] [blame] | 74 | var hasTimes = booklet.hasTimes; |
| 75 | if (hasTimes) { |
| 76 | booklet.songs.sort((a, b) => { |
| 77 | return a.begins - b.begins; |
| 78 | }); |
| 79 | } |
| 80 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 81 | document.getElementById('concert-title').textContent = booklet.title; |
| 82 | document.getElementById('concert-subtitle').textContent = booklet.subtitle; |
| 83 | |
avm99963 | a8c89e8 | 2021-03-01 22:41:39 +0100 | [diff] [blame] | 84 | 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 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 91 | var bookletContent = document.getElementById('booklet-content'); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 92 | bookletContent.textContent = ''; |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 93 | booklet.songs.forEach(song => { |
avm99963 | b2a3fa9 | 2020-12-21 16:07:29 +0100 | [diff] [blame] | 94 | var songEl = songElement(song, hasTimes); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 95 | bookletContent.append(songEl); |
| 96 | }); |
| 97 | |
| 98 | showSection('booklet'); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 99 | if (showBackButton) |
| 100 | document.getElementById('previous-concerts-btn').removeAttribute('hidden'); |
| 101 | } |
| 102 | |
| 103 | function loadPreviousBookletsList(booklets) { |
| 104 | var now = Date.now(); |
| 105 | var list = document.getElementById('previous-concerts-list'); |
| 106 | |
| 107 | var existsPreviousConcert = false; |
| 108 | booklets.forEach(booklet => { |
avm99963 | d6027c3 | 2021-02-28 00:54:49 +0100 | [diff] [blame] | 109 | if (showAllConcerts || booklet.ends * 1000 <= now) { |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 110 | 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'); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | function checkBooklets(booklets) { |
| 142 | if (isBookletShown) |
| 143 | return; |
| 144 | |
| 145 | var now = Date.now(); |
| 146 | var latest = null; |
avm99963 | 74028bc | 2021-03-06 22:30:04 +0100 | [diff] [blame] | 147 | for (var booklet of booklets) { |
| 148 | if (forceConcert !== null && ('codename' in booklet) && |
| 149 | booklet['codename'] == forceConcert) { |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 150 | latest = booklet; |
avm99963 | 74028bc | 2021-03-06 22:30:04 +0100 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
| 154 | if (booklet['begins'] * 1000 <= now && booklet['ends'] * 1000 >= now && |
| 155 | (latest === null || latest['begins'] < booklet['begins'])) |
| 156 | latest = booklet; |
| 157 | } |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 158 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 159 | if (latest !== null) { |
| 160 | document.getElementById('previous-concerts-btn').setAttribute('hidden', ''); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 161 | loadBooklet(latest); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 162 | window.clearInterval(interval); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 163 | isBookletShown = true; |
| 164 | } |
| 165 | } |
| 166 | |
avm99963 | 20fdf2a | 2020-12-20 13:08:16 +0100 | [diff] [blame] | 167 | function showUpdateFooter() { |
| 168 | document.getElementById('update-available').removeAttribute('hidden'); |
| 169 | } |
| 170 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 171 | window.addEventListener('load', _ => { |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 172 | document.getElementById('previous-concerts-btn') |
| 173 | .addEventListener('click', e => { |
| 174 | showSection('previous-concerts'); |
| 175 | document.getElementById('previous-concerts-btn') |
| 176 | .setAttribute('hidden', ''); |
| 177 | }); |
| 178 | |
avm99963 | d6027c3 | 2021-02-28 00:54:49 +0100 | [diff] [blame] | 179 | var searchParams = new URLSearchParams(location.search); |
| 180 | if (searchParams.has('showAllConcerts')) |
| 181 | showAllConcerts = true; |
avm99963 | 74028bc | 2021-03-06 22:30:04 +0100 | [diff] [blame] | 182 | if (searchParams.has('concert')) |
| 183 | forceConcert = searchParams.get('concert'); |
avm99963 | d6027c3 | 2021-02-28 00:54:49 +0100 | [diff] [blame] | 184 | |
Adrià Vilanova Martínez | b06de52 | 2021-03-06 22:15:19 +0000 | [diff] [blame] | 185 | if ('serviceWorker' in navigator) { |
| 186 | navigator.serviceWorker.register('/sw.js') |
| 187 | .then(reg => { |
| 188 | reg.addEventListener('updatefound', _ => { |
| 189 | showUpdateFooter(); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 190 | }); |
Adrià Vilanova Martínez | b06de52 | 2021-03-06 22:15:19 +0000 | [diff] [blame] | 191 | |
| 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'); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 213 | } |
| 214 | }); |