avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 1 | var bookletNames = [ |
| 2 | 'nadal2020joves', |
| 3 | 'nadal2020nens', |
| 4 | ]; |
| 5 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 6 | var interval = null; |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 7 | var isBookletShown = false; |
| 8 | |
| 9 | function showSection(section) { |
| 10 | document.querySelectorAll('section').forEach(el => { |
| 11 | if (el.id === section) |
| 12 | el.removeAttribute('hidden'); |
| 13 | else |
| 14 | el.setAttribute('hidden', ''); |
| 15 | }); |
| 16 | } |
| 17 | |
| 18 | function songElement(song) { |
| 19 | var div = document.createElement('div'); |
| 20 | div.classList.add('song'); |
| 21 | |
| 22 | var title = document.createElement('h3'); |
| 23 | title.classList.add('song-title'); |
| 24 | title.textContent = song.title; |
| 25 | |
| 26 | var author = document.createElement('div'); |
| 27 | author.classList.add('song-author'); |
| 28 | author.textContent = song.author; |
| 29 | |
| 30 | var performers = document.createElement('div'); |
| 31 | performers.classList.add('performers'); |
| 32 | |
| 33 | song.performers.forEach(performer => { |
| 34 | var item = document.createElement('div'); |
| 35 | item.classList.add('performers-item'); |
| 36 | |
| 37 | var span = document.createElement('span'); |
| 38 | span.classList.add('instrument'); |
| 39 | span.textContent = performer.instrument; |
| 40 | |
| 41 | var people = document.createTextNode(': ' + performer.names.join(', ')); |
| 42 | |
| 43 | item.append(span, people); |
| 44 | performers.append(item); |
| 45 | }); |
| 46 | |
| 47 | div.append(title, author, performers); |
| 48 | return div; |
| 49 | } |
| 50 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 51 | function loadBooklet(booklet, showBackButton = false) { |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 52 | console.info('Booklet: ', booklet); |
| 53 | |
| 54 | document.getElementById('concert-title').textContent = booklet.title; |
| 55 | document.getElementById('concert-subtitle').textContent = booklet.subtitle; |
| 56 | |
| 57 | var bookletContent = document.getElementById('booklet-content'); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 58 | bookletContent.textContent = ''; |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 59 | booklet.songs.forEach(song => { |
| 60 | var songEl = songElement(song); |
| 61 | bookletContent.append(songEl); |
| 62 | }); |
| 63 | |
| 64 | showSection('booklet'); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 65 | if (showBackButton) |
| 66 | document.getElementById('previous-concerts-btn').removeAttribute('hidden'); |
| 67 | } |
| 68 | |
| 69 | function loadPreviousBookletsList(booklets) { |
| 70 | var now = Date.now(); |
| 71 | var list = document.getElementById('previous-concerts-list'); |
| 72 | |
| 73 | var existsPreviousConcert = false; |
| 74 | booklets.forEach(booklet => { |
| 75 | if (booklet.ends * 1000 <= now) { |
| 76 | existsPreviousConcert = true; |
| 77 | |
| 78 | var el = document.createElement('div'); |
| 79 | el.classList.add('concert'); |
| 80 | |
| 81 | var title = document.createElement('div'); |
| 82 | title.classList.add('concert-title'); |
| 83 | title.textContent = booklet.title; |
| 84 | |
| 85 | var subtitle = document.createElement('div'); |
| 86 | subtitle.classList.add('concert-subtitle'); |
| 87 | subtitle.textContent = booklet.subtitle; |
| 88 | |
| 89 | var view = document.createElement('button'); |
| 90 | view.classList.add('concert-btn', 'linkify'); |
| 91 | view.textContent = 'Obre el programa'; |
| 92 | |
| 93 | el.append(title, subtitle, view); |
| 94 | |
| 95 | el.addEventListener('click', _ => { |
| 96 | loadBooklet(booklet, true); |
| 97 | }); |
| 98 | |
| 99 | list.append(el); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | if (existsPreviousConcert) |
| 104 | document.getElementById('previous-concerts-btn').removeAttribute('hidden'); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | function checkBooklets(booklets) { |
| 108 | if (isBookletShown) |
| 109 | return; |
| 110 | |
| 111 | var now = Date.now(); |
| 112 | var latest = null; |
| 113 | booklets.forEach(booklet => { |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 114 | if (booklet['begins'] * 1000 <= now && booklet['ends'] * 1000 >= now && |
| 115 | (latest === null || latest['begins'] < booklet['begins'])) |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 116 | latest = booklet; |
| 117 | }); |
| 118 | |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 119 | if (latest !== null) { |
| 120 | document.getElementById('previous-concerts-btn').setAttribute('hidden', ''); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 121 | loadBooklet(latest); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 122 | window.clearInterval(interval); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 123 | isBookletShown = true; |
| 124 | } |
| 125 | } |
| 126 | |
avm99963 | 20fdf2a | 2020-12-20 13:08:16 +0100 | [diff] [blame] | 127 | function showUpdateFooter() { |
| 128 | document.getElementById('update-available').removeAttribute('hidden'); |
| 129 | } |
| 130 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 131 | window.addEventListener('load', _ => { |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 132 | document.getElementById('previous-concerts-btn') |
| 133 | .addEventListener('click', e => { |
| 134 | showSection('previous-concerts'); |
| 135 | document.getElementById('previous-concerts-btn') |
| 136 | .setAttribute('hidden', ''); |
| 137 | }); |
| 138 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 139 | if ('serviceWorker' in navigator) { |
| 140 | navigator.serviceWorker.register('/sw.js') |
avm99963 | 20fdf2a | 2020-12-20 13:08:16 +0100 | [diff] [blame] | 141 | .then(reg => { |
| 142 | reg.addEventListener('updatefound', _ => { |
| 143 | showUpdateFooter(); |
| 144 | }); |
| 145 | |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 146 | var promises = []; |
| 147 | bookletNames.forEach(name => { |
| 148 | var p = fetch('concerts/' + name + '.json').then(res => res.json()); |
| 149 | promises.push(p); |
| 150 | }); |
| 151 | |
| 152 | Promise.all(promises).then(booklets => { |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 153 | showSection('wait-screen'); |
| 154 | loadPreviousBookletsList(booklets); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 155 | checkBooklets(booklets); |
avm99963 | 04c9754 | 2020-12-20 19:11:16 +0100 | [diff] [blame] | 156 | interval = window.setInterval(_ => { |
| 157 | checkBooklets(booklets); |
| 158 | }, 15 * 1000); |
avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 159 | }); |
| 160 | }) |
| 161 | .catch(err => { |
| 162 | showSection('cant-install'); |
| 163 | console.error('The service worker failed to be registered.'); |
| 164 | }); |
| 165 | } else { |
| 166 | showSection('no-service-worker'); |
| 167 | } |
| 168 | }); |