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/range-picker/range-picker.hl

10.7 KB

  1. // range-picker.hl — THE DATE RANGE PICKER, all Hybriel: one text field ("YYYY-MM-DD – YYYY-MM-DD", typing works) that opens a
  2. // month under it; a click on a day sets the start, a second click sets the end (an earlier second click swaps them), the
  3. // days between are highlighted. The month grid is built here (no Date arithmetic: plain integers, so the server render and
  4. // the browser agree); the browser's own today and language come in when the field gets focus (`Date` and `Intl` are
  5. // browser globals of View handlers). The popup is shown by CSS while the field or the popup has focus (:focus-within), so
  6. // no `if` re-creates the DOM. The field posts in a form by its `name`.
  7. import { now, timestamp } from 'hl:time'
  8. // the server's today (UTC) for the first render only; the browser corrects it when the field gets focus
  9. serverToday = () => { return timestamp(now()).slice(0, 10) }
  10. String name = 'range'
  11. String value = ''
  12. String kept = ''
  13. String pending = ''
  14. String start = ''
  15. String end = ''
  16. String invalidText = 'false'
  17. String today = serverToday()
  18. Number year = toNumber(today.slice(0, 4))
  19. Number month = toNumber(today.slice(5, 7))
  20. Hybrid monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  21. Hybrid weekdayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  22. Hybrid weekdays = weekdayNames
  23. String title = monthNames[month - 1] + ' ' + year
  24. Hybrid days = build(year, month, today, '', '')
  25. String prevLabel = 'Previous month'
  26. String nextLabel = 'Next month'
  27. String placeholder = 'YYYY-MM-DD – YYYY-MM-DD'
  28. String sep = ' – '
  29. pad(n) { return n < 10 ? '0' + n : '' + n }
  30. leap(y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 }
  31. daysIn(y, m) {
  32. if (m == 2) { return leap(y) ? 29 : 28 }
  33. if (m == 4 || m == 6 || m == 9 || m == 11) { return 30 }
  34. return 31
  35. }
  36. // 0 = Monday … 6 = Sunday (Sakamoto's method gives 0 = Sunday)
  37. weekdayOf(y, m, d) {
  38. let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  39. let yy = m < 3 ? y - 1 : y
  40. let sun = (yy + (yy - yy % 4) / 4 - (yy - yy % 100) / 100 + (yy - yy % 400) / 400 + t[m - 1] + d) % 7
  41. return (sun + 6) % 7
  42. }
  43. // "YYYY-MM-DD" → yyyymmdd as a number (strings have no ordering operators), or 0 when it is not a real day
  44. keyOf(s) {
  45. if (s.length != 10 || s.slice(4, 5) != '-' || s.slice(7, 8) != '-') { return 0 }
  46. let y = toNumber(s.slice(0, 4))
  47. let m = toNumber(s.slice(5, 7))
  48. let d = toNumber(s.slice(8, 10))
  49. if (y == null || m == null || d == null) { return 0 }
  50. if (m < 1 || m > 12 || d < 1 || d > daysIn(y, m)) { return 0 }
  51. if (('' + (10000 + y)).slice(1) + '-' + pad(m) + '-' + pad(d) != s) { return 0 }
  52. return y * 10000 + m * 100 + d
  53. }
  54. // 42 days (six weeks of seven) so the popup keeps its height; each carries its state as a class
  55. build(y, m, todayText, a, b) {
  56. let py = m == 1 ? y - 1 : y
  57. let pm = m == 1 ? 12 : m - 1
  58. let ny = m == 12 ? y + 1 : y
  59. let nm = m == 12 ? 1 : m + 1
  60. let lead = weekdayOf(y, m, 1)
  61. let n = daysIn(y, m)
  62. let pn = daysIn(py, pm)
  63. let ka = keyOf(a)
  64. let kb = keyOf(b)
  65. let out = []
  66. let day = 1 - lead
  67. let i = 0
  68. while (i < 42) {
  69. let cy = y
  70. let cm = m
  71. let cd = day
  72. let cls = ''
  73. if (day < 1) { cy = py cm = pm cd = pn + day cls = 'outside' }
  74. if (day > n) { cy = ny cm = nm cd = day - n cls = 'outside' }
  75. let date = ('' + (10000 + cy)).slice(1) + '-' + pad(cm) + '-' + pad(cd)
  76. let k = cy * 10000 + cm * 100 + cd
  77. if (date == todayText) { cls = cls + ' today' }
  78. if (ka != 0 && kb != 0 && k > ka && k < kb) { cls = cls + ' between' }
  79. if ((ka != 0 && k == ka) || (kb != 0 && k == kb)) { cls = cls + ' selected' }
  80. out.push({ n = cd date = date cls = cls })
  81. day = day + 1
  82. i = i + 1
  83. }
  84. return out
  85. }
  86. show(y, m) {
  87. year = y
  88. month = m
  89. title = monthNames[m - 1] + ' ' + y
  90. days = build(y, m, today, start, end)
  91. }
  92. // the field's text → sets start / end when it is two real days (either order), else marks the field
  93. apply(text) {
  94. let n = text.length
  95. let a = n >= 21 ? text.slice(0, 10) : ''
  96. let b = n >= 21 ? text.slice(n - 10, n) : ''
  97. let ka = keyOf(a)
  98. let kb = keyOf(b)
  99. if (ka != 0 && kb != 0) {
  100. start = ka <= kb ? a : b
  101. end = ka <= kb ? b : a
  102. invalidText = 'false'
  103. return true
  104. }
  105. start = ''
  106. end = ''
  107. invalidText = text != '' ? 'true' : 'false'
  108. return false
  109. }
  110. // the month of a "YYYY-MM-DD" day, when it is not the month on show
  111. gotoDay(s) {
  112. if (keyOf(s) == 0) { return null }
  113. show(toNumber(s.slice(0, 4)), toNumber(s.slice(5, 7)))
  114. }
  115. // 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.
  116. Style {
  117. 'range-picker' { position = 'relative' display = 'block' maxWidth = '18rem' }
  118. 'range-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' }
  119. 'range-picker input:focus' { outline = '2px solid var(--color-accent, #569bd4)' outlineOffset = '-1px' }
  120. 'range-picker input[aria-invalid="true"]' { borderColor = 'var(--color-danger, #f44747)' }
  121. 'range-picker picker-popup' { display = 'none' position = 'absolute' top = '100%' left = '0' zIndex = '4' width = 'min(22rem, 90vw)' marginTop = '.25rem' outline = 'none' }
  122. 'range-picker:focus-within picker-popup' { display = 'block' }
  123. 'range-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' }
  124. 'range-picker month-view header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '.5rem' }
  125. 'range-picker month-view h2' { margin = '0' fontSize = '1.1rem' color = 'var(--color-text-strong, rgb(245, 250, 255))' textAlign = 'center' flex = '1' textTransform = 'capitalize' }
  126. 'range-picker month-view ol' { display = 'grid' gridTemplateColumns = 'repeat(7, minmax(0, 1fr))' margin = '0' padding = '0' listStyle = 'none' }
  127. 'range-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' }
  128. 'range-picker button:hover' { background = 'var(--color-surface, rgb(35, 40, 45))' }
  129. 'range-picker ol.weekdays li' { padding = '.3rem 0' fontSize = '.8rem' fontWeight = '600' color = 'var(--color-text-muted, rgb(145, 150, 155))' textAlign = 'center' }
  130. 'range-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' }
  131. 'range-picker ol.days li:hover' { background = 'var(--color-background-sunken, rgb(15, 20, 25))' }
  132. 'range-picker ol.days time' { color = 'var(--color-text, rgb(195, 200, 205))' fontSize = '.95rem' }
  133. 'range-picker ol.days li.outside' { background = 'var(--color-background, rgb(25, 30, 35))' }
  134. 'range-picker ol.days li.outside time' { color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  135. 'range-picker ol.days li.today' { outline = '2px solid var(--color-accent, #569bd4)' outlineOffset = '-2px' }
  136. 'range-picker ol.days li.between' { background = 'var(--color-background-sunken, rgb(15, 20, 25))' outline = '1px solid var(--color-accent, #569bd4)' outlineOffset = '-1px' }
  137. 'range-picker ol.days li.selected' { background = 'var(--color-accent, #569bd4)' }
  138. 'range-picker ol.days li.selected time' { color = 'var(--color-accent-text, rgb(15, 20, 25))' }
  139. }
  140. View {
  141. rangePicker {
  142. input { id = "range-picker-input" name = name type = "text" autocomplete = "off" maxlength = "23" title = placeholder placeholder = placeholder value = value 'aria-invalid' = invalidText on focus(e) { emit rangeOpen(e) } on input(e) { emit rangeTyped(e.target.value) } on keydown(e) { emit rangeKey(e.key) } }
  143. pickerPopup { tabindex = "-1"
  144. monthView {
  145. header {
  146. button { id = "range-prev" type = "button" class = "quiet" title = prevLabel "‹" on click(e) { emit rangePrev(e) } }
  147. h2 { title }
  148. button { id = "range-next" type = "button" class = "quiet" title = nextLabel "›" on click(e) { emit rangeNext(e) } }
  149. }
  150. ol { class = "weekdays" for (wd of weekdays) { li { wd } } }
  151. ol { class = "days" for (d of days) { li { class = d.cls time { datetime = d.date d.n } on click(e) { emit rangePick(d.date) } } } }
  152. }
  153. }
  154. }
  155. }
  156. // the browser knows today and its language; the field gets focus first, so this runs before a day is clicked
  157. on rangeOpen(e) {
  158. let now = new Date()
  159. let lang = navigator.language != null ? navigator.language : 'en'
  160. let t = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  161. if (t != today) {
  162. let mf = Intl.DateTimeFormat(lang, { month = 'long' })
  163. let df = Intl.DateTimeFormat(lang, { weekday = 'short' })
  164. let ms = []
  165. let ds = []
  166. let i = 0
  167. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  168. i = 0
  169. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  170. today = t
  171. monthNames = ms
  172. weekdayNames = ds
  173. weekdays = ds
  174. let de = lang.slice(0, 2) == 'de'
  175. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  176. nextLabel = de ? 'Nächster Monat' : 'Next month'
  177. }
  178. // a half-picked start is dropped; the month shown is the range's start, else today's month
  179. if (pending != '') {
  180. pending = ''
  181. value = kept
  182. apply(kept)
  183. }
  184. if (start != '') { gotoDay(start) } else { show(toNumber(today.slice(0, 4)), toNumber(today.slice(5, 7))) }
  185. }
  186. on rangeTyped(text) {
  187. pending = ''
  188. value = text
  189. if (apply(text)) { gotoDay(start) } else { show(year, month) }
  190. kept = start != '' ? start + sep + end : ''
  191. }
  192. on rangeKey(key) {
  193. if (key == 'Escape' || key == 'Enter') {
  194. if (pending != '') {
  195. pending = ''
  196. value = kept
  197. apply(kept)
  198. show(year, month)
  199. }
  200. document.activeElement.blur()
  201. }
  202. }
  203. on rangePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }
  204. on rangeNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }
  205. on rangePick(d) {
  206. if (pending == '') {
  207. kept = start != '' ? start + sep + end : ''
  208. pending = d
  209. start = d
  210. end = ''
  211. value = d + sep
  212. invalidText = 'false'
  213. show(year, month)
  214. return null
  215. }
  216. let a = keyOf(pending) <= keyOf(d) ? pending : d
  217. let b = keyOf(pending) <= keyOf(d) ? d : pending
  218. pending = ''
  219. start = a
  220. end = b
  221. value = a + sep + b
  222. kept = value
  223. show(year, month)
  224. document.activeElement.blur()
  225. }

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