gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf20313aaf20313aa|||PR bound calendar context and recover state across account changes and resyncsAtlasf20313aa/components/calendar.hl

14.1 KB

  1. parent './main.hl'
  2. import { build, fill, blank, localView } from '../lib/month-grid.hl'
  3. import EventEditor from './event-editor.hl'
  4. import { mergeEvents, inWindow } from '../lib/calendar-cache.hl'
  5. import { serverToday } from '../lib/clock.hl'
  6. import { pad, validMonth } from '../lib/dates.hl'
  7. import { namesFor } from '../lib/names.hl'
  8. import { navigate } from 'hl:web'
  9. import { userIdOfSession } from '../lib/users.hl'
  10. import { saveEvent, deleteEvent, calendarView, settingsView, eventsBetween, eventDelta, monthDataFor } from '../lib/events.hl'
  11. import { saveDefaults } from '../lib/settings.hl'
  12. import { validWindow } from '../lib/events-helpers.hl'
  13. // F20 hybriel#441: layout-local edits cannot reach a retained slot page offline.
  14. persistent = true
  15. __title = 'Calendar'
  16. route = null
  17. headers = null
  18. acceptLanguage = headers != null && headers['accept-language'] != null ? headers['accept-language'] : ''
  19. monthAddress = route != null && route.query != null ? route.query.m : null
  20. session = null
  21. userId = userIdOfSession(session)
  22. signedIn = userId != null
  23. // Each open is an action, including reopening the same unchanged event.
  24. editorVersion = 0
  25. editorRequest = null
  26. defaultText = $parent.defaultText
  27. defRows = $parent.defRows
  28. Hybrid known = namesFor(acceptLanguage)
  29. Boolean namesKnown = known != null
  30. String today = serverToday()
  31. String addressedMonth = ''
  32. Number year = 2000
  33. Number month = 1
  34. String prevUrl = ''
  35. String nextUrl = ''
  36. Hybrid monthNames = namesKnown ? known.months : ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
  37. Hybrid weekdayNames = namesKnown ? known.days : ['·', '·', '·', '·', '·', '·', '·']
  38. String title = ''
  39. Hybrid base = []
  40. Hybrid weekdays = weekdayNames
  41. String winFirst = ''
  42. String winLast = ''
  43. snapshot = signedIn ? monthDataFor(userId, monthAddress) : null
  44. Hybrid events = $parent.allEvents
  45. Hybrid days = []
  46. String prevLabel = namesKnown ? known.prev : '‹'
  47. String nextLabel = namesKnown ? known.next : '›'
  48. Number seen = 0
  49. Number paged = monthAddress != null ? 1 : 0
  50. String fZone = 'UTC'
  51. // The construction body prepares the grid from its address; hydration keeps that prepared state.
  52. {
  53. if (snapshot != null) { events = snapshot.events }
  54. addressedMonth = validMonth(monthAddress) ? monthAddress : today.slice(0, 7)
  55. show(toNumber(addressedMonth.slice(0, 4)), toNumber(addressedMonth.slice(5, 7)), events)
  56. }
  57. show(Number y, Number m, Hybrid evs) {
  58. year = y
  59. month = m
  60. prevUrl = '/?m=' + (m == 1 ? y - 1 : y) + '-' + pad(m == 1 ? 12 : m - 1)
  61. nextUrl = '/?m=' + (m == 12 ? y + 1 : y) + '-' + pad(m == 12 ? 1 : m + 1)
  62. title = monthNames[m - 1] + ' ' + y
  63. base = build(y, m, today)
  64. winFirst = base[0].date
  65. winLast = base[41].date
  66. events = inWindow(evs, winFirst, winLast)
  67. days = fill(base, events)
  68. }
  69. Style {
  70. monthView {
  71. display = 'grid'
  72. gap = '.5rem'
  73. header {
  74. display = 'flex'
  75. flexWrap = 'wrap'
  76. alignItems = 'center'
  77. justifyContent = 'space-between'
  78. gap = '.5rem'
  79. button {
  80. minWidth = '2.75rem'
  81. minHeight = '2.75rem'
  82. }
  83. '#month-prev, #month-next' {
  84. minWidth = '2.75rem'
  85. minHeight = '2.75rem'
  86. display = 'inline-flex'
  87. alignItems = 'center'
  88. justifyContent = 'center'
  89. }
  90. }
  91. '#settings-open' {
  92. marginLeft = 'auto'
  93. }
  94. h2 {
  95. margin = '0'
  96. fontSize = '1.1rem'
  97. color = 'var(--color-text-strong, rgb(245, 250, 255))'
  98. textAlign = 'center'
  99. flex = '1 1 auto'
  100. whiteSpace = 'nowrap'
  101. textTransform = 'capitalize'
  102. }
  103. '.hidden' {
  104. display = 'none'
  105. }
  106. }
  107. '#month-weekdays, #month-days' {
  108. display = 'grid'
  109. gridTemplateColumns = 'repeat(7, minmax(0, 1fr))'
  110. margin = '0'
  111. padding = '0'
  112. listStyle = 'none'
  113. }
  114. '#month-weekdays' {
  115. li {
  116. margin = '0'
  117. padding = '.3rem 0'
  118. fontSize = '.8rem'
  119. fontWeight = '600'
  120. color = 'var(--color-text-muted, rgb(145, 150, 155))'
  121. textAlign = 'center'
  122. }
  123. }
  124. '#month-days' {
  125. li {
  126. position = 'relative'
  127. minHeight = '3.5rem'
  128. margin = '0'
  129. padding = '.2rem 0'
  130. textAlign = 'right'
  131. background = 'var(--color-surface, rgb(35, 40, 45))'
  132. cursor = 'pointer'
  133. overflow = 'hidden'
  134. }
  135. 'button.day-add' {
  136. position = 'absolute'
  137. inset = '0'
  138. width = '100%'
  139. height = '100%'
  140. minWidth = '0'
  141. minHeight = '0'
  142. padding = '0'
  143. border = '0'
  144. borderRadius = '0'
  145. background = 'transparent'
  146. focusVisible {
  147. outline = '2px solid var(--color-accent)'
  148. outlineOffset = '-2px'
  149. }
  150. }
  151. time {
  152. pointerEvents = 'none'
  153. position = 'relative'
  154. display = 'block'
  155. paddingRight = '.25rem'
  156. color = 'var(--color-text, rgb(195, 200, 205))'
  157. fontSize = '.95rem'
  158. }
  159. 'li.outside' {
  160. background = 'var(--color-background, rgb(25, 30, 35))'
  161. time {
  162. color = 'var(--color-text-muted, rgb(145, 150, 155))'
  163. }
  164. }
  165. 'li.today' {
  166. outline = '2px solid var(--color-accent, #569bd4)'
  167. outlineOffset = '-2px'
  168. time {
  169. color = 'var(--color-accent, #569bd4)'
  170. fontWeight = '800'
  171. }
  172. }
  173. }
  174. dayEvents {
  175. position = 'relative'
  176. pointerEvents = 'none'
  177. display = 'grid'
  178. gap = '.15rem'
  179. marginTop = '.2rem'
  180. textAlign = 'left'
  181. }
  182. 'button.unused' {
  183. display = 'none'
  184. }
  185. 'button.hidden' {
  186. display = 'none'
  187. }
  188. 'button.event, button.more' {
  189. pointerEvents = 'auto'
  190. display = 'block'
  191. minWidth = '0'
  192. width = '100%'
  193. padding = '.15rem .2rem'
  194. overflow = 'hidden'
  195. fontSize = '.75rem'
  196. fontWeight = '500'
  197. lineHeight = '1.3'
  198. textAlign = 'left'
  199. textOverflow = 'ellipsis'
  200. whiteSpace = 'nowrap'
  201. color = 'var(--color-accent)'
  202. background = 'var(--color-background-sunken)'
  203. }
  204. 'button.more' {
  205. color = 'var(--color-text-muted)'
  206. background = 'transparent'
  207. }
  208. @media {
  209. minWidth = '60rem'
  210. 'button.event, button.more' {
  211. minHeight = '0'
  212. padding = '.05rem .3rem'
  213. borderRadius = 'var(--radius-small)'
  214. }
  215. '#month-days' {
  216. li {
  217. minHeight = '5rem'
  218. padding = '.3rem'
  219. }
  220. }
  221. }
  222. }
  223. View {
  224. monthView { on pointerover(Hybrid e) { emit monthEnv(e) } on focusin(Hybrid e) { emit monthEnv(e) } on touchstart(Hybrid e) { emit monthEnv(e) }
  225. header {
  226. a { id = "month-prev" href = prevUrl class = "button quiet" title = prevLabel "‹" }
  227. h2 { id = "month-title" title }
  228. a { id = "month-next" href = nextUrl class = "button quiet" title = nextLabel "›" } if (signedIn) { button { id = "settings-open" type = "button" class = "quiet" popovertarget = "settingseditor" popovertargetaction = "show" "Reminder settings" } }
  229. }
  230. ol { id = "month-weekdays" for (name of weekdays) { li { name } } }
  231. ol { id = "month-days"
  232. for (d of days) {
  233. li { class = d.cls
  234. if (signedIn) { button { type = "button" class = "day-add" 'aria-label' = d.addLabel on click() { addDay(d.date) } } }
  235. time { datetime = d.date d.n }
  236. dayEvents {
  237. button { class = d.c0 type = "button" value = d.v0 d.t0 on click() { pickEvent(d.v0, d.date) } }
  238. button { class = d.c1 type = "button" value = d.v1 d.t1 on click() { pickEvent(d.v1, d.date) } }
  239. button { class = d.c2 type = "button" value = d.v2 d.t2 on click() { pickEvent(d.v2, d.date) } }
  240. button { class = d.c3 type = "button" value = d.v3 d.t3 on click() { pickEvent(d.v3, d.date) } }
  241. }
  242. }
  243. }
  244. }
  245. EventEditor { request = editorRequest winFirst = winFirst winLast = winLast defaultText = defaultText defRows = defRows }
  246. input { id = "month-view-goto" type = "hidden" on change(Hybrid e) { emit monthGoto(e.target.value) } }
  247. }
  248. }
  249. on monthEnv(Hybrid e) {
  250. browserEnv()
  251. // every ten minutes of use the timers are set again (a long-open page keeps the next week planned)
  252. if (signedIn && Date.now() - $parent.lastArm > 600000) { $parent.refreshDevice() }
  253. }
  254. browserEnv() {
  255. if (seen == 1) { return null }
  256. seen = 1
  257. serverDay = today
  258. now = new Date()
  259. lang = navigator.language != null ? navigator.language : 'en'
  260. zone = Intl.DateTimeFormat().resolvedOptions().timeZone
  261. browserDay = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  262. if (browserDay != serverDay) { today = browserDay }
  263. // the server already wrote the names of a language it knows: they stay, so paging never switches them
  264. if (!namesKnown) {
  265. mf = Intl.DateTimeFormat(lang, { month = 'long' timeZone = 'UTC' })
  266. df = Intl.DateTimeFormat(lang, { weekday = 'short' timeZone = 'UTC' })
  267. ms = []
  268. ds = []
  269. let i = 0
  270. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  271. i = 0
  272. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  273. // (only what differs is assigned: a same-valued assignment still repaints the title and the days)
  274. if (ms.join('|') != monthNames.join('|')) { monthNames = ms }
  275. if (ds.join('|') != weekdayNames.join('|')) { weekdayNames = ds weekdays = ds }
  276. de = lang.slice(0, 2) == 'de'
  277. pl = de ? 'Voriger Monat' : 'Previous month'
  278. nl = de ? 'Nächster Monat' : 'Next month'
  279. if (pl != prevLabel) { prevLabel = pl }
  280. if (nl != nextLabel) { nextLabel = nl }
  281. }
  282. fZone = zone != null && zone != '' ? zone : 'UTC'
  283. $parent.refreshDevice()
  284. // calendar#9: a phone freezes or drops an app it switched away from, and its timers with it. Whenever the app is
  285. // shown again the timers are set again, and what came due meanwhile is shown at once (arm → missed)
  286. // Nothing is repainted unless the browser's day is not the server's: the server already drew this month with its events,
  287. // and a round trip here, answered while the user pages or taps a day, used to put the month back or swallow the tap.
  288. if (browserDay == serverDay) {
  289. if (!namesKnown) {
  290. named = monthNames[month - 1] + ' ' + year
  291. if (named != title) { title = named }
  292. }
  293. } else if (paged == 0) {
  294. // the month shown follows the corrected today only while the user has not paged yet (also when they page meanwhile)
  295. y = toNumber(today.slice(0, 4))
  296. m = toNumber(today.slice(5, 7))
  297. evs = events
  298. if (paged == 0) { show(y, m, evs) } else { show(year, month, events) }
  299. } else { show(year, month, events) }
  300. // (offline the server did not answer: the timers are set from the reminders the page already holds)
  301. $parent.arm()
  302. }
  303. on monthGoto(String text) {
  304. paged = 1
  305. y = toNumber(text.slice(0, 4))
  306. m = toNumber(text.slice(5, 7))
  307. navigate('/?m=' + y + '-' + pad(m))
  308. }
  309. addDay(String date) {
  310. if (!signedIn) { return null }
  311. editorVersion = editorVersion + 1
  312. editorRequest = { version = editorVersion fields = blank(date, fZone) heading = 'New event' fresh = true }
  313. }
  314. pickEvent(String id, String date) {
  315. if (!signedIn) { return null }
  316. // The overflow chip has always opened a new event for its day.
  317. if (id == '') { addDay(date) return null }
  318. let j = 0
  319. while (j < events.length) {
  320. ev = events[j]
  321. if (ev.id == id) {
  322. fields = { id = ev.id title = ev.title allDay = ev.allDay startDate = ev.startDate startTime = ev.startTime endDate = ev.endDate endTime = ev.endTime place = ev.place notes = ev.notes zone = fZone custom = ev.custom rows = ev.rows }
  323. editorVersion = editorVersion + 1
  324. editorRequest = { version = editorVersion fields = fields heading = 'Edit event' fresh = false }
  325. return null
  326. }
  327. j = j + 1
  328. }
  329. }
  330. on client shown(Hybrid r) {
  331. browserEnv()
  332. show(year, month, mergeEvents(events, $parent.localEdits))
  333. }
  334. on client calendarInvalidated(String owner) {
  335. if (owner != userId) { return null }
  336. r = emit server calLoad(winFirst, winLast)
  337. if (r != null && r.error == null) { saved(r) }
  338. }
  339. on client calendarChanged(String owner, Hybrid all, Hybrid r) {
  340. if (owner != userId) { return null }
  341. defaultText = r.defaults
  342. defRows = r.defaultRows
  343. show(year, month, mergeEvents(events, all))
  344. }
  345. saveFields(Hybrid f, String first, String last) { return emit server calSave(f, first, last) }
  346. queueFields(Hybrid f, String first, String last) { emit server calSaveQueued(f, first, last) }
  347. removeFields(String id, String first, String last) { return emit server calDelete(id, first, last) }
  348. saved(Hybrid r) {
  349. $parent.takeSoon(r)
  350. defaultText = r.defaults
  351. defRows = r.defaultRows
  352. show(year, month, r.events)
  353. }
  354. saveOffline(Hybrid f) {
  355. v = localView(f, defRows)
  356. evs = []
  357. for (q of events) { if (q.id != v.id) { evs.push(q) } }
  358. evs.push(v)
  359. show(year, month, evs)
  360. $parent.localSoon(v)
  361. }
  362. on server calLoad(first, last, session) {
  363. u = userIdOfSession(session)
  364. if (u == null) { return { error = 'log in with ident first' } }
  365. if (!validWindow(first, last)) { return { error = 'not a date' } }
  366. return calendarView(u, first, last)
  367. }
  368. on server calSave(fields, first, last, session) {
  369. u = userIdOfSession(session)
  370. if (u == null) { return { error = 'log in with ident first' } }
  371. if (!validWindow(first, last)) { return { error = 'not a date' } }
  372. if (fields == null || hlTypeName(fields) != 'Hybrid') { return { error = 'the event must be an object' } }
  373. r = saveEvent(u, fields)
  374. if (r.error != null) { return { error = r.error } }
  375. emit client calendarChanged(u, eventDelta(u, r.id), settingsView(u))
  376. return calendarView(u, first, last)
  377. }
  378. on server calDelete(id, first, last, session) {
  379. u = userIdOfSession(session)
  380. if (u == null) { return { error = 'log in with ident first' } }
  381. if (!validWindow(first, last)) { return { error = 'not a date' } }
  382. r = deleteEvent(u, id)
  383. if (r.error != null) { return { error = r.error } }
  384. emit client calendarChanged(u, [{ id = r.id ref = r.ref deleted = true }], settingsView(u))
  385. return calendarView(u, first, last)
  386. }
  387. // the calendar settings: the default reminders (rows)
  388. // the same save as calSave, sent while the server cannot be reached: a statement-form emit the offline layer queues and
  389. // replays (an event that is also emitted in value form is never queued — hence its own name)
  390. on server calSaveQueued(fields, first, last, session) {
  391. u = userIdOfSession(session)
  392. if (u == null) { return { error = 'log in with ident first' } }
  393. if (fields == null || hlTypeName(fields) != 'Hybrid') { return { error = 'the event must be an object' } }
  394. r = saveEvent(u, fields)
  395. if (r.error != null) { return { error = r.error } }
  396. emit client calendarChanged(u, eventDelta(u, r.id), settingsView(u))
  397. return { ok = true }
  398. }
  399. // Web Push (calendar#9): this device's push subscription and zone — stored, so a reminder reaches it with the page closed
  400. // "Send a test notification": one test push to this device in a few seconds (calendar#12: a phone owner can see that push reaches it)
  401. // P1: up to when the server has pushed this device's reminders (the page does not show those again as missed)
  402. // this device stops getting pushes

Branches

Latest commits

  • f20313aa|||PR bound calendar context and recover state across account changes and resyncsAtlas
  • 651752c0|||PR run the browser gate once per deployAtlas
  • e24e28a1|||PR preserve calendar state across month addresses and split dialogsAtlas
  • 384d3d23|||PR give calendar months their own addressAtlas
  • fff2f537Merge branch 'calendar31-lib' into mainAtlas
  • 07cde3dc|||PR keep worker records out of the repo; README vendor is day72Atlas
  • a753b0ab|||PR move shared calendar logic to libAtlas
  • b32a921dMerge branch 'vendor-day72' into mainAtlas
  • 60a0dd19|||PR Hybriel day72 (00c9dbb8) instead of the withdrawn day73Atlas
  • 53f5464dMerge branch 'calendar26-day73' into mainAtlas
  • 338499ec|||PR Keep reminder settings open during editing and rejected savesAtlas
  • ad9e8d59|||PR Use framework page and popover state, Hybriel day73Atlas
  • cab224a1Re-vendor calendar to verified Hybriel day73Atlas
  • f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
  • 66341793|||PR Vendor Hybriel day61Atlas
  • 36fc596bMerge branch 'vendor-day59' into mainAtlas
  • b37cc1da|||PR Vendor Hybriel day59Atlas
  • 05665102Merge branch 'vendor-day56' into mainAtlas
  • 687296fb|||PR Vendor Hybriel day56Atlas
  • a6166d4eMerge branch 'hl-standard-3' into mainAtlas