exact-scheduler 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ // exact-scheduler: one to seven days as columns of hours, with all-day
2
+ // events above, overlapping events side by side, and a line at the time now.
3
+ //
4
+ // The keyboard: events are Tab stops in day order. With `slots`, one hour is
5
+ // a Tab stop too, and the arrows move it by an hour or a day.
6
+
7
+ use SchedulerNames, SchedulerStrings, SchedulerRules, SchedulerSegment, SchedulerViewport from "./shapes.contract"
8
+ use addDays, startOfWeek, dateOf, weekdayOf, withinLimits, dayAllowed, daysBetween from "./days.contract"
9
+ use dayTitle, weekTitle, dayWords, localDigits, named, tintOf from "./text.contract"
10
+ use SchedulerBar, SchedulerChip, pageMotion, swipeWay, titleDirection, titleSide, schedulerForward, schedulerBackward from "./parts.contract"
11
+
12
+ // The hours of a day that no event touches: the ones drawn as slots.
13
+ fn freeHours(timed: SchedulerSegment[], day: string): number[] = range(24).filter(h => timed.filter(s => s.day == day and s.startMinute < h * 60 + 60 and max(s.endMinute, s.startMinute + 1) > h * 60).length == 0)
14
+ // The first free hour from `h` on, else the last one before it.
15
+ fn nearHour(free: number[], h: number): number = match first(free.filter(x => x >= h)) { case some(x) => x, case none => (match free.at(-1) { case some(x) => x, case none => h }) }
16
+ fn hourBefore(free: number[], h: number): number = match free.filter(x => x < h).at(-1) { case some(x) => x, case none => nearHour(free, h) }
17
+
18
+ // One day.
19
+ component DayTimeline
20
+ props
21
+ id: string
22
+ day: string
23
+ today: string
24
+ nowMinute: number
25
+ segments: SchedulerSegment[]
26
+ rules: SchedulerRules
27
+ names: SchedulerNames
28
+ strings: SchedulerStrings
29
+ label: string
30
+ hourHeight: number
31
+ openHour: number
32
+ slots: bool
33
+ rtl: bool
34
+ pressEvent: action
35
+ pressSlot: action
36
+ pickDay: action
37
+ turnTo: action
38
+ action event(eventId: string)
39
+ pressEvent(eventId)
40
+ action slot(at: string, minute: number)
41
+ pressSlot(at, minute)
42
+ action head(at: string)
43
+ pickDay(at)
44
+ action go(at: string)
45
+ turnTo(at)
46
+ view
47
+ Timeline(id=id, from=day, days=1, today=today, nowMinute=nowMinute, segments=segments, rules=rules, names=names, strings=strings, label=label, hourHeight=hourHeight, openHour=openHour, slots=slots, rtl=rtl, pressEvent=event, pressSlot=slot, pickDay=head, turnTo=go)
48
+
49
+ // The week a day is in, from the locale's first day.
50
+ component WeekTimeline
51
+ props
52
+ id: string
53
+ day: string
54
+ today: string
55
+ nowMinute: number
56
+ segments: SchedulerSegment[]
57
+ rules: SchedulerRules
58
+ names: SchedulerNames
59
+ strings: SchedulerStrings
60
+ label: string
61
+ hourHeight: number
62
+ openHour: number
63
+ slots: bool
64
+ rtl: bool
65
+ pressEvent: action
66
+ pressSlot: action
67
+ pickDay: action
68
+ turnTo: action
69
+ action event(eventId: string)
70
+ pressEvent(eventId)
71
+ action slot(at: string, minute: number)
72
+ pressSlot(at, minute)
73
+ action head(at: string)
74
+ pickDay(at)
75
+ action go(at: string)
76
+ turnTo(at)
77
+ view
78
+ Timeline(id=id, from=startOfWeek(day, names.firstDay), days=7, today=today, nowMinute=nowMinute, segments=segments, rules=rules, names=names, strings=strings, label=label, hourHeight=hourHeight, openHour=openHour, slots=slots, rtl=rtl, pressEvent=event, pressSlot=slot, pickDay=head, turnTo=go)
79
+
80
+ // `days` days from `from`.
81
+ component Timeline
82
+ props
83
+ id: string
84
+ from: string
85
+ days: number
86
+ today: string
87
+ nowMinute: number
88
+ segments: SchedulerSegment[]
89
+ rules: SchedulerRules
90
+ names: SchedulerNames
91
+ strings: SchedulerStrings
92
+ label: string
93
+ hourHeight: number
94
+ openHour: number
95
+ slots: bool
96
+ rtl: bool
97
+ pressEvent: action
98
+ pressSlot: action
99
+ pickDay: action
100
+ turnTo: action
101
+ resource screen = exactViewport() as shape SchedulerViewport
102
+ state step = 0
103
+ state slotDay = 0
104
+ state slotHour = 9
105
+ state wanted = ""
106
+ state fromX = 0
107
+ state fromY = 0
108
+ state swiped = false
109
+ derive tabSlotDay = min(slotDay, days - 1)
110
+ derive still = screen.prefersReducedMotion
111
+ derive shown = range(days).map(i => addDays(from, i))
112
+ derive last = addDays(from, days - 1)
113
+ derive visible = segments.filter(s => s.day >= from and s.day <= last)
114
+ derive allDay = visible.filter(s => s.allDay)
115
+ derive timed = visible.filter(s => not s.allDay)
116
+ derive canPrevious = withinLimits(rules, addDays(from, -1)) or (rules.maxDay != "" and from > rules.maxDay)
117
+ derive canNext = withinLimits(rules, addDays(last, 1)) or (rules.minDay != "" and last < rules.minDay)
118
+
119
+ action turn(n: number)
120
+ if (n > 0 ? canNext : canPrevious)
121
+ step = n
122
+ turnTo(addDays(from, n * days))
123
+ // A sideways drag turns the page, from a finger on a button too (a
124
+ // `swipeleft` leaves those to the button), so it is read from the pointer.
125
+ // The press that the same contact may end in is not one.
126
+ action down(e: PointerEvent)
127
+ fromX = e.clientX
128
+ fromY = e.clientY
129
+ swiped = false
130
+ action up(e: PointerEvent)
131
+ let way = swipeWay(e.clientX - fromX, e.clientY - fromY, rtl)
132
+ if way != 0
133
+ swiped = true
134
+ turn(way)
135
+ task settle when swiped
136
+ after(80, settled)
137
+ action settled
138
+ swiped = false
139
+ action unused
140
+ step = 0
141
+ action event(eventId: string)
142
+ if not swiped
143
+ pressEvent(eventId)
144
+ action head(day: string)
145
+ pickDay(day)
146
+ action slot(i: number, hour: number)
147
+ slotDay = i
148
+ slotHour = hour
149
+ let day = addDays(from, i)
150
+ if not swiped and dayAllowed(rules, day)
151
+ pressSlot(day, hour * 60)
152
+ action slotKey(k: string)
153
+ let side = (k == "ArrowRight" ? 1 : (k == "ArrowLeft" ? -1 : 0)) * (rtl ? -1 : 1)
154
+ let below = (k == "ArrowDown" ? 1 : (k == "ArrowUp" ? -1 : 0))
155
+ if slots and (side != 0 or below != 0)
156
+ preventDefault()
157
+ let d = max(0, min(days - 1, tabSlotDay + side))
158
+ let day = addDays(from, d)
159
+ let free = freeHours(timed, day)
160
+ let h = below < 0 ? hourBefore(free, slotHour) : nearHour(free, slotHour + below)
161
+ slotDay = d
162
+ slotHour = h
163
+ wanted = `${id}-slot-${d}-${h}`
164
+ task follow when wanted != "" key=wanted
165
+ after(1, land)
166
+ action land
167
+ focus(wanted)
168
+ wanted = ""
169
+
170
+ view
171
+ column flex=1 min-height=0 testId=id
172
+ SchedulerBar(id=id, title=(days == 1 ? dayTitle(names, from) : weekTitle(names, from, days)), titleLabel="", previousLabel=(days == 1 ? strings.previousDay : strings.previousWeek), nextLabel=(days == 1 ? strings.nextDay : strings.nextWeek), canPrevious=canPrevious, canNext=canNext, opens=false, expanded=false, previous=turn(-1), next=turn(1), open=unused)
173
+ if days > 1
174
+ row padding-bottom=4
175
+ view width=52 flex-shrink=0 aria-hidden=true
176
+ for day in shown key=day
177
+ button press=head(day) aria-label=dayWords(names, day, [(day == today ? strings.today : "")]) aria-current=(day == today ? "date" : "false") appearance="none" background-color="transparent" flex="1 1 0%" min-width=0 min-height=44 padding=0 display="flex" flex-direction="column" align-items="center" testId=`${id}-head-${day}`
178
+ text (screen.width >= 640 ? named(names.weekdaysShort, weekdayOf(day)) : named(names.weekdaysNarrow, weekdayOf(day))) font-size="min(-exact-caption1, 14px)" color="-exact-secondary-label" white-space="nowrap" aria-hidden=true
179
+ view min-width=28 height=28 border-radius=14 display="flex" align-items="center" justify-content="center" background-color=(day == today ? "AccentColor" : "transparent") aria-hidden=true
180
+ text localDigits(names.digits, dateOf(day)) font-size="min(-exact-subheadline, 18px)" font-weight=600 font-variant-numeric="tabular-nums" color=(day == today ? "AccentColorText" : (names.weekend.includes(weekdayOf(day)) ? "-exact-secondary-label" : "-exact-label"))
181
+ if allDay.length > 0
182
+ row padding="2px 0" border-top="1px solid -exact-separator" role="group" aria-label=strings.allDay testId=`${id}-all-day`
183
+ text strings.allDay width=52 flex-shrink=0 padding="3px 4px" box-sizing="border-box" font-size="min(-exact-caption2, 12px)" color="-exact-secondary-label" text-align="end" overflow="hidden" aria-hidden=true
184
+ for day in shown key=day
185
+ column flex="1 1 0%" min-width=0 gap=2 padding="0 1px" box-sizing="border-box"
186
+ for s in allDay.filter(x => x.day == day) key=s.key
187
+ SchedulerChip(segment=s, rtl=rtl, press=event(s.id), testId=`${id}-event-${s.key}`)
188
+ scroll scrollTop=(openHour * hourHeight) flex=1 min-height=0 border-top="1px solid -exact-separator" role="group" aria-label=`${label}, ${days == 1 ? dayTitle(names, from) : weekTitle(names, from, days)}` testId=`${id}-scroll`
189
+ for page in [from] key=page
190
+ row position="relative" height=`${24 * hourHeight + 12}px` padding-top=12 box-sizing="border-box" key=slotKey pointerdown=down pointerup=up touch-action="pan-y" animation=pageMotion(still, step, rtl) testId=`${id}-hours`
191
+ view width=52 flex-shrink=0 position="relative" aria-hidden=true
192
+ for h in range(24) key=`${h}`
193
+ text named(names.hours, h) position="absolute" top=`${h * hourHeight - 8}px` left=0 right=6 text-align="end" font-size="min(-exact-caption2, 12px)" line-height="16px" color="-exact-secondary-label" white-space="nowrap" overflow="hidden"
194
+ view position="relative" flex=1 min-width=0
195
+ for h in range(24) key=`${h}`
196
+ view position="absolute" left=0 right=0 top=`${h * hourHeight}px` height=1 background-color="-exact-label" opacity=0.14 pointer-events="none" aria-hidden=true
197
+ row position="absolute" inset=0
198
+ for day, i in shown key=day
199
+ view position="relative" flex="1 1 0%" min-width=0 role="group" aria-label=dayWords(names, day, [(day == today ? strings.today : "")]) testId=`${id}-column-${day}`
200
+ if i > 0
201
+ view position="absolute" top=0 bottom=0 left=(rtl ? "auto" : "0px") right=(rtl ? "0px" : "auto") width=1 background-color="-exact-label" opacity=0.14 pointer-events="none" aria-hidden=true
202
+ if slots
203
+ for h in freeHours(timed, day) key=`${h}`
204
+ button id=`${id}-slot-${i}-${h}` press=slot(i, h) tabindex=(i == tabSlotDay and h == nearHour(freeHours(timed, day), slotHour) ? 0 : -1) aria-disabled=(not dayAllowed(rules, day)) aria-label=`${dayTitle(names, day)}, ${named(names.hours, h)}` appearance="none" background-color="transparent" position="absolute" left=0 right=0 top=`${h * hourHeight}px` height=`${hourHeight}px` padding=0 testId=`${id}-slot-${day}-${h}`
205
+ text "" aria-hidden=true
206
+ for s in timed.filter(x => x.day == day) key=s.key
207
+ TimelineEvent(segment=s, hourHeight=hourHeight, rtl=rtl, roomy=(days == 1), press=event(s.id), testId=`${id}-event-${s.key}`)
208
+ if day == today and nowMinute >= 0
209
+ view position="absolute" left=0 right=0 top=`${nowMinute * hourHeight / 60 - 1}px` height=2 background-color="-exact-system-red" pointer-events="none" role="img" aria-label=strings.now testId=`${id}-now`
210
+
211
+ // A timed event in its lane, at its time.
212
+ component TimelineEvent
213
+ props
214
+ segment: SchedulerSegment
215
+ hourHeight: number
216
+ rtl: bool
217
+ roomy: bool
218
+ press: action
219
+ testId: string
220
+ derive s = segment
221
+ derive side = `${100 * s.lane / s.lanes}%`
222
+ derive tall = max(24, (s.endMinute - s.startMinute) * hourHeight / 60 - 1)
223
+ // Beside another event in a narrow column, the title keeps to one line: broken
224
+ // anywhere, it stood one letter a line.
225
+ derive narrow = not roomy and s.lanes > 1
226
+ view
227
+ // `touch-action` again on the button: it clips, and a clipping box is
228
+ // where the hours' own stops applying.
229
+ view position="absolute" top=`${s.startMinute * hourHeight / 60}px` height=`${tall}px` left=(rtl ? "auto" : side) right=(rtl ? side : "auto") width=`${100 / s.lanes}%` padding="0 1px" box-sizing="border-box"
230
+ button press=press aria-label=[s.title, s.time, s.detail].filter(x => x != "").join(", ") appearance="none" display="flex" flex-direction="row" align-items="stretch" position="relative" overflow="hidden" touch-action="pan-y" padding=0 width="100%" height="100%" border-radius=6 background-color="-exact-background" testId=testId
231
+ view position="absolute" inset=0 background-color=tintOf(s.tint) opacity=0.18 pointer-events="none" aria-hidden=true
232
+ view width=3 flex-shrink=0 background-color=tintOf(s.tint) aria-hidden=true
233
+ column flex=1 min-width=0 padding=(roomy ? "3px 4px" : "3px 2px") overflow="hidden" align-items="flex-start" pointer-events="none" user-select="none" aria-hidden=true
234
+ text s.title width="100%" font-size="min(-exact-caption1, 14px)" line-height="18px" font-weight=600 color="-exact-label" direction=titleDirection(s.dir, rtl) text-align=titleSide(rtl) overflow="hidden" text-overflow="ellipsis" line-clamp=(roomy or narrow ? 1 : max(1, floor((tall - 6) / 18))) overflow-wrap=(narrow ? "normal" : "anywhere") white-space=(narrow ? "nowrap" : "normal")
235
+ if roomy and tall >= 44
236
+ text (s.detail == "" ? s.time : `${s.time} · ${s.detail}`) width="100%" font-size="min(-exact-caption2, 13px)" line-height="16px" color="-exact-label" text-align="start" white-space="nowrap" overflow="hidden" text-overflow="ellipsis"