@jsb188/schedule 1.1.11
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 +61 -0
- package/src/constants/schedule.ts +549 -0
- package/src/css.d.ts +7 -0
- package/src/libs/gantt-geometry.ts +771 -0
- package/src/libs/gantt-histogram.ts +124 -0
- package/src/libs/gantt-keyboard.ts +113 -0
- package/src/libs/gantt-state.ts +57 -0
- package/src/libs/gantt-url.ts +63 -0
- package/src/modules/Gantt.helpers.ts +138 -0
- package/src/modules/Gantt.tsx +719 -0
- package/src/types/schedule.d.ts +1149 -0
- package/src/ui/ActivityFormUI.tsx +133 -0
- package/src/ui/GanttEditorsUI.tsx +102 -0
- package/src/ui/GanttHistogramUI.tsx +236 -0
- package/src/ui/GanttUI.tsx +792 -0
- package/src/ui/ProjectFormUI.tsx +273 -0
- package/src/ui/ProjectMobileListUI.tsx +80 -0
- package/src/ui/ProjectToolbarUI.tsx +54 -0
- package/src/ui/RelationshipListUI.tsx +176 -0
- package/src/ui/ScheduleRunUI.tsx +173 -0
- package/src/ui/ScheduleSettingsUI.tsx +420 -0
- package/src/utils/coverage.helpers.ts +679 -0
- package/src/utils/coverage.ts +630 -0
- package/src/utils/cpm.helpers.ts +674 -0
- package/src/utils/cpm.ts +907 -0
- package/src/utils/cpmLeveling.helpers.ts +564 -0
- package/src/utils/cpmLeveling.ts +367 -0
- package/src/utils/defaults.ts +8 -0
- package/src/utils/eligibility.ts +457 -0
- package/src/utils/planCsv.ts +344 -0
- package/src/utils/predecessorText.ts +96 -0
- package/src/utils/recurrence.ts +616 -0
- package/src/utils/request.ts +153 -0
- package/src/utils/resource.ts +182 -0
- package/src/utils/rule.ts +216 -0
- package/src/utils/run.ts +43 -0
- package/src/utils/series.ts +161 -0
- package/src/utils/slot.ts +208 -0
- package/src/utils/workTime.ts +1382 -0
- package/tests/coverage.fixtures.ts +111 -0
- package/tests/coverage.test.ts +503 -0
- package/tests/cpm.bench.test.ts +154 -0
- package/tests/cpm.test.ts +354 -0
- package/tests/cpmCorrectness.test.ts +327 -0
- package/tests/cpmLeveling.test.ts +471 -0
- package/tests/eligibility.test.ts +320 -0
- package/tests/gantt-geometry.test.ts +367 -0
- package/tests/gantt-histogram.test.ts +78 -0
- package/tests/gantt-keyboard.test.ts +69 -0
- package/tests/helpers/cpmFixtures.ts +101 -0
- package/tests/projectInputs.test.ts +165 -0
- package/tests/recurrence.test.ts +68 -0
- package/tests/request.test.ts +99 -0
- package/tests/resource.test.ts +95 -0
- package/tests/rule.test.ts +98 -0
- package/tests/series.test.ts +120 -0
- package/tests/workTime.test.ts +1007 -0
- package/tests/workTimeContracts.test.ts +111 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import type {
|
|
3
|
+
ScheduleActivityTypeEnum,
|
|
4
|
+
ScheduleRelationshipTypeEnum,
|
|
5
|
+
} from '../types/schedule.d.ts';
|
|
6
|
+
import type { WorkCalendarTable } from '../utils/workTime.ts';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gantt geometry: turning a project's rows and dates into pixels.
|
|
10
|
+
*
|
|
11
|
+
* Pure functions, no React. The container (modules/Gantt.tsx) calls these
|
|
12
|
+
* once per render and hands finished numbers to the dumb components in /ui/,
|
|
13
|
+
* which never do date math. Positions are DATA (a function of when an
|
|
14
|
+
* activity happens and how wide a day is), so they travel as inline styles;
|
|
15
|
+
* everything that is design uses the global utility classes.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type GanttZoomEnum = 'DAY' | 'WEEK' | 'MONTH';
|
|
19
|
+
|
|
20
|
+
// Pixels one calendar day occupies at each zoom
|
|
21
|
+
export const GANTT_DAY_WIDTHS: Record<GanttZoomEnum, number> = {
|
|
22
|
+
DAY: 40,
|
|
23
|
+
WEEK: 14,
|
|
24
|
+
MONTH: 4,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const GANTT_ROW_HEIGHT = 32;
|
|
28
|
+
export const GANTT_HEADER_HEIGHT = 44;
|
|
29
|
+
export const GANTT_BAR_HEIGHT = 16;
|
|
30
|
+
export const GANTT_MILESTONE_SIZE = 14;
|
|
31
|
+
export const GANTT_TABLE_INDENT = 14;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* One drawable row: an activity or a WBS group heading.
|
|
35
|
+
*/
|
|
36
|
+
export interface GanttRowObj {
|
|
37
|
+
earlyStartAt?: Date | null;
|
|
38
|
+
levelingDelayMinutes: number;
|
|
39
|
+
unreviewed: boolean;
|
|
40
|
+
id: string;
|
|
41
|
+
kind: 'WBS' | 'ACTIVITY';
|
|
42
|
+
depth: number;
|
|
43
|
+
code: string;
|
|
44
|
+
name: string;
|
|
45
|
+
type: ScheduleActivityTypeEnum | null;
|
|
46
|
+
status: string | null;
|
|
47
|
+
isCritical: boolean;
|
|
48
|
+
startAt: Date | null;
|
|
49
|
+
finishAt: Date | null;
|
|
50
|
+
lateStartAt: Date | null;
|
|
51
|
+
lateFinishAt: Date | null;
|
|
52
|
+
baselineStartAt: Date | null;
|
|
53
|
+
baselineFinishAt: Date | null;
|
|
54
|
+
durationMinutes: number;
|
|
55
|
+
remainingMinutes: number;
|
|
56
|
+
totalFloatMinutes: number | null;
|
|
57
|
+
wbsId: string | null;
|
|
58
|
+
collapsed: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The inputs the row builder reads; a subset of the network read so the
|
|
63
|
+
* container can pass GraphQL rows straight through.
|
|
64
|
+
*/
|
|
65
|
+
export interface GanttWbsInput {
|
|
66
|
+
id: string;
|
|
67
|
+
parentId: string | null;
|
|
68
|
+
code: string;
|
|
69
|
+
name: string;
|
|
70
|
+
ordinal: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface GanttActivityInput {
|
|
74
|
+
startAt: Date | string | null;
|
|
75
|
+
finishAt: Date | string | null;
|
|
76
|
+
levelingDelayMinutes?: number;
|
|
77
|
+
source?: { reviewStatus?: string | null } | null;
|
|
78
|
+
id: string;
|
|
79
|
+
wbsId: string | null;
|
|
80
|
+
code: string;
|
|
81
|
+
name: string;
|
|
82
|
+
type: ScheduleActivityTypeEnum;
|
|
83
|
+
status: string;
|
|
84
|
+
plannedDurationMinutes: number;
|
|
85
|
+
remainingDurationMinutes: number;
|
|
86
|
+
earlyStartAt: Date | string | null;
|
|
87
|
+
earlyFinishAt: Date | string | null;
|
|
88
|
+
lateStartAt: Date | string | null;
|
|
89
|
+
lateFinishAt: Date | string | null;
|
|
90
|
+
totalFloatMinutes: number | null;
|
|
91
|
+
isCritical: boolean;
|
|
92
|
+
ordinal: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface GanttBaselineInput {
|
|
96
|
+
activityId: string | null;
|
|
97
|
+
startAt?: Date | string | null;
|
|
98
|
+
finishAt?: Date | string | null;
|
|
99
|
+
plannedStartAt?: Date | string | null;
|
|
100
|
+
plannedFinishAt?: Date | string | null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface GanttRelationshipInput {
|
|
104
|
+
isDriving?: boolean;
|
|
105
|
+
id: string;
|
|
106
|
+
predecessorId: string;
|
|
107
|
+
successorId: string;
|
|
108
|
+
type: ScheduleRelationshipTypeEnum;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The visible time axis.
|
|
113
|
+
*/
|
|
114
|
+
export interface GanttScaleObj {
|
|
115
|
+
zoom: GanttZoomEnum;
|
|
116
|
+
timeZone: string;
|
|
117
|
+
// Local midnight the axis starts on
|
|
118
|
+
startAt: Date;
|
|
119
|
+
endAt: Date;
|
|
120
|
+
dayWidth: number;
|
|
121
|
+
totalWidth: number;
|
|
122
|
+
// Major ticks: months (DAY and WEEK zoom) or quarters (MONTH zoom)
|
|
123
|
+
majorTicks: Array<{ x: number; width: number; label: string }>;
|
|
124
|
+
// Minor ticks: days (DAY zoom), weeks (WEEK zoom), months (MONTH zoom)
|
|
125
|
+
minorTicks: Array<{
|
|
126
|
+
x: number;
|
|
127
|
+
width: number;
|
|
128
|
+
label: string;
|
|
129
|
+
isWeekend: boolean;
|
|
130
|
+
}>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface GanttBarObj {
|
|
134
|
+
delayX: number | null;
|
|
135
|
+
delayWidth: number;
|
|
136
|
+
rowIndex: number;
|
|
137
|
+
x: number;
|
|
138
|
+
width: number;
|
|
139
|
+
// Progress fill width in pixels (planned minus remaining, proportionally)
|
|
140
|
+
progressWidth: number;
|
|
141
|
+
isMilestone: boolean;
|
|
142
|
+
isSummary: boolean;
|
|
143
|
+
isCritical: boolean;
|
|
144
|
+
isComplete: boolean;
|
|
145
|
+
baselineX: number | null;
|
|
146
|
+
baselineWidth: number | null;
|
|
147
|
+
// Late-date slack drawn as a thin tail after the bar
|
|
148
|
+
floatX: number | null;
|
|
149
|
+
floatWidth: number | null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface GanttLinkObj {
|
|
153
|
+
fromRowIndex: number;
|
|
154
|
+
toRowIndex: number;
|
|
155
|
+
minX: number;
|
|
156
|
+
maxX: number;
|
|
157
|
+
isDriving: boolean;
|
|
158
|
+
id: string;
|
|
159
|
+
// SVG path in timeline pixel space
|
|
160
|
+
path: string;
|
|
161
|
+
isCritical: boolean;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/*
|
|
165
|
+
* Coerce an optional instant.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
function toDate(value: Date | string | null | undefined): Date | null {
|
|
169
|
+
if (!value) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
174
|
+
|
|
175
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Order a project's WBS tree and activities into drawable rows: each WBS node
|
|
180
|
+
* followed by its activities and children, depth-first, honouring ordinals.
|
|
181
|
+
* Collapsed nodes keep their heading and hide everything beneath.
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
export function buildGanttRows(
|
|
185
|
+
wbsNodes: GanttWbsInput[],
|
|
186
|
+
activities: GanttActivityInput[],
|
|
187
|
+
baselineActivities: GanttBaselineInput[],
|
|
188
|
+
collapsedWbsIds: Set<string>,
|
|
189
|
+
): GanttRowObj[] {
|
|
190
|
+
const baselineById = new Map<string, GanttBaselineInput>();
|
|
191
|
+
for (const row of baselineActivities) {
|
|
192
|
+
if (row.activityId) baselineById.set(row.activityId, row);
|
|
193
|
+
}
|
|
194
|
+
const nodes = new Map(wbsNodes.map((node) => [node.id, node]));
|
|
195
|
+
const children = new Map<string | null, GanttWbsInput[]>();
|
|
196
|
+
const members = new Map<string | null, GanttActivityInput[]>();
|
|
197
|
+
for (const node of [...wbsNodes].sort(
|
|
198
|
+
(a, b) => a.ordinal - b.ordinal || a.code.localeCompare(b.code),
|
|
199
|
+
)) {
|
|
200
|
+
const parent =
|
|
201
|
+
node.parentId && nodes.has(node.parentId) && node.parentId !== node.id
|
|
202
|
+
? node.parentId
|
|
203
|
+
: null;
|
|
204
|
+
const list = children.get(parent) || [];
|
|
205
|
+
list.push(node);
|
|
206
|
+
children.set(parent, list);
|
|
207
|
+
}
|
|
208
|
+
for (const activity of [...activities].sort(
|
|
209
|
+
(a, b) => a.ordinal - b.ordinal || a.code.localeCompare(b.code),
|
|
210
|
+
)) {
|
|
211
|
+
const parent =
|
|
212
|
+
activity.wbsId && nodes.has(activity.wbsId) ? activity.wbsId : null;
|
|
213
|
+
const list = members.get(parent) || [];
|
|
214
|
+
list.push(activity);
|
|
215
|
+
members.set(parent, list);
|
|
216
|
+
}
|
|
217
|
+
const spans = new Map<
|
|
218
|
+
string,
|
|
219
|
+
{ start: Date | null; finish: Date | null; critical: boolean }
|
|
220
|
+
>();
|
|
221
|
+
const visiting = new Set<string>();
|
|
222
|
+
/* Aggregate descendant spans once per WBS node, preserving linear tree traversal. */
|
|
223
|
+
function span(id: string): {
|
|
224
|
+
start: Date | null;
|
|
225
|
+
finish: Date | null;
|
|
226
|
+
critical: boolean;
|
|
227
|
+
} {
|
|
228
|
+
const cached = spans.get(id);
|
|
229
|
+
if (cached) return cached;
|
|
230
|
+
const result = {
|
|
231
|
+
start: null as Date | null,
|
|
232
|
+
finish: null as Date | null,
|
|
233
|
+
critical: false,
|
|
234
|
+
};
|
|
235
|
+
if (visiting.has(id)) return result;
|
|
236
|
+
visiting.add(id);
|
|
237
|
+
/* Include a direct activity or child span in the parent extent. */
|
|
238
|
+
const include = (
|
|
239
|
+
start: Date | null,
|
|
240
|
+
finish: Date | null,
|
|
241
|
+
critical: boolean,
|
|
242
|
+
) => {
|
|
243
|
+
if (start && (!result.start || start < result.start))
|
|
244
|
+
result.start = start;
|
|
245
|
+
if (finish && (!result.finish || finish > result.finish))
|
|
246
|
+
result.finish = finish;
|
|
247
|
+
result.critical ||= critical;
|
|
248
|
+
};
|
|
249
|
+
for (const activity of members.get(id) || [])
|
|
250
|
+
include(
|
|
251
|
+
toDate(activity.startAt),
|
|
252
|
+
toDate(activity.finishAt),
|
|
253
|
+
activity.isCritical,
|
|
254
|
+
);
|
|
255
|
+
for (const child of children.get(id) || []) {
|
|
256
|
+
const nested = span(child.id);
|
|
257
|
+
include(nested.start, nested.finish, nested.critical);
|
|
258
|
+
}
|
|
259
|
+
visiting.delete(id);
|
|
260
|
+
spans.set(id, result);
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
for (const node of wbsNodes) span(node.id);
|
|
264
|
+
const rows: GanttRowObj[] = [];
|
|
265
|
+
/* Project one activity's effective dates and optional baseline into its display row. */
|
|
266
|
+
const pushActivity = (activity: GanttActivityInput, depth: number) => {
|
|
267
|
+
const baseline = baselineById.get(activity.id);
|
|
268
|
+
rows.push({
|
|
269
|
+
id: activity.id,
|
|
270
|
+
kind: 'ACTIVITY',
|
|
271
|
+
depth,
|
|
272
|
+
code: activity.code,
|
|
273
|
+
name: activity.name,
|
|
274
|
+
type: activity.type,
|
|
275
|
+
status: activity.status,
|
|
276
|
+
isCritical: !!activity.isCritical,
|
|
277
|
+
startAt: toDate(activity.startAt),
|
|
278
|
+
finishAt: toDate(activity.finishAt),
|
|
279
|
+
earlyStartAt: toDate(activity.earlyStartAt),
|
|
280
|
+
lateStartAt: toDate(activity.lateStartAt),
|
|
281
|
+
lateFinishAt: toDate(activity.lateFinishAt),
|
|
282
|
+
baselineStartAt: toDate(baseline?.startAt ?? baseline?.plannedStartAt),
|
|
283
|
+
baselineFinishAt: toDate(baseline?.finishAt ?? baseline?.plannedFinishAt),
|
|
284
|
+
durationMinutes: activity.plannedDurationMinutes,
|
|
285
|
+
remainingMinutes: activity.remainingDurationMinutes,
|
|
286
|
+
totalFloatMinutes: activity.totalFloatMinutes,
|
|
287
|
+
wbsId: activity.wbsId,
|
|
288
|
+
collapsed: false,
|
|
289
|
+
levelingDelayMinutes: activity.levelingDelayMinutes || 0,
|
|
290
|
+
unreviewed: activity.source?.reviewStatus === 'UNREVIEWED',
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
const visited = new Set<string>();
|
|
294
|
+
/* Flatten only expanded branches; malformed cycles cannot recurse indefinitely. */
|
|
295
|
+
function walk(node: GanttWbsInput, depth: number) {
|
|
296
|
+
if (visited.has(node.id)) return;
|
|
297
|
+
visited.add(node.id);
|
|
298
|
+
const extent = spans.get(node.id)!;
|
|
299
|
+
const collapsed = collapsedWbsIds.has(node.id);
|
|
300
|
+
rows.push({
|
|
301
|
+
id: node.id,
|
|
302
|
+
kind: 'WBS',
|
|
303
|
+
depth,
|
|
304
|
+
code: node.code,
|
|
305
|
+
name: node.name,
|
|
306
|
+
type: null,
|
|
307
|
+
status: null,
|
|
308
|
+
isCritical: extent.critical,
|
|
309
|
+
startAt: extent.start,
|
|
310
|
+
finishAt: extent.finish,
|
|
311
|
+
lateStartAt: null,
|
|
312
|
+
lateFinishAt: null,
|
|
313
|
+
baselineStartAt: null,
|
|
314
|
+
baselineFinishAt: null,
|
|
315
|
+
durationMinutes: 0,
|
|
316
|
+
remainingMinutes: 0,
|
|
317
|
+
totalFloatMinutes: null,
|
|
318
|
+
wbsId: node.id,
|
|
319
|
+
collapsed,
|
|
320
|
+
levelingDelayMinutes: 0,
|
|
321
|
+
unreviewed: false,
|
|
322
|
+
});
|
|
323
|
+
if (collapsed) return;
|
|
324
|
+
for (const activity of members.get(node.id) || [])
|
|
325
|
+
pushActivity(activity, depth + 1);
|
|
326
|
+
for (const child of children.get(node.id) || []) walk(child, depth + 1);
|
|
327
|
+
}
|
|
328
|
+
for (const activity of members.get(null) || []) pushActivity(activity, 0);
|
|
329
|
+
for (const node of children.get(null) || []) walk(node, 0);
|
|
330
|
+
return rows;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/*
|
|
334
|
+
* Return the axis bounds: the earliest and latest date across the rows,
|
|
335
|
+
* padded, or a window around "now" for an unscheduled project.
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
function getGanttBounds(
|
|
339
|
+
rows: GanttRowObj[],
|
|
340
|
+
now: Date,
|
|
341
|
+
timeZone: string,
|
|
342
|
+
zoom: GanttZoomEnum,
|
|
343
|
+
): { start: DateTime; end: DateTime } {
|
|
344
|
+
let min: Date | null = null;
|
|
345
|
+
let max: Date | null = null;
|
|
346
|
+
|
|
347
|
+
for (const row of rows) {
|
|
348
|
+
for (const date of [
|
|
349
|
+
row.startAt,
|
|
350
|
+
row.finishAt,
|
|
351
|
+
row.baselineStartAt,
|
|
352
|
+
row.baselineFinishAt,
|
|
353
|
+
row.lateFinishAt,
|
|
354
|
+
]) {
|
|
355
|
+
if (!date) {
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (!min || date < min) {
|
|
359
|
+
min = date;
|
|
360
|
+
}
|
|
361
|
+
if (!max || date > max) {
|
|
362
|
+
max = date;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const paddingDays = zoom === 'DAY' ? 3 : zoom === 'WEEK' ? 7 : 30;
|
|
368
|
+
const start = DateTime.fromJSDate(min || now, { zone: timeZone })
|
|
369
|
+
.startOf('day')
|
|
370
|
+
.minus({ days: paddingDays });
|
|
371
|
+
const fallbackEnd = DateTime.fromJSDate(now, { zone: timeZone }).plus({
|
|
372
|
+
days: zoom === 'DAY' ? 30 : zoom === 'WEEK' ? 90 : 365,
|
|
373
|
+
});
|
|
374
|
+
const end = (max ? DateTime.fromJSDate(max, { zone: timeZone }) : fallbackEnd)
|
|
375
|
+
.startOf('day')
|
|
376
|
+
.plus({ days: paddingDays + 1 });
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
start,
|
|
380
|
+
end: end > start.plus({ days: 7 }) ? end : start.plus({ days: 7 }),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Build the time axis for a set of rows.
|
|
386
|
+
*/
|
|
387
|
+
|
|
388
|
+
export function buildGanttScale(
|
|
389
|
+
rows: GanttRowObj[],
|
|
390
|
+
now: Date,
|
|
391
|
+
timeZone: string,
|
|
392
|
+
zoom: GanttZoomEnum,
|
|
393
|
+
formatters?: {
|
|
394
|
+
majorTick?: (date: Date, zoom: GanttZoomEnum, timeZone: string) => string;
|
|
395
|
+
minorTick?: (date: Date, zoom: GanttZoomEnum, timeZone: string) => string;
|
|
396
|
+
},
|
|
397
|
+
): GanttScaleObj {
|
|
398
|
+
const { start, end } = getGanttBounds(rows, now, timeZone, zoom);
|
|
399
|
+
const dayWidth = GANTT_DAY_WIDTHS[zoom];
|
|
400
|
+
const totalDays = Math.max(1, Math.round(end.diff(start, 'days').days));
|
|
401
|
+
const majorTicks: GanttScaleObj['majorTicks'] = [];
|
|
402
|
+
const minorTicks: GanttScaleObj['minorTicks'] = [];
|
|
403
|
+
|
|
404
|
+
// Minor ticks
|
|
405
|
+
if (zoom === 'DAY') {
|
|
406
|
+
for (let day = 0; day < totalDays; day++) {
|
|
407
|
+
const date = start.plus({ days: day });
|
|
408
|
+
minorTicks.push({
|
|
409
|
+
x: day * dayWidth,
|
|
410
|
+
width: dayWidth,
|
|
411
|
+
label:
|
|
412
|
+
formatters?.minorTick?.(date.toJSDate(), zoom, timeZone) ||
|
|
413
|
+
new Intl.DateTimeFormat(undefined, {
|
|
414
|
+
day: 'numeric',
|
|
415
|
+
timeZone,
|
|
416
|
+
}).format(date.toJSDate()),
|
|
417
|
+
isWeekend: date.weekday >= 6,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
} else if (zoom === 'WEEK') {
|
|
421
|
+
let cursor = start;
|
|
422
|
+
while (cursor < end) {
|
|
423
|
+
const weekEnd = cursor.plus({ days: 7 - ((cursor.weekday - 1 + 7) % 7) });
|
|
424
|
+
const next = weekEnd < end ? weekEnd : end;
|
|
425
|
+
const x = cursor.diff(start, 'days').days * dayWidth;
|
|
426
|
+
minorTicks.push({
|
|
427
|
+
x,
|
|
428
|
+
width: next.diff(cursor, 'days').days * dayWidth,
|
|
429
|
+
label:
|
|
430
|
+
formatters?.minorTick?.(cursor.toJSDate(), zoom, timeZone) ||
|
|
431
|
+
new Intl.DateTimeFormat(undefined, {
|
|
432
|
+
month: 'short',
|
|
433
|
+
day: 'numeric',
|
|
434
|
+
timeZone,
|
|
435
|
+
}).format(cursor.toJSDate()),
|
|
436
|
+
isWeekend: false,
|
|
437
|
+
});
|
|
438
|
+
cursor = next;
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
let cursor = start;
|
|
442
|
+
while (cursor < end) {
|
|
443
|
+
const monthEnd = cursor.plus({ months: 1 }).startOf('month');
|
|
444
|
+
const next = monthEnd < end ? monthEnd : end;
|
|
445
|
+
const x = cursor.diff(start, 'days').days * dayWidth;
|
|
446
|
+
minorTicks.push({
|
|
447
|
+
x,
|
|
448
|
+
width: next.diff(cursor, 'days').days * dayWidth,
|
|
449
|
+
label:
|
|
450
|
+
formatters?.minorTick?.(cursor.toJSDate(), zoom, timeZone) ||
|
|
451
|
+
new Intl.DateTimeFormat(undefined, {
|
|
452
|
+
month: 'short',
|
|
453
|
+
timeZone,
|
|
454
|
+
}).format(cursor.toJSDate()),
|
|
455
|
+
isWeekend: false,
|
|
456
|
+
});
|
|
457
|
+
cursor = next;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// Major ticks
|
|
462
|
+
let cursor = start;
|
|
463
|
+
while (cursor < end) {
|
|
464
|
+
const unitEnd =
|
|
465
|
+
zoom === 'MONTH'
|
|
466
|
+
? cursor.plus({ quarters: 1 }).startOf('quarter')
|
|
467
|
+
: cursor.plus({ months: 1 }).startOf('month');
|
|
468
|
+
const next = unitEnd < end ? unitEnd : end;
|
|
469
|
+
const x = cursor.diff(start, 'days').days * dayWidth;
|
|
470
|
+
majorTicks.push({
|
|
471
|
+
x,
|
|
472
|
+
width: next.diff(cursor, 'days').days * dayWidth,
|
|
473
|
+
label:
|
|
474
|
+
formatters?.majorTick?.(cursor.toJSDate(), zoom, timeZone) ||
|
|
475
|
+
new Intl.DateTimeFormat(undefined, {
|
|
476
|
+
month: 'long',
|
|
477
|
+
year: 'numeric',
|
|
478
|
+
timeZone,
|
|
479
|
+
}).format(cursor.toJSDate()),
|
|
480
|
+
});
|
|
481
|
+
cursor = next;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return {
|
|
485
|
+
zoom,
|
|
486
|
+
timeZone,
|
|
487
|
+
startAt: start.toJSDate(),
|
|
488
|
+
endAt: end.toJSDate(),
|
|
489
|
+
dayWidth,
|
|
490
|
+
totalWidth: totalDays * dayWidth,
|
|
491
|
+
majorTicks,
|
|
492
|
+
minorTicks,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Return the x pixel of one instant on a scale.
|
|
498
|
+
*/
|
|
499
|
+
|
|
500
|
+
export function getGanttX(scale: GanttScaleObj, at: Date): number {
|
|
501
|
+
const instant = DateTime.fromJSDate(at, { zone: scale.timeZone });
|
|
502
|
+
const day = instant.startOf('day');
|
|
503
|
+
const nextDay = day.plus({ days: 1 });
|
|
504
|
+
const wholeDays = day.diff(
|
|
505
|
+
DateTime.fromJSDate(scale.startAt, { zone: scale.timeZone }),
|
|
506
|
+
'days',
|
|
507
|
+
).days;
|
|
508
|
+
const fraction =
|
|
509
|
+
(instant.toMillis() - day.toMillis()) /
|
|
510
|
+
(nextDay.toMillis() - day.toMillis());
|
|
511
|
+
return (wholeDays + fraction) * scale.dayWidth;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export interface GanttNonWorkingColumn {
|
|
515
|
+
x: number;
|
|
516
|
+
width: number;
|
|
517
|
+
isException: boolean;
|
|
518
|
+
note: string | null;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/* Merge consecutive non-working days from the actual project calendar, including dated holidays. */
|
|
522
|
+
export function getGanttNonWorkingColumns(
|
|
523
|
+
scale: GanttScaleObj,
|
|
524
|
+
table: WorkCalendarTable,
|
|
525
|
+
): GanttNonWorkingColumn[] {
|
|
526
|
+
const columns: GanttNonWorkingColumn[] = [];
|
|
527
|
+
let current: GanttNonWorkingColumn | null = null;
|
|
528
|
+
for (let index = 0; index < table.days.length; index++) {
|
|
529
|
+
const day = table.days[index];
|
|
530
|
+
const nextMs = table.days[index + 1]?.dayStartMs ?? table.lastDayEndMs;
|
|
531
|
+
if (day.dayStartMs >= scale.endAt.getTime()) break;
|
|
532
|
+
if (nextMs <= scale.startAt.getTime()) continue;
|
|
533
|
+
if (day.workingMinutes > 0) {
|
|
534
|
+
current = null;
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
const x = Math.max(0, getGanttX(scale, new Date(day.dayStartMs)));
|
|
538
|
+
const end = Math.min(scale.totalWidth, getGanttX(scale, new Date(nextMs)));
|
|
539
|
+
const isException = Object.hasOwn(
|
|
540
|
+
table.calendar.exceptions || {},
|
|
541
|
+
day.dateKey,
|
|
542
|
+
);
|
|
543
|
+
if (current && Math.abs(current.x + current.width - x) < 0.01) {
|
|
544
|
+
current.width = end - current.x;
|
|
545
|
+
current.isException ||= isException;
|
|
546
|
+
} else {
|
|
547
|
+
current = { x, width: Math.max(0, end - x), isException, note: null };
|
|
548
|
+
columns.push(current);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return columns.filter(
|
|
552
|
+
(column) =>
|
|
553
|
+
column.width > 0 &&
|
|
554
|
+
(scale.dayWidth > GANTT_DAY_WIDTHS.MONTH ||
|
|
555
|
+
column.width >= scale.dayWidth * 3),
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Compute the bar of every row.
|
|
561
|
+
*/
|
|
562
|
+
|
|
563
|
+
export function buildGanttBars(
|
|
564
|
+
rows: GanttRowObj[],
|
|
565
|
+
scale: GanttScaleObj,
|
|
566
|
+
): GanttBarObj[] {
|
|
567
|
+
const bars: GanttBarObj[] = [];
|
|
568
|
+
|
|
569
|
+
rows.forEach((row, rowIndex) => {
|
|
570
|
+
if (!row.startAt || !row.finishAt) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const isMilestone =
|
|
575
|
+
row.type === 'START_MILESTONE' || row.type === 'FINISH_MILESTONE';
|
|
576
|
+
const x = getGanttX(scale, row.startAt);
|
|
577
|
+
const rawWidth = getGanttX(scale, row.finishAt) - x;
|
|
578
|
+
const width = isMilestone ? 0 : Math.max(2, rawWidth);
|
|
579
|
+
const progress =
|
|
580
|
+
row.durationMinutes > 0
|
|
581
|
+
? Math.max(
|
|
582
|
+
0,
|
|
583
|
+
Math.min(
|
|
584
|
+
1,
|
|
585
|
+
(row.durationMinutes - row.remainingMinutes) /
|
|
586
|
+
row.durationMinutes,
|
|
587
|
+
),
|
|
588
|
+
)
|
|
589
|
+
: row.status === 'COMPLETE'
|
|
590
|
+
? 1
|
|
591
|
+
: 0;
|
|
592
|
+
|
|
593
|
+
let baselineX: number | null = null;
|
|
594
|
+
let baselineWidth: number | null = null;
|
|
595
|
+
if (row.baselineStartAt && row.baselineFinishAt) {
|
|
596
|
+
baselineX = getGanttX(scale, row.baselineStartAt);
|
|
597
|
+
baselineWidth = Math.max(
|
|
598
|
+
2,
|
|
599
|
+
getGanttX(scale, row.baselineFinishAt) - baselineX,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
let floatX: number | null = null;
|
|
604
|
+
let floatWidth: number | null = null;
|
|
605
|
+
if (
|
|
606
|
+
row.lateFinishAt &&
|
|
607
|
+
row.totalFloatMinutes !== null &&
|
|
608
|
+
row.totalFloatMinutes > 0 &&
|
|
609
|
+
row.status !== 'COMPLETE'
|
|
610
|
+
) {
|
|
611
|
+
floatX = x + width;
|
|
612
|
+
floatWidth = Math.max(0, getGanttX(scale, row.lateFinishAt) - floatX);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
bars.push({
|
|
616
|
+
delayX:
|
|
617
|
+
row.levelingDelayMinutes > 0 && row.earlyStartAt
|
|
618
|
+
? getGanttX(scale, row.earlyStartAt)
|
|
619
|
+
: null,
|
|
620
|
+
delayWidth:
|
|
621
|
+
row.levelingDelayMinutes > 0 && row.earlyStartAt
|
|
622
|
+
? Math.max(0, x - getGanttX(scale, row.earlyStartAt))
|
|
623
|
+
: 0,
|
|
624
|
+
rowIndex,
|
|
625
|
+
x,
|
|
626
|
+
width,
|
|
627
|
+
progressWidth: Math.round(width * progress),
|
|
628
|
+
isMilestone,
|
|
629
|
+
isSummary:
|
|
630
|
+
row.kind === 'WBS' ||
|
|
631
|
+
row.type === 'WBS_SUMMARY' ||
|
|
632
|
+
row.type === 'LEVEL_OF_EFFORT',
|
|
633
|
+
isCritical: row.isCritical,
|
|
634
|
+
isComplete: row.status === 'COMPLETE',
|
|
635
|
+
baselineX,
|
|
636
|
+
baselineWidth,
|
|
637
|
+
floatX,
|
|
638
|
+
floatWidth,
|
|
639
|
+
});
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
return bars;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Compute the link paths between activity bars for the visible rows.
|
|
647
|
+
* A link whose end is off-screen (collapsed) is skipped.
|
|
648
|
+
*/
|
|
649
|
+
|
|
650
|
+
export function buildGanttLinks(
|
|
651
|
+
rows: GanttRowObj[],
|
|
652
|
+
bars: GanttBarObj[],
|
|
653
|
+
relationships: GanttRelationshipInput[],
|
|
654
|
+
): GanttLinkObj[] {
|
|
655
|
+
const barByRowId = new Map<string, GanttBarObj>();
|
|
656
|
+
for (const bar of bars) {
|
|
657
|
+
const row = rows[bar.rowIndex];
|
|
658
|
+
if (row?.kind === 'ACTIVITY') barByRowId.set(row.id, bar);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/* Return the vertical center used by every relationship endpoint. */
|
|
662
|
+
const rowCenter = (bar: GanttBarObj) =>
|
|
663
|
+
bar.rowIndex * GANTT_ROW_HEIGHT + GANTT_ROW_HEIGHT / 2;
|
|
664
|
+
const links: GanttLinkObj[] = [];
|
|
665
|
+
|
|
666
|
+
for (const relationship of relationships) {
|
|
667
|
+
const from = barByRowId.get(relationship.predecessorId);
|
|
668
|
+
const to = barByRowId.get(relationship.successorId);
|
|
669
|
+
|
|
670
|
+
if (!from || !to) {
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const fromStart = relationship.type === 'SS' || relationship.type === 'SF';
|
|
675
|
+
const toFinish = relationship.type === 'FF' || relationship.type === 'SF';
|
|
676
|
+
const x1 = fromStart ? from.x : from.x + from.width;
|
|
677
|
+
const y1 = rowCenter(from);
|
|
678
|
+
const x2 = toFinish ? to.x + to.width : to.x;
|
|
679
|
+
const y2 = rowCenter(to);
|
|
680
|
+
const stub = 8;
|
|
681
|
+
|
|
682
|
+
// Out of the predecessor, across, down beside the successor, and in
|
|
683
|
+
const midX = fromStart
|
|
684
|
+
? Math.min(x1, x2) - stub
|
|
685
|
+
: Math.max(x1 + stub, x2 - stub);
|
|
686
|
+
const path = toFinish
|
|
687
|
+
? `M ${x1} ${y1} H ${Math.max(x1, x2) + stub} V ${y2} H ${x2}`
|
|
688
|
+
: `M ${x1} ${y1} H ${midX} V ${y2} H ${x2}`;
|
|
689
|
+
|
|
690
|
+
links.push({
|
|
691
|
+
id: relationship.id,
|
|
692
|
+
fromRowIndex: from.rowIndex,
|
|
693
|
+
toRowIndex: to.rowIndex,
|
|
694
|
+
minX: Math.min(x1, x2, midX) - stub,
|
|
695
|
+
maxX: Math.max(x1, x2, midX) + stub,
|
|
696
|
+
isDriving: !!relationship.isDriving,
|
|
697
|
+
path,
|
|
698
|
+
isCritical: !!relationship.isDriving && from.isCritical && to.isCritical,
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
return links;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Format working minutes as days for a table column.
|
|
707
|
+
*/
|
|
708
|
+
|
|
709
|
+
export function formatGanttDays(
|
|
710
|
+
minutes: number | null | undefined,
|
|
711
|
+
minutesPerDay: number,
|
|
712
|
+
): string {
|
|
713
|
+
if (minutes === null || minutes === undefined) {
|
|
714
|
+
return '';
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const days = minutes / (minutesPerDay || 480);
|
|
718
|
+
|
|
719
|
+
return new Intl.NumberFormat(undefined, { maximumFractionDigits: 1 }).format(
|
|
720
|
+
days,
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Format one instant as a short local date for a table column.
|
|
726
|
+
*/
|
|
727
|
+
|
|
728
|
+
export function formatGanttDate(at: Date | null, timeZone: string): string {
|
|
729
|
+
if (!at) {
|
|
730
|
+
return '';
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return new Intl.DateTimeFormat(undefined, {
|
|
734
|
+
day: '2-digit',
|
|
735
|
+
month: 'short',
|
|
736
|
+
year: '2-digit',
|
|
737
|
+
timeZone,
|
|
738
|
+
}).format(at);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/* Convert timeline pixels back to a local-calendar instant without assuming every day is 24 hours. */
|
|
742
|
+
export function getGanttDate(scale: GanttScaleObj, x: number): Date {
|
|
743
|
+
const days = x / scale.dayWidth;
|
|
744
|
+
const wholeDays = Math.floor(days);
|
|
745
|
+
const day = DateTime.fromJSDate(scale.startAt, { zone: scale.timeZone }).plus(
|
|
746
|
+
{ days: wholeDays },
|
|
747
|
+
);
|
|
748
|
+
const dayMs = day.plus({ days: 1 }).toMillis() - day.toMillis();
|
|
749
|
+
return new Date(Math.round(day.toMillis() + (days - wholeDays) * dayMs));
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/* Cull relationship paths and tick geometry against the mounted viewport. */
|
|
753
|
+
export function getGanttVisibleRange(
|
|
754
|
+
scrollTop: number,
|
|
755
|
+
height: number,
|
|
756
|
+
rowCount: number,
|
|
757
|
+
overscan = 8,
|
|
758
|
+
) {
|
|
759
|
+
if (height <= 0 || rowCount <= 0) return { start: 0, end: 0 };
|
|
760
|
+
const start = Math.max(
|
|
761
|
+
0,
|
|
762
|
+
Math.floor(scrollTop / GANTT_ROW_HEIGHT) - overscan,
|
|
763
|
+
);
|
|
764
|
+
return {
|
|
765
|
+
start,
|
|
766
|
+
end: Math.min(
|
|
767
|
+
rowCount,
|
|
768
|
+
Math.ceil((scrollTop + height) / GANTT_ROW_HEIGHT) + overscan,
|
|
769
|
+
),
|
|
770
|
+
};
|
|
771
|
+
}
|