gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitcf175c80cf175c80calendar mission 008 (A7): re-vendor Hybriel master 0e3c9830 (bin 9d957fed, plugins whole: hl:crypto Web Push primitives, hl:web sendPush)mrecf175c80/components/month-view/month-view.hl

28.8 KB

  1. // month-view.hl — THE MONTH VIEW: prev / next buttons on top, the month's name and the year between them, the
  2. // days below (weeks start on Monday; the days of the neighbouring months are dimmed; today is marked).
  3. // THE CALENDAR'S COPY of the pure-Hybriel month view of components.hybriel.worldapi.org (no .js), plus the user's
  4. // events on the days and the editor for them (add: click a day; edit / delete: click an event). Events come from
  5. // events.hl, one server round trip per change (faces calLoad / calSave / calDelete in calendar.hl). The date
  6. // arithmetic is written here (plain integers), so it runs the same on the server (first render) and in the browser.
  7. // What only the browser knows — today in the user's time zone, the month and weekday names in the user's language,
  8. // the time zone's name (`Date` and `Intl` are browser globals of View handlers) — is read the moment the pointer, a
  9. // finger or the keyboard reaches the month view; until then the server's UTC date and language-neutral names (month number, dots) show — the server never knows
  10. // the language, so it never guesses one: names appear once, in the browser's language, and stay.
  11. // The page reads `year` and `month` (1–12) as its own members. USE ONE PER PAGE: a composed component's
  12. // members are the page's state (COMPONENTS.md §12).
  13. import { serverToday } from './clock.hl'
  14. import { pad, daysIn, before } from './dates.hl'
  15. import { namesFor } from './names.hl'
  16. import { userIdOfSession } from '../../lib/users.hl'
  17. import { eventsBetween, upcoming } from '../../lib/events.hl'
  18. import { defaultsOf } from '../../lib/settings.hl'
  19. import { notify } from 'hl:web'
  20. import { plan, missed, shift, describe, withIndex, defaultRows } from './reminders.hl'
  21. // the request's Accept-Language, handed in by the page (calendar.hl reads it from `headers`, hybriel #105): the first
  22. // render already writes the names
  23. acceptLanguage = null
  24. Hybrid known = namesFor(acceptLanguage)
  25. Boolean namesKnown = known != null
  26. String today = serverToday()
  27. Number year = toNumber(today.slice(0, 4))
  28. Number month = toNumber(today.slice(5, 7))
  29. Hybrid monthNames = namesKnown ? known.months : ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
  30. Hybrid weekdayNames = namesKnown ? known.days : ['·', '·', '·', '·', '·', '·', '·']
  31. String title = monthNames[month - 1] + ' ' + year
  32. Hybrid base = build(year, month, today)
  33. Hybrid weekdays = weekdayNames
  34. session = null
  35. userId = userIdOfSession(session)
  36. Boolean signedIn = userId != null
  37. String winFirst = base[0].date
  38. String winLast = base[41].date
  39. Hybrid events = signedIn ? eventsBetween(userId, winFirst, winLast) : []
  40. Hybrid days = fill(base, events)
  41. // the reminders (ticket calendar#4): the events of the next days with the reminders that apply (`soon`, kept with the page
  42. // state, so an offline page still knows them), the calendar's defaults in words, the timers the browser holds
  43. Hybrid soon = signedIn ? upcoming(userId, shift(today, -2), shift(today, 9)) : []
  44. String defaultText = signedIn ? describe(defaultsOf(userId)) : ''
  45. Hybrid defRows = signedIn ? defaultsOf(userId) : []
  46. Boolean fCustom = false
  47. Hybrid remRows = []
  48. Hybrid setRows = []
  49. Hybrid timers = []
  50. Number lastArm = 0
  51. String notifyState = 'unknown'
  52. String setClass = 'closed'
  53. String setError = ''
  54. String barClass = signedIn ? 'bar' : 'hidden'
  55. String askClass = 'hidden'
  56. String blockedClass = 'hidden'
  57. String plannedText = ''
  58. String fId = ''
  59. String fTitle = ''
  60. Boolean fAllDay = false
  61. String fStartDate = ''
  62. String fStartTime = '09:00'
  63. String fEndDate = ''
  64. String fEndTime = '10:00'
  65. String fPlace = ''
  66. String fNotes = ''
  67. String fZone = 'UTC'
  68. String formClass = 'closed'
  69. String formError = ''
  70. String formHeading = 'New event'
  71. Boolean isNew = true
  72. String formKind = 'timed'
  73. String ownClass = 'quiet'
  74. String calClass = ''
  75. String defaultClass = ''
  76. String rowsClass = 'hidden'
  77. String addClass = 'quiet hidden'
  78. String timedButton = ''
  79. String alldayButton = 'quiet'
  80. String deleteClass = 'danger hidden'
  81. String prevLabel = namesKnown ? known.prev : '‹'
  82. String nextLabel = namesKnown ? known.next : '›'
  83. Number seen = 0
  84. Number paged = 0
  85. // 0 = Monday … 6 = Sunday (Sakamoto's method gives 0 = Sunday)
  86. weekdayOf(y, m, d) {
  87. t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  88. yy = m < 3 ? y - 1 : y
  89. sun = (yy + (yy - yy % 4) / 4 - (yy - yy % 100) / 100 + (yy - yy % 400) / 400 + t[m - 1] + d) % 7
  90. return (sun + 6) % 7
  91. }
  92. // 42 days (six weeks of seven), so the view keeps its height from month to month
  93. build(y, m, todayText) {
  94. py = m == 1 ? y - 1 : y
  95. pm = m == 1 ? 12 : m - 1
  96. ny = m == 12 ? y + 1 : y
  97. nm = m == 12 ? 1 : m + 1
  98. lead = weekdayOf(y, m, 1)
  99. let n = daysIn(y, m)
  100. pn = daysIn(py, pm)
  101. out = []
  102. let day = 1 - lead
  103. let w = 0
  104. while (w < 6) {
  105. let i = 0
  106. while (i < 7) {
  107. let cy = y
  108. let cm = m
  109. let cd = day
  110. let cls = ''
  111. if (day < 1) { cy = py cm = pm cd = pn + day cls = 'outside' }
  112. if (day > n) { cy = ny cm = nm cd = day - n cls = 'outside' }
  113. let date = cy + '-' + pad(cm) + '-' + pad(cd)
  114. if (date == todayText) { cls = 'today' }
  115. out.push({ n = cd date = date cls = cls })
  116. day = day + 1
  117. i = i + 1
  118. }
  119. w = w + 1
  120. }
  121. return out
  122. }
  123. // the events that touch a day, as four chips on the day cell (a nested `for` does not repaint when the outer list is
  124. // reassigned: hybriel #86 — so the chips are fixed members of the day: t = text, c = class, v = event id).
  125. // Timed events start with their time on the first day. More than four: the fourth chip says "+N more".
  126. fill(list, evs) {
  127. out = []
  128. let i = 0
  129. while (i < list.length) {
  130. let d = list[i]
  131. let hit = []
  132. let j = 0
  133. while (j < evs.length) {
  134. let e = evs[j]
  135. if (!before(d.date, e.first) && !before(e.last, d.date)) { hit.push(e) }
  136. j = j + 1
  137. }
  138. let row = { n = d.n date = d.date cls = d.cls }
  139. let k = 0
  140. while (k < 4) {
  141. let text = ''
  142. let cls = 'unused'
  143. let id = ''
  144. if (k < hit.length && (hit.length <= 4 || k < 3)) {
  145. let e = hit[k]
  146. text = (e.allDay || d.date != e.first ? '' : e.startTime + ' ') + e.title
  147. cls = 'event'
  148. id = e.id
  149. }
  150. if (k == 3 && hit.length > 4) { text = '+' + (hit.length - 3) + ' more' cls = 'more' }
  151. row['t' + k] = text
  152. row['c' + k] = cls
  153. row['v' + k] = id
  154. k = k + 1
  155. }
  156. out.push(row)
  157. i = i + 1
  158. }
  159. return out
  160. }
  161. // the form's fields for a new event on `date` (all text; allDay a Boolean); zone = the browser's time zone name
  162. blank(date, zone) {
  163. return { id = '' title = '' allDay = false startDate = date startTime = '09:00' endDate = date endTime = '10:00' place = '' notes = '' zone = zone custom = false rows = [] }
  164. }
  165. show(y, m, evs) {
  166. year = y
  167. month = m
  168. title = monthNames[m - 1] + ' ' + y
  169. base = build(y, m, today)
  170. winFirst = base[0].date
  171. winLast = base[41].date
  172. events = evs
  173. days = fill(base, evs)
  174. }
  175. // Layout only (mission 006): the theme (theme/theme.hl, `html month-view …`) tiles the day cells and takes the frames and radii away, buttons
  176. // and fields come from the theme (44 px taps, 16 px fields). Colours: the semantic WorldAPI tokens as css variables, each with its palette
  177. // value as fallback (a component's browser module cannot import tokens.hl itself); the app sets --color-accent in its styles.hl.
  178. // PHONE FIRST: a day cell has no side padding (its event chips are 44 px tall and as wide as the cell); the wide layout is from 60rem.
  179. Style {
  180. 'month-view' { display = 'grid' gap = '.5rem' }
  181. 'month-view header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '.5rem' }
  182. 'month-view h2' { margin = '0' fontSize = '1.1rem' color = 'var(--color-text-strong, rgb(245, 250, 255))' textAlign = 'center' flex = '1' textTransform = 'capitalize' }
  183. 'month-view ol' { display = 'grid' gridTemplateColumns = 'repeat(7, minmax(0, 1fr))' margin = '0' padding = '0' listStyle = 'none' }
  184. '#month-weekdays li' { margin = '0' padding = '.3rem 0' fontSize = '.8rem' fontWeight = '600' color = 'var(--color-text-muted, rgb(145, 150, 155))' textAlign = 'center' }
  185. '#month-days li' { minHeight = '3.5rem' margin = '0' padding = '.2rem 0' textAlign = 'right' background = 'var(--color-surface, rgb(35, 40, 45))' cursor = 'pointer' overflow = 'hidden' }
  186. '#month-days time' { display = 'block' paddingRight = '.25rem' color = 'var(--color-text, rgb(195, 200, 205))' fontSize = '.95rem' }
  187. '#month-days li.outside' { background = 'var(--color-background, rgb(25, 30, 35))' }
  188. '#month-days li.outside time' { color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  189. '#month-days li.today' { outline = '2px solid var(--color-accent, #569bd4)' outlineOffset = '-2px' }
  190. '#month-days li.today time' { color = 'var(--color-accent, #569bd4)' fontWeight = '800' }
  191. 'month-view bar' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.5rem' }
  192. 'month-view bar.hidden' { display = 'none' }
  193. 'month-view bar p' { margin = '0' fontSize = '.85rem' color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  194. 'month-view .hidden' { display = 'none' }
  195. 'reminder-box' { display = 'grid' gap = '.4rem' }
  196. 'reminder-box h3' { margin = '0' fontSize = '.85rem' fontWeight = '600' color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  197. 'reminder-box p' { margin = '0' fontSize = '.85rem' color = 'var(--color-text-muted, rgb(145, 150, 155))' }
  198. 'reminder-box ol' { display = 'grid' gap = '.4rem' }
  199. 'reminder-row' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.4rem' }
  200. 'reminder-row input[type=number]' { width = '5rem' }
  201. // a wrapped row never squeezes a field under the 44 px tap width (it was 43 px)
  202. 'reminder-row input,
  203. reminder-row select' { minWidth = '2.75rem' flex = 'none' }
  204. 'reminder-row.other input[type=time]' { display = 'none' }
  205. '@media (min-width: 60rem)' {
  206. '#month-days li' { minHeight = '5rem' padding = '.3rem' }
  207. }
  208. }
  209. View {
  210. monthView { on pointerover(e) { emit monthEnv(e) } on focusin(e) { emit monthEnv(e) } on touchstart(e) { emit monthEnv(e) }
  211. header {
  212. button { id = "month-prev" type = "button" class = "quiet" title = prevLabel "‹" on click(e) { emit monthPrev(e) } }
  213. h2 { id = "month-title" title }
  214. button { id = "month-next" type = "button" class = "quiet" title = nextLabel "›" on click(e) { emit monthNext(e) } }
  215. }
  216. bar { id = "reminder-bar" class = barClass
  217. button { id = "notify-ask" type = "button" class = askClass "Turn on notifications" on click(e) { emit askNotify(e) } }
  218. p { id = "notify-blocked" class = blockedClass "Notifications are blocked for this site in the browser settings." }
  219. p { id = "notify-planned" plannedText }
  220. button { id = "settings-open" type = "button" class = "quiet" "Reminder settings" on click(e) { emit openSettings(e) } }
  221. }
  222. ol { id = "month-weekdays" for (name of weekdays) { li { name } } }
  223. ol { id = "month-days" on click(e) { emit dayClick(e) }
  224. for (d of days) {
  225. li { class = d.cls
  226. time { datetime = d.date d.n }
  227. dayEvents {
  228. button { class = d.c0 type = "button" value = d.v0 d.t0 }
  229. button { class = d.c1 type = "button" value = d.v1 d.t1 }
  230. button { class = d.c2 type = "button" value = d.v2 d.t2 }
  231. button { class = d.c3 type = "button" value = d.v3 d.t3 }
  232. }
  233. }
  234. }
  235. }
  236. eventEditor { id = "eventeditor" class = formClass
  237. form { id = "eventform" class = formKind
  238. on submit(e) {
  239. e.preventDefault()
  240. emit saveEvent(e)
  241. }
  242. h2 { id = "formheading" formHeading }
  243. kindSwitch {
  244. button { id = "kind-timed" type = "button" class = timedButton "At a time" on click(e) { emit setKind(false) } }
  245. button { id = "kind-allday" type = "button" class = alldayButton "All day" on click(e) { emit setKind(true) } }
  246. }
  247. label { "Title"
  248. input { id = "f-title" name = "title" maxlength = "200" autocomplete = "off" required = "required" value = fTitle on input(e) { emit setF('title', e.target.value) } }
  249. }
  250. fieldRow {
  251. label { "From"
  252. input { id = "f-startdate" name = "startDate" type = "date" required = "required" value = fStartDate on input(e) { emit setF('startDate', e.target.value) } }
  253. }
  254. label { class = "timed" "at"
  255. input { id = "f-starttime" name = "startTime" type = "time" value = fStartTime on input(e) { emit setF('startTime', e.target.value) } }
  256. }
  257. }
  258. fieldRow {
  259. label { "To"
  260. input { id = "f-enddate" name = "endDate" type = "date" value = fEndDate on input(e) { emit setF('endDate', e.target.value) } }
  261. }
  262. label { class = "timed" "at"
  263. input { id = "f-endtime" name = "endTime" type = "time" value = fEndTime on input(e) { emit setF('endTime', e.target.value) } }
  264. }
  265. }
  266. label { "Place"
  267. input { id = "f-place" name = "place" maxlength = "200" autocomplete = "off" value = fPlace on input(e) { emit setF('place', e.target.value) } }
  268. }
  269. label { "Notes"
  270. textarea { id = "f-notes" name = "notes" rows = "4" maxlength = "5000" value = fNotes on input(e) { emit setF('notes', e.target.value) } }
  271. }
  272. reminderBox {
  273. h3 { "Reminders" }
  274. kindSwitch {
  275. button { id = "rem-calendar" type = "button" class = calClass "Calendar's" on click(e) { emit setCustom(false) } }
  276. button { id = "rem-own" type = "button" class = ownClass "Own" on click(e) { emit setCustom(true) } }
  277. }
  278. p { id = "rem-default-text" class = defaultClass defaultText }
  279. ol { id = "rem-rows" class = rowsClass
  280. for (r of remRows) {
  281. reminderRow { class = r.cls
  282. input { type = "number" min = "0" value = r.n on change(e) { emit setRem('rem', r.i, 'n', e.target.value) } }
  283. select { value = r.unit on change(e) { emit setRem('rem', r.i, 'unit', e.target.value) }
  284. option { value = "minutes" "minutes before" }
  285. option { value = "hours" "hours before" }
  286. option { value = "days" "days before, at" }
  287. }
  288. input { type = "time" value = r.time on change(e) { emit setRem('rem', r.i, 'time', e.target.value) } }
  289. button { type = "button" class = "quiet" "×" on click(e) { emit delRem('rem', r.i) } }
  290. }
  291. }
  292. }
  293. button { id = "rem-add" type = "button" class = addClass "Add reminder" on click(e) { emit addRem('rem') } }
  294. }
  295. p { id = "formerror" class = "message" formError }
  296. formButtons {
  297. button { id = "event-save" type = "submit" "Save" }
  298. button { id = "event-cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } }
  299. button { id = "event-delete" type = "button" class = deleteClass "Delete" on click(e) { emit deleteEvent(e) } }
  300. }
  301. }
  302. }
  303. eventEditor { id = "settingseditor" class = setClass
  304. form { id = "settingsform" class = "timed"
  305. on submit(e) {
  306. e.preventDefault()
  307. emit saveSettings(e)
  308. }
  309. h2 { "Reminder settings" }
  310. p { class = "muted" "Every event without reminders of its own is reminded like this:" }
  311. ol { id = "set-rows"
  312. for (r of setRows) {
  313. reminderRow { class = r.cls
  314. input { type = "number" min = "0" value = r.n on change(e) { emit setRem('set', r.i, 'n', e.target.value) } }
  315. select { value = r.unit on change(e) { emit setRem('set', r.i, 'unit', e.target.value) }
  316. option { value = "minutes" "minutes before" }
  317. option { value = "hours" "hours before" }
  318. option { value = "days" "days before, at" }
  319. }
  320. input { type = "time" value = r.time on change(e) { emit setRem('set', r.i, 'time', e.target.value) } }
  321. button { type = "button" class = "quiet" "×" on click(e) { emit delRem('set', r.i) } }
  322. }
  323. }
  324. }
  325. button { id = "set-add" type = "button" class = "quiet" "Add reminder" on click(e) { emit addRem('set') } }
  326. p { id = "settingserror" class = "message" setError }
  327. formButtons {
  328. button { id = "set-save" type = "submit" "Save" }
  329. button { id = "set-cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeSettings(e) } }
  330. }
  331. }
  332. }
  333. input { id = "month-view-goto" type = "hidden" on change(e) { emit monthGoto(e.target.value) } }
  334. }
  335. }
  336. // the events of the six weeks shown, from the server (none while signed out)
  337. loadEvents(y, m) {
  338. if (!signedIn) { return [] }
  339. b = build(y, m, today)
  340. r = emit server calLoad(b[0].date, b[41].date)
  341. if (r != null && r.soon != null) { takeSoon(r) }
  342. return r == null || r.events == null ? [] : r.events
  343. }
  344. on monthPrev(e) {
  345. browserEnv()
  346. paged = 1
  347. y = month == 1 ? year - 1 : year
  348. m = month == 1 ? 12 : month - 1
  349. show(y, m, loadEvents(y, m))
  350. }
  351. on monthNext(e) {
  352. browserEnv()
  353. paged = 1
  354. y = month == 12 ? year + 1 : year
  355. m = month == 12 ? 1 : month + 1
  356. show(y, m, loadEvents(y, m))
  357. }
  358. // the browser's today, language and time zone, once: today follows the user's time zone, the names their language
  359. on monthEnv(e) {
  360. browserEnv()
  361. // every ten minutes of use the timers are set again (a long-open page keeps the next week planned)
  362. if (signedIn && Date.now() - lastArm > 600000) { arm() }
  363. }
  364. browserEnv() {
  365. if (seen == 1) { return null }
  366. seen = 1
  367. serverDay = today
  368. now = new Date()
  369. lang = navigator.language != null ? navigator.language : 'en'
  370. zone = Intl.DateTimeFormat().resolvedOptions().timeZone
  371. browserDay = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  372. if (browserDay != serverDay) { today = browserDay }
  373. // the server already wrote the names of a language it knows: they stay, so paging never switches them
  374. if (!namesKnown) {
  375. mf = Intl.DateTimeFormat(lang, { month = 'long' })
  376. df = Intl.DateTimeFormat(lang, { weekday = 'short' })
  377. ms = []
  378. ds = []
  379. let i = 0
  380. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  381. i = 0
  382. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  383. // (only what differs is assigned: a same-valued assignment still repaints the title and the days)
  384. if (ms.join('|') != monthNames.join('|')) { monthNames = ms }
  385. if (ds.join('|') != weekdayNames.join('|')) { weekdayNames = ds weekdays = ds }
  386. de = lang.slice(0, 2) == 'de'
  387. pl = de ? 'Voriger Monat' : 'Previous month'
  388. nl = de ? 'Nächster Monat' : 'Next month'
  389. if (pl != prevLabel) { prevLabel = pl }
  390. if (nl != nextLabel) { nextLabel = nl }
  391. }
  392. fZone = zone != null && zone != '' ? zone : 'UTC'
  393. readPermission()
  394. // calendar#9: a phone freezes or drops an app it switched away from, and its timers with it. Whenever the app is
  395. // shown again the timers are set again, and what came due meanwhile is shown at once (arm → missed)
  396. document.addEventListener('visibilitychange', () => { if (document.visibilityState == 'visible') { arm() } })
  397. window.addEventListener('pageshow', () => { arm() })
  398. // Nothing is repainted unless the browser's day is not the server's: the server already drew this month with its events,
  399. // and a round trip here, answered while the user pages or taps a day, used to put the month back or swallow the tap.
  400. if (browserDay == serverDay) {
  401. if (!namesKnown) {
  402. named = monthNames[month - 1] + ' ' + year
  403. if (named != title) { title = named }
  404. }
  405. } else if (paged == 0) {
  406. // the month shown follows the corrected today only while the user has not paged yet (also when they page meanwhile)
  407. y = toNumber(today.slice(0, 4))
  408. m = toNumber(today.slice(5, 7))
  409. evs = loadEvents(y, m)
  410. if (paged == 0) { show(y, m, evs) } else { show(year, month, events) }
  411. } else { show(year, month, events) }
  412. // (offline the server did not answer: the timers are set from the reminders the page already holds)
  413. arm()
  414. }
  415. // ---- the reminders: notifications the browser holds a timer for (they fire while the app or its worker is open, also offline) ----
  416. // the server's newest answer about the reminders to come; the timers are set again from it
  417. takeSoon(r) {
  418. soon = r.soon
  419. if (r.defaults != null) { defaultText = r.defaults }
  420. if (r.defaultRows != null) { defRows = r.defaultRows }
  421. arm()
  422. }
  423. // one timer per reminder that is still to come within a week; the old timers are dropped first. Before that, calendar#9:
  424. // the reminders that came due since this device last planned or showed one, while no timer could fire (the app switched
  425. // away and frozen, dropped, or closed), are shown now — late, for events that have not started yet
  426. arm() {
  427. if (!signedIn) { return null }
  428. let i = 0
  429. while (i < timers.length) { clearTimeout(timers[i]) i = i + 1 }
  430. nowMs = Date.now()
  431. readPermission()
  432. since = remindedUntil(nowMs)
  433. if (notifyState == 'granted') {
  434. late = missed(soon, since, nowMs, today)
  435. i = 0
  436. while (i < late.length) {
  437. let q = late[i]
  438. notify(q.title, { body = q.late tag = q.key icon = '/icons/icon-192.png' })
  439. i = i + 1
  440. }
  441. }
  442. remember(nowMs)
  443. list = plan(soon, nowMs, 604800000)
  444. ids = []
  445. i = 0
  446. while (i < list.length) {
  447. // `p` is a new binding on every pass (hybriel #106): each timer keeps ITS reminder
  448. let p = list[i]
  449. ids.push(setTimeout(() => {
  450. remember(p.at)
  451. notify(p.title, { body = p.body tag = p.key icon = '/icons/icon-192.png' })
  452. }, p.at - nowMs))
  453. i = i + 1
  454. }
  455. timers = ids
  456. lastArm = nowMs
  457. plannedText = list.length == 0 ? '' : (list.length == 1 ? '1 reminder' : list.length + ' reminders') + ' set for the next 7 days'
  458. }
  459. // the instant up to which this device has planned or shown the user's reminders (the browser's localStorage); none
  460. // kept yet → the last 12 hours count as missed
  461. remindedKey() {
  462. return 'calendar-reminded-until-' + userId
  463. }
  464. remindedUntil(nowMs) {
  465. store = window.localStorage
  466. kept = store != null ? store.getItem(remindedKey()) : null
  467. return kept != null && kept != '' ? toNumber(kept) : nowMs - 43200000
  468. }
  469. remember(at) {
  470. store = window.localStorage
  471. if (store == null) { return null }
  472. kept = store.getItem(remindedKey())
  473. if (kept == null || kept == '' || toNumber(kept) < at) { store.setItem(remindedKey(), '' + at) }
  474. }
  475. readPermission() {
  476. n = window.Notification
  477. notifyState = n == null ? 'unsupported' : n.permission
  478. askClass = notifyState == 'default' ? '' : 'hidden'
  479. blockedClass = notifyState == 'denied' ? '' : 'hidden'
  480. }
  481. // the button is the click the browser wants before it allows notifications
  482. on askNotify(e) {
  483. notify('Notifications are on', { body = 'Your reminders show up here.' })
  484. readPermission()
  485. }
  486. // from a host script (the date picker): "YYYY-MM-DD" → show that date's month
  487. on monthGoto(text) {
  488. paged = 1
  489. y = toNumber(text.slice(0, 4))
  490. m = toNumber(text.slice(5, 7))
  491. show(y, m, loadEvents(y, m))
  492. }
  493. // ---- the editor ----
  494. // the reminders switch: the calendar's or the event's own
  495. syncCustom() {
  496. calClass = fCustom ? 'quiet' : ''
  497. ownClass = fCustom ? '' : 'quiet'
  498. defaultClass = fCustom ? 'hidden' : ''
  499. rowsClass = fCustom ? '' : 'hidden'
  500. addClass = fCustom ? 'quiet' : 'quiet hidden'
  501. }
  502. // the classes that follow the form's kind and whether it edits an existing event
  503. syncForm() {
  504. formKind = fAllDay ? 'allday' : 'timed'
  505. timedButton = fAllDay ? 'quiet' : ''
  506. alldayButton = fAllDay ? '' : 'quiet'
  507. deleteClass = isNew ? 'danger hidden' : 'danger'
  508. }
  509. openForm(fields, heading, fresh) {
  510. fId = fields.id
  511. fTitle = fields.title
  512. fAllDay = fields.allDay
  513. fStartDate = fields.startDate
  514. fStartTime = fields.startTime
  515. fEndDate = fields.endDate
  516. fEndTime = fields.endTime
  517. fPlace = fields.place
  518. fNotes = fields.notes
  519. isNew = fresh
  520. fCustom = fields.custom
  521. remRows = withIndex(fields.rows)
  522. syncCustom()
  523. formHeading = heading
  524. formError = ''
  525. formClass = 'open'
  526. syncForm()
  527. }
  528. // one click on the day list: an event button edits that event, anywhere else on a day adds one to that day
  529. on dayClick(e) {
  530. if (!signedIn) { return null }
  531. li = e.target.closest('li')
  532. if (li == null) { return null }
  533. date = li.querySelector('time').getAttribute('datetime')
  534. b = e.target.closest('button.event')
  535. if (b == null) {
  536. fresh = blank(date, fZone)
  537. openForm(fresh, 'New event', true)
  538. return null
  539. }
  540. let j = 0
  541. while (j < events.length) {
  542. let ev = events[j]
  543. if (ev.id == b.value) {
  544. let 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 }
  545. openForm(fields, 'Edit event', false)
  546. return null
  547. }
  548. j = j + 1
  549. }
  550. }
  551. on setF(name, value) {
  552. if (name == 'title') { fTitle = value }
  553. if (name == 'startDate') { fStartDate = value }
  554. if (name == 'startTime') { fStartTime = value }
  555. if (name == 'endDate') { fEndDate = value }
  556. if (name == 'endTime') { fEndTime = value }
  557. if (name == 'place') { fPlace = value }
  558. if (name == 'notes') { fNotes = value }
  559. }
  560. on setCustom(own) {
  561. fCustom = own
  562. // switching to own reminders starts from the calendar's, so nothing has to be typed for the usual case
  563. if (own && remRows.length == 0) { remRows = withIndex(defaultRows) }
  564. syncCustom()
  565. }
  566. // one reminder row of the event editor (which = 'rem') or of the settings (which = 'set')
  567. on setRem(which, i, field, value) {
  568. rows = which == 'rem' ? remRows : setRows
  569. if (i < 0 || i >= rows.length) { return null }
  570. out = []
  571. let k = 0
  572. while (k < rows.length) {
  573. let r = rows[k]
  574. let n = r.n
  575. let unit = r.unit
  576. let time = r.time
  577. if (k == i && field == 'n') { n = value }
  578. if (k == i && field == 'unit') { unit = value }
  579. if (k == i && field == 'time') { time = value }
  580. out.push({ unit = unit n = n time = time })
  581. k = k + 1
  582. }
  583. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  584. }
  585. on addRem(which) {
  586. rows = which == 'rem' ? remRows : setRows
  587. out = []
  588. let k = 0
  589. while (k < rows.length) { out.push({ unit = rows[k].unit n = rows[k].n time = rows[k].time }) k = k + 1 }
  590. out.push({ unit = 'minutes' n = '10' time = '09:00' })
  591. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  592. }
  593. on delRem(which, i) {
  594. rows = which == 'rem' ? remRows : setRows
  595. out = []
  596. let k = 0
  597. while (k < rows.length) {
  598. if (k != i) { out.push({ unit = rows[k].unit n = rows[k].n time = rows[k].time }) }
  599. k = k + 1
  600. }
  601. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  602. }
  603. // ---- the calendar settings: the default reminders ----
  604. on openSettings(e) {
  605. r = emit server calSettingsLoad()
  606. if (r == null || r.error != null) {
  607. setError = r == null ? 'the settings need the server: you are offline' : r.error
  608. setClass = 'open'
  609. return null
  610. }
  611. setRows = withIndex(r.rows)
  612. setError = ''
  613. setClass = 'open'
  614. }
  615. on closeSettings(e) {
  616. setClass = 'closed'
  617. }
  618. on saveSettings(e) {
  619. r = emit server calSettingsSave(setRows)
  620. if (r == null || r.error != null) {
  621. setError = r == null ? 'the settings could not be saved (offline?)' : r.error
  622. return null
  623. }
  624. setClass = 'closed'
  625. takeSoon(r)
  626. }
  627. on setKind(allDay) {
  628. fAllDay = allDay
  629. if (!allDay && fStartTime == '') { fStartTime = '09:00' }
  630. if (!allDay && fEndTime == '' && fEndDate != '') { fEndTime = '10:00' }
  631. syncForm()
  632. }
  633. on closeForm(e) {
  634. formClass = 'closed'
  635. }
  636. // what the form holds, as the face takes it
  637. formFields() {
  638. return { id = fId title = fTitle allDay = fAllDay startDate = fStartDate startTime = fStartTime endDate = fEndDate endTime = fEndTime place = fPlace notes = fNotes zone = fZone custom = fCustom reminders = remRows }
  639. }
  640. // the event as the page shows it, for an event saved while offline (the server sends the real one when it is back)
  641. localView(f) {
  642. let allDay = f.allDay
  643. let last = f.endDate != '' ? f.endDate : f.startDate
  644. mine = []
  645. let k = 0
  646. while (k < f.reminders.length) { mine.push({ unit = f.reminders[k].unit n = f.reminders[k].n time = f.reminders[k].time }) k = k + 1 }
  647. return { id = f.id title = f.title allDay = allDay first = f.startDate last = last
  648. startDate = f.startDate startTime = allDay ? '' : f.startTime endDate = f.endDate endTime = allDay ? '' : f.endTime
  649. place = f.place notes = f.notes start = f.startDate zone = f.zone custom = f.custom rows = f.custom ? mine : defRows }
  650. }
  651. on saveEvent(e) {
  652. f = formFields()
  653. if (fTitle.trim() == '' || fStartDate == '') {
  654. formError = 'the event needs a title and a start date'
  655. return null
  656. }
  657. r = emit server calSave(f, winFirst, winLast)
  658. if (r == null) {
  659. // OFFLINE: the save waits in the app's queue (it is sent when the connection is back); here the event and its
  660. // reminders are set at once, so the notification still comes
  661. if (f.id == '') { f.id = 'local-' + Date.now() }
  662. emit server calSaveQueued(f, winFirst, winLast)
  663. v = localView(f)
  664. evs = []
  665. let k = 0
  666. while (k < events.length) { if (events[k].id != v.id) { evs.push(events[k]) } k = k + 1 }
  667. evs.push(v)
  668. mine = []
  669. k = 0
  670. while (k < soon.length) { if (soon[k].id != v.id) { mine.push(soon[k]) } k = k + 1 }
  671. mine.push({ id = v.id title = v.title allDay = v.allDay startDate = v.startDate startTime = v.startTime place = v.place zone = v.zone rows = v.rows })
  672. soon = mine
  673. formClass = 'closed'
  674. show(year, month, evs)
  675. arm()
  676. return null
  677. }
  678. if (r.error != null) {
  679. formError = r.error
  680. return null
  681. }
  682. formClass = 'closed'
  683. takeSoon(r)
  684. show(year, month, r.events)
  685. }
  686. on deleteEvent(e) {
  687. r = emit server calDelete(fId, winFirst, winLast)
  688. if (r == null || r.error != null) {
  689. formError = r == null ? 'the event could not be deleted (offline?)' : r.error
  690. return null
  691. }
  692. formClass = 'closed'
  693. takeSoon(r)
  694. show(year, month, r.events)
  695. }

Branches

Latest commits

  • cf175c80calendar mission 008 (A7): re-vendor Hybriel master 0e3c9830 (bin 9d957fed, plugins whole: hl:crypto Web Push primitives, hl:web sendPush)mre
  • 9bf3c38bcalendar mission 008: re-vendor Hybriel f9833544 (gates old = new); Web Push stopped: hl:crypto half missing on mastermre
  • 186f969bcalendar mission 007: signed-in gate for the first-pointer race (fails on 3c5604f), README theme section, reportmre
  • ba9b4955calendar mission 006: first pointer event no longer re-runs the month (stale overwrite race), gate check, e2e settings cleanup, reportmre
  • bd730759calendar mission 006: theme move (WIP): brief, report, LOG, STATUSmre
  • b117b153calendar mission 006 WIP: reminder row fields keep 44 px; e2e script flagsmre
  • 53e3b0debrowser gate: Log out through the user menu, theme colour rgb(15, 20, 25)mre
  • 90bf9e2ecalendar mission 006 WIP: theme dbe3de7, theme shell (AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst), styles cut to the app's own rules, check-theme gate, e2e toolsmre
  • 3c5604f9identkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 4996bbe7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • f5bf0803calendar mission 004: re-vendor Hybriel to the shared static build 0f396652; compose cache volume + tmpfs; notify-paths gate in deploy.sh; gate start waits 150 s; reportmre
  • eb86526fmission brief 004mre
  • 2f953b16deploy.sh: wait up to DEPLOY_URL_WAIT (120 s) for 200 after the restart, as the other apps (cold compile, hybriel#148)mre
  • 9078e1b7calendar mission 003 (2/2): report, LOG, STATUS (cause: timers only in an open, front, touched page; gate old 5/2 new 7/0; Hybriel gaps Web Push + page-shown hook, STOP)mre
  • 31ffe910calendar mission 003 (1/2): calendar#9 — missed reminders shown when the app is back, timers set again when it is shownmre
  • 26c997d3mission brief 003mre
  • c6809c76calendar mission 002: report, LOG, STATUS (test bug; fixed gate 78/0 at 00:30 05:40 12:00 19:59 20:01; 23:30 not run, quota stop)mre
  • 00f72783calendar mission 002 (2/2): TEST bug — the reminder steps get their own identity (carol): alice's all-day Vacation (20th-22nd) from the events steps added 2 reminders from the 12th 20:00 to the 19th 23:00; Tokyo check no longer skipped in the Tokyo eveningmre
  • 54fb0ce1calendar mission 002 (1/2): gate clock override GATE_CLOCK=HH:MM (this process and every page get Date shifted to that local time today)mre
  • c838ee79mission brief 002mre