gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainf9b8f918Merge branch 'calendar-vendor-day61' into mainAtlasmain/tests/dst/old-reminders.hl

8.6 KB

  1. // tests/dst/old-reminders.hl — the reminders.hl of main ce16a4e BEFORE mission 009 (browser times by hand-rolled Intl, `new Date` for the device zone), kept only so tests/dst.mjs can show old = new. Not used by the app.
  2. // reminders.hl — THE REMINDERS (ticket calendar#4), shared by the server (events.hl checks and stores them) and the
  3. // month view in the browser (the editor rows, the notification times).
  4. // Stored as WorldAPI Event `$reminders` (https://worldapi.org/standards/event): `{ $before: 'PT30M' }` (30 minutes, or 'PT2H'
  5. // hours before the start) and `{ $days_before: 1, $time: '20:00' }` (that many days before, at that wall-clock time).
  6. // The page edits them as ROWS: { unit = 'minutes' | 'hours' | 'days' n = '30' time = '20:00' } (all text).
  7. // Event without `$reminders` = inherits the calendar's; `[]` = none.
  8. import { isText, isTime, pad, prevDay, nextDay } from '../../components/month-view/dates.hl'
  9. static maxRows = 8
  10. static defaultRows = [
  11. { unit = 'hours' n = '1' time = '09:00' }
  12. { unit = 'days' n = '1' time = '20:00' }
  13. ]
  14. // digits only, no sign, no leading noise; 1..5 digits
  15. static isCount = (s) => {
  16. if (!isText(s) || s.length < 1 || s.length > 5) { return false }
  17. let i = 0
  18. while (i < s.length) {
  19. let c = s.charCodeAt(i)
  20. if (c < 48 || c > 57) { return false }
  21. i = i + 1
  22. }
  23. return true
  24. }
  25. // rows the page sent → { error } or { reminders } (the stored `$reminders` list)
  26. static toReminders = (rows) => {
  27. if (rows == null || hlTypeName(rows) != 'Hybrid') { return { error = 'the reminders must be a list' } }
  28. if (rows.length > maxRows) { return { error = 'at most ' + maxRows + ' reminders' } }
  29. out = []
  30. let i = 0
  31. while (i < rows.length) {
  32. let r = rows[i]
  33. if (r == null || hlTypeName(r) != 'Hybrid' || !isCount(r.n)) { return { error = 'a reminder needs a whole number' } }
  34. let n = toNumber(r.n)
  35. if (r.unit == 'minutes') {
  36. if (n < 1 || n > 10080) { return { error = 'minutes must be 1 to 10080' } }
  37. let o = {}
  38. o['$before'] = 'PT' + n + 'M'
  39. out.push(o)
  40. } else if (r.unit == 'hours') {
  41. if (n < 1 || n > 168) { return { error = 'hours must be 1 to 168' } }
  42. let o = {}
  43. o['$before'] = 'PT' + n + 'H'
  44. out.push(o)
  45. } else if (r.unit == 'days') {
  46. if (n > 30) { return { error = 'days must be 0 to 30' } }
  47. if (!isTime(r.time)) { return { error = 'a day reminder needs a time' } }
  48. let o = {}
  49. o['$days_before'] = n
  50. o['$time'] = r.time
  51. out.push(o)
  52. } else { return { error = 'a reminder is minutes, hours or days before' } }
  53. i = i + 1
  54. }
  55. return { reminders = out }
  56. }
  57. // the stored `$reminders` → rows for the editor (what is not ours to read is left out)
  58. static toRows = (list) => {
  59. out = []
  60. if (list == null) { return out }
  61. let i = 0
  62. while (i < list.length) {
  63. let r = list[i]
  64. let b = r['$before']
  65. if (isText(b) && b.length > 3 && b.slice(0, 2) == 'PT') {
  66. let unit = b.slice(b.length - 1) == 'H' ? 'hours' : 'minutes'
  67. let digits = b.slice(2, b.length - 1)
  68. if ((b.slice(b.length - 1) == 'H' || b.slice(b.length - 1) == 'M') && isCount(digits)) { out.push({ unit = unit n = digits time = '09:00' }) }
  69. } else if (r['$days_before'] != null && isTime(r['$time'])) {
  70. out.push({ unit = 'days' n = '' + r['$days_before'] time = r['$time'] })
  71. }
  72. i = i + 1
  73. }
  74. return out
  75. }
  76. // a date moved by n days (n may be negative)
  77. static shift = (date, n) => {
  78. let d = date
  79. let k = n
  80. while (k > 0) { d = nextDay(d) k = k - 1 }
  81. while (k < 0) { d = prevDay(d) k = k + 1 }
  82. return d
  83. }
  84. // rows for a list in the editor: each carries its position `i` and the class that shows the time only for days
  85. static withIndex = (rows) => {
  86. out = []
  87. let i = 0
  88. while (i < rows.length) {
  89. let r = rows[i]
  90. out.push({ i = i unit = r.unit n = r.n time = r.time cls = r.unit == 'days' ? 'days' : 'other' })
  91. i = i + 1
  92. }
  93. return out
  94. }
  95. static plural = (n, one, many) => { return n + ' ' + (n == 1 ? one : many) }
  96. // one row as words: "1 hour before", "the day before at 20:00", "2 days before at 09:00", "the same day at 08:00"
  97. static describeRow = (r) => {
  98. n = toNumber(r.n)
  99. if (r.unit == 'minutes') { return plural(n, 'minute', 'minutes') + ' before' }
  100. if (r.unit == 'hours') { return plural(n, 'hour', 'hours') + ' before' }
  101. if (n == 0) { return 'the same day at ' + r.time }
  102. if (n == 1) { return 'the day before at ' + r.time }
  103. return n + ' days before at ' + r.time
  104. }
  105. static describe = (rows) => {
  106. if (rows == null || rows.length == 0) { return 'no reminders' }
  107. let out = ''
  108. let i = 0
  109. while (i < rows.length) {
  110. out = out + (i == 0 ? '' : ', ') + describeRow(rows[i])
  111. i = i + 1
  112. }
  113. return out
  114. }
  115. // ---- browser side: the time a reminder fires (epoch ms) --------------------------------------------------
  116. // The wall clock of a time zone as an offset (ms) from UTC at the instant t.
  117. static offsetOf = (zone, t) => {
  118. f = Intl.DateTimeFormat('en-US', { timeZone = zone hourCycle = 'h23' year = 'numeric' month = 'numeric' day = 'numeric' hour = 'numeric' minute = 'numeric' second = 'numeric' })
  119. parts = f.formatToParts(new Date(t))
  120. v = { year = 0 month = 1 day = 1 hour = 0 minute = 0 second = 0 }
  121. let i = 0
  122. while (i < parts.length) {
  123. let p = parts[i]
  124. if (p.type != 'literal') { v[p.type] = toNumber(p.value) }
  125. i = i + 1
  126. }
  127. return Date.UTC(v.year, v.month - 1, v.day, v.hour, v.minute, v.second) - (t - t % 1000)
  128. }
  129. // 'YYYY-MM-DDTHH:MM:SS' wall clock in `zone` ('' = the device's own zone) → the instant, epoch ms
  130. static instantOf = (wall, zone) => {
  131. y = toNumber(wall.slice(0, 4))
  132. mo = toNumber(wall.slice(5, 7))
  133. d = toNumber(wall.slice(8, 10))
  134. h = toNumber(wall.slice(11, 13))
  135. mi = toNumber(wall.slice(14, 16))
  136. if (zone == null || zone == '') { return new Date(y, mo - 1, d, h, mi, 0).getTime() }
  137. guess = Date.UTC(y, mo - 1, d, h, mi, 0)
  138. t = guess - offsetOf(zone, guess)
  139. return guess - offsetOf(zone, t)
  140. }
  141. // the words of the notification (title = the event's title)
  142. static bodyOf = (ev, r) => {
  143. at = ev.allDay ? '' : ' at ' + ev.startTime
  144. let text = ''
  145. if (r.unit == 'minutes') { text = 'Starts in ' + plural(toNumber(r.n), 'minute', 'minutes') }
  146. else if (r.unit == 'hours') { text = 'Starts in ' + plural(toNumber(r.n), 'hour', 'hours') }
  147. else {
  148. n = toNumber(r.n)
  149. text = n == 0 ? 'Today' + at : (n == 1 ? 'Tomorrow' + at : 'In ' + n + ' days' + at + ', ' + ev.startDate)
  150. }
  151. return ev.place != '' ? text + ' · ' + ev.place : text
  152. }
  153. // the words of a reminder shown LATE (its time passed while the app could not run: switched away, dropped, closed):
  154. // when the event starts, not "Starts in 1 hour"
  155. static lateBodyOf = (ev, todayText) => {
  156. let text = ''
  157. if (ev.allDay) { text = ev.startDate == todayText ? 'Today, all day' : 'All day, ' + ev.startDate }
  158. else { text = 'Starts at ' + ev.startTime + (ev.startDate == todayText ? '' : ', ' + ev.startDate) }
  159. return ev.place != '' ? text + ' · ' + ev.place : text
  160. }
  161. // the events { id title allDay startDate startTime place zone rows } → their reminders with fromMs < at < toMs:
  162. // [{ key, at, start, title, body, late }] (late = the words for showing it after its time; todayText = the browser's today)
  163. static oldPlanBetween = (events, fromMs, toMs, todayText) => {
  164. out = []
  165. let i = 0
  166. while (i < events.length) {
  167. let ev = events[i]
  168. let zone = ev.allDay ? '' : ev.zone
  169. let startWall = ev.startDate + 'T' + (ev.allDay ? '00:00' : ev.startTime) + ':00'
  170. let start = instantOf(startWall, zone)
  171. let j = 0
  172. while (j < ev.rows.length) {
  173. let r = ev.rows[j]
  174. let at = 0
  175. if (r.unit == 'minutes') { at = start - toNumber(r.n) * 60000 }
  176. else if (r.unit == 'hours') { at = start - toNumber(r.n) * 3600000 }
  177. else {
  178. let day = ev.startDate
  179. let k = toNumber(r.n)
  180. while (k > 0) { day = prevDay(day) k = k - 1 }
  181. at = instantOf(day + 'T' + r.time + ':00', zone)
  182. }
  183. if (at > fromMs && at < toMs) {
  184. out.push({ id = ev.id key = ev.id + ':' + at at = at start = start title = ev.title body = bodyOf(ev, r) late = lateBodyOf(ev, todayText) })
  185. }
  186. j = j + 1
  187. }
  188. i = i + 1
  189. }
  190. return out
  191. }
  192. // the reminders still to come within `horizon` ms of `nowMs` (the browser timers)
  193. static oldPlan = (events, nowMs, horizon) => { return oldPlanBetween(events, nowMs, nowMs + horizon, '') }
  194. // calendar#9: the reminders whose time came after `sinceMs` and up to now while no timer could show them, for events
  195. // that have not started yet — shown at once, late; one per event (the newest of its reminders)
  196. static oldMissed = (events, sinceMs, nowMs, todayText) => {
  197. all = oldPlanBetween(events, sinceMs, nowMs + 1, todayText)
  198. out = []
  199. let i = 0
  200. while (i < all.length) {
  201. let q = all[i]
  202. let newest = q.start > nowMs
  203. let j = 0
  204. while (j < all.length) {
  205. if (all[j].id == q.id && all[j].at > q.at) { newest = false }
  206. j = j + 1
  207. }
  208. if (newest) { out.push(q) }
  209. i = i + 1
  210. }
  211. return out
  212. }

Branches

Latest commits

  • 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
  • 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
  • add4b499|||PR Make calendar day actions keyboard accessibleAtlas
  • 79e49b4dMerge branch 'hl-standard' into mainAtlas
  • e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
  • eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
  • cef81394Wait for the DST fixture page instead of an unused socketmre
  • 42434f47Check lazy-session cookies after login with a stored anonymous fixturemre
  • 728e84e3Re-vendor complete Hybriel day41 plugin directoriesmre
  • cbd7ff43Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 59d1da0acalendar: plain declarations instead of needless let in 9 files (calendar#17): 49 declarations that are never reassigned; reassigned ones and the timer closure's p stay letmre
  • f5415cf9calendar: the Style blocks in the language's nested form (calendar#16): rules named after the View's own elements with their children nested, pseudo-classes and @media nested, merged duplicate select rule; same CSS, shots identical in Chrome at 4 sizesmre
  • edb7c947calendar: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 56+4 failing → 60 passedmre
  • bf8cc0fcStyles move into their components: the shell's styles into main.hl, the events and the editors into the month view's Style block; styles.hl keeps only the theme inherit, the accent and the message line; .gitignore ignores a bin symlink too; tests/styles-shots.mjs for the before/after pixel comparemre