gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchvl27-resetkeya06fc778Reset virtual-list window when the host changes resetKeyAtlasvl27-resetkey/components/date-picker/date-picker.hl

10.9 KB

  1. // date-picker.hl — THE DATE PICKER for input fields, all Hybriel (no JavaScript file): `DatePicker { names = "arrival,departure" }`
  2. // makes one text field per name; click a field, a month opens under it, click a day and the field shows it as YYYY-MM-DD.
  3. // Typing works too. Each field posts in a form by its name. The month grid is built here (plain integers, so the server render
  4. // and the browser agree); the browser's own today and language come in when a field gets focus (`Date` and `Intl` are browser
  5. // globals of View handlers). The popup is shown by CSS while its field or the popup has focus (:focus-within), so no `if`
  6. // re-creates the DOM. Use ONE `DatePicker` per page (a composed component's members are the page's state) — give it all the fields.
  7. import { now, timestamp } from 'hl:time'
  8. // the server's today (UTC) for the first render only; the browser corrects it when a field gets focus
  9. serverToday = () => { return timestamp(now()).slice(0, 10) }
  10. String names = 'date'
  11. Hybrid fields = names.split(',')
  12. Hybrid values = blanks(fields)
  13. Hybrid rows = makeRows(fields)
  14. String today = serverToday()
  15. Number year = toNumber(today.slice(0, 4))
  16. Number month = toNumber(today.slice(5, 7))
  17. Hybrid monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  18. Hybrid weekdayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  19. Hybrid weekdays = weekdayNames
  20. String title = monthNames[month - 1] + ' ' + year
  21. String selected = ''
  22. Hybrid days = build(year, month, today, '')
  23. String prevLabel = 'Previous month'
  24. String nextLabel = 'Next month'
  25. String placeholder = 'YYYY-MM-DD'
  26. blanks(list) {
  27. out = []
  28. let i = 0
  29. while (i < list.length) { out.push('') i = i + 1 }
  30. return out
  31. }
  32. // `rows` is never reassigned: re-rendering the rows would re-create the input elements, and a re-created input on a slow
  33. // round trip shows an empty value although its attribute is set. A field's text and its invalid mark are written to the
  34. // element itself (see field()) instead of being bound.
  35. makeRows(list) {
  36. out = []
  37. let i = 0
  38. while (i < list.length) {
  39. out.push({ name = list[i] hint = list[i] + ', ' + placeholder })
  40. i = i + 1
  41. }
  42. return out
  43. }
  44. pad(n) { return n < 10 ? '0' + n : '' + n }
  45. leap(y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 }
  46. daysIn(y, m) {
  47. if (m == 2) { return leap(y) ? 29 : 28 }
  48. if (m == 4 || m == 6 || m == 9 || m == 11) { return 30 }
  49. return 31
  50. }
  51. // 0 = Monday … 6 = Sunday (Sakamoto's method gives 0 = Sunday)
  52. weekdayOf(y, m, d) {
  53. t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  54. yy = m < 3 ? y - 1 : y
  55. sun = (yy + (yy - yy % 4) / 4 - (yy - yy % 100) / 100 + (yy - yy % 400) / 400 + t[m - 1] + d) % 7
  56. return (sun + 6) % 7
  57. }
  58. // "YYYY-MM-DD" → yyyymmdd as a number (strings have no ordering operators), or 0 when it is not a real day
  59. keyOf(s) {
  60. if (s.length != 10 || s.slice(4, 5) != '-' || s.slice(7, 8) != '-') { return 0 }
  61. y = toNumber(s.slice(0, 4))
  62. m = toNumber(s.slice(5, 7))
  63. d = toNumber(s.slice(8, 10))
  64. if (y == null || m == null || d == null) { return 0 }
  65. if (m < 1 || m > 12 || d < 1 || d > daysIn(y, m)) { return 0 }
  66. if (('' + (10000 + y)).slice(1) + '-' + pad(m) + '-' + pad(d) != s) { return 0 }
  67. return y * 10000 + m * 100 + d
  68. }
  69. // 42 days (six weeks of seven) so the popup keeps its height; each carries its state as a class
  70. build(y, m, todayText, chosen) {
  71. py = m == 1 ? y - 1 : y
  72. pm = m == 1 ? 12 : m - 1
  73. ny = m == 12 ? y + 1 : y
  74. nm = m == 12 ? 1 : m + 1
  75. lead = weekdayOf(y, m, 1)
  76. let n = daysIn(y, m)
  77. pn = daysIn(py, pm)
  78. out = []
  79. let day = 1 - lead
  80. let i = 0
  81. while (i < 42) {
  82. let cy = y
  83. let cm = m
  84. let cd = day
  85. let cls = ''
  86. if (day < 1) { cy = py cm = pm cd = pn + day cls = 'outside' }
  87. if (day > n) { cy = ny cm = nm cd = day - n cls = 'outside' }
  88. let date = ('' + (10000 + cy)).slice(1) + '-' + pad(cm) + '-' + pad(cd)
  89. if (date == todayText) { cls = cls + ' today' }
  90. if (date == chosen) { cls = cls + ' selected' }
  91. out.push({ n = cd date = date cls = cls })
  92. day = day + 1
  93. i = i + 1
  94. }
  95. return out
  96. }
  97. show(y, m) {
  98. year = y
  99. month = m
  100. title = monthNames[m - 1] + ' ' + y
  101. days = build(y, m, today, selected)
  102. }
  103. // the row of a field name
  104. indexOf(name) {
  105. let i = 0
  106. while (i < fields.length) {
  107. if (fields[i] == name) { return i }
  108. i = i + 1
  109. }
  110. return 0
  111. }
  112. // the input element of a field name
  113. field(name) { return document.querySelector('date-picker input[name="' + name + '"]') }
  114. // a field's text changed (typed or picked): keep it, mark it when it is not a real day, show its month
  115. setValue(name, text, showMonth) {
  116. i = indexOf(name)
  117. k = keyOf(text)
  118. values[i] = text
  119. el = field(name)
  120. if (el.value != text) { el.value = text }
  121. el.setAttribute('aria-invalid', text != '' && k == 0 ? 'true' : 'false')
  122. selected = k != 0 ? text : ''
  123. if (k != 0 && showMonth) { show(toNumber(text.slice(0, 4)), toNumber(text.slice(5, 7))) } else { show(year, month) }
  124. }
  125. // Look: black, white and the greys #555 #999 #ddd #f2f2f2 (the library's base, named in shared/base.hl; README "Look"). Every colour is a semantic token
  126. // with its base value as fallback, `var(--color-…, #000)`, so a bare copy looks right and an app that declares the tokens (layouts.worldapi.org's
  127. // theme) restyles it. No frames: a border or ring only where it means something (focus, today, an invalid field). A component's browser module
  128. // cannot import tokens itself.
  129. Style {
  130. datePicker {
  131. display = 'grid'
  132. gap = '.75rem'
  133. maxWidth = '18rem'
  134. input {
  135. width = '100%'
  136. boxSizing = 'border-box'
  137. minHeight = '2.75rem'
  138. font = 'inherit'
  139. padding = '.4rem .6rem'
  140. color = 'var(--color-text, #000)'
  141. background = 'var(--color-background-sunken, #f2f2f2)'
  142. border = '0'
  143. borderRadius = '.4rem'
  144. focus {
  145. outline = '2px solid var(--color-accent, #555)'
  146. outlineOffset = '-1px'
  147. }
  148. }
  149. // Class or attribute assigned by the View or code; no compound-selector form.
  150. 'input[aria-invalid="true"]' {
  151. outline = '2px dashed var(--color-danger, #000)'
  152. outlineOffset = '-2px'
  153. }
  154. pickerPopup {
  155. display = 'none'
  156. position = 'absolute'
  157. top = '100%'
  158. left = '0'
  159. zIndex = '4'
  160. width = 'min(22rem, 90vw)'
  161. marginTop = '.25rem'
  162. outline = 'none'
  163. }
  164. monthView {
  165. display = 'grid'
  166. gap = '.5rem'
  167. padding = '.5rem'
  168. background = 'var(--color-background, #fff)'
  169. border = '0'
  170. borderRadius = '.5rem'
  171. boxShadow = '0 .5rem 1.5rem rgba(0, 0, 0, .25)'
  172. header {
  173. display = 'flex'
  174. alignItems = 'center'
  175. justifyContent = 'space-between'
  176. gap = '.5rem'
  177. }
  178. h2 {
  179. margin = '0'
  180. fontSize = '1.1rem'
  181. color = 'var(--color-text-strong, #000)'
  182. textAlign = 'center'
  183. flex = '1'
  184. textTransform = 'capitalize'
  185. }
  186. ol {
  187. display = 'grid'
  188. gridTemplateColumns = 'repeat(7, minmax(0, 1fr))'
  189. gap = '.2rem'
  190. margin = '0'
  191. padding = '0'
  192. listStyle = 'none'
  193. }
  194. }
  195. button {
  196. font = 'inherit'
  197. fontWeight = '600'
  198. minWidth = '2.75rem'
  199. minHeight = '2.75rem'
  200. padding = '.4rem .9rem'
  201. color = 'var(--color-text, #000)'
  202. background = 'transparent'
  203. border = '0'
  204. borderRadius = '.4rem'
  205. cursor = 'pointer'
  206. hover { background = 'var(--color-surface, #f2f2f2)' }
  207. }
  208. // Class or attribute assigned by the View or code; no compound-selector form.
  209. 'ol.weekdays' {
  210. li {
  211. padding = '.3rem 0'
  212. fontSize = '.8rem'
  213. fontWeight = '600'
  214. color = 'var(--color-text-muted, #555)'
  215. textAlign = 'center'
  216. }
  217. }
  218. // Class or attribute assigned by the View or code; no compound-selector form.
  219. 'ol.days' {
  220. li {
  221. minHeight = '2.75rem'
  222. padding = '.3rem'
  223. border = '0'
  224. borderRadius = '.3rem'
  225. textAlign = 'right'
  226. background = 'var(--color-surface, #f2f2f2)'
  227. cursor = 'pointer'
  228. hover { background = 'var(--color-background-sunken, #ddd)' }
  229. }
  230. time {
  231. color = 'var(--color-text, #000)'
  232. fontSize = '.95rem'
  233. }
  234. // Class or attribute assigned by the View or code; no compound-selector form.
  235. 'li.outside' {
  236. background = 'var(--color-background, #fff)'
  237. time { color = 'var(--color-text-muted, #555)' }
  238. }
  239. // Class or attribute assigned by the View or code; no compound-selector form.
  240. 'li.today' {
  241. outline = '2px solid var(--color-accent, #000)'
  242. outlineOffset = '-2px'
  243. }
  244. // Class or attribute assigned by the View or code; no compound-selector form.
  245. 'li.selected' {
  246. background = 'var(--color-accent, #000)'
  247. time { color = 'var(--color-accent-text, #fff)' }
  248. }
  249. }
  250. }
  251. dateField {
  252. position = 'relative'
  253. display = 'block'
  254. focusWithin {
  255. pickerPopup { display = 'block' }
  256. }
  257. }
  258. }
  259. View {
  260. datePicker {
  261. for (r of rows) {
  262. dateField {
  263. input { name = r.name type = "text" inputmode = "numeric" autocomplete = "off" maxlength = "10" title = r.hint placeholder = placeholder 'aria-invalid' = "false" on focus(e) { emit dateOpen(r.name) } on input(e) { emit dateTyped(r.name, e.target.value) } on keydown(e) { emit dateKey(e.key) } }
  264. pickerPopup { tabindex = "-1"
  265. monthView {
  266. header {
  267. button { type = "button" class = "quiet prev" title = prevLabel "‹" on click(e) { emit datePrev(e) } }
  268. h2 { title }
  269. button { type = "button" class = "quiet next" title = nextLabel "›" on click(e) { emit dateNext(e) } }
  270. }
  271. ol { class = "weekdays" for (wd of weekdays) { li { wd } } }
  272. ol { class = "days" for (d of days) { li { class = d.cls time { datetime = d.date d.n } on click(e) { emit datePick(r.name, d.date) } } } }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. // the browser knows today; the names follow the page language (<html lang>, English when none); a field gets focus first, so this runs before a day is clicked
  280. on dateOpen(name) {
  281. let now = new Date()
  282. lang = document.documentElement.lang != '' ? document.documentElement.lang : 'en'
  283. t = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  284. if (t != today || lang.slice(0, 2) != 'en') {
  285. mf = Intl.DateTimeFormat(lang, { month = 'long' timeZone = 'UTC' })
  286. df = Intl.DateTimeFormat(lang, { weekday = 'short' timeZone = 'UTC' })
  287. ms = []
  288. ds = []
  289. let i = 0
  290. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  291. i = 0
  292. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  293. today = t
  294. monthNames = ms
  295. weekdayNames = ds
  296. weekdays = ds
  297. de = lang.slice(0, 2) == 'de'
  298. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  299. nextLabel = de ? 'Nächster Monat' : 'Next month'
  300. }
  301. text = values[indexOf(name)]
  302. selected = keyOf(text) != 0 ? text : ''
  303. if (selected != '') { show(toNumber(text.slice(0, 4)), toNumber(text.slice(5, 7))) } else { show(toNumber(today.slice(0, 4)), toNumber(today.slice(5, 7))) }
  304. }
  305. on dateTyped(name, text) { setValue(name, text, true) }
  306. on dateKey(key) {
  307. if (key == 'Escape' || key == 'Enter') { document.activeElement.blur() }
  308. }
  309. on datePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }
  310. on dateNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }
  311. on datePick(name, d) {
  312. setValue(name, d, false)
  313. document.activeElement.blur()
  314. }

Branches

Latest commits

  • a06fc778Reset virtual-list window when the host changes resetKeyAtlas
  • 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
  • 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
  • f5f131c5Record AP29 autoplay comparisons and passing gatesmre
  • 51c38187Gate media autoplay on first load and after client mountmre
  • a3f2223dRecord successful B44 deployment and live proofmre
  • 06c1e021Record passing B44 browser rerunmre
  • c9861533Record B44 component proof and browser gate blockermre
  • 1b7413beIgnore detached media events and space demo option outputsmre
  • ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
  • df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
  • 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
  • 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
  • 9b872cedRecord IC28 deployment and live icon verificationmre
  • 46fefaa7Count the icons entry in the offline home-page checkmre
  • c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre
  • 5e090ffdRecord IC28 review evidence and gate resultsmre
  • adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre