Repository navigation
Expand file tree
/
Copy pathversion-tag.js
More file actions
165 lines (148 loc) · 5.7 KB
/
Copy pathversion-tag.js
File metadata and controls
165 lines (148 loc) · 5.7 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Reusable version reveal: a subtle footer control that opens a compact popover.
*
* Drop into another app: include version-tag.css, load build-info.js (or pass buildIso),
* then call initVersionTag({ root, version, buildIso, changelogUrl, label, strings }).
*
* label — visible control text (e.g. the copyright line). Version is not shown until open.
* strings.updatedLabel — e.g. "Updated" / "Bijgewerkt"
* strings.changelogLabel — link text (same target as your existing changelog URL)
* strings.tagAria — include "{v}" for the version number, e.g. "Show version {v} and release notes"
*/
const formatBuildTimeAmsterdam = (iso) => {
if (!iso) return null
const date = new Date(iso)
if (Number.isNaN(date.getTime())) return null
const parts = new Intl.DateTimeFormat('nl-NL', {
timeZone: 'Europe/Amsterdam',
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23',
timeZoneName: 'short',
}).formatToParts(date)
const pick = (type) => parts.find((p) => p.type === type)?.value ?? ''
const datePart = `${pick('day')}-${pick('month')}-${pick('year')}`
const timePart = `${pick('hour')}:${pick('minute')} ${pick('timeZoneName')}`.trim()
return `${datePart} ${timePart}`.trim()
}
const initVersionTag = ({ root, version, buildIso, changelogUrl, label, strings }) => {
if (!root) return { update () {} }
root.classList.add('version-tag')
const panelId = `${root.id || 'version-tag'}-panel`
const button = document.createElement('button')
button.type = 'button'
button.className = 'version-tag-button'
button.setAttribute('aria-expanded', 'false')
button.setAttribute('aria-controls', panelId)
const popover = document.createElement('div')
popover.className = 'version-tag-popover'
popover.id = panelId
popover.hidden = true
popover.setAttribute('role', 'dialog')
popover.setAttribute('aria-modal', 'false')
const meta = document.createElement('div')
meta.className = 'version-tag-meta'
const versionEl = document.createElement('span')
versionEl.className = 'version-tag-version'
const afterVersion = document.createElement('span')
afterVersion.className = 'version-tag-tail'
const sepAfterVersion = document.createElement('span')
sepAfterVersion.className = 'version-tag-sep'
sepAfterVersion.setAttribute('aria-hidden', 'true')
sepAfterVersion.textContent = '·'
afterVersion.append(document.createTextNode('\u00a0'), sepAfterVersion, document.createTextNode(' '))
const updatedLine = document.createElement('span')
updatedLine.className = 'version-tag-updated-line'
const updatedLabel = document.createElement('span')
updatedLabel.className = 'version-tag-updated-label'
const updatedTime = document.createElement('time')
updatedLine.append(updatedLabel, document.createTextNode(' '), updatedTime)
const tail = document.createElement('span')
tail.className = 'version-tag-tail'
const sep = document.createElement('span')
sep.className = 'version-tag-sep'
sep.setAttribute('aria-hidden', 'true')
sep.textContent = '·'
const changelog = document.createElement('a')
changelog.className = 'version-tag-changelog'
changelog.target = '_blank'
changelog.rel = 'noopener'
tail.append(document.createTextNode('\u00a0'), sep, document.createTextNode(' '), changelog)
meta.append(versionEl, afterVersion, updatedLine, tail)
popover.append(meta)
root.append(button, popover)
let open = false
const setOpen = (next) => {
open = next
popover.hidden = !open
button.setAttribute('aria-expanded', String(open))
}
const close = () => setOpen(false)
button.addEventListener('click', (e) => {
e.stopPropagation()
setOpen(!open)
})
document.addEventListener('click', (e) => {
if (!open) return
if (e.target.closest('.version-tag') === root) return
close()
})
document.addEventListener('keydown', (e) => {
if (e.key !== 'Escape' || !open) return
close()
button.focus()
e.stopPropagation()
e.preventDefault()
}, true)
const apply = ({ version: v, buildIso: iso, changelogUrl: url, label: nextLabel, strings: s }) => {
const ver = v ?? version
const stamp = iso !== undefined ? iso : buildIso
const href = url ?? changelogUrl
const copy = s ?? strings
const controlLabel = nextLabel !== undefined ? nextLabel : label
button.textContent = controlLabel || `v${ver}`
button.setAttribute(
'aria-label',
(copy.tagAria || 'Show version {v} and release notes').replace('{v}', ver)
)
popover.setAttribute('aria-label', copy.tagAria ? copy.tagAria.replace('{v}', ver) : `v${ver}`)
versionEl.textContent = `v${ver}`
updatedLabel.textContent = copy.updatedLabel || 'Updated'
const formatted = formatBuildTimeAmsterdam(stamp)
if (formatted) {
updatedTime.dateTime = stamp
updatedTime.replaceChildren()
const spaceIdx = formatted.indexOf(' ')
if (spaceIdx > 0) {
updatedTime.append(
document.createTextNode(formatted.slice(0, spaceIdx)),
document.createTextNode(' '),
document.createTextNode(formatted.slice(spaceIdx + 1))
)
} else {
updatedTime.append(document.createTextNode(formatted))
}
updatedTime.hidden = false
updatedLine.hidden = false
afterVersion.hidden = false
} else {
updatedTime.removeAttribute('datetime')
updatedTime.replaceChildren()
updatedTime.hidden = true
updatedLine.hidden = true
afterVersion.hidden = true
}
changelog.href = href
changelog.textContent = copy.changelogLabel || 'Changelog'
}
apply({ version, buildIso, changelogUrl, label, strings })
return {
update (opts) {
apply(opts)
},
close,
}
}