gitoriaLog in with ident

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit172914a3172914a3components code order (mission 002) 2/4: lib/uploads.hl (demo upload storage), thin demo handlers, project.hl map; gate 220/0, same outputmre172914a3/components/date-picker/date-picker.hl

10.5 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. let 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. let 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. let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  54. let yy = m < 3 ? y - 1 : y
  55. let 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. let y = toNumber(s.slice(0, 4))
  62. let m = toNumber(s.slice(5, 7))
  63. let 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. let py = m == 1 ? y - 1 : y
  72. let pm = m == 1 ? 12 : m - 1
  73. let ny = m == 12 ? y + 1 : y
  74. let nm = m == 12 ? 1 : m + 1
  75. let lead = weekdayOf(y, m, 1)
  76. let n = daysIn(y, m)
  77. let pn = daysIn(py, pm)
  78. let 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. let i = indexOf(name)
  117. let k = keyOf(text)
  118. values[i] = text
  119. let 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. // Colours: the semantic WorldAPI tokens as css variables, each with its palette value as fallback, so a copy of this file is styled in an app that declares no tokens (an app that imports shared/tokens.hl in its styles.hl and sets --color-accent wins). A component's browser module cannot import tokens.hl itself.
  126. Style {
  127. 'date-picker' { display = 'grid' gap = '.75rem' maxWidth = '18rem' }
  128. 'date-field' { position = 'relative' display = 'block' }
  129. 'date-picker input' { width = '100%' boxSizing = 'border-box' font = 'inherit' padding = '.4rem .6rem' color = 'var(--color-text, rgb(195, 200, 205))' background = 'var(--color-background-sunken, rgb(15, 20, 25))' border = '1px solid var(--color-border, rgb(70, 75, 80))' borderRadius = '.4rem' }
  130. 'date-picker input:focus' { outline = '2px solid var(--color-accent, #569bd4)' outlineOffset = '-1px' }
  131. 'date-picker input[aria-invalid="true"]' { borderColor = 'var(--color-danger, #f44747)' }
  132. 'date-picker picker-popup' { display = 'none' position = 'absolute' top = '100%' left = '0' zIndex = '4' width = 'min(22rem, 90vw)' marginTop = '.25rem' outline = 'none' }
  133. 'date-field:focus-within picker-popup' { display = 'block' }
  134. 'date-picker month-view' { display = 'grid' gap = '.5rem' padding = '.5rem' background = 'var(--color-background, rgb(25, 30, 35))' border = '1px solid var(--color-border, rgb(70, 75, 80))' borderRadius = '.5rem' }
  135. 'date-picker month-view header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '.5rem' }
  136. 'date-picker month-view h2' { margin = '0' fontSize = '1.1rem' color = 'var(--color-text-strong, rgb(245, 250, 255))' textAlign = 'center' flex = '1' textTransform = 'capitalize' }
  137. 'date-picker month-view ol' { display = 'grid' gridTemplateColumns = 'repeat(7, minmax(0, 1fr))' margin = '0' padding = '0' listStyle = 'none' }
  138. 'date-picker button' { font = 'inherit' fontWeight = '600' padding = '.4rem .9rem' color = 'var(--color-text, rgb(195, 200, 205))' background = 'transparent' border = '1px solid var(--color-border, rgb(70, 75, 80))' borderRadius = '.4rem' cursor = 'pointer' }
  139. 'date-picker button:hover' { background = 'var(--color-surface, rgb(35, 40, 45))' }
  140. 'date-picker ol.weekdays li' { padding = '.3rem 0' fontSize = '.8rem' fontWeight = '600' color = 'var(--color-text-muted, rgb(145, 150, 155))' textAlign = 'center' }
  141. 'date-picker ol.days li' { minHeight = '2rem' padding = '.3rem' border = '1px solid var(--color-border, rgb(70, 75, 80))' textAlign = 'right' background = 'var(--color-surface, rgb(35, 40, 45))' cursor = 'pointer' }
  142. 'date-picker ol.days li:hover' { background = 'var(--color-background-sunken, rgb(15, 20, 25))' }
  143. 'date-picker ol.days time' { color = 'var(--color-text, rgb(195, 200, 205))' fontSize = '.95rem' }
  144. 'date-picker ol.days li.outside' { background = 'var(--color-background, rgb(25, 30, 35))' }
  145. 'date-picker ol.days li.outside time' { color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  146. 'date-picker ol.days li.today' { outline = '2px solid var(--color-accent, #569bd4)' outlineOffset = '-2px' }
  147. 'date-picker ol.days li.selected' { background = 'var(--color-accent, #569bd4)' }
  148. 'date-picker ol.days li.selected time' { color = 'var(--color-accent-text, rgb(15, 20, 25))' }
  149. }
  150. View {
  151. datePicker {
  152. for (r of rows) {
  153. dateField {
  154. 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) } }
  155. pickerPopup { tabindex = "-1"
  156. monthView {
  157. header {
  158. button { type = "button" class = "quiet prev" title = prevLabel "‹" on click(e) { emit datePrev(e) } }
  159. h2 { title }
  160. button { type = "button" class = "quiet next" title = nextLabel "›" on click(e) { emit dateNext(e) } }
  161. }
  162. ol { class = "weekdays" for (wd of weekdays) { li { wd } } }
  163. 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) } } } }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. // the browser knows today and its language; a field gets focus first, so this runs before a day is clicked
  171. on dateOpen(name) {
  172. let now = new Date()
  173. let lang = navigator.language != null ? navigator.language : 'en'
  174. let t = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  175. if (t != today) {
  176. let mf = Intl.DateTimeFormat(lang, { month = 'long' })
  177. let df = Intl.DateTimeFormat(lang, { weekday = 'short' })
  178. let ms = []
  179. let ds = []
  180. let i = 0
  181. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  182. i = 0
  183. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  184. today = t
  185. monthNames = ms
  186. weekdayNames = ds
  187. weekdays = ds
  188. let de = lang.slice(0, 2) == 'de'
  189. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  190. nextLabel = de ? 'Nächster Monat' : 'Next month'
  191. }
  192. let text = values[indexOf(name)]
  193. selected = keyOf(text) != 0 ? text : ''
  194. 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))) }
  195. }
  196. on dateTyped(name, text) { setValue(name, text, true) }
  197. on dateKey(key) {
  198. if (key == 'Escape' || key == 'Enter') { document.activeElement.blur() }
  199. }
  200. on datePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }
  201. on dateNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }
  202. on datePick(name, d) {
  203. setValue(name, d, false)
  204. document.activeElement.blur()
  205. }

Branches

Latest commits

  • 172914a3components code order (mission 002) 2/4: lib/uploads.hl (demo upload storage), thin demo handlers, project.hl map; gate 220/0, same outputmre
  • d29f35b1components code order (mission 002) 1/4: styles.hl -> components/styles.hl; gate 220/0, same outputmre
  • 09945750components: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, http1 773de63e); gate 220/0mre
  • ede4d19acomponents: Hybriel master 190aa11d (fc838894 GC correctness, #126 closure scopes, #127); gate 220/0mre
  • 2bb9a935components: Hybriel master 8efba065 (re-vendor round 069: #126 memory, #48 lambda copy)mre
  • e4f01768antcolony#40: mission references point to the moved missionsmre
  • d32b44e2antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 0fb80b57components: Hybriel master ff51cf46 (re-vendor round)mre
  • 57408b6ecomponents#14: installable app (manifest, service worker, offline index), own iconmre
  • 317bd09fdeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • becbd59bcomponents#13: modal dialog (<dialog> based, focus wrap, backdrop/Esc/x close with reason, scroll lock, footer buttons)mre
  • 367dd19adeploy.sh: never send .git or .gitignore to Byrodinmre
  • 4b4dabe5State of 2026-09-27, before the move to gitoriamre