|
| 1 | +const setMessage = (countdown, msg) => { |
| 2 | + const message = countdown.querySelector(".message"); |
| 3 | + message.textContent = msg; |
| 4 | +}; |
| 5 | + |
| 6 | +const getRemainingTime = (time) => { |
| 7 | + const days = parseInt(time / (60 * 60 * 24), 10); |
| 8 | + const hours = parseInt((time / (60 * 60)) % 24, 10); |
| 9 | + const minutes = parseInt((time / 60) % 60, 10); |
| 10 | + const seconds = parseInt(time % 60, 10); |
| 11 | + |
| 12 | + return [days, hours, minutes, seconds]; |
| 13 | +}; |
| 14 | + |
| 15 | +const updateCounters = (countdown, remaining) => { |
| 16 | + const labels = countdown.querySelectorAll(".label"); |
| 17 | + const counters = countdown.querySelectorAll(".count"); |
| 18 | + const units = ["day", "hour", "minute", "second"]; |
| 19 | + |
| 20 | + for (let i = 0; i < counters.length; i += 1) { |
| 21 | + const el = counters[i]; |
| 22 | + const count = remaining[i]; |
| 23 | + const label = labels[i]; |
| 24 | + const unit = units[i]; |
| 25 | + |
| 26 | + // Zero-pad values below 10 |
| 27 | + el.textContent = count < 10 ? `0${count}` : count; |
| 28 | + label.textContent = count === 1 ? unit : `${unit}s`; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +const embedVid = (container, beforeElement, youtube) => { |
| 33 | + const videoFrameContainer = document.createElement("div"); |
| 34 | + const embed = document.createElement("iframe"); |
| 35 | + const w = window.innerWidth; |
| 36 | + const attrs = { |
| 37 | + src: youtube, |
| 38 | + frameborder: 0, |
| 39 | + allowfullscreen: "", |
| 40 | + }; |
| 41 | + |
| 42 | + attrs.width = w < 400 ? w : 400; |
| 43 | + attrs.height = (attrs.width * 3) / 4; |
| 44 | + Object.entries(attrs).map(([key, val]) => embed.setAttribute(key, val)); |
| 45 | + |
| 46 | + videoFrameContainer.setAttribute("class", "vid-container"); |
| 47 | + videoFrameContainer.appendChild(embed); |
| 48 | + |
| 49 | + container.insertBefore(videoFrameContainer, beforeElement); |
| 50 | +}; |
| 51 | + |
| 52 | +const generateQueryString = (query) => { |
| 53 | + const queryString = new URLSearchParams(query); |
| 54 | + return queryString.toString(); |
| 55 | +}; |
| 56 | + |
| 57 | +const claudiaCountdown = () => { |
| 58 | + const container = document.querySelector(".countdown"); |
| 59 | + const time = document.querySelector(".countdown time"); |
| 60 | + const date = new Date(time.getAttribute("datetime")); |
| 61 | + |
| 62 | + const youtubeOptions = { |
| 63 | + autoplay: 0, |
| 64 | + color: "white", |
| 65 | + controls: 1, |
| 66 | + }; |
| 67 | + const leavingYoutubeOptions = { |
| 68 | + ...youtubeOptions, |
| 69 | + end: 60, |
| 70 | + }; |
| 71 | + const returningYoutubeOptions = { |
| 72 | + ...youtubeOptions, |
| 73 | + start: 35, |
| 74 | + }; |
| 75 | + const extras = { |
| 76 | + leaving: { |
| 77 | + youtubeLink: `https://www.youtube.com/embed/s0SUEMGZU04?${generateQueryString( |
| 78 | + leavingYoutubeOptions |
| 79 | + )}`, |
| 80 | + messageBefore: "until Claudia leaves :(", |
| 81 | + messageAfter: "since Claudia left :(", |
| 82 | + messageDone: "that Claudia was gone, but now she's back! 🎊", |
| 83 | + }, |
| 84 | + returning: { |
| 85 | + youtubeLink: `https://www.youtube.com/embed/vNey0PR46F4?${generateQueryString( |
| 86 | + returningYoutubeOptions |
| 87 | + )}`, |
| 88 | + messageBefore: "until Claudia returns... ⏳", |
| 89 | + messageAfter: "since Claudia came back! 🎉🎉", |
| 90 | + date: new Date("Mon Dec 26 2022 18:55:26 PDT"), |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + // eslint-disable-next-line no-undef -- from page variable set within layout |
| 95 | + const action = claudiaAction; |
| 96 | + const data = extras[action]; |
| 97 | + |
| 98 | + if (action === "leaving") { |
| 99 | + const tdeltaSeconds = parseInt((date - extras.returning.date) / 1000, 10); |
| 100 | + const secondsToDone = parseInt( |
| 101 | + (Date.now() - extras.returning.date) / 1000, |
| 102 | + 10 |
| 103 | + ); |
| 104 | + const done = secondsToDone >= 0; |
| 105 | + if (done) { |
| 106 | + updateCounters(container, getRemainingTime(-tdeltaSeconds)); |
| 107 | + setMessage(container, extras.leaving.messageDone); |
| 108 | + embedVid(container, time, extras.returning.youtubeLink); |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Calculate number of seconds |
| 114 | + const tdeltaSecondsOriginal = parseInt((date - Date.now()) / 1000, 10); |
| 115 | + const direction = tdeltaSecondsOriginal > 0 ? -1 : 1; |
| 116 | + const message = |
| 117 | + tdeltaSecondsOriginal > 0 ? data.messageBefore : data.messageAfter; |
| 118 | + let tdeltaSeconds = |
| 119 | + tdeltaSecondsOriginal > 0 ? tdeltaSecondsOriginal : -tdeltaSecondsOriginal; |
| 120 | + |
| 121 | + updateCounters(container, getRemainingTime((tdeltaSeconds += direction))); |
| 122 | + setMessage(container, message); |
| 123 | + if (tdeltaSecondsOriginal <= 0) { |
| 124 | + embedVid(container, time, data.youtubeLink); |
| 125 | + } |
| 126 | + |
| 127 | + const timer = setInterval(() => { |
| 128 | + updateCounters(container, getRemainingTime((tdeltaSeconds += direction))); |
| 129 | + // on page when countdown reaches end datetime |
| 130 | + if (tdeltaSeconds === 0) { |
| 131 | + clearInterval(timer); |
| 132 | + embedVid(container, time, data.youtubeLink); |
| 133 | + setMessage(container, data.messageAfter); |
| 134 | + } |
| 135 | + }, 1000); |
| 136 | +}; |
| 137 | + |
| 138 | +claudiaCountdown(); |
0 commit comments