avm99963 | 1ca663e | 2020-12-17 19:34:22 +0100 | [diff] [blame] | 1 | var bookletNames = [ |
| 2 | 'nadal2020joves', |
| 3 | 'nadal2020nens', |
| 4 | ]; |
| 5 | |
| 6 | var isBookletShown = false; |
| 7 | |
| 8 | function showSection(section) { |
| 9 | document.querySelectorAll('section').forEach(el => { |
| 10 | if (el.id === section) |
| 11 | el.removeAttribute('hidden'); |
| 12 | else |
| 13 | el.setAttribute('hidden', ''); |
| 14 | }); |
| 15 | } |
| 16 | |
| 17 | function songElement(song) { |
| 18 | var div = document.createElement('div'); |
| 19 | div.classList.add('song'); |
| 20 | |
| 21 | var title = document.createElement('h3'); |
| 22 | title.classList.add('song-title'); |
| 23 | title.textContent = song.title; |
| 24 | |
| 25 | var author = document.createElement('div'); |
| 26 | author.classList.add('song-author'); |
| 27 | author.textContent = song.author; |
| 28 | |
| 29 | var performers = document.createElement('div'); |
| 30 | performers.classList.add('performers'); |
| 31 | |
| 32 | song.performers.forEach(performer => { |
| 33 | var item = document.createElement('div'); |
| 34 | item.classList.add('performers-item'); |
| 35 | |
| 36 | var span = document.createElement('span'); |
| 37 | span.classList.add('instrument'); |
| 38 | span.textContent = performer.instrument; |
| 39 | |
| 40 | var people = document.createTextNode(': ' + performer.names.join(', ')); |
| 41 | |
| 42 | item.append(span, people); |
| 43 | performers.append(item); |
| 44 | }); |
| 45 | |
| 46 | div.append(title, author, performers); |
| 47 | return div; |
| 48 | } |
| 49 | |
| 50 | function loadBooklet(booklet) { |
| 51 | console.info('Booklet: ', booklet); |
| 52 | |
| 53 | document.getElementById('concert-title').textContent = booklet.title; |
| 54 | document.getElementById('concert-subtitle').textContent = booklet.subtitle; |
| 55 | |
| 56 | var bookletContent = document.getElementById('booklet-content'); |
| 57 | booklet.songs.forEach(song => { |
| 58 | var songEl = songElement(song); |
| 59 | bookletContent.append(songEl); |
| 60 | }); |
| 61 | |
| 62 | showSection('booklet'); |
| 63 | } |
| 64 | |
| 65 | function checkBooklets(booklets) { |
| 66 | if (isBookletShown) |
| 67 | return; |
| 68 | |
| 69 | var now = Date.now(); |
| 70 | var latest = null; |
| 71 | booklets.forEach(booklet => { |
| 72 | if (booklet.begins * 1000 <= now && |
| 73 | (latest === null || booklet.begins * 1000 < booklet.begins)) |
| 74 | latest = booklet; |
| 75 | }); |
| 76 | |
| 77 | if (latest === null) |
| 78 | showSection('wait-screen'); |
| 79 | else { |
| 80 | loadBooklet(latest); |
| 81 | isBookletShown = true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | window.addEventListener('load', _ => { |
| 86 | if ('serviceWorker' in navigator) { |
| 87 | navigator.serviceWorker.register('/sw.js') |
| 88 | .then(_ => { |
| 89 | var promises = []; |
| 90 | bookletNames.forEach(name => { |
| 91 | var p = fetch('concerts/' + name + '.json').then(res => res.json()); |
| 92 | promises.push(p); |
| 93 | }); |
| 94 | |
| 95 | Promise.all(promises).then(booklets => { |
| 96 | checkBooklets(booklets); |
| 97 | window.setTimeout(_ => { checkBooklets(booklets); }, 60 * 1000); |
| 98 | }); |
| 99 | }) |
| 100 | .catch(err => { |
| 101 | showSection('cant-install'); |
| 102 | console.error('The service worker failed to be registered.'); |
| 103 | }); |
| 104 | } else { |
| 105 | showSection('no-service-worker'); |
| 106 | } |
| 107 | }); |