150 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-11-18 02:04:13 +00:00
import plural from "./Utils/plural"
2017-07-01 11:35:21 +00:00
const oneSecond = 1000
const oneMinute = 60 * oneSecond
const oneHour = 60 * oneMinute
const oneDay = 24 * oneHour
const oneWeek = 7 * oneDay
const oneMonth = 30 * oneDay
const oneYear = 365.25 * oneDay
2017-06-30 15:51:17 +00:00
2019-11-17 09:25:14 +00:00
export let monthNames = [
2017-06-30 15:51:17 +00:00
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
]
2019-11-17 09:25:14 +00:00
export let dayNames = [
2017-06-30 15:51:17 +00:00
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
2017-07-01 11:35:21 +00:00
function getRemainingTime(remaining: number): string {
2019-11-17 09:25:14 +00:00
const remainingAbs = Math.abs(remaining)
2017-07-01 11:35:21 +00:00
if(remainingAbs >= oneYear) {
return plural(Math.round(remaining / oneYear), "year")
}
if(remainingAbs >= oneMonth) {
return plural(Math.round(remaining / oneMonth), "month")
}
if(remainingAbs >= oneWeek) {
return plural(Math.round(remaining / oneWeek), "week")
}
if(remainingAbs >= oneDay) {
return plural(Math.round(remaining / oneDay), "day")
}
if(remainingAbs >= oneHour) {
return plural(Math.round(remaining / oneHour), " hour")
}
if(remainingAbs >= oneMinute) {
return plural(Math.round(remaining / oneMinute), " minute")
}
if(remainingAbs >= oneSecond) {
return plural(Math.round(remaining / oneSecond), " second")
}
2017-11-22 11:20:57 +00:00
2017-07-01 11:35:21 +00:00
return "Just now"
}
2017-07-07 15:16:40 +00:00
export function displayAiringDate(element: HTMLElement, now: Date) {
2019-04-19 13:12:33 +00:00
if(!element.dataset.startDate || !element.dataset.endDate) {
2018-06-28 06:30:24 +00:00
element.textContent = ""
2017-07-10 18:39:50 +00:00
return
}
2019-11-17 09:25:14 +00:00
const startDate = new Date(element.dataset.startDate)
const endDate = new Date(element.dataset.endDate)
2017-06-30 15:51:17 +00:00
let h = startDate.getHours()
let m = startDate.getMinutes()
2019-11-17 09:25:14 +00:00
const startTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
2017-06-30 15:51:17 +00:00
h = endDate.getHours()
m = endDate.getMinutes()
2019-11-17 09:25:14 +00:00
const endTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
2017-11-22 11:20:57 +00:00
2017-07-01 11:35:21 +00:00
let airingVerb = "will be airing"
2019-11-17 09:25:14 +00:00
const remaining = startDate.getTime() - now.getTime()
2017-07-01 11:35:21 +00:00
let remainingString = getRemainingTime(remaining)
// Add "ago" if the date is in the past
if(remainingString.startsWith("-")) {
remainingString = remainingString.substring(1) + " ago"
2017-06-30 15:51:17 +00:00
}
2018-06-28 06:30:24 +00:00
element.textContent = remainingString
2017-06-30 15:51:17 +00:00
2017-07-01 11:35:21 +00:00
if(remaining < 0) {
airingVerb = "aired"
2017-06-30 15:51:17 +00:00
}
2019-11-17 09:25:14 +00:00
const tooltip = "Episode " + element.dataset.episodeNumber + " " + airingVerb + " " + dayNames[startDate.getDay()] + " from " + startTime + " - " + endTime
2018-04-18 12:34:33 +00:00
if(element.classList.contains("no-tip")) {
element.title = tooltip
} else {
element.setAttribute("aria-label", tooltip)
element.classList.add("tip")
}
2017-07-07 15:16:40 +00:00
}
export function displayDate(element: HTMLElement, now: Date) {
2019-04-19 13:50:52 +00:00
if(!element.dataset.date) {
2018-06-28 06:30:24 +00:00
element.textContent = ""
2017-07-10 18:39:50 +00:00
return
}
2019-11-17 09:25:14 +00:00
const startDate = new Date(element.dataset.date)
2017-07-07 15:16:40 +00:00
2019-11-17 09:25:14 +00:00
const h = startDate.getHours()
const m = startDate.getMinutes()
const startTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
2017-11-22 11:20:57 +00:00
2019-11-17 09:25:14 +00:00
const remaining = startDate.getTime() - now.getTime()
2017-07-07 15:16:40 +00:00
let remainingString = getRemainingTime(remaining)
// Add "ago" if the date is in the past
if(remainingString.startsWith("-")) {
remainingString = remainingString.substring(1) + " ago"
}
2018-06-28 06:30:24 +00:00
element.textContent = remainingString
2019-11-17 09:25:14 +00:00
const tooltip = dayNames[startDate.getDay()] + " " + startTime
2017-07-07 15:16:40 +00:00
2018-04-18 14:27:08 +00:00
if(element.classList.contains("no-tip")) {
element.title = tooltip
} else {
element.setAttribute("aria-label", tooltip)
element.classList.add("tip")
}
2017-11-22 11:20:57 +00:00
}
2019-04-19 13:12:33 +00:00
export function displayTime(element: HTMLElement) {
if(!element.dataset.date) {
2018-06-28 06:30:24 +00:00
element.textContent = ""
2017-11-22 11:20:57 +00:00
return
}
2019-11-17 09:25:14 +00:00
const startDate = new Date(element.dataset.date)
2017-11-22 11:20:57 +00:00
2019-11-17 09:25:14 +00:00
const h = startDate.getHours()
const m = startDate.getMinutes()
const startTime = (h <= 9 ? "0" + h : h) + ":" + (m <= 9 ? "0" + m : m)
2017-11-22 11:20:57 +00:00
2018-06-28 06:30:24 +00:00
element.textContent = startTime
2019-11-17 09:44:30 +00:00
}