@quartzio/gantt 0.0.0-stage → 0.0.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.
- package/API.md +533 -0
- package/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/index.d.ts +1341 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6556 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +1299 -0
- package/package.json +51 -3
- package/src/calendar/duration.ts +42 -0
- package/src/calendar/project.ts +29 -0
- package/src/calendar/workingCalendar.ts +218 -0
- package/src/data/draft.ts +78 -0
- package/src/data/graph.ts +215 -0
- package/src/data/normalize.ts +661 -0
- package/src/data/operations.ts +112 -0
- package/src/data/project.ts +194 -0
- package/src/data/serialize.ts +26 -0
- package/src/data/transaction.ts +460 -0
- package/src/data/tree.ts +156 -0
- package/src/data/types.ts +229 -0
- package/src/index.ts +139 -0
- package/src/scheduling/schedule.ts +310 -0
- package/src/styles.css +1299 -0
- package/src/timeaxis/align.ts +120 -0
- package/src/timeaxis/format.ts +72 -0
- package/src/timeaxis/presets.ts +219 -0
- package/src/timeaxis/timeAxis.ts +205 -0
- package/src/util/civil.ts +38 -0
- package/src/util/emitter.ts +53 -0
- package/src/util/equal.ts +23 -0
- package/src/util/errors.ts +4 -0
- package/src/util/parse.ts +70 -0
- package/src/util/time.ts +18 -0
- package/src/util/zone.ts +303 -0
- package/src/view/bars.ts +63 -0
- package/src/view/binding.ts +162 -0
- package/src/view/cellEditor.ts +239 -0
- package/src/view/cells.ts +89 -0
- package/src/view/columns.ts +197 -0
- package/src/view/createGantt.ts +1164 -0
- package/src/view/dates.ts +14 -0
- package/src/view/dependencies.ts +147 -0
- package/src/view/editing.ts +303 -0
- package/src/view/history.ts +72 -0
- package/src/view/interaction.ts +671 -0
- package/src/view/keyboard.ts +149 -0
- package/src/view/listWidth.ts +201 -0
- package/src/view/menu.ts +265 -0
- package/src/view/nonWorking.ts +113 -0
- package/src/view/range.ts +41 -0
- package/src/view/rows.ts +235 -0
- package/src/view/selection.ts +156 -0
- package/src/view/taskActions.ts +71 -0
- package/src/view/taskEditor.ts +553 -0
- package/src/view/timeline.ts +155 -0
- package/src/view/tooltip.ts +117 -0
- package/src/view/types.ts +328 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { isEqual } from '../util/equal';
|
|
2
|
+
import { QuartzioError } from '../util/errors';
|
|
3
|
+
import type { Draft } from './draft';
|
|
4
|
+
import {
|
|
5
|
+
assertDependencyEnds,
|
|
6
|
+
assertNoParentCycle,
|
|
7
|
+
assertNoStructureFields,
|
|
8
|
+
assertTaskConstraint,
|
|
9
|
+
assertTaskDates,
|
|
10
|
+
isId,
|
|
11
|
+
normalizeCalendarChanges,
|
|
12
|
+
normalizeCalendarFields,
|
|
13
|
+
normalizeDependencyChanges,
|
|
14
|
+
normalizeDependencyFields,
|
|
15
|
+
normalizeSettingsChanges,
|
|
16
|
+
normalizeTaskChanges,
|
|
17
|
+
normalizeTaskFields,
|
|
18
|
+
type CalendarFieldsInput,
|
|
19
|
+
type DependencyFieldsInput,
|
|
20
|
+
type TaskFieldsInput,
|
|
21
|
+
} from './normalize';
|
|
22
|
+
import { applyOperation } from './operations';
|
|
23
|
+
import { getScheduleGraph } from './graph';
|
|
24
|
+
import { buildDraftTreeIndex, type DraftTreeIndex } from './tree';
|
|
25
|
+
import type {
|
|
26
|
+
Calendar,
|
|
27
|
+
Dependency,
|
|
28
|
+
Id,
|
|
29
|
+
Operation,
|
|
30
|
+
ProjectState,
|
|
31
|
+
ProjectSettings,
|
|
32
|
+
ProjectSettingsInput,
|
|
33
|
+
StoreName,
|
|
34
|
+
Table,
|
|
35
|
+
Task,
|
|
36
|
+
} from './types';
|
|
37
|
+
|
|
38
|
+
/** Where to put a task. `parentId` defaults to root (for add) or the current parent (for move); `index` to last. */
|
|
39
|
+
export interface TaskPosition {
|
|
40
|
+
parentId?: Id | null;
|
|
41
|
+
/** Position among the new siblings. */
|
|
42
|
+
index?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TaskAddInput extends TaskFieldsInput {
|
|
46
|
+
/** Generated when omitted. */
|
|
47
|
+
id?: Id;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type TaskUpdateInput = TaskFieldsInput;
|
|
51
|
+
|
|
52
|
+
export interface DependencyAddInput extends DependencyFieldsInput {
|
|
53
|
+
/** Generated when omitted. */
|
|
54
|
+
id?: Id;
|
|
55
|
+
from: Id;
|
|
56
|
+
to: Id;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface DependencyUpdateInput extends DependencyFieldsInput {
|
|
60
|
+
from?: Id;
|
|
61
|
+
to?: Id;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Property signatures (not methods) so the functions can be destructured and passed around.
|
|
65
|
+
export interface TaskTransaction {
|
|
66
|
+
/** Reads reflect earlier writes in the same transaction. */
|
|
67
|
+
get: (id: Id) => Task | undefined;
|
|
68
|
+
children: (parentId: Id | null) => readonly Id[];
|
|
69
|
+
add: (input: TaskAddInput, position?: TaskPosition) => Task;
|
|
70
|
+
/** Fields that equal the current value are skipped, so no-op updates produce no operations. */
|
|
71
|
+
update: (id: Id, changes: TaskUpdateInput) => void;
|
|
72
|
+
move: (id: Id, position: TaskPosition) => void;
|
|
73
|
+
/** Also removes all descendants and every dependency connected to them. */
|
|
74
|
+
remove: (id: Id) => void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface DependencyTransaction {
|
|
78
|
+
get: (id: Id) => Dependency | undefined;
|
|
79
|
+
add: (input: DependencyAddInput) => Dependency;
|
|
80
|
+
update: (id: Id, changes: DependencyUpdateInput) => void;
|
|
81
|
+
remove: (id: Id) => void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface CalendarAddInput extends CalendarFieldsInput {
|
|
85
|
+
/** Generated when omitted. */
|
|
86
|
+
id?: Id;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type CalendarUpdateInput = CalendarFieldsInput;
|
|
90
|
+
|
|
91
|
+
export interface CalendarTransaction {
|
|
92
|
+
get: (id: Id) => Calendar | undefined;
|
|
93
|
+
add: (input: CalendarAddInput) => Calendar;
|
|
94
|
+
update: (id: Id, changes: CalendarUpdateInput) => void;
|
|
95
|
+
/** Fails while the calendar is the project calendar. */
|
|
96
|
+
remove: (id: Id) => void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface SettingsTransaction {
|
|
100
|
+
get: () => ProjectSettings;
|
|
101
|
+
/**
|
|
102
|
+
* Changing `timeZone` does not move existing dates (they are instants), but it changes how wall-clock
|
|
103
|
+
* logic and date strings in later input are interpreted.
|
|
104
|
+
*/
|
|
105
|
+
update: (changes: ProjectSettingsInput) => void;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface Transaction {
|
|
109
|
+
readonly settings: SettingsTransaction;
|
|
110
|
+
readonly calendars: CalendarTransaction;
|
|
111
|
+
readonly tasks: TaskTransaction;
|
|
112
|
+
readonly dependencies: DependencyTransaction;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type IdGenerator = (store: StoreName, exists: (id: Id) => boolean) => Id;
|
|
116
|
+
|
|
117
|
+
export interface TransactionRecorder {
|
|
118
|
+
transaction: Transaction;
|
|
119
|
+
draft: Draft;
|
|
120
|
+
operations: Operation[];
|
|
121
|
+
inverse: Operation[];
|
|
122
|
+
/** Checks structural invariants once the transaction function is done (e.g. no dependency cycles). */
|
|
123
|
+
validate: () => void;
|
|
124
|
+
/**
|
|
125
|
+
* Checks invariants that may be broken until the change is complete, on the final state (after propagation,
|
|
126
|
+
* which can repair them, e.g. a new start after the old end on a scheduled task).
|
|
127
|
+
*/
|
|
128
|
+
validateResult: (state: ProjectState) => void;
|
|
129
|
+
/** Makes every further use of the transaction throw. Called when the transaction function returns. */
|
|
130
|
+
close: () => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const clampIndex = (index: number | undefined, length: number): number => {
|
|
134
|
+
if (index === undefined) return length;
|
|
135
|
+
// NaN can't be clamped to anything meaningful (and would put a task in different places in the tree and in
|
|
136
|
+
// `order`); out-of-range numbers, including ±Infinity, are clamped.
|
|
137
|
+
if (typeof index !== 'number' || Number.isNaN(index)) {
|
|
138
|
+
throw new QuartzioError(`Position index must be a number, got ${String(index)}.`);
|
|
139
|
+
}
|
|
140
|
+
return Math.max(0, Math.min(Math.trunc(index), length));
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Subtree size from which placementFor looks up positions through a map instead of scanning `order`.
|
|
144
|
+
const MAP_ABOVE = 8;
|
|
145
|
+
|
|
146
|
+
/** Creates a transaction that applies every change to `draft` immediately and records the operations. */
|
|
147
|
+
export function createTransaction(draft: Draft, generateId: IdGenerator): TransactionRecorder {
|
|
148
|
+
const operations: Operation[] = [];
|
|
149
|
+
const inverse: Operation[] = [];
|
|
150
|
+
const touchedTasks = new Set<Id>();
|
|
151
|
+
let closed = false;
|
|
152
|
+
|
|
153
|
+
// The draft's tables become the committed state, so a transaction used after it finished
|
|
154
|
+
// (e.g. after an `await`) would silently mutate "immutable" state.
|
|
155
|
+
const assertOpen = (): void => {
|
|
156
|
+
if (closed) {
|
|
157
|
+
throw new QuartzioError(
|
|
158
|
+
'This transaction has already finished. Transactions must be synchronous; do not keep or await them.',
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const apply = (op: Operation): void => {
|
|
164
|
+
assertOpen();
|
|
165
|
+
inverse.push(applyOperation(draft, op));
|
|
166
|
+
operations.push(op);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const tasks = (): Table<Task> => {
|
|
170
|
+
assertOpen();
|
|
171
|
+
return draft.read('tasks');
|
|
172
|
+
};
|
|
173
|
+
const dependencies = (): Table<Dependency> => {
|
|
174
|
+
assertOpen();
|
|
175
|
+
return draft.read('dependencies');
|
|
176
|
+
};
|
|
177
|
+
const calendars = (): Table<Calendar> => {
|
|
178
|
+
assertOpen();
|
|
179
|
+
return draft.read('calendars');
|
|
180
|
+
};
|
|
181
|
+
const settings = (): ProjectSettings => {
|
|
182
|
+
assertOpen();
|
|
183
|
+
return draft.readSettings();
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const newId = (store: StoreName, requested: Id | undefined, exists: (id: Id) => boolean): Id => {
|
|
187
|
+
const id = requested ?? generateId(store, exists);
|
|
188
|
+
if (!isId(id)) {
|
|
189
|
+
throw new QuartzioError(
|
|
190
|
+
`${store === 'tasks' ? 'Task' : 'Dependency'} id must be a non-empty string or a finite number.`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return id;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// Rebuilt only after structural changes other than adds, which are inserted in place (see add).
|
|
197
|
+
let tree: { version: number; index: DraftTreeIndex } | undefined;
|
|
198
|
+
const treeIndex = (): DraftTreeIndex => {
|
|
199
|
+
const version = draft.structureVersionOf('tasks');
|
|
200
|
+
if (tree?.version !== version) tree = { version, index: buildDraftTreeIndex(tasks()) };
|
|
201
|
+
return tree.index;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// Dependency ids per task (both ends), kept in step with adds and removals here and rebuilt after other
|
|
205
|
+
// structural changes, so removing many tasks doesn't scan every dependency each time.
|
|
206
|
+
let links: { version: number; byTask: Map<Id, Set<Id>> } | undefined;
|
|
207
|
+
const linksOf = (): Map<Id, Set<Id>> => {
|
|
208
|
+
const version = draft.structureVersionOf('dependencies');
|
|
209
|
+
if (links?.version !== version) {
|
|
210
|
+
const byTask = new Map<Id, Set<Id>>();
|
|
211
|
+
for (const dependency of dependencies().byId.values()) link(byTask, dependency, true);
|
|
212
|
+
links = { version, byTask };
|
|
213
|
+
}
|
|
214
|
+
return links.byTask;
|
|
215
|
+
};
|
|
216
|
+
const link = (byTask: Map<Id, Set<Id>>, dependency: Dependency, add: boolean): void => {
|
|
217
|
+
for (const taskId of [dependency.from, dependency.to]) {
|
|
218
|
+
let set = byTask.get(taskId);
|
|
219
|
+
if (!set) byTask.set(taskId, (set = new Set()));
|
|
220
|
+
if (add) set.add(dependency.id);
|
|
221
|
+
else set.delete(dependency.id);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
// Applies a dependency add or remove and updates the links in place instead of invalidating them.
|
|
225
|
+
const applyLinked = (op: Operation, dependency: Dependency, add: boolean): void => {
|
|
226
|
+
const byTask = linksOf();
|
|
227
|
+
apply(op);
|
|
228
|
+
link(byTask, dependency, add);
|
|
229
|
+
if (links) links.version = draft.structureVersionOf('dependencies');
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const requireTask = (id: Id): Task => {
|
|
233
|
+
const task = tasks().byId.get(id);
|
|
234
|
+
if (!task) throw new QuartzioError(`Task "${String(id)}" does not exist.`);
|
|
235
|
+
return task;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const requireDependency = (id: Id): Dependency => {
|
|
239
|
+
const dependency = dependencies().byId.get(id);
|
|
240
|
+
if (!dependency) throw new QuartzioError(`Dependency "${String(id)}" does not exist.`);
|
|
241
|
+
return dependency;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const requireParent = (parentId: Id | null): void => {
|
|
245
|
+
if (parentId !== null) requireTask(parentId);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Where a task goes to be at `index` among the children of `parentId`: its index in `order`, and among its
|
|
250
|
+
* siblings. With `moving`, positions are counted as if that task were already taken out (move semantics).
|
|
251
|
+
*/
|
|
252
|
+
const placementFor = (
|
|
253
|
+
parentId: Id | null,
|
|
254
|
+
index: number | undefined,
|
|
255
|
+
moving?: Id,
|
|
256
|
+
): { orderIndex: number; siblingIndex: number } => {
|
|
257
|
+
const tree = treeIndex();
|
|
258
|
+
// Positions come straight from `order`: it changes with every add, so a position map would need
|
|
259
|
+
// rebuilding each time. Large subtrees get a map for their one lookup below.
|
|
260
|
+
const order = tasks().order;
|
|
261
|
+
const movingPosition = moving === undefined ? -1 : order.indexOf(moving);
|
|
262
|
+
const adjust = (p: number): number => (movingPosition !== -1 && p > movingPosition ? p - 1 : p);
|
|
263
|
+
|
|
264
|
+
const all = tree.childrenView(parentId);
|
|
265
|
+
const siblings = moving === undefined ? all : all.filter((id) => id !== moving);
|
|
266
|
+
const siblingIndex = clampIndex(index, siblings.length);
|
|
267
|
+
const before = siblings[siblingIndex];
|
|
268
|
+
if (before !== undefined) return { orderIndex: adjust(order.indexOf(before)), siblingIndex };
|
|
269
|
+
|
|
270
|
+
const last = siblings[siblings.length - 1];
|
|
271
|
+
if (last !== undefined) {
|
|
272
|
+
const subtree = tree.descendants(last);
|
|
273
|
+
// One scan per descendant is cheaper than a map for small subtrees, the common case when adding.
|
|
274
|
+
const positions = subtree.length > MAP_ABOVE ? new Map(order.map((id, i) => [id, i])) : undefined;
|
|
275
|
+
// A loop, not Math.max(...spread): large subtrees would overflow the call stack.
|
|
276
|
+
let end = order.lastIndexOf(last); // appends usually go after the last task: search from the end
|
|
277
|
+
for (const id of subtree) end = Math.max(end, positions?.get(id) ?? order.indexOf(id));
|
|
278
|
+
return { orderIndex: adjust(end) + 1, siblingIndex };
|
|
279
|
+
}
|
|
280
|
+
if (parentId !== null) return { orderIndex: adjust(order.indexOf(parentId)) + 1, siblingIndex };
|
|
281
|
+
return { orderIndex: order.length - (movingPosition === -1 ? 0 : 1), siblingIndex };
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const changedFields = <R extends object>(record: R, changes: Partial<R>): Partial<R> => {
|
|
285
|
+
const result: Partial<R> = {};
|
|
286
|
+
for (const key of Object.keys(changes) as (keyof R)[]) {
|
|
287
|
+
// Structural, so re-sending equal arrays/objects (e.g. a calendar's week) is not a change.
|
|
288
|
+
if (!isEqual(record[key], changes[key])) result[key] = changes[key];
|
|
289
|
+
}
|
|
290
|
+
return result;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const taskTransaction: TaskTransaction = {
|
|
294
|
+
get: (id) => tasks().byId.get(id),
|
|
295
|
+
children: (parentId) => treeIndex().children(parentId),
|
|
296
|
+
|
|
297
|
+
add(input, position = {}) {
|
|
298
|
+
const { id: requestedId, ...fields } = input;
|
|
299
|
+
const id = newId('tasks', requestedId, (candidate) => tasks().byId.has(candidate));
|
|
300
|
+
const owner = `Task "${String(id)}"`;
|
|
301
|
+
assertNoStructureFields(fields, owner);
|
|
302
|
+
const parentId = position.parentId ?? null;
|
|
303
|
+
requireParent(parentId);
|
|
304
|
+
const record: Task = { id, parentId, ...normalizeTaskFields(fields, owner, settings().timeZone) };
|
|
305
|
+
assertTaskConstraint(record);
|
|
306
|
+
const { orderIndex, siblingIndex } = placementFor(parentId, position.index);
|
|
307
|
+
apply({ type: 'add', store: 'tasks', record, index: orderIndex });
|
|
308
|
+
// placementFor brought the tree up to date; insert instead of rebuilding it for the next add.
|
|
309
|
+
if (tree) {
|
|
310
|
+
tree.index.insert(id, parentId, siblingIndex);
|
|
311
|
+
tree.version = draft.structureVersionOf('tasks');
|
|
312
|
+
}
|
|
313
|
+
touchedTasks.add(id);
|
|
314
|
+
return record;
|
|
315
|
+
},
|
|
316
|
+
|
|
317
|
+
update(id, input) {
|
|
318
|
+
const task = requireTask(id);
|
|
319
|
+
const owner = `Task "${String(id)}"`;
|
|
320
|
+
assertNoStructureFields(input, owner);
|
|
321
|
+
const changes = changedFields(task, normalizeTaskChanges(input, owner, settings().timeZone));
|
|
322
|
+
// A new end before the start is an error. (A new start after the old end isn't: scheduling moves the
|
|
323
|
+
// end, keeping the duration; see validateResult.)
|
|
324
|
+
if ('endDate' in changes) assertTaskDates({ ...task, ...changes });
|
|
325
|
+
assertTaskConstraint({ ...task, ...changes });
|
|
326
|
+
if (Object.keys(changes).length > 0) apply({ type: 'update', store: 'tasks', id, changes });
|
|
327
|
+
touchedTasks.add(id);
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
move(id, position) {
|
|
331
|
+
const task = requireTask(id);
|
|
332
|
+
const parentId = position.parentId === undefined ? task.parentId : position.parentId;
|
|
333
|
+
requireParent(parentId);
|
|
334
|
+
assertNoParentCycle(tasks().byId, id, parentId);
|
|
335
|
+
|
|
336
|
+
if (parentId !== task.parentId) apply({ type: 'update', store: 'tasks', id, changes: { parentId } });
|
|
337
|
+
const { orderIndex } = placementFor(parentId, position.index, id);
|
|
338
|
+
if (orderIndex !== tasks().order.indexOf(id))
|
|
339
|
+
apply({ type: 'move', store: 'tasks', id, index: orderIndex });
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
remove(id) {
|
|
343
|
+
requireTask(id);
|
|
344
|
+
const index = treeIndex();
|
|
345
|
+
const descendants = index.descendants(id);
|
|
346
|
+
const subtree = [id, ...descendants];
|
|
347
|
+
// Dependencies first, then tasks leaf-first, so the inverse re-adds parents before children
|
|
348
|
+
// and tasks before the dependencies that reference them.
|
|
349
|
+
const byTask = linksOf();
|
|
350
|
+
const touching = new Set(subtree.flatMap((taskId) => [...(byTask.get(taskId) ?? [])]));
|
|
351
|
+
for (const dependencyId of touching) {
|
|
352
|
+
const dependency = requireDependency(dependencyId);
|
|
353
|
+
applyLinked({ type: 'remove', store: 'dependencies', id: dependencyId }, dependency, false);
|
|
354
|
+
}
|
|
355
|
+
for (const taskId of [...subtree].reverse()) apply({ type: 'remove', store: 'tasks', id: taskId });
|
|
356
|
+
index.removeSubtree(id, descendants);
|
|
357
|
+
if (tree) tree.version = draft.structureVersionOf('tasks');
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const dependencyTransaction: DependencyTransaction = {
|
|
362
|
+
get: (id) => dependencies().byId.get(id),
|
|
363
|
+
|
|
364
|
+
add(input) {
|
|
365
|
+
const { id: requestedId, from, to, ...fields } = input;
|
|
366
|
+
const id = newId('dependencies', requestedId, (candidate) => dependencies().byId.has(candidate));
|
|
367
|
+
const owner = `Dependency "${String(id)}"`;
|
|
368
|
+
assertDependencyEnds(tasks().byId, from, to, owner);
|
|
369
|
+
const record: Dependency = { id, from, to, ...normalizeDependencyFields(fields, owner) };
|
|
370
|
+
applyLinked(
|
|
371
|
+
{ type: 'add', store: 'dependencies', record, index: dependencies().order.length },
|
|
372
|
+
record,
|
|
373
|
+
true,
|
|
374
|
+
);
|
|
375
|
+
return record;
|
|
376
|
+
},
|
|
377
|
+
|
|
378
|
+
update(id, input) {
|
|
379
|
+
const dependency = requireDependency(id);
|
|
380
|
+
const owner = `Dependency "${String(id)}"`;
|
|
381
|
+
const { from = dependency.from, to = dependency.to, ...fields } = input;
|
|
382
|
+
assertDependencyEnds(tasks().byId, from, to, owner);
|
|
383
|
+
const changes = changedFields(dependency, { from, to, ...normalizeDependencyChanges(fields, owner) });
|
|
384
|
+
if (Object.keys(changes).length > 0) apply({ type: 'update', store: 'dependencies', id, changes });
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
remove(id) {
|
|
388
|
+
applyLinked({ type: 'remove', store: 'dependencies', id }, requireDependency(id), false);
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
const calendarTransaction: CalendarTransaction = {
|
|
393
|
+
get: (id) => calendars().byId.get(id),
|
|
394
|
+
|
|
395
|
+
add(input) {
|
|
396
|
+
const { id: requestedId, ...fields } = input;
|
|
397
|
+
const id = newId('calendars', requestedId, (candidate) => calendars().byId.has(candidate));
|
|
398
|
+
const record: Calendar = { id, ...normalizeCalendarFields(fields, `Calendar "${String(id)}"`) };
|
|
399
|
+
apply({ type: 'add', store: 'calendars', record, index: calendars().order.length });
|
|
400
|
+
return record;
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
update(id, input) {
|
|
404
|
+
const calendar = calendars().byId.get(id);
|
|
405
|
+
if (!calendar) throw new QuartzioError(`Calendar "${String(id)}" does not exist.`);
|
|
406
|
+
const changes = changedFields(calendar, normalizeCalendarChanges(input, `Calendar "${String(id)}"`));
|
|
407
|
+
if (Object.keys(changes).length > 0) apply({ type: 'update', store: 'calendars', id, changes });
|
|
408
|
+
},
|
|
409
|
+
|
|
410
|
+
remove(id) {
|
|
411
|
+
if (!calendars().byId.has(id)) throw new QuartzioError(`Calendar "${String(id)}" does not exist.`);
|
|
412
|
+
if (settings().calendarId === id) {
|
|
413
|
+
throw new QuartzioError(
|
|
414
|
+
`Calendar "${String(id)}" is the project calendar. Change settings.calendarId first.`,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
apply({ type: 'remove', store: 'calendars', id });
|
|
418
|
+
},
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
const settingsTransaction: SettingsTransaction = {
|
|
422
|
+
get: settings,
|
|
423
|
+
update(input) {
|
|
424
|
+
const changes = changedFields(settings(), normalizeSettingsChanges(input, settings().timeZone));
|
|
425
|
+
if (changes.calendarId != null && !calendars().byId.has(changes.calendarId)) {
|
|
426
|
+
throw new QuartzioError(`Project settings: calendar "${String(changes.calendarId)}" does not exist.`);
|
|
427
|
+
}
|
|
428
|
+
if (Object.keys(changes).length > 0) apply({ type: 'settings', changes });
|
|
429
|
+
},
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
transaction: {
|
|
434
|
+
settings: settingsTransaction,
|
|
435
|
+
calendars: calendarTransaction,
|
|
436
|
+
tasks: taskTransaction,
|
|
437
|
+
dependencies: dependencyTransaction,
|
|
438
|
+
},
|
|
439
|
+
draft,
|
|
440
|
+
operations,
|
|
441
|
+
// Inverse operations must run in reverse order; pushed in order and reversed on read.
|
|
442
|
+
get inverse() {
|
|
443
|
+
return [...inverse].reverse();
|
|
444
|
+
},
|
|
445
|
+
validate: () => {
|
|
446
|
+
// Dependency cycles, also through the hierarchy (a move can create one). Cached when nothing structural
|
|
447
|
+
// changed.
|
|
448
|
+
getScheduleGraph(draft.finish());
|
|
449
|
+
},
|
|
450
|
+
validateResult: (state) => {
|
|
451
|
+
for (const id of touchedTasks) {
|
|
452
|
+
const task = state.tasks.byId.get(id);
|
|
453
|
+
if (task) assertTaskDates(task);
|
|
454
|
+
}
|
|
455
|
+
},
|
|
456
|
+
close: () => {
|
|
457
|
+
closed = true;
|
|
458
|
+
},
|
|
459
|
+
};
|
|
460
|
+
}
|
package/src/data/tree.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { Id, Table, Task } from './types';
|
|
2
|
+
|
|
3
|
+
const EMPTY: readonly Id[] = [];
|
|
4
|
+
|
|
5
|
+
/** Derived hierarchy for a task table. Built in O(n); cached per table so repeated lookups are cheap. */
|
|
6
|
+
export interface TreeIndex {
|
|
7
|
+
/** Child ids in sibling order. `null` returns the root tasks. */
|
|
8
|
+
children: (parentId: Id | null) => readonly Id[];
|
|
9
|
+
/** Position of each task in the table's `order`. */
|
|
10
|
+
position: (id: Id) => number;
|
|
11
|
+
/** Ancestors from the direct parent up to the root. */
|
|
12
|
+
ancestors: (id: Id) => Id[];
|
|
13
|
+
/** All descendants, depth-first in sibling order. */
|
|
14
|
+
descendants: (id: Id) => Id[];
|
|
15
|
+
depth: (id: Id) => number;
|
|
16
|
+
isLeaf: (id: Id) => boolean;
|
|
17
|
+
/** Tasks depth-first in sibling order: the order rows appear in when everything is expanded. */
|
|
18
|
+
flatten: () => readonly Id[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The hierarchy of a table that is still changing (a transaction draft). Added and removed tasks are updated in
|
|
23
|
+
* place, so adding or removing many tasks doesn't rebuild it each time. No positions: they shift with every
|
|
24
|
+
* change (use `order`).
|
|
25
|
+
*/
|
|
26
|
+
export interface DraftTreeIndex extends Omit<TreeIndex, 'position' | 'flatten'> {
|
|
27
|
+
/** Like `children`, without copying: only valid until the next insert. */
|
|
28
|
+
childrenView: (parentId: Id | null) => readonly Id[];
|
|
29
|
+
insert: (id: Id, parentId: Id | null, siblingIndex: number) => void;
|
|
30
|
+
/** Removes a task with all its descendants (given, so they aren't walked twice). */
|
|
31
|
+
removeSubtree: (id: Id, descendants: readonly Id[]) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const cache = new WeakMap<Table<Task>, TreeIndex>();
|
|
35
|
+
// Tables with the same structure (order and parents) as an earlier table, whose index they share once one is
|
|
36
|
+
// built. Points at the first table of such a run, never along a chain, so it keeps at most one old table alive.
|
|
37
|
+
const sameStructure = new WeakMap<Table<Task>, Table<Task>>();
|
|
38
|
+
|
|
39
|
+
export function getTreeIndex(tasks: Table<Task>): TreeIndex {
|
|
40
|
+
const cached = cache.get(tasks);
|
|
41
|
+
if (cached) return cached;
|
|
42
|
+
const earlier = sameStructure.get(tasks);
|
|
43
|
+
const index = (earlier && cache.get(earlier)) ?? buildTreeIndex(tasks);
|
|
44
|
+
if (earlier) cache.set(earlier, index);
|
|
45
|
+
cache.set(tasks, index);
|
|
46
|
+
sameStructure.delete(tasks);
|
|
47
|
+
return index;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Records that `next` has the same order and parents as `previous` (only other fields changed), so it can use
|
|
52
|
+
* the same index instead of building one: field edits are far more common than structural ones.
|
|
53
|
+
*/
|
|
54
|
+
export function shareTreeIndex(previous: Table<Task>, next: Table<Task>): void {
|
|
55
|
+
if (previous === next) return;
|
|
56
|
+
const index = cache.get(previous);
|
|
57
|
+
if (index) cache.set(next, index);
|
|
58
|
+
else sameStructure.set(next, sameStructure.get(previous) ?? previous);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Uncached build of a finished table's index. */
|
|
62
|
+
export function buildTreeIndex(tasks: Table<Task>): TreeIndex {
|
|
63
|
+
const structure = readStructure(tasks);
|
|
64
|
+
const positions = new Map<Id, number>();
|
|
65
|
+
tasks.order.forEach((id, position) => positions.set(id, position));
|
|
66
|
+
const walk = walker(structure);
|
|
67
|
+
let flattened: Id[] | undefined;
|
|
68
|
+
return {
|
|
69
|
+
...walk,
|
|
70
|
+
position: (id) => positions.get(id) ?? -1,
|
|
71
|
+
flatten: () => {
|
|
72
|
+
flattened ??= walk.children(null).flatMap((rootId) => [rootId, ...walk.descendants(rootId)]);
|
|
73
|
+
return flattened;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function buildDraftTreeIndex(tasks: Table<Task>): DraftTreeIndex {
|
|
79
|
+
const structure = readStructure(tasks);
|
|
80
|
+
const walk = walker(structure);
|
|
81
|
+
return {
|
|
82
|
+
...walk,
|
|
83
|
+
// A copy: the lists change with later inserts.
|
|
84
|
+
children: (parentId) => [...walk.children(parentId)],
|
|
85
|
+
childrenView: walk.children,
|
|
86
|
+
insert: (id, parentId, siblingIndex) => {
|
|
87
|
+
structure.parents.set(id, parentId);
|
|
88
|
+
const siblings = structure.childrenByParent.get(parentId);
|
|
89
|
+
if (siblings) siblings.splice(siblingIndex, 0, id);
|
|
90
|
+
else structure.childrenByParent.set(parentId, [id]);
|
|
91
|
+
},
|
|
92
|
+
removeSubtree: (id, descendants) => {
|
|
93
|
+
// Only the root leaves a sibling list; the descendants' lists go with them.
|
|
94
|
+
const siblings = structure.childrenByParent.get(structure.parents.get(id) ?? null);
|
|
95
|
+
const at = siblings?.indexOf(id) ?? -1;
|
|
96
|
+
if (at !== -1) siblings?.splice(at, 1);
|
|
97
|
+
for (const removed of [id, ...descendants]) {
|
|
98
|
+
structure.parents.delete(removed);
|
|
99
|
+
structure.childrenByParent.delete(removed);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface Structure {
|
|
106
|
+
childrenByParent: Map<Id | null, Id[]>;
|
|
107
|
+
// Kept in the index rather than read from the table, so a shared index doesn't hold on to old tables.
|
|
108
|
+
parents: Map<Id, Id | null>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function readStructure(tasks: Table<Task>): Structure {
|
|
112
|
+
const childrenByParent = new Map<Id | null, Id[]>();
|
|
113
|
+
const parents = new Map<Id, Id | null>();
|
|
114
|
+
for (const id of tasks.order) {
|
|
115
|
+
const parentId = tasks.byId.get(id)?.parentId ?? null;
|
|
116
|
+
parents.set(id, parentId);
|
|
117
|
+
const siblings = childrenByParent.get(parentId);
|
|
118
|
+
if (siblings) siblings.push(id);
|
|
119
|
+
else childrenByParent.set(parentId, [id]);
|
|
120
|
+
}
|
|
121
|
+
return { childrenByParent, parents };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function walker({ childrenByParent, parents }: Structure): Omit<TreeIndex, 'position' | 'flatten'> {
|
|
125
|
+
const children = (parentId: Id | null): readonly Id[] => childrenByParent.get(parentId) ?? EMPTY;
|
|
126
|
+
|
|
127
|
+
const descendants = (id: Id): Id[] => {
|
|
128
|
+
const result: Id[] = [];
|
|
129
|
+
const stack = [...children(id)].reverse();
|
|
130
|
+
while (stack.length > 0) {
|
|
131
|
+
const current = stack.pop() as Id;
|
|
132
|
+
result.push(current);
|
|
133
|
+
const kids = children(current);
|
|
134
|
+
for (let i = kids.length - 1; i >= 0; i--) stack.push(kids[i] as Id);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const ancestors = (id: Id): Id[] => {
|
|
140
|
+
const result: Id[] = [];
|
|
141
|
+
let parentId = parents.get(id) ?? null;
|
|
142
|
+
while (parentId !== null) {
|
|
143
|
+
result.push(parentId);
|
|
144
|
+
parentId = parents.get(parentId) ?? null;
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
children,
|
|
151
|
+
ancestors,
|
|
152
|
+
descendants,
|
|
153
|
+
depth: (id) => ancestors(id).length,
|
|
154
|
+
isLeaf: (id) => children(id).length === 0,
|
|
155
|
+
};
|
|
156
|
+
}
|