calendar
All repositories: gitoria
5.1 KB
// lib/events-helpers.hl — what lib/events.hl looks up (calendar mission 001, code order): the size limits, what the// page sends → a WorldAPI Event record (build), a stored event → what the page shows (viewOf), the temporary ids of// events made offline, the window check of the faces and the order by start.import { isText, isDate, isTime, isZone, nextDay, prevDay, before } from './dates.hl'import { envOr } from './util.hl'import { toReminders, toRows } from './reminders.hl'static maxTitle = 200static maxPlace = 200static maxNotes = 5000// events per user (security mission SC: a signed-in user could fill the disk): CALENDAR_MAX_EVENTSstatic maxEvents = toNumber(envOr('CALENDAR_MAX_EVENTS', '20000'))// what the page sends: { id?, custom, reminders (rows), title, allDay, startDate, startTime, endDate, endTime, place, notes, zone }// → { error } or { record } (a WorldAPI Event without owner/id). endDate/endTime may be empty (no end); all-day: the// last day, inclusive ('' = one day).static build = (b) => {if (b == null || hlTypeName(b) != 'Hybrid') { return { error = 'the event must be an object' } }if (!isText(b.title) || b.title.trim() == '') { return { error = 'the event needs a title' } }if (b.title.length > maxTitle) { return { error = 'the title is too long (at most ' + maxTitle + ' characters)' } }if (!isText(b.place) || b.place.length > maxPlace) { return { error = 'the place is too long (at most ' + maxPlace + ' characters)' } }if (!isText(b.notes) || b.notes.length > maxNotes) { return { error = 'the notes are too long (at most ' + maxNotes + ' characters)' } }if (b.allDay != true && b.allDay != false) { return { error = 'allDay must be true or false' } }if (!isDate(b.startDate)) { return { error = 'the start date is not a date' } }if (!isZone(b.zone)) { return { error = 'the time zone is missing' } }r = {}r['$type'] = 'event'r['$version'] = 1r['$title'] = b.title.trim()if (b.notes.trim() != '') { r['$description'] = b.notes }if (b.place.trim() != '') { r['$location'] = b.place.trim() }r['$status'] = 'confirmed'r['$visibility'] = 'private'// no own reminders: `$reminders` stays out and the calendar's apply; own ones (also none at all) are writtenif (b.custom == true) {rem = toReminders(b.reminders)if (rem.error != null) { return { error = rem.error } }r['$reminders'] = rem.reminders}if (b.allDay) {r['$kind'] = 'allday'r['$start'] = b.startDateif (isText(b.endDate) && b.endDate != '') {if (!isDate(b.endDate)) { return { error = 'the end date is not a date' } }if (before(b.endDate, b.startDate)) { return { error = 'the end is before the start' } }if (b.endDate != b.startDate) { r['$end'] = nextDay(b.endDate) }}return { record = r }}if (!isTime(b.startTime)) { return { error = 'the start time is not a time' } }r['$kind'] = 'appointment'r['$timezone'] = b.zoner['$start'] = b.startDate + 'T' + b.startTime + ':00'hasEnd = (isText(b.endDate) && b.endDate != '') || (isText(b.endTime) && b.endTime != '')if (hasEnd) {ed = isText(b.endDate) && b.endDate != '' ? b.endDate : b.startDateif (!isDate(ed)) { return { error = 'the end date is not a date' } }if (!isTime(b.endTime)) { return { error = 'the end time is not a time' } }end = ed + 'T' + b.endTime + ':00'if (before(end, r['$start'])) { return { error = 'the end is before the start' } }r['$end'] = end}return { record = r }}// what the page shows of a stored event: first / last day (inclusive), times, textsstatic viewOf = (Hybrid e) => {allDay = e['$kind'] == 'allday'start = e['$start']end = e['$end']first = start.slice(0, 10)let last = firstif (allDay) {if (end != null) { last = prevDay(end) }} else if (end != null) {last = end.slice(0, 10)}v = { id = e.id title = e['$title'] allDay = allDay first = first last = laststartDate = first startTime = allDay ? '' : start.slice(11, 16)endDate = end == null ? '' : last endTime = allDay || end == null ? '' : end.slice(11, 16)place = e['$location'] == null ? '' : e['$location'] notes = e['$description'] == null ? '' : e['$description']start = start zone = e['$timezone'] == null ? '' : e['$timezone']custom = e['$reminders'] != null rows = e['$reminders'] == null ? [] : toRows(e['$reminders']) }return v}// an event made offline carries a temporary id `local-…` until the page reloads; the server keeps it as `ref`, so an edit// (or delete) sent with that id reaches the same event instead of making a second onestatic isLocalId = (id) => { return isText(id) && id.length > 6 && id.length <= 60 && id.slice(0, 6) == 'local-' }// the window is two dates the page shows; anything else is refusedstatic validWindow = (first, last) => { return isDate(first) && isDate(last) }// the views ordered by start (a hand-written selection sort)static byStart = (Hybrid views) => {m = views.lengthout = []used = []let i = 0while (i < m) { used.push(false) i = i + 1 }let round = 0while (round < m) {let best = -1let j = 0while (j < m) {if (!used[j] && (best < 0 || before(views[j].start, views[best].start))) { best = j }j = j + 1}used[best] = trueout.push(views[best])round = round + 1}return out}
Branches
Latest commits
- 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
- 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