From 0075e40aaef169d48c069b919bbce6736d4fcdd3 Mon Sep 17 00:00:00 2001 From: ugyballoons Date: Wed, 24 May 2023 17:02:52 +0100 Subject: [PATCH] Add UTC clock to header --- src/js/hostbanner.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/sass/style.sass | 24 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/js/hostbanner.js b/src/js/hostbanner.js index 58a0a416..773a870c 100644 --- a/src/js/hostbanner.js +++ b/src/js/hostbanner.js @@ -1,3 +1,5 @@ +import { _elWithAttrs, _elWithClass } from './modules/utils.js' + window.addEventListener('DOMContentLoaded', () => { const display = [] @@ -15,5 +17,44 @@ window.addEventListener('DOMContentLoaded', displayEl.appendChild(text) document.body.append(displayEl) } + addClock() } ) + +function addClock () { + const clock = _elWithAttrs('div', { id: 'clock' }) + const hm = _elWithClass('span', 'hours-mins') + const secs = _elWithClass('span', 'secs') + const clockEls = { hm, secs, displayed: false } + clock.append(hm, secs) + + document.querySelector('.header-page-buttons').append(clock) + + window.setInterval(() => { + updateClock(clockEls) + }, 1000) + + window.addEventListener('showClock', () => { + clock.classList.add('show') + }) +} + +function updateClock (clockEls) { + const d = new Date() + clockEls.hm.textContent = getHoursAndMins(d) + clockEls.secs.textContent = padZero(d.getUTCSeconds()) + if (!clockEls.displayed) { + window.dispatchEvent(new Event('showClock')) + clockEls.displayed = true + } +} + +function getHoursAndMins (dateObj) { + const h = padZero(dateObj.getUTCHours()) + const m = padZero(dateObj.getUTCMinutes()) + return h + ':' + m +} + +function padZero (num) { + return (num + 100).toString().slice(-2) +} diff --git a/src/sass/style.sass b/src/sass/style.sass index ab2523d3..f53ba6de 100644 --- a/src/sass/style.sass +++ b/src/sass/style.sass @@ -60,6 +60,7 @@ header margin-top: 12.5px .header-page-buttons + position: relative flex-grow: 1 flex-direction: row !important justify-content: flex-start @@ -108,6 +109,29 @@ header a color: #fff +#clock + opacity: 0 + transition: opacity 0.3s + font-size: 0.8em + padding: 8px + font-family: monospace + background-color: #fff + color: #000 + border-radius: 0 0 12px 12px + position: absolute + top: -16px + right: 3em + &::after + content: "UTC" + margin-left: 0.2em + &.show + opacity: 1 + .hours-mins + font-size: 1.4em + .secs + color: #5f5f5f + margin-left: 0.2em + main padding: 1em 30px 25px