2019-03-04 13:02:53 +00:00
|
|
|
(function(window) {
|
|
|
|
|
|
|
|
function fetchEvents() {
|
|
|
|
return new Promise((fullfill, reject) => {
|
2019-03-04 13:14:32 +00:00
|
|
|
fetch('./events.json')
|
|
|
|
.then(answer => answer.json())
|
|
|
|
.then(events => fullfill(events))
|
|
|
|
.catch(err => reject(err))
|
2019-03-04 13:02:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateMeasures = {
|
|
|
|
day : (60 * 60 * 24),
|
|
|
|
hour : (60 * 60),
|
|
|
|
minute : 60
|
|
|
|
}
|
|
|
|
|
|
|
|
function millisecondsToHumanReadable(mseconds) {
|
|
|
|
const seconds = mseconds / 1000
|
|
|
|
const days = Math.floor(seconds / dateMeasures.day)
|
|
|
|
const hours = Math.floor((seconds - days * dateMeasures.day) / dateMeasures.hour)
|
|
|
|
const minutes = Math.floor((seconds - days * dateMeasures.day - hours * dateMeasures.hour) / dateMeasures.minute)
|
|
|
|
const secondsLeft = (seconds - days * dateMeasures.day - hours * dateMeasures.hour - minutes * dateMeasures.minute).toFixed(2)
|
|
|
|
return `${days} days, ${hours < 10 ? '0' + hours: hours}:${minutes < 10 ? '0' + minutes : minutes}:${secondsLeft < 10 ? '0' + secondsLeft : secondsLeft} left`
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:01:19 +00:00
|
|
|
function renderEventTitle(event) {
|
|
|
|
const title = document.createElement('p')
|
2019-03-04 13:02:53 +00:00
|
|
|
title.textContent = event.name
|
2019-03-04 14:01:19 +00:00
|
|
|
title.classList += 'title'
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEventDate(event) {
|
|
|
|
const date = document.createElement('p')
|
2019-03-04 13:02:53 +00:00
|
|
|
date.textContent = event.date
|
2019-03-04 14:01:19 +00:00
|
|
|
date.classList += 'date'
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEventDescription(event) {
|
2019-03-04 13:45:12 +00:00
|
|
|
const desc = document.createElement('p')
|
|
|
|
desc.textContent = event.description
|
2019-03-04 14:01:19 +00:00
|
|
|
desc.classList += 'description'
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEventCountdown(event) {
|
2019-03-04 13:02:53 +00:00
|
|
|
const eventUnix = Date.parse(event.date)
|
2019-03-04 14:01:19 +00:00
|
|
|
const countdown = document.createElement('p')
|
|
|
|
countdown.classList += 'countdown'
|
2019-03-04 13:02:53 +00:00
|
|
|
countdown.textContent = eventUnix - Date.now()
|
|
|
|
setInterval(() => {
|
|
|
|
const diff = eventUnix - Date.now()
|
|
|
|
countdown.textContent = millisecondsToHumanReadable(diff)
|
|
|
|
}, 200)
|
2019-03-04 14:01:19 +00:00
|
|
|
return countdown
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEvent(event) {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
container.classList += 'event_container'
|
|
|
|
container.appendChild(renderEventTitle(event))
|
|
|
|
container.appendChild(renderEventDate(event))
|
|
|
|
container.appendChild(renderEventDescription(event))
|
|
|
|
container.appendChild(renderEventCountdown(event))
|
2019-03-04 13:02:53 +00:00
|
|
|
window.document.body.appendChild(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onload = () => {
|
|
|
|
fetchEvents().then(events => {
|
2019-03-04 13:45:12 +00:00
|
|
|
events
|
|
|
|
.sort((a, b) => Date.parse(a.date) - Date.parse(b.date))
|
|
|
|
.forEach(event => renderEvent(event))
|
2019-03-04 13:02:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})(window);
|