gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitba30727aba30727afile-upload: nest Style rules in the language's formmreba30727a/components/range-picker/range-picker.hl

11.1 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. t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  39. yy = m < 3 ? y - 1 : y
  40. 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. y = toNumber(s.slice(0, 4))
  47. m = toNumber(s.slice(5, 7))
  48. 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. py = m == 1 ? y - 1 : y
  57. pm = m == 1 ? 12 : m - 1
  58. ny = m == 12 ? y + 1 : y
  59. nm = m == 12 ? 1 : m + 1
  60. lead = weekdayOf(y, m, 1)
  61. let n = daysIn(y, m)
  62. pn = daysIn(py, pm)
  63. ka = keyOf(a)
  64. kb = keyOf(b)
  65. 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. n = text.length
  95. a = n >= 21 ? text.slice(0, 10) : ''
  96. b = n >= 21 ? text.slice(n - 10, n) : ''
  97. ka = keyOf(a)
  98. 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. // 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
  116. // 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
  117. // theme) restyles it. No frames: a border or ring only where it means something (focus, today, an invalid field). A component's browser module
  118. // cannot import tokens itself.
  119. Style {
  120. rangePicker {
  121. position = 'relative'
  122. display = 'block'
  123. maxWidth = '18rem'
  124. pickerPopup {
  125. display = 'none'
  126. position = 'absolute'
  127. top = '100%'
  128. left = '0'
  129. zIndex = '4'
  130. width = 'min(22rem, 90vw)'
  131. marginTop = '.25rem'
  132. outline = 'none'
  133. }
  134. focusWithin {
  135. pickerPopup { display = 'block' }
  136. }
  137. monthView {
  138. display = 'grid'
  139. gap = '.5rem'
  140. padding = '.5rem'
  141. background = 'var(--color-background, #fff)'
  142. border = '0'
  143. borderRadius = '.5rem'
  144. boxShadow = '0 .5rem 1.5rem rgba(0, 0, 0, .25)'
  145. header {
  146. display = 'flex'
  147. alignItems = 'center'
  148. justifyContent = 'space-between'
  149. gap = '.5rem'
  150. }
  151. h2 {
  152. margin = '0'
  153. fontSize = '1.1rem'
  154. color = 'var(--color-text-strong, #000)'
  155. textAlign = 'center'
  156. flex = '1'
  157. textTransform = 'capitalize'
  158. }
  159. ol {
  160. display = 'grid'
  161. gridTemplateColumns = 'repeat(7, minmax(0, 1fr))'
  162. gap = '.2rem'
  163. margin = '0'
  164. padding = '0'
  165. listStyle = 'none'
  166. }
  167. }
  168. button {
  169. font = 'inherit'
  170. fontWeight = '600'
  171. minWidth = '2.75rem'
  172. minHeight = '2.75rem'
  173. padding = '.4rem .9rem'
  174. color = 'var(--color-text, #000)'
  175. background = 'transparent'
  176. border = '0'
  177. borderRadius = '.4rem'
  178. cursor = 'pointer'
  179. hover { background = 'var(--color-surface, #f2f2f2)' }
  180. }
  181. // Class or attribute assigned by the View or code; no compound-selector form.
  182. 'ol.weekdays' {
  183. li {
  184. padding = '.3rem 0'
  185. fontSize = '.8rem'
  186. fontWeight = '600'
  187. color = 'var(--color-text-muted, #555)'
  188. textAlign = 'center'
  189. }
  190. }
  191. // Class or attribute assigned by the View or code; no compound-selector form.
  192. 'ol.days' {
  193. li {
  194. minHeight = '2.75rem'
  195. padding = '.3rem'
  196. border = '0'
  197. borderRadius = '.3rem'
  198. textAlign = 'right'
  199. background = 'var(--color-surface, #f2f2f2)'
  200. cursor = 'pointer'
  201. hover { background = 'var(--color-background-sunken, #ddd)' }
  202. }
  203. time {
  204. color = 'var(--color-text, #000)'
  205. fontSize = '.95rem'
  206. }
  207. // Class or attribute assigned by the View or code; no compound-selector form.
  208. 'li.outside' {
  209. background = 'var(--color-background, #fff)'
  210. time { color = 'var(--color-text-muted, #555)' }
  211. }
  212. // Class or attribute assigned by the View or code; no compound-selector form.
  213. 'li.today' {
  214. outline = '2px solid var(--color-accent, #000)'
  215. outlineOffset = '-2px'
  216. }
  217. // Class or attribute assigned by the View or code; no compound-selector form.
  218. 'li.between' { background = 'var(--color-background-sunken, #ddd)' }
  219. // Class or attribute assigned by the View or code; no compound-selector form.
  220. 'li.selected' {
  221. background = 'var(--color-accent, #000)'
  222. time { color = 'var(--color-accent-text, #fff)' }
  223. }
  224. }
  225. }
  226. // Existing hyphenated id: the language does not kebab-case id rules yet; keep the id unchanged.
  227. '#range-picker-input' {
  228. width = '100%'
  229. boxSizing = 'border-box'
  230. minHeight = '2.75rem'
  231. font = 'inherit'
  232. padding = '.4rem .6rem'
  233. color = 'var(--color-text, #000)'
  234. background = 'var(--color-background-sunken, #f2f2f2)'
  235. border = '0'
  236. borderRadius = '.4rem'
  237. focus {
  238. outline = '2px solid var(--color-accent, #555)'
  239. outlineOffset = '-1px'
  240. }
  241. }
  242. // Existing hyphenated id with a state attribute; no compound-selector form.
  243. '#range-picker-input[aria-invalid="true"]' {
  244. outline = '2px dashed var(--color-danger, #000)'
  245. outlineOffset = '-2px'
  246. }
  247. }
  248. View {
  249. rangePicker {
  250. 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) } }
  251. pickerPopup { tabindex = "-1"
  252. monthView {
  253. header {
  254. button { id = "range-prev" type = "button" class = "quiet" title = prevLabel "‹" on click(e) { emit rangePrev(e) } }
  255. h2 { title }
  256. button { id = "range-next" type = "button" class = "quiet" title = nextLabel "›" on click(e) { emit rangeNext(e) } }
  257. }
  258. ol { class = "weekdays" for (wd of weekdays) { li { wd } } }
  259. ol { class = "days" for (d of days) { li { class = d.cls time { datetime = d.date d.n } on click(e) { emit rangePick(d.date) } } } }
  260. }
  261. }
  262. }
  263. }
  264. // the browser knows today; the names follow the page language (<html lang>, English when none); the field gets focus first, so this runs before a day is clicked
  265. on rangeOpen(e) {
  266. let now = new Date()
  267. lang = document.documentElement.lang != '' ? document.documentElement.lang : 'en'
  268. t = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  269. if (t != today || lang.slice(0, 2) != 'en') {
  270. mf = Intl.DateTimeFormat(lang, { month = 'long' timeZone = 'UTC' })
  271. df = Intl.DateTimeFormat(lang, { weekday = 'short' timeZone = 'UTC' })
  272. ms = []
  273. ds = []
  274. let i = 0
  275. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  276. i = 0
  277. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  278. today = t
  279. monthNames = ms
  280. weekdayNames = ds
  281. weekdays = ds
  282. de = lang.slice(0, 2) == 'de'
  283. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  284. nextLabel = de ? 'Nächster Monat' : 'Next month'
  285. }
  286. // a half-picked start is dropped; the month shown is the range's start, else today's month
  287. if (pending != '') {
  288. pending = ''
  289. value = kept
  290. apply(kept)
  291. }
  292. if (start != '') { gotoDay(start) } else { show(toNumber(today.slice(0, 4)), toNumber(today.slice(5, 7))) }
  293. }
  294. on rangeTyped(text) {
  295. pending = ''
  296. value = text
  297. if (apply(text)) { gotoDay(start) } else { show(year, month) }
  298. kept = start != '' ? start + sep + end : ''
  299. }
  300. on rangeKey(key) {
  301. if (key == 'Escape' || key == 'Enter') {
  302. if (pending != '') {
  303. pending = ''
  304. value = kept
  305. apply(kept)
  306. show(year, month)
  307. }
  308. document.activeElement.blur()
  309. }
  310. }
  311. on rangePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }
  312. on rangeNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }
  313. on rangePick(d) {
  314. if (pending == '') {
  315. kept = start != '' ? start + sep + end : ''
  316. pending = d
  317. start = d
  318. end = ''
  319. value = d + sep
  320. invalidText = 'false'
  321. show(year, month)
  322. return null
  323. }
  324. a = keyOf(pending) <= keyOf(d) ? pending : d
  325. b = keyOf(pending) <= keyOf(d) ? d : pending
  326. pending = ''
  327. start = a
  328. end = b
  329. value = a + sep + b
  330. kept = value
  331. show(year, month)
  332. document.activeElement.blur()
  333. }

Branches

Latest commits

  • ba30727afile-upload: nest Style rules in the language's formmre
  • 42fefa4emodal: nest Style rules in the language's formmre
  • 6fd953e3range-picker: nest Style rules in the language's formmre
  • f661951ddate-picker: nest Style rules in the language's formmre
  • 363577e6media-player: nest Style rules in the language's formmre
  • 0b3fd8c9comments: nest Style rules in the language's formmre
  • 67e043c8plugins/web/README.md from hybriel cb9e480dmre
  • 519b56e9styles.hl: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 29f63d01md-editor: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 869f49c7files-upload: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • f60dab20month-view: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 5b3c349aRe-vendor to Hybriel set day35 (cb9e480d): plugins core and web sources, README vendoring notemre
  • 3449b117components: a startup error prints its message before exit 1 (hybriel#247)mre
  • 59704ce1Merge branch 'n5b-pipes'mre
  • 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
  • b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
  • ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
  • 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
  • 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
  • 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre