gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit53e3b0de53e3b0debrowser gate: Log out through the user menu, theme colour rgb(15, 20, 25)mre53e3b0de/components/month-view/month-view.hl

27.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. 'reminder-row.other input[type=time]' { display = 'none' }
  202. '@media (min-width: 60rem)' {
  203. '#month-days li' { minHeight = '5rem' padding = '.3rem' }
  204. }
  205. }
  206. View {
  207. monthView { on pointerover(e) { emit monthEnv(e) } on focusin(e) { emit monthEnv(e) } on touchstart(e) { emit monthEnv(e) }
  208. header {
  209. button { id = "month-prev" type = "button" class = "quiet" title = prevLabel "‹" on click(e) { emit monthPrev(e) } }
  210. h2 { id = "month-title" title }
  211. button { id = "month-next" type = "button" class = "quiet" title = nextLabel "›" on click(e) { emit monthNext(e) } }
  212. }
  213. bar { id = "reminder-bar" class = barClass
  214. button { id = "notify-ask" type = "button" class = askClass "Turn on notifications" on click(e) { emit askNotify(e) } }
  215. p { id = "notify-blocked" class = blockedClass "Notifications are blocked for this site in the browser settings." }
  216. p { id = "notify-planned" plannedText }
  217. button { id = "settings-open" type = "button" class = "quiet" "Reminder settings" on click(e) { emit openSettings(e) } }
  218. }
  219. ol { id = "month-weekdays" for (name of weekdays) { li { name } } }
  220. ol { id = "month-days" on click(e) { emit dayClick(e) }
  221. for (d of days) {
  222. li { class = d.cls
  223. time { datetime = d.date d.n }
  224. dayEvents {
  225. button { class = d.c0 type = "button" value = d.v0 d.t0 }
  226. button { class = d.c1 type = "button" value = d.v1 d.t1 }
  227. button { class = d.c2 type = "button" value = d.v2 d.t2 }
  228. button { class = d.c3 type = "button" value = d.v3 d.t3 }
  229. }
  230. }
  231. }
  232. }
  233. eventEditor { id = "eventeditor" class = formClass
  234. form { id = "eventform" class = formKind
  235. on submit(e) {
  236. e.preventDefault()
  237. emit saveEvent(e)
  238. }
  239. h2 { id = "formheading" formHeading }
  240. kindSwitch {
  241. button { id = "kind-timed" type = "button" class = timedButton "At a time" on click(e) { emit setKind(false) } }
  242. button { id = "kind-allday" type = "button" class = alldayButton "All day" on click(e) { emit setKind(true) } }
  243. }
  244. label { "Title"
  245. input { id = "f-title" name = "title" maxlength = "200" autocomplete = "off" required = "required" value = fTitle on input(e) { emit setF('title', e.target.value) } }
  246. }
  247. fieldRow {
  248. label { "From"
  249. input { id = "f-startdate" name = "startDate" type = "date" required = "required" value = fStartDate on input(e) { emit setF('startDate', e.target.value) } }
  250. }
  251. label { class = "timed" "at"
  252. input { id = "f-starttime" name = "startTime" type = "time" value = fStartTime on input(e) { emit setF('startTime', e.target.value) } }
  253. }
  254. }
  255. fieldRow {
  256. label { "To"
  257. input { id = "f-enddate" name = "endDate" type = "date" value = fEndDate on input(e) { emit setF('endDate', e.target.value) } }
  258. }
  259. label { class = "timed" "at"
  260. input { id = "f-endtime" name = "endTime" type = "time" value = fEndTime on input(e) { emit setF('endTime', e.target.value) } }
  261. }
  262. }
  263. label { "Place"
  264. input { id = "f-place" name = "place" maxlength = "200" autocomplete = "off" value = fPlace on input(e) { emit setF('place', e.target.value) } }
  265. }
  266. label { "Notes"
  267. textarea { id = "f-notes" name = "notes" rows = "4" maxlength = "5000" value = fNotes on input(e) { emit setF('notes', e.target.value) } }
  268. }
  269. reminderBox {
  270. h3 { "Reminders" }
  271. kindSwitch {
  272. button { id = "rem-calendar" type = "button" class = calClass "Calendar's" on click(e) { emit setCustom(false) } }
  273. button { id = "rem-own" type = "button" class = ownClass "Own" on click(e) { emit setCustom(true) } }
  274. }
  275. p { id = "rem-default-text" class = defaultClass defaultText }
  276. ol { id = "rem-rows" class = rowsClass
  277. for (r of remRows) {
  278. reminderRow { class = r.cls
  279. input { type = "number" min = "0" value = r.n on change(e) { emit setRem('rem', r.i, 'n', e.target.value) } }
  280. select { value = r.unit on change(e) { emit setRem('rem', r.i, 'unit', e.target.value) }
  281. option { value = "minutes" "minutes before" }
  282. option { value = "hours" "hours before" }
  283. option { value = "days" "days before, at" }
  284. }
  285. input { type = "time" value = r.time on change(e) { emit setRem('rem', r.i, 'time', e.target.value) } }
  286. button { type = "button" class = "quiet" "×" on click(e) { emit delRem('rem', r.i) } }
  287. }
  288. }
  289. }
  290. button { id = "rem-add" type = "button" class = addClass "Add reminder" on click(e) { emit addRem('rem') } }
  291. }
  292. p { id = "formerror" class = "message" formError }
  293. formButtons {
  294. button { id = "event-save" type = "submit" "Save" }
  295. button { id = "event-cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } }
  296. button { id = "event-delete" type = "button" class = deleteClass "Delete" on click(e) { emit deleteEvent(e) } }
  297. }
  298. }
  299. }
  300. eventEditor { id = "settingseditor" class = setClass
  301. form { id = "settingsform" class = "timed"
  302. on submit(e) {
  303. e.preventDefault()
  304. emit saveSettings(e)
  305. }
  306. h2 { "Reminder settings" }
  307. p { class = "muted" "Every event without reminders of its own is reminded like this:" }
  308. ol { id = "set-rows"
  309. for (r of setRows) {
  310. reminderRow { class = r.cls
  311. input { type = "number" min = "0" value = r.n on change(e) { emit setRem('set', r.i, 'n', e.target.value) } }
  312. select { value = r.unit on change(e) { emit setRem('set', r.i, 'unit', e.target.value) }
  313. option { value = "minutes" "minutes before" }
  314. option { value = "hours" "hours before" }
  315. option { value = "days" "days before, at" }
  316. }
  317. input { type = "time" value = r.time on change(e) { emit setRem('set', r.i, 'time', e.target.value) } }
  318. button { type = "button" class = "quiet" "×" on click(e) { emit delRem('set', r.i) } }
  319. }
  320. }
  321. }
  322. button { id = "set-add" type = "button" class = "quiet" "Add reminder" on click(e) { emit addRem('set') } }
  323. p { id = "settingserror" class = "message" setError }
  324. formButtons {
  325. button { id = "set-save" type = "submit" "Save" }
  326. button { id = "set-cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeSettings(e) } }
  327. }
  328. }
  329. }
  330. input { id = "month-view-goto" type = "hidden" on change(e) { emit monthGoto(e.target.value) } }
  331. }
  332. }
  333. // the events of the six weeks shown, from the server (none while signed out)
  334. loadEvents(y, m) {
  335. if (!signedIn) { return [] }
  336. b = build(y, m, today)
  337. r = emit server calLoad(b[0].date, b[41].date)
  338. if (r != null && r.soon != null) { takeSoon(r) }
  339. return r == null || r.events == null ? [] : r.events
  340. }
  341. on monthPrev(e) {
  342. browserEnv()
  343. paged = 1
  344. y = month == 1 ? year - 1 : year
  345. m = month == 1 ? 12 : month - 1
  346. show(y, m, loadEvents(y, m))
  347. }
  348. on monthNext(e) {
  349. browserEnv()
  350. paged = 1
  351. y = month == 12 ? year + 1 : year
  352. m = month == 12 ? 1 : month + 1
  353. show(y, m, loadEvents(y, m))
  354. }
  355. // the browser's today, language and time zone, once: today follows the user's time zone, the names their language
  356. on monthEnv(e) {
  357. browserEnv()
  358. // every ten minutes of use the timers are set again (a long-open page keeps the next week planned)
  359. if (signedIn && Date.now() - lastArm > 600000) { arm() }
  360. }
  361. browserEnv() {
  362. if (seen == 1) { return null }
  363. seen = 1
  364. now = new Date()
  365. lang = navigator.language != null ? navigator.language : 'en'
  366. zone = Intl.DateTimeFormat().resolvedOptions().timeZone
  367. today = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  368. // the server already wrote the names of a language it knows: they stay, so paging never switches them
  369. if (!namesKnown) {
  370. mf = Intl.DateTimeFormat(lang, { month = 'long' })
  371. df = Intl.DateTimeFormat(lang, { weekday = 'short' })
  372. ms = []
  373. ds = []
  374. let i = 0
  375. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  376. i = 0
  377. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  378. monthNames = ms
  379. weekdayNames = ds
  380. weekdays = ds
  381. de = lang.slice(0, 2) == 'de'
  382. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  383. nextLabel = de ? 'Nächster Monat' : 'Next month'
  384. }
  385. fZone = zone != null && zone != '' ? zone : 'UTC'
  386. readPermission()
  387. // calendar#9: a phone freezes or drops an app it switched away from, and its timers with it. Whenever the app is
  388. // shown again the timers are set again, and what came due meanwhile is shown at once (arm → missed)
  389. document.addEventListener('visibilitychange', () => { if (document.visibilityState == 'visible') { arm() } })
  390. window.addEventListener('pageshow', () => { arm() })
  391. // the month shown follows the corrected today only while the user has not paged yet
  392. if (paged == 0) {
  393. y = toNumber(today.slice(0, 4))
  394. m = toNumber(today.slice(5, 7))
  395. show(y, m, loadEvents(y, m))
  396. } else { show(year, month, loadEvents(year, month)) }
  397. // (offline the server did not answer: the timers are set from the reminders the page already holds)
  398. arm()
  399. }
  400. // ---- the reminders: notifications the browser holds a timer for (they fire while the app or its worker is open, also offline) ----
  401. // the server's newest answer about the reminders to come; the timers are set again from it
  402. takeSoon(r) {
  403. soon = r.soon
  404. if (r.defaults != null) { defaultText = r.defaults }
  405. if (r.defaultRows != null) { defRows = r.defaultRows }
  406. arm()
  407. }
  408. // one timer per reminder that is still to come within a week; the old timers are dropped first. Before that, calendar#9:
  409. // the reminders that came due since this device last planned or showed one, while no timer could fire (the app switched
  410. // away and frozen, dropped, or closed), are shown now — late, for events that have not started yet
  411. arm() {
  412. if (!signedIn) { return null }
  413. let i = 0
  414. while (i < timers.length) { clearTimeout(timers[i]) i = i + 1 }
  415. nowMs = Date.now()
  416. readPermission()
  417. since = remindedUntil(nowMs)
  418. if (notifyState == 'granted') {
  419. late = missed(soon, since, nowMs, today)
  420. i = 0
  421. while (i < late.length) {
  422. let q = late[i]
  423. notify(q.title, { body = q.late tag = q.key icon = '/icons/icon-192.png' })
  424. i = i + 1
  425. }
  426. }
  427. remember(nowMs)
  428. list = plan(soon, nowMs, 604800000)
  429. ids = []
  430. i = 0
  431. while (i < list.length) {
  432. // `p` is a new binding on every pass (hybriel #106): each timer keeps ITS reminder
  433. let p = list[i]
  434. ids.push(setTimeout(() => {
  435. remember(p.at)
  436. notify(p.title, { body = p.body tag = p.key icon = '/icons/icon-192.png' })
  437. }, p.at - nowMs))
  438. i = i + 1
  439. }
  440. timers = ids
  441. lastArm = nowMs
  442. plannedText = list.length == 0 ? '' : (list.length == 1 ? '1 reminder' : list.length + ' reminders') + ' set for the next 7 days'
  443. }
  444. // the instant up to which this device has planned or shown the user's reminders (the browser's localStorage); none
  445. // kept yet → the last 12 hours count as missed
  446. remindedKey() {
  447. return 'calendar-reminded-until-' + userId
  448. }
  449. remindedUntil(nowMs) {
  450. store = window.localStorage
  451. kept = store != null ? store.getItem(remindedKey()) : null
  452. return kept != null && kept != '' ? toNumber(kept) : nowMs - 43200000
  453. }
  454. remember(at) {
  455. store = window.localStorage
  456. if (store == null) { return null }
  457. kept = store.getItem(remindedKey())
  458. if (kept == null || kept == '' || toNumber(kept) < at) { store.setItem(remindedKey(), '' + at) }
  459. }
  460. readPermission() {
  461. n = window.Notification
  462. notifyState = n == null ? 'unsupported' : n.permission
  463. askClass = notifyState == 'default' ? '' : 'hidden'
  464. blockedClass = notifyState == 'denied' ? '' : 'hidden'
  465. }
  466. // the button is the click the browser wants before it allows notifications
  467. on askNotify(e) {
  468. notify('Notifications are on', { body = 'Your reminders show up here.' })
  469. readPermission()
  470. }
  471. // from a host script (the date picker): "YYYY-MM-DD" → show that date's month
  472. on monthGoto(text) {
  473. paged = 1
  474. y = toNumber(text.slice(0, 4))
  475. m = toNumber(text.slice(5, 7))
  476. show(y, m, loadEvents(y, m))
  477. }
  478. // ---- the editor ----
  479. // the reminders switch: the calendar's or the event's own
  480. syncCustom() {
  481. calClass = fCustom ? 'quiet' : ''
  482. ownClass = fCustom ? '' : 'quiet'
  483. defaultClass = fCustom ? 'hidden' : ''
  484. rowsClass = fCustom ? '' : 'hidden'
  485. addClass = fCustom ? 'quiet' : 'quiet hidden'
  486. }
  487. // the classes that follow the form's kind and whether it edits an existing event
  488. syncForm() {
  489. formKind = fAllDay ? 'allday' : 'timed'
  490. timedButton = fAllDay ? 'quiet' : ''
  491. alldayButton = fAllDay ? '' : 'quiet'
  492. deleteClass = isNew ? 'danger hidden' : 'danger'
  493. }
  494. openForm(fields, heading, fresh) {
  495. fId = fields.id
  496. fTitle = fields.title
  497. fAllDay = fields.allDay
  498. fStartDate = fields.startDate
  499. fStartTime = fields.startTime
  500. fEndDate = fields.endDate
  501. fEndTime = fields.endTime
  502. fPlace = fields.place
  503. fNotes = fields.notes
  504. isNew = fresh
  505. fCustom = fields.custom
  506. remRows = withIndex(fields.rows)
  507. syncCustom()
  508. formHeading = heading
  509. formError = ''
  510. formClass = 'open'
  511. syncForm()
  512. }
  513. // one click on the day list: an event button edits that event, anywhere else on a day adds one to that day
  514. on dayClick(e) {
  515. if (!signedIn) { return null }
  516. li = e.target.closest('li')
  517. if (li == null) { return null }
  518. date = li.querySelector('time').getAttribute('datetime')
  519. b = e.target.closest('button.event')
  520. if (b == null) {
  521. fresh = blank(date, fZone)
  522. openForm(fresh, 'New event', true)
  523. return null
  524. }
  525. let j = 0
  526. while (j < events.length) {
  527. let ev = events[j]
  528. if (ev.id == b.value) {
  529. 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 }
  530. openForm(fields, 'Edit event', false)
  531. return null
  532. }
  533. j = j + 1
  534. }
  535. }
  536. on setF(name, value) {
  537. if (name == 'title') { fTitle = value }
  538. if (name == 'startDate') { fStartDate = value }
  539. if (name == 'startTime') { fStartTime = value }
  540. if (name == 'endDate') { fEndDate = value }
  541. if (name == 'endTime') { fEndTime = value }
  542. if (name == 'place') { fPlace = value }
  543. if (name == 'notes') { fNotes = value }
  544. }
  545. on setCustom(own) {
  546. fCustom = own
  547. // switching to own reminders starts from the calendar's, so nothing has to be typed for the usual case
  548. if (own && remRows.length == 0) { remRows = withIndex(defaultRows) }
  549. syncCustom()
  550. }
  551. // one reminder row of the event editor (which = 'rem') or of the settings (which = 'set')
  552. on setRem(which, i, field, value) {
  553. rows = which == 'rem' ? remRows : setRows
  554. if (i < 0 || i >= rows.length) { return null }
  555. out = []
  556. let k = 0
  557. while (k < rows.length) {
  558. let r = rows[k]
  559. let n = r.n
  560. let unit = r.unit
  561. let time = r.time
  562. if (k == i && field == 'n') { n = value }
  563. if (k == i && field == 'unit') { unit = value }
  564. if (k == i && field == 'time') { time = value }
  565. out.push({ unit = unit n = n time = time })
  566. k = k + 1
  567. }
  568. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  569. }
  570. on addRem(which) {
  571. rows = which == 'rem' ? remRows : setRows
  572. out = []
  573. let k = 0
  574. while (k < rows.length) { out.push({ unit = rows[k].unit n = rows[k].n time = rows[k].time }) k = k + 1 }
  575. out.push({ unit = 'minutes' n = '10' time = '09:00' })
  576. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  577. }
  578. on delRem(which, i) {
  579. rows = which == 'rem' ? remRows : setRows
  580. out = []
  581. let k = 0
  582. while (k < rows.length) {
  583. if (k != i) { out.push({ unit = rows[k].unit n = rows[k].n time = rows[k].time }) }
  584. k = k + 1
  585. }
  586. if (which == 'rem') { remRows = withIndex(out) } else { setRows = withIndex(out) }
  587. }
  588. // ---- the calendar settings: the default reminders ----
  589. on openSettings(e) {
  590. r = emit server calSettingsLoad()
  591. if (r == null || r.error != null) {
  592. setError = r == null ? 'the settings need the server: you are offline' : r.error
  593. setClass = 'open'
  594. return null
  595. }
  596. setRows = withIndex(r.rows)
  597. setError = ''
  598. setClass = 'open'
  599. }
  600. on closeSettings(e) {
  601. setClass = 'closed'
  602. }
  603. on saveSettings(e) {
  604. r = emit server calSettingsSave(setRows)
  605. if (r == null || r.error != null) {
  606. setError = r == null ? 'the settings could not be saved (offline?)' : r.error
  607. return null
  608. }
  609. setClass = 'closed'
  610. takeSoon(r)
  611. }
  612. on setKind(allDay) {
  613. fAllDay = allDay
  614. if (!allDay && fStartTime == '') { fStartTime = '09:00' }
  615. if (!allDay && fEndTime == '' && fEndDate != '') { fEndTime = '10:00' }
  616. syncForm()
  617. }
  618. on closeForm(e) {
  619. formClass = 'closed'
  620. }
  621. // what the form holds, as the face takes it
  622. formFields() {
  623. 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 }
  624. }
  625. // the event as the page shows it, for an event saved while offline (the server sends the real one when it is back)
  626. localView(f) {
  627. let allDay = f.allDay
  628. let last = f.endDate != '' ? f.endDate : f.startDate
  629. mine = []
  630. let k = 0
  631. 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 }
  632. return { id = f.id title = f.title allDay = allDay first = f.startDate last = last
  633. startDate = f.startDate startTime = allDay ? '' : f.startTime endDate = f.endDate endTime = allDay ? '' : f.endTime
  634. place = f.place notes = f.notes start = f.startDate zone = f.zone custom = f.custom rows = f.custom ? mine : defRows }
  635. }
  636. on saveEvent(e) {
  637. f = formFields()
  638. if (fTitle.trim() == '' || fStartDate == '') {
  639. formError = 'the event needs a title and a start date'
  640. return null
  641. }
  642. r = emit server calSave(f, winFirst, winLast)
  643. if (r == null) {
  644. // OFFLINE: the save waits in the app's queue (it is sent when the connection is back); here the event and its
  645. // reminders are set at once, so the notification still comes
  646. if (f.id == '') { f.id = 'local-' + Date.now() }
  647. emit server calSaveQueued(f, winFirst, winLast)
  648. v = localView(f)
  649. evs = []
  650. let k = 0
  651. while (k < events.length) { if (events[k].id != v.id) { evs.push(events[k]) } k = k + 1 }
  652. evs.push(v)
  653. mine = []
  654. k = 0
  655. while (k < soon.length) { if (soon[k].id != v.id) { mine.push(soon[k]) } k = k + 1 }
  656. 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 })
  657. soon = mine
  658. formClass = 'closed'
  659. show(year, month, evs)
  660. arm()
  661. return null
  662. }
  663. if (r.error != null) {
  664. formError = r.error
  665. return null
  666. }
  667. formClass = 'closed'
  668. takeSoon(r)
  669. show(year, month, r.events)
  670. }
  671. on deleteEvent(e) {
  672. r = emit server calDelete(fId, winFirst, winLast)
  673. if (r == null || r.error != null) {
  674. formError = r == null ? 'the event could not be deleted (offline?)' : r.error
  675. return null
  676. }
  677. formClass = 'closed'
  678. takeSoon(r)
  679. show(year, month, r.events)
  680. }

Branches

Latest commits

  • 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
  • f2bc67ecdocker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 2881119fcalendar mission 001 (4/4): code order — README layout + same-output test, STATUS, LOG, report; tests/letcount.py (sub-folders), tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 3b2a4cd0calendar mission 001 (3/4): code order — let only where reassigned (147 dropped incl. components/month-view; 110 left: 67 reassigned, 43 loop-bound); gates 78/0 (3 of 4 runs; 1 known 'next month' flake) + 18/0, live-data run = step 2mre
  • f5ce6b1dcalendar mission 001 (2/4): code order — topics, map, thin faces: lib/util.hl, lib/events(-helpers).hl (+calendarView/settingsView/soonOf), lib/settings.hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login route takes &req/&sessions (failed-login reason now kept); gates 78/0 + 18/0mre
  • c6fbd011calendar mission 001 (1/4): code order — files moved: lib/events.hl, lib/users.hl, components/styles.hl (imports only); gates 78/0 + 18/0, live-data run identicalmre
  • d6c59290calendar: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 78/0 + 18/0mre