@jsb188/calendar 1.1.2
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.
- package/package.json +45 -0
- package/src/constants/calendar.ts +153 -0
- package/src/css.d.ts +7 -0
- package/src/libs/calendar-geometry.ts +500 -0
- package/src/libs/calendar-gesture.ts +341 -0
- package/src/libs/calendar-now.ts +39 -0
- package/src/libs/calendar-range.ts +401 -0
- package/src/libs/calendar-state.ts +26 -0
- package/src/libs/calendar-url.ts +191 -0
- package/src/libs/roster-layout.ts +388 -0
- package/src/main.ts +32 -0
- package/src/modules/Calendar.tsx +736 -0
- package/src/modules/MiniCalendar.tsx +376 -0
- package/src/types/calendar.d.ts +128 -0
- package/src/types/shift-form.d.ts +19 -0
- package/src/ui/CalendarAgendaUI.tsx +115 -0
- package/src/ui/CalendarMonthUI.tsx +142 -0
- package/src/ui/CalendarToolbarUI.tsx +133 -0
- package/src/ui/CalendarUI.tsx +401 -0
- package/src/ui/CalendarYearUI.tsx +67 -0
- package/src/ui/RosterUI.tsx +273 -0
- package/src/ui/ShiftFormUI.tsx +333 -0
- package/tests/calendar-geometry.test.ts +549 -0
- package/tests/calendar-gesture.test.ts +340 -0
- package/tests/calendar-range.test.ts +358 -0
- package/tests/calendar-url.test.ts +189 -0
- package/tests/roster-layout.test.ts +210 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +11 -0
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jsb188/calendar",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "Calendar and scheduling surface for web.",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "jsb188",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/main.ts",
|
|
10
|
+
"./constants/calendar": "./src/constants/calendar.ts",
|
|
11
|
+
"./libs/calendar-geometry": "./src/libs/calendar-geometry.ts",
|
|
12
|
+
"./libs/calendar-now": "./src/libs/calendar-now.ts",
|
|
13
|
+
"./libs/calendar-gesture": "./src/libs/calendar-gesture.ts",
|
|
14
|
+
"./libs/calendar-range": "./src/libs/calendar-range.ts",
|
|
15
|
+
"./libs/calendar-state": "./src/libs/calendar-state.ts",
|
|
16
|
+
"./libs/calendar-url": "./src/libs/calendar-url.ts",
|
|
17
|
+
"./modules/MiniCalendar": "./src/modules/MiniCalendar.tsx",
|
|
18
|
+
"./types/calendar.d.ts": "./src/types/calendar.d.ts",
|
|
19
|
+
"./ui/CalendarAgendaUI": "./src/ui/CalendarAgendaUI.tsx",
|
|
20
|
+
"./ui/CalendarMonthUI": "./src/ui/CalendarMonthUI.tsx",
|
|
21
|
+
"./ui/CalendarToolbarUI": "./src/ui/CalendarToolbarUI.tsx",
|
|
22
|
+
"./ui/CalendarUI": "./src/ui/CalendarUI.tsx",
|
|
23
|
+
"./ui/CalendarYearUI": "./src/ui/CalendarYearUI.tsx",
|
|
24
|
+
"./libs/roster-layout": "./src/libs/roster-layout.ts",
|
|
25
|
+
"./ui/RosterUI": "./src/ui/RosterUI.tsx",
|
|
26
|
+
"./ui/ShiftFormUI": "./src/ui/ShiftFormUI.tsx",
|
|
27
|
+
"./types/shift-form.d.ts": "./src/types/shift-form.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@jsb188/app": "1.1.2",
|
|
31
|
+
"@jsb188/graphql": "1.1.2",
|
|
32
|
+
"@jsb188/mday": "1.1.2",
|
|
33
|
+
"@jsb188/react": "1.1.2",
|
|
34
|
+
"@jsb188/react-web": "1.1.2",
|
|
35
|
+
"jotai": "^2.20.2",
|
|
36
|
+
"luxon": "^3.7.2",
|
|
37
|
+
"react": "^19.3.0",
|
|
38
|
+
"react-router": "^8.4.0",
|
|
39
|
+
"@jsb188/form": "1.1.2"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "vitest run --config ./vitest.config.ts --passWithNoTests",
|
|
43
|
+
"build": "tsc"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calendar surface constants.
|
|
3
|
+
*
|
|
4
|
+
* Everything here describes how the calendar is DRAWN and NAVIGATED. Nothing
|
|
5
|
+
* about what a schedule means lives in this package — that is
|
|
6
|
+
* @jsb188/mday/constants/schedule.ts, and duplicating any of it here would
|
|
7
|
+
* give the client a second opinion about the rules.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The view modes. DAY / FOUR_DAYS / WEEK are the same time-grid component at
|
|
12
|
+
* different widths; MONTH, YEAR and AGENDA are their own surfaces.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const CALENDAR_VIEW_ENUMS = [
|
|
16
|
+
'ROSTER',
|
|
17
|
+
'DAY',
|
|
18
|
+
'FOUR_DAYS',
|
|
19
|
+
'WEEK',
|
|
20
|
+
'MONTH',
|
|
21
|
+
'YEAR',
|
|
22
|
+
'AGENDA',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export type CalendarViewEnum = (typeof CALENDAR_VIEW_ENUMS)[number];
|
|
26
|
+
|
|
27
|
+
// How many days each time-grid view shows. MONTH/YEAR/AGENDA are absent on
|
|
28
|
+
// purpose: their spans are derived from the anchor date, not from a count.
|
|
29
|
+
export const CALENDAR_VIEW_DAY_COUNTS: Partial<
|
|
30
|
+
Record<CalendarViewEnum, number>
|
|
31
|
+
> = {
|
|
32
|
+
ROSTER: 7,
|
|
33
|
+
DAY: 1,
|
|
34
|
+
FOUR_DAYS: 4,
|
|
35
|
+
WEEK: 7,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// The agenda list runs forward from its anchor rather than around it; a
|
|
39
|
+
// worker opening "Schedule" wants what is coming, not what they already did.
|
|
40
|
+
export const CALENDAR_AGENDA_DAY_COUNT = 30;
|
|
41
|
+
|
|
42
|
+
// How often the calendar re-reads the clock. The now line moves in minutes and
|
|
43
|
+
// "today" changes once a day, so polling faster buys nothing; polling slower
|
|
44
|
+
// would leave a calendar open past midnight highlighting yesterday.
|
|
45
|
+
export const CALENDAR_NOW_TICK_MS = 30_000;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Time-grid geometry. These are the defaults; the grid scales its hour height
|
|
49
|
+
* to the container, so they are starting points rather than fixed sizes.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
// Pixels per hour at 100% zoom. 48 shows a full 24-hour day in roughly a
|
|
53
|
+
// laptop viewport without the 15-minute lines collapsing into each other.
|
|
54
|
+
export const CALENDAR_HOUR_HEIGHT = 48;
|
|
55
|
+
export const CALENDAR_HOUR_HEIGHT_MIN = 24;
|
|
56
|
+
export const CALENDAR_HOUR_HEIGHT_MAX = 160;
|
|
57
|
+
|
|
58
|
+
// Width of the left-hand time gutter, and the height of the all-day band's
|
|
59
|
+
// single row. Both are needed by pointer math, so they cannot live in CSS
|
|
60
|
+
// alone.
|
|
61
|
+
export const CALENDAR_GUTTER_WIDTH = 56;
|
|
62
|
+
export const CALENDAR_ALL_DAY_ROW_HEIGHT = 22;
|
|
63
|
+
|
|
64
|
+
// The grid snaps drags and click-to-create to this many minutes. Fifteen is
|
|
65
|
+
// the smallest increment anybody schedules staff in, and it divides the hour
|
|
66
|
+
// lines evenly so the snap always lands on something drawn.
|
|
67
|
+
export const CALENDAR_SNAP_MINUTES = 15;
|
|
68
|
+
|
|
69
|
+
// An event shorter than this is still drawn this tall, so a 5-minute entry
|
|
70
|
+
// stays clickable instead of becoming a hairline.
|
|
71
|
+
export const CALENDAR_MIN_EVENT_MINUTES = 20;
|
|
72
|
+
|
|
73
|
+
// Click-to-create makes a block this long; a drag overrides it.
|
|
74
|
+
export const CALENDAR_DEFAULT_NEW_EVENT_MINUTES = 60;
|
|
75
|
+
|
|
76
|
+
// Below this pointer travel a press is a click, not a drag. Without it every
|
|
77
|
+
// click creates a zero-length event, because no hand is perfectly still.
|
|
78
|
+
export const CALENDAR_DRAG_THRESHOLD_PX = 4;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Overlap layout. When several events share a time range they split the day
|
|
82
|
+
* column between them; past this many they stack and the rest collapse into a
|
|
83
|
+
* "+N more" affordance rather than becoming unreadable slivers.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
export const CALENDAR_MAX_OVERLAP_COLUMNS = 6;
|
|
87
|
+
|
|
88
|
+
// Each stacked event peeks out from under the one in front by this fraction
|
|
89
|
+
// of a column, so the ones behind stay discoverable.
|
|
90
|
+
export const CALENDAR_OVERLAP_STACK_RATIO = 0.12;
|
|
91
|
+
|
|
92
|
+
// A month cell shows this many events before collapsing to "+N more".
|
|
93
|
+
export const CALENDAR_MONTH_CELL_MAX_EVENTS = 3;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The search-param keys the page reads and writes.
|
|
97
|
+
*
|
|
98
|
+
* View and date live in the URL rather than in state because the app shell
|
|
99
|
+
* remounts every route on a PATHNAME change but not on a search change — so
|
|
100
|
+
* this is what lets somebody move from March to April without the calendar
|
|
101
|
+
* being torn down and rebuilt, and it makes a particular week linkable.
|
|
102
|
+
*
|
|
103
|
+
* The keys are one or two characters and every default is omitted, so the
|
|
104
|
+
* common case is a bare "/app/calendar" and a shared link reads as
|
|
105
|
+
* "/app/calendar?v=month&d=2026-07-25" rather than a query string somebody
|
|
106
|
+
* has to decode.
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
export const CALENDAR_SEARCH_PARAMS = {
|
|
110
|
+
view: 'v',
|
|
111
|
+
calendar: 'cal',
|
|
112
|
+
room: 'room',
|
|
113
|
+
groupByRoom: 'rooms',
|
|
114
|
+
panel: 'p',
|
|
115
|
+
open: 'o',
|
|
116
|
+
date: 'd',
|
|
117
|
+
mine: 'me',
|
|
118
|
+
// The calendars the viewer has HIDDEN; absent means show everything
|
|
119
|
+
hiddenCalendars: 'hide',
|
|
120
|
+
} as const;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* URL spellings of the views. Lowercase and human-readable, because these are
|
|
124
|
+
* the part of the URL a person actually reads; "4day" beats "FOUR_DAYS".
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
export const CALENDAR_VIEW_SLUGS: Record<CalendarViewEnum, string> = {
|
|
128
|
+
ROSTER: 'roster',
|
|
129
|
+
DAY: 'day',
|
|
130
|
+
FOUR_DAYS: '4day',
|
|
131
|
+
WEEK: 'week',
|
|
132
|
+
MONTH: 'month',
|
|
133
|
+
YEAR: 'year',
|
|
134
|
+
AGENDA: 'agenda',
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// Omitted from the URL, so an untouched calendar has no query string at all
|
|
138
|
+
export const CALENDAR_DEFAULT_VIEW: CalendarViewEnum = 'WEEK';
|
|
139
|
+
|
|
140
|
+
// Written into the URL and parsed back, so it must stay a plain local date
|
|
141
|
+
export const CALENDAR_DATE_PARAM_FORMAT = 'yyyy-MM-dd';
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Occurrence status → surface treatment. DRAFT is drawn with the shared
|
|
145
|
+
* stripe pattern so an unpublished shift reads as provisional at a glance,
|
|
146
|
+
* and CANCELED is struck through rather than hidden — somebody looking for a
|
|
147
|
+
* shift that was cancelled needs to find it, not wonder where it went.
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
// The stripe pattern is a MASK on a :before pseudo-element, so it needs both a
|
|
151
|
+
// fill colour class ("*_bf") and a positioned parent ("rel", or any absolutely
|
|
152
|
+
// positioned element) or it renders as nothing at all.
|
|
153
|
+
export const CALENDAR_DRAFT_PATTERN_CLASS_NAME = 'pattern_stripes medium_bf';
|
package/src/css.d.ts
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CALENDAR_MAX_OVERLAP_COLUMNS,
|
|
3
|
+
CALENDAR_MIN_EVENT_MINUTES,
|
|
4
|
+
CALENDAR_OVERLAP_STACK_RATIO,
|
|
5
|
+
} from '../constants/calendar.ts';
|
|
6
|
+
import type {
|
|
7
|
+
CalendarDayObj,
|
|
8
|
+
CalendarEventLayoutObj,
|
|
9
|
+
CalendarEventObj,
|
|
10
|
+
} from '../types/calendar.d.ts';
|
|
11
|
+
import {
|
|
12
|
+
getDayLengthMinutes,
|
|
13
|
+
getMinutesFromDayStart,
|
|
14
|
+
} from './calendar-range.ts';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Time-grid geometry: turning a list of events into rectangles.
|
|
18
|
+
*
|
|
19
|
+
* Two problems are solved here, and both are pure functions of the events and
|
|
20
|
+
* the days — no DOM, no measurement, no React. That matters because this runs
|
|
21
|
+
* on every scroll-free re-render of a week that may hold hundreds of shifts,
|
|
22
|
+
* and because the answers are exactly the kind of thing that is easy to get
|
|
23
|
+
* subtly wrong and impossible to notice without a test.
|
|
24
|
+
*
|
|
25
|
+
* 1. An entry that crosses midnight is drawn once per day it touches. An
|
|
26
|
+
* overnight shift is one event, but it is three rectangles across a
|
|
27
|
+
* Friday-to-Sunday weekend, and each has to be clipped to its own day.
|
|
28
|
+
*
|
|
29
|
+
* 2. Events that overlap in time share the width of their day column. The
|
|
30
|
+
* standard approach — cluster, then greedily assign columns — is what
|
|
31
|
+
* every calendar does, but the detail that matters is that the column
|
|
32
|
+
* COUNT is per cluster, not per event: two events at 9am and a third at
|
|
33
|
+
* 2pm should give the first two half the width each and the third all of
|
|
34
|
+
* it, not thirds all day.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* Return the inclusive day-index span one event touches, or null when it
|
|
39
|
+
* touches none of the days on screen.
|
|
40
|
+
*
|
|
41
|
+
* Days are consecutive and sorted, so the span is found by binary search rather
|
|
42
|
+
* than by testing the event against every day. Without this every pass over the
|
|
43
|
+
* grid is events × days: a month of a busy calendar is 42 days against several
|
|
44
|
+
* hundred entries, walked again on each of the three layout passes and on every
|
|
45
|
+
* re-render they are not memoized through.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
function getEventDaySpan(
|
|
49
|
+
event: CalendarEventObj,
|
|
50
|
+
days: CalendarDayObj[],
|
|
51
|
+
): { startDayIndex: number; endDayIndex: number } | null {
|
|
52
|
+
if (!days.length) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const eventStartMs = event.startAt.getTime();
|
|
57
|
+
const eventEndMs = event.endAt.getTime();
|
|
58
|
+
|
|
59
|
+
if (
|
|
60
|
+
eventStartMs >= days[days.length - 1].endAt.getTime() ||
|
|
61
|
+
eventEndMs <= days[0].startAt.getTime()
|
|
62
|
+
) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// First day whose end is strictly after the event's start
|
|
67
|
+
let low = 0;
|
|
68
|
+
let high = days.length - 1;
|
|
69
|
+
|
|
70
|
+
while (low < high) {
|
|
71
|
+
const mid = (low + high) >> 1;
|
|
72
|
+
|
|
73
|
+
if (days[mid].endAt.getTime() > eventStartMs) {
|
|
74
|
+
high = mid;
|
|
75
|
+
} else {
|
|
76
|
+
low = mid + 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const startDayIndex = low;
|
|
81
|
+
|
|
82
|
+
// Last day whose start is strictly before the event's end
|
|
83
|
+
low = startDayIndex;
|
|
84
|
+
high = days.length - 1;
|
|
85
|
+
|
|
86
|
+
while (low < high) {
|
|
87
|
+
const mid = (low + high + 1) >> 1;
|
|
88
|
+
|
|
89
|
+
if (days[mid].startAt.getTime() < eventEndMs) {
|
|
90
|
+
low = mid;
|
|
91
|
+
} else {
|
|
92
|
+
high = mid - 1;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { startDayIndex, endDayIndex: low };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface PlacedEvent {
|
|
100
|
+
event: CalendarEventObj;
|
|
101
|
+
dayIndex: number;
|
|
102
|
+
startMinute: number;
|
|
103
|
+
endMinute: number;
|
|
104
|
+
// The extent used for OVERLAP testing, which is at least the minimum
|
|
105
|
+
// drawable height — otherwise two 5-minute events an instant apart would be
|
|
106
|
+
// laid out full-width and then drawn on top of each other
|
|
107
|
+
layoutEndMinute: number;
|
|
108
|
+
continuesFromPreviousDay: boolean;
|
|
109
|
+
continuesToNextDay: boolean;
|
|
110
|
+
column: number;
|
|
111
|
+
columnCount: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Clip one event to one day, returning null when it does not touch that day.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
function clipEventToDay(
|
|
119
|
+
event: CalendarEventObj,
|
|
120
|
+
day: CalendarDayObj,
|
|
121
|
+
dayIndex: number,
|
|
122
|
+
): PlacedEvent | null {
|
|
123
|
+
const dayStartMs = day.startAt.getTime();
|
|
124
|
+
const dayEndMs = day.endAt.getTime();
|
|
125
|
+
const eventStartMs = event.startAt.getTime();
|
|
126
|
+
const eventEndMs = event.endAt.getTime();
|
|
127
|
+
|
|
128
|
+
// Half-open on both sides: an event ending exactly at midnight belongs to
|
|
129
|
+
// the day it ran through, not to the empty morning after it
|
|
130
|
+
if (eventStartMs >= dayEndMs || eventEndMs <= dayStartMs) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// A zero-length event is still somebody's calendar entry; give it the
|
|
135
|
+
// minimum height rather than dropping it
|
|
136
|
+
const dayLengthMinutes = getDayLengthMinutes(day);
|
|
137
|
+
const startMinute = Math.max(
|
|
138
|
+
0,
|
|
139
|
+
getMinutesFromDayStart(event.startAt, day.startAt),
|
|
140
|
+
);
|
|
141
|
+
const endMinute = Math.min(
|
|
142
|
+
dayLengthMinutes,
|
|
143
|
+
getMinutesFromDayStart(event.endAt, day.startAt),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
event,
|
|
148
|
+
dayIndex,
|
|
149
|
+
startMinute,
|
|
150
|
+
endMinute,
|
|
151
|
+
layoutEndMinute: Math.min(
|
|
152
|
+
dayLengthMinutes,
|
|
153
|
+
Math.max(endMinute, startMinute + CALENDAR_MIN_EVENT_MINUTES),
|
|
154
|
+
),
|
|
155
|
+
continuesFromPreviousDay: eventStartMs < dayStartMs,
|
|
156
|
+
continuesToNextDay: eventEndMs > dayEndMs,
|
|
157
|
+
column: 0,
|
|
158
|
+
columnCount: 1,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Assign overlap columns within one day.
|
|
164
|
+
*
|
|
165
|
+
* Events are swept in start order. A cluster is a maximal run of events joined
|
|
166
|
+
* by overlap — not merely pairwise-overlapping, because A-overlaps-B and
|
|
167
|
+
* B-overlaps-C means all three share the column budget even when A and C do
|
|
168
|
+
* not touch. The cluster closes when an event starts at or after every open
|
|
169
|
+
* event has ended, and only then is the column count known for everybody in
|
|
170
|
+
* it.
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
function assignOverlapColumns(placed: PlacedEvent[]): void {
|
|
174
|
+
placed.sort(
|
|
175
|
+
(left, right) =>
|
|
176
|
+
left.startMinute - right.startMinute ||
|
|
177
|
+
right.layoutEndMinute - left.layoutEndMinute ||
|
|
178
|
+
String(left.event.id).localeCompare(String(right.event.id)),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
let cluster: PlacedEvent[] = [];
|
|
182
|
+
let clusterEnd = -Infinity;
|
|
183
|
+
|
|
184
|
+
const closeCluster = () => {
|
|
185
|
+
if (!cluster.length) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const columnCount = Math.max(...cluster.map((entry) => entry.column)) + 1;
|
|
190
|
+
|
|
191
|
+
for (const entry of cluster) {
|
|
192
|
+
entry.columnCount = columnCount;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
cluster = [];
|
|
196
|
+
clusterEnd = -Infinity;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// Column index -> the minute that column is free from
|
|
200
|
+
let columnFreeFrom: number[] = [];
|
|
201
|
+
|
|
202
|
+
for (const entry of placed) {
|
|
203
|
+
if (entry.startMinute >= clusterEnd) {
|
|
204
|
+
closeCluster();
|
|
205
|
+
columnFreeFrom = [];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let column = columnFreeFrom.findIndex(
|
|
209
|
+
(freeFrom) => entry.startMinute >= freeFrom,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
if (column === -1) {
|
|
213
|
+
column = columnFreeFrom.length;
|
|
214
|
+
columnFreeFrom.push(entry.layoutEndMinute);
|
|
215
|
+
} else {
|
|
216
|
+
columnFreeFrom[column] = entry.layoutEndMinute;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
entry.column = column;
|
|
220
|
+
cluster.push(entry);
|
|
221
|
+
clusterEnd = Math.max(clusterEnd, entry.layoutEndMinute);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
closeCluster();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/*
|
|
228
|
+
* Turn a placed event's column assignment into horizontal geometry.
|
|
229
|
+
*
|
|
230
|
+
* Up to CALENDAR_MAX_OVERLAP_COLUMNS the columns simply divide the width. Past
|
|
231
|
+
* that they overlap in a stack: each event still gets a readable width and
|
|
232
|
+
* peeks out from behind the one in front, which stays legible where a
|
|
233
|
+
* seventh-of-a-column sliver would not.
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
function getHorizontalGeometry(
|
|
237
|
+
column: number,
|
|
238
|
+
columnCount: number,
|
|
239
|
+
): { left: number; width: number } {
|
|
240
|
+
if (columnCount <= CALENDAR_MAX_OVERLAP_COLUMNS) {
|
|
241
|
+
return {
|
|
242
|
+
left: column / columnCount,
|
|
243
|
+
width: 1 / columnCount,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const left = Math.min(
|
|
248
|
+
column * CALENDAR_OVERLAP_STACK_RATIO,
|
|
249
|
+
1 - CALENDAR_OVERLAP_STACK_RATIO,
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
left,
|
|
254
|
+
width: 1 - left,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Lay out timed events across a range of days.
|
|
260
|
+
*
|
|
261
|
+
* All-day entries are excluded here — they belong to the band above the grid,
|
|
262
|
+
* which is a different layout problem (horizontal bars spanning day columns)
|
|
263
|
+
* and is solved by getCalendarAllDayLayout.
|
|
264
|
+
*/
|
|
265
|
+
|
|
266
|
+
export function getCalendarTimedLayout(
|
|
267
|
+
events: CalendarEventObj[],
|
|
268
|
+
days: CalendarDayObj[],
|
|
269
|
+
): CalendarEventLayoutObj[] {
|
|
270
|
+
const byDay: PlacedEvent[][] = days.map(() => []);
|
|
271
|
+
|
|
272
|
+
for (const event of events) {
|
|
273
|
+
if (event.allDay) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const span = getEventDaySpan(event, days);
|
|
278
|
+
|
|
279
|
+
if (!span) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
for (
|
|
284
|
+
let dayIndex = span.startDayIndex;
|
|
285
|
+
dayIndex <= span.endDayIndex;
|
|
286
|
+
dayIndex++
|
|
287
|
+
) {
|
|
288
|
+
const placed = clipEventToDay(event, days[dayIndex], dayIndex);
|
|
289
|
+
|
|
290
|
+
if (placed) {
|
|
291
|
+
byDay[dayIndex].push(placed);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const layout: CalendarEventLayoutObj[] = [];
|
|
297
|
+
|
|
298
|
+
for (const dayEvents of byDay) {
|
|
299
|
+
assignOverlapColumns(dayEvents);
|
|
300
|
+
|
|
301
|
+
for (const entry of dayEvents) {
|
|
302
|
+
const { left, width } = getHorizontalGeometry(
|
|
303
|
+
entry.column,
|
|
304
|
+
entry.columnCount,
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
layout.push({
|
|
308
|
+
event: entry.event,
|
|
309
|
+
dayIndex: entry.dayIndex,
|
|
310
|
+
startMinute: entry.startMinute,
|
|
311
|
+
endMinute: entry.endMinute,
|
|
312
|
+
left,
|
|
313
|
+
width,
|
|
314
|
+
column: entry.column,
|
|
315
|
+
columnCount: entry.columnCount,
|
|
316
|
+
continuesFromPreviousDay: entry.continuesFromPreviousDay,
|
|
317
|
+
continuesToNextDay: entry.continuesToNextDay,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return layout;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* One all-day bar: which day columns it spans, and which row of the band it
|
|
327
|
+
* sits in.
|
|
328
|
+
*/
|
|
329
|
+
|
|
330
|
+
export interface CalendarAllDayBarObj {
|
|
331
|
+
event: CalendarEventObj;
|
|
332
|
+
// Inclusive day-column indices
|
|
333
|
+
startDayIndex: number;
|
|
334
|
+
endDayIndex: number;
|
|
335
|
+
row: number;
|
|
336
|
+
continuesFromPreviousDay: boolean;
|
|
337
|
+
continuesToNextDay: boolean;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Lay out all-day and multi-day entries as stacked horizontal bars.
|
|
342
|
+
*
|
|
343
|
+
* Rows are packed greedily in start order: each bar takes the first row where
|
|
344
|
+
* nothing already occupies its day span. That keeps the band as short as
|
|
345
|
+
* possible, which matters because it eats vertical space the time grid needs.
|
|
346
|
+
*/
|
|
347
|
+
|
|
348
|
+
export function getCalendarAllDayLayout(
|
|
349
|
+
events: CalendarEventObj[],
|
|
350
|
+
days: CalendarDayObj[],
|
|
351
|
+
): CalendarAllDayBarObj[] {
|
|
352
|
+
if (!days.length) {
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const rangeStartMs = days[0].startAt.getTime();
|
|
357
|
+
const rangeEndMs = days[days.length - 1].endAt.getTime();
|
|
358
|
+
|
|
359
|
+
const bars: CalendarAllDayBarObj[] = [];
|
|
360
|
+
|
|
361
|
+
for (const event of events) {
|
|
362
|
+
// One span lookup serves the membership test and the geometry: the day
|
|
363
|
+
// range an event touches IS the answer to "does it span more than one
|
|
364
|
+
// day on screen", so asking separately walked the day list three times
|
|
365
|
+
const span = getEventDaySpan(event, days);
|
|
366
|
+
|
|
367
|
+
if (!span) {
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Timed overnight events remain in the timed grid only.
|
|
372
|
+
if (!event.allDay) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
bars.push({
|
|
377
|
+
event,
|
|
378
|
+
startDayIndex: span.startDayIndex,
|
|
379
|
+
endDayIndex: span.endDayIndex,
|
|
380
|
+
row: 0,
|
|
381
|
+
continuesFromPreviousDay: event.startAt.getTime() < rangeStartMs,
|
|
382
|
+
continuesToNextDay: event.endAt.getTime() > rangeEndMs,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
bars.sort(
|
|
387
|
+
(left, right) =>
|
|
388
|
+
left.startDayIndex - right.startDayIndex ||
|
|
389
|
+
right.endDayIndex -
|
|
390
|
+
right.startDayIndex -
|
|
391
|
+
(left.endDayIndex - left.startDayIndex) ||
|
|
392
|
+
String(left.event.id).localeCompare(String(right.event.id)),
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
// Row index -> the day index that row is free from
|
|
396
|
+
const rowFreeFrom: number[] = [];
|
|
397
|
+
|
|
398
|
+
for (const bar of bars) {
|
|
399
|
+
let row = rowFreeFrom.findIndex(
|
|
400
|
+
(freeFrom) => bar.startDayIndex >= freeFrom,
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
if (row === -1) {
|
|
404
|
+
row = rowFreeFrom.length;
|
|
405
|
+
rowFreeFrom.push(bar.endDayIndex + 1);
|
|
406
|
+
} else {
|
|
407
|
+
rowFreeFrom[row] = bar.endDayIndex + 1;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
bar.row = row;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return bars;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* One month-grid cell: the day, the events that fit, and how many did not.
|
|
418
|
+
*/
|
|
419
|
+
|
|
420
|
+
export interface CalendarMonthCellObj {
|
|
421
|
+
day: CalendarDayObj;
|
|
422
|
+
events: CalendarEventObj[];
|
|
423
|
+
// Events the cell could not show; drives the "+N more" affordance
|
|
424
|
+
overflowCount: number;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Build the month grid's cells, truncating each to what will actually fit.
|
|
429
|
+
*
|
|
430
|
+
* The truncation happens here rather than in the component so that "+N more"
|
|
431
|
+
* is a number the layout computed, not something a CSS overflow hid. A cell
|
|
432
|
+
* that silently clipped its third event would tell a manager the day is
|
|
433
|
+
* quieter than it is.
|
|
434
|
+
*/
|
|
435
|
+
|
|
436
|
+
export function getCalendarMonthCells(
|
|
437
|
+
events: CalendarEventObj[],
|
|
438
|
+
days: CalendarDayObj[],
|
|
439
|
+
maxPerCell: number,
|
|
440
|
+
): CalendarMonthCellObj[] {
|
|
441
|
+
const byDateKey = getCalendarEventsByDateKey(events, days);
|
|
442
|
+
|
|
443
|
+
return days.map((day) => {
|
|
444
|
+
const dayEvents = byDateKey.get(day.dateKey) || [];
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
day,
|
|
448
|
+
events: dayEvents.slice(0, maxPerCell),
|
|
449
|
+
overflowCount: Math.max(0, dayEvents.length - maxPerCell),
|
|
450
|
+
};
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Group events by the local day they should appear under, for the month grid
|
|
456
|
+
* and the agenda list.
|
|
457
|
+
*
|
|
458
|
+
* A multi-day entry is listed under every day it covers, because a month cell
|
|
459
|
+
* that omitted the middle days of a conference would be lying about those days
|
|
460
|
+
* being free.
|
|
461
|
+
*/
|
|
462
|
+
|
|
463
|
+
export function getCalendarEventsByDateKey(
|
|
464
|
+
events: CalendarEventObj[],
|
|
465
|
+
days: CalendarDayObj[],
|
|
466
|
+
): Map<string, CalendarEventObj[]> {
|
|
467
|
+
const byDateKey = new Map<string, CalendarEventObj[]>();
|
|
468
|
+
|
|
469
|
+
for (const day of days) {
|
|
470
|
+
byDateKey.set(day.dateKey, []);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
for (const event of events) {
|
|
474
|
+
const span = getEventDaySpan(event, days);
|
|
475
|
+
|
|
476
|
+
if (!span) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
for (
|
|
481
|
+
let dayIndex = span.startDayIndex;
|
|
482
|
+
dayIndex <= span.endDayIndex;
|
|
483
|
+
dayIndex++
|
|
484
|
+
) {
|
|
485
|
+
byDateKey.get(days[dayIndex].dateKey)?.push(event);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
for (const dayEvents of byDateKey.values()) {
|
|
490
|
+
dayEvents.sort(
|
|
491
|
+
(left, right) =>
|
|
492
|
+
// All-day entries head the list; below them, chronological
|
|
493
|
+
Number(right.allDay) - Number(left.allDay) ||
|
|
494
|
+
left.startAt.getTime() - right.startAt.getTime() ||
|
|
495
|
+
String(left.id).localeCompare(String(right.id)),
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return byDateKey;
|
|
500
|
+
}
|