-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime-ago.js
71 lines (56 loc) · 1.24 KB
/
time-ago.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"use strict"
class TimeAgo extends HTMLTimeElement
{
get dateTime()
{
return new Date(super.dateTime)
}
set dateTime(value)
{
super.dateTime = value instanceof Date
? value.toISOString()
: value
}
get isShort()
{
return "isShort" in this.dataset
}
set isShort(value)
{
let data = this.dataset
if (value) data.isShort = ""
else delete data.isShort
}
connectedCallback()
{
let attr = this.closest("[lang]")
this._lang = attr && attr.lang || "en"
}
disconnectedCallback()
{
clearTimeout(this._id)
}
attributeChangedCallback()
{
clearTimeout(this._id)
let { isShort } = this
let lang = this._lang + (isShort ? "-short" : "")
let date = moment(this.dateTime).locale(lang)
this.title = date.format(isShort ? "lll" : "LLLL")
const render = () =>
{
this.textContent = date.fromNow()
this._id = setTimeout(render, timeout(date))
}
render()
}
}
TimeAgo.observedAttributes =
[
"datetime",
"data-is-short",
]
customElements.define("time-ago", TimeAgo,
{
extends: "time",
})