@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,553 @@
|
|
|
1
|
+
import { workingMsPerUnit } from '../calendar/duration';
|
|
2
|
+
import type { PlannedChange } from '../data/project';
|
|
3
|
+
import type { Transaction } from '../data/transaction';
|
|
4
|
+
import type { TreeIndex } from '../data/tree';
|
|
5
|
+
import type { DependencyType, Id, ProjectState, Task } from '../data/types';
|
|
6
|
+
import type { TimeUnit } from '../util/time';
|
|
7
|
+
import { QuartzioError } from '../util/errors';
|
|
8
|
+
import { fromWallTime } from '../util/zone';
|
|
9
|
+
import { taskDates } from './dates';
|
|
10
|
+
import {
|
|
11
|
+
durationText,
|
|
12
|
+
editKind,
|
|
13
|
+
initialText,
|
|
14
|
+
inputValue,
|
|
15
|
+
MAX_WORKING_DAYS,
|
|
16
|
+
parseDuration,
|
|
17
|
+
parseEdit,
|
|
18
|
+
parseInputDate,
|
|
19
|
+
type EditKind,
|
|
20
|
+
} from './editing';
|
|
21
|
+
|
|
22
|
+
export type TaskEditorTab = 'general' | 'predecessors' | 'successors' | 'advanced';
|
|
23
|
+
export type TaskEditorField =
|
|
24
|
+
'name' | 'startDate' | 'endDate' | 'duration' | 'percentDone' | 'constraintDate';
|
|
25
|
+
export type TaskEditorSide = 'predecessors' | 'successors';
|
|
26
|
+
export type TaskEditorConstraint = 'none' | 'startnoearlierthan';
|
|
27
|
+
|
|
28
|
+
export interface TaskEditorFieldState {
|
|
29
|
+
readonly text: string;
|
|
30
|
+
readonly kind: EditKind;
|
|
31
|
+
readonly error: string | null;
|
|
32
|
+
/** Can't be edited (e.g. a parent's dates, which roll up). */
|
|
33
|
+
readonly disabled: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A predecessor or successor in the editor. */
|
|
37
|
+
export interface TaskEditorDependency {
|
|
38
|
+
/** Stable key for rendering and actions. */
|
|
39
|
+
readonly key: string;
|
|
40
|
+
/** The dependency it edits, or `null` for a new one. */
|
|
41
|
+
readonly dependencyId: Id | null;
|
|
42
|
+
/** The other task, or `null` until one is chosen. */
|
|
43
|
+
readonly taskId: Id | null;
|
|
44
|
+
readonly type: DependencyType;
|
|
45
|
+
/** Lag as text, e.g. `2d` or `-1d`. */
|
|
46
|
+
readonly lag: string;
|
|
47
|
+
readonly error: string | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The open task editor. */
|
|
51
|
+
export interface TaskEditorState {
|
|
52
|
+
readonly taskId: Id;
|
|
53
|
+
/** The task as it is now. */
|
|
54
|
+
readonly task: Task;
|
|
55
|
+
readonly tab: TaskEditorTab;
|
|
56
|
+
readonly fields: Readonly<Record<TaskEditorField, TaskEditorFieldState>>;
|
|
57
|
+
readonly manuallyScheduled: boolean;
|
|
58
|
+
readonly constraintType: TaskEditorConstraint;
|
|
59
|
+
readonly predecessors: readonly TaskEditorDependency[];
|
|
60
|
+
readonly successors: readonly TaskEditorDependency[];
|
|
61
|
+
/** Tasks a dependency can link to, in tree order. */
|
|
62
|
+
readonly candidates: readonly { readonly id: Id; readonly name: string; readonly depth: number }[];
|
|
63
|
+
/** Why saving failed as a whole (e.g. a dependency cycle). */
|
|
64
|
+
readonly error: string | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type TaskEditorAction =
|
|
68
|
+
| { readonly type: 'input'; readonly field: TaskEditorField; readonly value: string }
|
|
69
|
+
| { readonly type: 'input'; readonly field: 'manuallyScheduled'; readonly value: boolean }
|
|
70
|
+
| { readonly type: 'input'; readonly field: 'constraintType'; readonly value: TaskEditorConstraint }
|
|
71
|
+
| { readonly type: 'tab'; readonly tab: TaskEditorTab }
|
|
72
|
+
| { readonly type: 'addDependency'; readonly side: TaskEditorSide }
|
|
73
|
+
| {
|
|
74
|
+
readonly type: 'updateDependency';
|
|
75
|
+
readonly side: TaskEditorSide;
|
|
76
|
+
readonly key: string;
|
|
77
|
+
readonly changes: {
|
|
78
|
+
readonly taskId?: Id | null | undefined;
|
|
79
|
+
readonly type?: DependencyType | undefined;
|
|
80
|
+
readonly lag?: string | undefined;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
| { readonly type: 'removeDependency'; readonly side: TaskEditorSide; readonly key: string }
|
|
84
|
+
| { readonly type: 'save' }
|
|
85
|
+
| { readonly type: 'close' };
|
|
86
|
+
|
|
87
|
+
export interface TaskEditorView {
|
|
88
|
+
readonly project: ProjectState;
|
|
89
|
+
readonly tree: TreeIndex;
|
|
90
|
+
readonly dateField: 'date' | 'datetime';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface TaskEditorContext {
|
|
94
|
+
readonly view: () => TaskEditorView;
|
|
95
|
+
readonly enabled: () => boolean;
|
|
96
|
+
readonly plan: (fn: (tx: Transaction) => void) => PlannedChange | null;
|
|
97
|
+
readonly commit: (fn: (tx: Transaction) => void) => void;
|
|
98
|
+
readonly show: () => void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface TaskEditor {
|
|
102
|
+
readonly current: (view: TaskEditorView) => TaskEditorState | null;
|
|
103
|
+
readonly open: (id: Id) => boolean;
|
|
104
|
+
readonly act: (action: TaskEditorAction) => boolean;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface DraftDependency {
|
|
108
|
+
readonly key: string;
|
|
109
|
+
readonly dependencyId: Id | null;
|
|
110
|
+
readonly taskId: Id | null;
|
|
111
|
+
readonly type: DependencyType;
|
|
112
|
+
readonly lag: string;
|
|
113
|
+
/** As loaded, to tell whether it changed. */
|
|
114
|
+
readonly initial: {
|
|
115
|
+
readonly taskId: Id | null;
|
|
116
|
+
readonly type: DependencyType;
|
|
117
|
+
readonly lag: string;
|
|
118
|
+
/** The exact lag, kept while its text isn't changed (the text is rounded). */
|
|
119
|
+
readonly value: number;
|
|
120
|
+
readonly unit: TimeUnit;
|
|
121
|
+
} | null;
|
|
122
|
+
readonly error: string | null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface Draft {
|
|
126
|
+
readonly taskId: Id;
|
|
127
|
+
readonly tab: TaskEditorTab;
|
|
128
|
+
readonly texts: Readonly<Record<TaskEditorField, string>>;
|
|
129
|
+
/** The values' texts: a field whose text is still this wasn't typed in (and follows the value). */
|
|
130
|
+
readonly initial: Readonly<Record<TaskEditorField, string>>;
|
|
131
|
+
readonly errors: Readonly<Partial<Record<TaskEditorField, string>>>;
|
|
132
|
+
readonly manual: boolean;
|
|
133
|
+
readonly constraint: TaskEditorConstraint;
|
|
134
|
+
readonly predecessors: readonly DraftDependency[];
|
|
135
|
+
readonly successors: readonly DraftDependency[];
|
|
136
|
+
readonly removed: readonly Id[];
|
|
137
|
+
readonly error: string | null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const FIELDS: readonly TaskEditorField[] = [
|
|
141
|
+
'name',
|
|
142
|
+
'startDate',
|
|
143
|
+
'endDate',
|
|
144
|
+
'duration',
|
|
145
|
+
'percentDone',
|
|
146
|
+
'constraintDate',
|
|
147
|
+
];
|
|
148
|
+
const TABS = new Set<TaskEditorTab>(['general', 'predecessors', 'successors', 'advanced']);
|
|
149
|
+
const TYPES = new Set<DependencyType>(['FS', 'SS', 'FF', 'SF']);
|
|
150
|
+
// The fields a save reads first; the end comes last, read against where the rest puts the task.
|
|
151
|
+
const SAVE_ORDER = ['name', 'percentDone', 'startDate', 'duration'] as const;
|
|
152
|
+
|
|
153
|
+
/** The value of each field as text. */
|
|
154
|
+
function texts(task: Task, view: TaskEditorView): Record<TaskEditorField, string> {
|
|
155
|
+
const zone = view.project.settings.timeZone;
|
|
156
|
+
const dates = taskDates(task);
|
|
157
|
+
const text = (field: Exclude<TaskEditorField, 'constraintDate'>) =>
|
|
158
|
+
initialText(field, field === 'name' ? 'text' : kindOf(field, view), task, dates, zone);
|
|
159
|
+
return {
|
|
160
|
+
name: text('name'),
|
|
161
|
+
startDate: text('startDate'),
|
|
162
|
+
endDate: text('endDate'),
|
|
163
|
+
duration: text('duration'),
|
|
164
|
+
percentDone: text('percentDone'),
|
|
165
|
+
constraintDate: task.constraintDate === null ? '' : inputValue(task.constraintDate, view.dateField, zone),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function kindOf(field: TaskEditorField, view: TaskEditorView): EditKind {
|
|
170
|
+
if (field === 'name') return 'text';
|
|
171
|
+
if (field === 'duration') return 'duration';
|
|
172
|
+
if (field === 'percentDone') return 'percent';
|
|
173
|
+
return view.dateField;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** A lag like `2d` or `-1.5h`, in working time. */
|
|
177
|
+
function parseLag(text: string, view: TaskEditorView): { value: number; unit: TimeUnit } | string {
|
|
178
|
+
const match = /^\s*([+-]?)\s*(.*)$/.exec(text) as RegExpExecArray;
|
|
179
|
+
const parsed = parseDuration(match[2] as string, 'day');
|
|
180
|
+
if (typeof parsed === 'string') return 'Enter a lag, like 2d or -1d.';
|
|
181
|
+
const settings = view.project.settings;
|
|
182
|
+
const days = (parsed.value * workingMsPerUnit(parsed.unit, settings)) / workingMsPerUnit('day', settings);
|
|
183
|
+
if (days > MAX_WORKING_DAYS) return `Enter a lag of at most ${String(MAX_WORKING_DAYS)} working days.`;
|
|
184
|
+
return { value: match[1] === '-' ? -parsed.value : parsed.value, unit: parsed.unit };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Editing a task in a dialog: a draft, saved as one change. */
|
|
188
|
+
export function createTaskEditor(context: TaskEditorContext): TaskEditor {
|
|
189
|
+
let draft: Draft | null = null;
|
|
190
|
+
let nextKey = 0;
|
|
191
|
+
let shown: { draft: Draft; task: Task; project: ProjectState; state: TaskEditorState } | null = null;
|
|
192
|
+
let candidatesCache: { tasks: unknown; taskId: Id; list: TaskEditorState['candidates'] } | null = null;
|
|
193
|
+
|
|
194
|
+
const candidatesFor = (view: TaskEditorView, id: Id): TaskEditorState['candidates'] => {
|
|
195
|
+
if (candidatesCache?.tasks === view.project.tasks && candidatesCache.taskId === id)
|
|
196
|
+
return candidatesCache.list;
|
|
197
|
+
// Not itself, its ancestors or its descendants: links between a parent and its children are refused.
|
|
198
|
+
const excluded = new Set<Id>([id, ...view.tree.ancestors(id), ...view.tree.descendants(id)]);
|
|
199
|
+
const list = view.tree
|
|
200
|
+
.flatten()
|
|
201
|
+
.filter((each) => !excluded.has(each))
|
|
202
|
+
.map((each) => ({
|
|
203
|
+
id: each,
|
|
204
|
+
name: view.project.tasks.byId.get(each)?.name ?? '',
|
|
205
|
+
depth: view.tree.depth(each),
|
|
206
|
+
}));
|
|
207
|
+
candidatesCache = { tasks: view.project.tasks, taskId: id, list };
|
|
208
|
+
return list;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const dependencyRows = (view: TaskEditorView, id: Id, side: TaskEditorSide): DraftDependency[] => {
|
|
212
|
+
const rows: DraftDependency[] = [];
|
|
213
|
+
for (const dependency of view.project.dependencies.byId.values()) {
|
|
214
|
+
const mine = side === 'predecessors' ? dependency.to === id : dependency.from === id;
|
|
215
|
+
if (!mine) continue;
|
|
216
|
+
const taskId = side === 'predecessors' ? dependency.from : dependency.to;
|
|
217
|
+
const lag = durationText(dependency.lag, dependency.lagUnit);
|
|
218
|
+
rows.push({
|
|
219
|
+
key: `d${String(nextKey++)}`,
|
|
220
|
+
dependencyId: dependency.id,
|
|
221
|
+
taskId,
|
|
222
|
+
type: dependency.type,
|
|
223
|
+
lag,
|
|
224
|
+
initial: { taskId, type: dependency.type, lag, value: dependency.lag, unit: dependency.lagUnit },
|
|
225
|
+
error: null,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return rows;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const current = (view: TaskEditorView): TaskEditorState | null => {
|
|
232
|
+
const task = draft && context.enabled() ? view.project.tasks.byId.get(draft.taskId) : undefined;
|
|
233
|
+
if (!draft || !task) {
|
|
234
|
+
draft = null;
|
|
235
|
+
shown = null;
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
// Fields nobody typed in follow the value (changed elsewhere, or an earlier save the app accepted).
|
|
239
|
+
const now = texts(task, view);
|
|
240
|
+
const { texts: typed, initial: loaded } = draft;
|
|
241
|
+
const followed = FIELDS.filter((field) => typed[field] === loaded[field] && now[field] !== loaded[field]);
|
|
242
|
+
if (followed.length > 0) {
|
|
243
|
+
const next = { ...draft.texts };
|
|
244
|
+
const initial = { ...draft.initial };
|
|
245
|
+
for (const field of followed) next[field] = initial[field] = now[field];
|
|
246
|
+
draft = { ...draft, texts: next, initial };
|
|
247
|
+
}
|
|
248
|
+
if (shown?.draft === draft && shown.task === task && shown.project === view.project) return shown.state;
|
|
249
|
+
|
|
250
|
+
const isParent = !view.tree.isLeaf(task.id);
|
|
251
|
+
const dates = taskDates(task);
|
|
252
|
+
const field = (name: TaskEditorField): TaskEditorFieldState => {
|
|
253
|
+
let disabled = false;
|
|
254
|
+
if (name === 'constraintDate') disabled = draft?.constraint === 'none';
|
|
255
|
+
else if (name !== 'name') {
|
|
256
|
+
const builtIn = name === 'percentDone' ? 'percentDone' : name;
|
|
257
|
+
disabled = editKind(builtIn, task, isParent, dates, view.dateField) === null;
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
text: draft?.texts[name] ?? '',
|
|
261
|
+
kind: kindOf(name, view),
|
|
262
|
+
error: draft?.errors[name] ?? null,
|
|
263
|
+
disabled,
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
const row = ({ key, dependencyId, taskId, type, lag, error }: DraftDependency): TaskEditorDependency => ({
|
|
267
|
+
key,
|
|
268
|
+
dependencyId,
|
|
269
|
+
taskId,
|
|
270
|
+
type,
|
|
271
|
+
lag,
|
|
272
|
+
error,
|
|
273
|
+
});
|
|
274
|
+
const state: TaskEditorState = {
|
|
275
|
+
taskId: task.id,
|
|
276
|
+
task,
|
|
277
|
+
tab: draft.tab,
|
|
278
|
+
fields: {
|
|
279
|
+
name: field('name'),
|
|
280
|
+
startDate: field('startDate'),
|
|
281
|
+
endDate: field('endDate'),
|
|
282
|
+
duration: field('duration'),
|
|
283
|
+
percentDone: field('percentDone'),
|
|
284
|
+
constraintDate: field('constraintDate'),
|
|
285
|
+
},
|
|
286
|
+
manuallyScheduled: draft.manual,
|
|
287
|
+
constraintType: draft.constraint,
|
|
288
|
+
predecessors: draft.predecessors.map(row),
|
|
289
|
+
successors: draft.successors.map(row),
|
|
290
|
+
candidates: candidatesFor(view, task.id),
|
|
291
|
+
error: draft.error,
|
|
292
|
+
};
|
|
293
|
+
shown = { draft, task, project: view.project, state };
|
|
294
|
+
return state;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const open = (id: Id): boolean => {
|
|
298
|
+
draft = null;
|
|
299
|
+
const view = context.view();
|
|
300
|
+
const task = context.enabled() ? view.project.tasks.byId.get(id) : undefined;
|
|
301
|
+
if (!task) {
|
|
302
|
+
context.show();
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
const initial = texts(task, view);
|
|
306
|
+
draft = {
|
|
307
|
+
taskId: id,
|
|
308
|
+
tab: 'general',
|
|
309
|
+
texts: initial,
|
|
310
|
+
initial,
|
|
311
|
+
errors: {},
|
|
312
|
+
manual: task.manuallyScheduled,
|
|
313
|
+
constraint: task.constraintType === 'startnoearlierthan' ? 'startnoearlierthan' : 'none',
|
|
314
|
+
predecessors: dependencyRows(view, id, 'predecessors'),
|
|
315
|
+
successors: dependencyRows(view, id, 'successors'),
|
|
316
|
+
removed: [],
|
|
317
|
+
error: null,
|
|
318
|
+
};
|
|
319
|
+
context.show();
|
|
320
|
+
return true;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const set = (next: Draft): true => {
|
|
324
|
+
draft = next;
|
|
325
|
+
context.show();
|
|
326
|
+
return true;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const save = (): boolean => {
|
|
330
|
+
if (!draft) return false;
|
|
331
|
+
const view = context.view();
|
|
332
|
+
const task = view.project.tasks.byId.get(draft.taskId);
|
|
333
|
+
const state = task ? current(view) : null;
|
|
334
|
+
if (!task || !state) return false;
|
|
335
|
+
const settings = view.project.settings;
|
|
336
|
+
const zone = settings.timeZone;
|
|
337
|
+
const errors: Partial<Record<TaskEditorField, string>> = {};
|
|
338
|
+
const changes: ((tx: Transaction) => void)[] = [];
|
|
339
|
+
const id = task.id;
|
|
340
|
+
|
|
341
|
+
if (draft.manual !== task.manuallyScheduled) {
|
|
342
|
+
const manual = draft.manual;
|
|
343
|
+
changes.push((tx) => {
|
|
344
|
+
tx.tasks.update(id, { manuallyScheduled: manual });
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
// Read against the task as it will be scheduled (a start on a task switched to manual is its start date).
|
|
348
|
+
const asScheduled = { ...task, manuallyScheduled: draft.manual };
|
|
349
|
+
const dates = taskDates(task);
|
|
350
|
+
const typed = (name: TaskEditorField) =>
|
|
351
|
+
!state.fields[name].disabled && (draft as Draft).texts[name] !== (draft as Draft).initial[name];
|
|
352
|
+
// Each decides the end: which one to keep would be a guess.
|
|
353
|
+
if (typed('duration') && typed('endDate')) errors.endDate = 'Change the duration or the end, not both.';
|
|
354
|
+
for (const name of SAVE_ORDER) {
|
|
355
|
+
if (!typed(name)) continue;
|
|
356
|
+
const parsed = parseEdit(
|
|
357
|
+
name,
|
|
358
|
+
state.fields[name].kind,
|
|
359
|
+
draft.texts[name],
|
|
360
|
+
asScheduled,
|
|
361
|
+
dates,
|
|
362
|
+
settings,
|
|
363
|
+
);
|
|
364
|
+
if ('error' in parsed) errors[name] = parsed.error;
|
|
365
|
+
else changes.push(parsed.change);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const constraintChanged =
|
|
369
|
+
draft.constraint !== (task.constraintType === 'startnoearlierthan' ? 'startnoearlierthan' : 'none');
|
|
370
|
+
if (constraintChanged || draft.texts.constraintDate !== draft.initial.constraintDate) {
|
|
371
|
+
if (typed('startDate') && !draft.manual) {
|
|
372
|
+
// An automatic task's start is its constraint: both changed contradict each other.
|
|
373
|
+
errors.constraintDate = 'Change the start or the constraint, not both.';
|
|
374
|
+
} else if (draft.constraint === 'none') {
|
|
375
|
+
changes.push((tx) => {
|
|
376
|
+
tx.tasks.update(id, { constraintType: null, constraintDate: null });
|
|
377
|
+
});
|
|
378
|
+
} else {
|
|
379
|
+
const wall = parseInputDate(draft.texts.constraintDate, view.dateField);
|
|
380
|
+
if (typeof wall === 'string') errors.constraintDate = wall;
|
|
381
|
+
else {
|
|
382
|
+
const date = fromWallTime(wall, zone);
|
|
383
|
+
changes.push((tx) => {
|
|
384
|
+
tx.tasks.update(id, { constraintType: 'startnoearlierthan', constraintDate: date });
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
let failed = Object.keys(errors).length > 0;
|
|
391
|
+
const checkSide = (side: TaskEditorSide): DraftDependency[] => {
|
|
392
|
+
const seen = new Set<Id>();
|
|
393
|
+
return (draft as Draft)[side].map((row) => {
|
|
394
|
+
let error: string | null = null;
|
|
395
|
+
const initial = row.initial;
|
|
396
|
+
const lag =
|
|
397
|
+
row.lag === initial?.lag ? { value: initial.value, unit: initial.unit } : parseLag(row.lag, view);
|
|
398
|
+
if (row.taskId === null || !view.project.tasks.byId.has(row.taskId)) error = 'Choose a task.';
|
|
399
|
+
else if (seen.has(row.taskId)) error = 'This task is linked twice.';
|
|
400
|
+
else if (typeof lag === 'string') error = lag;
|
|
401
|
+
if (row.taskId !== null) seen.add(row.taskId);
|
|
402
|
+
if (error) failed = true;
|
|
403
|
+
else if (
|
|
404
|
+
row.initial?.taskId !== row.taskId ||
|
|
405
|
+
row.initial.type !== row.type ||
|
|
406
|
+
row.initial.lag !== row.lag
|
|
407
|
+
) {
|
|
408
|
+
const other = row.taskId as Id;
|
|
409
|
+
const { value, unit } = lag as { value: number; unit: TimeUnit };
|
|
410
|
+
const ends = side === 'predecessors' ? { from: other, to: id } : { from: id, to: other };
|
|
411
|
+
const fields = { ...ends, type: row.type, lag: value, lagUnit: unit };
|
|
412
|
+
const dependencyId = row.dependencyId;
|
|
413
|
+
changes.push((tx) => {
|
|
414
|
+
if (dependencyId === null) tx.dependencies.add(fields);
|
|
415
|
+
else tx.dependencies.update(dependencyId, fields);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
return row.error === error ? row : { ...row, error };
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
const predecessors = checkSide('predecessors');
|
|
422
|
+
const successors = checkSide('successors');
|
|
423
|
+
for (const dependencyId of draft.removed) {
|
|
424
|
+
changes.unshift((tx) => {
|
|
425
|
+
if (tx.dependencies.get(dependencyId)) tx.dependencies.remove(dependencyId);
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
if (failed) return !set({ ...draft, errors, predecessors, successors, error: null });
|
|
429
|
+
const refuse = (message: { field: TaskEditorField; text: string } | string) =>
|
|
430
|
+
!set({
|
|
431
|
+
...(draft as Draft),
|
|
432
|
+
errors: typeof message === 'string' ? errors : { ...errors, [message.field]: message.text },
|
|
433
|
+
predecessors,
|
|
434
|
+
successors,
|
|
435
|
+
error: typeof message === 'string' ? message : null,
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
const fn = (tx: Transaction) => {
|
|
439
|
+
for (const change of changes) change(tx);
|
|
440
|
+
};
|
|
441
|
+
try {
|
|
442
|
+
if (typed('endDate')) {
|
|
443
|
+
// The end is read against where the other changes put the task (a new start moves it).
|
|
444
|
+
const moved = changes.length > 0 ? context.plan(fn)?.state.tasks.byId.get(id) : undefined;
|
|
445
|
+
const base = { ...(moved ?? task), manuallyScheduled: draft.manual };
|
|
446
|
+
const kind = state.fields.endDate.kind;
|
|
447
|
+
const end = parseEdit('endDate', kind, draft.texts.endDate, base, taskDates(base), settings);
|
|
448
|
+
if ('error' in end) return refuse({ field: 'endDate', text: end.error });
|
|
449
|
+
// The duration comes from the start to the end: the new start (an automatic task's is only its constraint
|
|
450
|
+
// until it's scheduled).
|
|
451
|
+
const start = taskDates(base)?.start;
|
|
452
|
+
if (moved && start !== undefined) {
|
|
453
|
+
changes.push((tx) => {
|
|
454
|
+
tx.tasks.update(id, { startDate: start });
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
changes.push(end.change);
|
|
458
|
+
}
|
|
459
|
+
if (changes.length > 0 && context.plan(fn)) context.commit(fn);
|
|
460
|
+
} catch (error) {
|
|
461
|
+
if (!(error instanceof QuartzioError)) throw error;
|
|
462
|
+
return refuse(error.message);
|
|
463
|
+
}
|
|
464
|
+
draft = null;
|
|
465
|
+
context.show();
|
|
466
|
+
return true;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
const updateSide = (
|
|
470
|
+
side: TaskEditorSide,
|
|
471
|
+
update: (rows: readonly DraftDependency[]) => DraftDependency[] | null,
|
|
472
|
+
) => {
|
|
473
|
+
if (!draft) return false;
|
|
474
|
+
const rows = update(draft[side]);
|
|
475
|
+
return rows ? set({ ...draft, [side]: rows, error: null }) : false;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const act = (action: TaskEditorAction): boolean => {
|
|
479
|
+
if (!draft || typeof action !== 'object' || (action as unknown) === null) return false;
|
|
480
|
+
if ('side' in action && action.side !== 'predecessors' && (action.side as unknown) !== 'successors')
|
|
481
|
+
return false;
|
|
482
|
+
switch (action.type) {
|
|
483
|
+
case 'input': {
|
|
484
|
+
const { field, value } = action;
|
|
485
|
+
if (field === 'manuallyScheduled')
|
|
486
|
+
return typeof value === 'boolean' ? set({ ...draft, manual: value, error: null }) : false;
|
|
487
|
+
if (field === 'constraintType') {
|
|
488
|
+
const constraint: unknown = value;
|
|
489
|
+
if (constraint !== 'none' && constraint !== 'startnoearlierthan') return false;
|
|
490
|
+
const { constraintDate: _cleared, ...rest } = draft.errors;
|
|
491
|
+
return set({ ...draft, constraint: value, errors: rest, error: null });
|
|
492
|
+
}
|
|
493
|
+
if (!FIELDS.includes(field) || typeof value !== 'string') return false;
|
|
494
|
+
const { [field]: _cleared, ...errors } = draft.errors;
|
|
495
|
+
return set({ ...draft, texts: { ...draft.texts, [field]: value }, errors, error: null });
|
|
496
|
+
}
|
|
497
|
+
case 'tab':
|
|
498
|
+
return TABS.has(action.tab) ? set({ ...draft, tab: action.tab }) : false;
|
|
499
|
+
case 'addDependency':
|
|
500
|
+
return updateSide(action.side, (rows) => [
|
|
501
|
+
...rows,
|
|
502
|
+
{
|
|
503
|
+
key: `n${String(nextKey++)}`,
|
|
504
|
+
dependencyId: null,
|
|
505
|
+
taskId: null,
|
|
506
|
+
type: 'FS',
|
|
507
|
+
lag: '0d',
|
|
508
|
+
initial: null,
|
|
509
|
+
error: null,
|
|
510
|
+
},
|
|
511
|
+
]);
|
|
512
|
+
case 'updateDependency': {
|
|
513
|
+
const changes: unknown = action.changes;
|
|
514
|
+
if (typeof changes !== 'object' || changes === null) return false;
|
|
515
|
+
const { taskId, type, lag } = changes as Record<string, unknown>;
|
|
516
|
+
return updateSide(action.side, (rows) => {
|
|
517
|
+
if (!rows.some((row) => row.key === action.key)) return null;
|
|
518
|
+
if (type !== undefined && !TYPES.has(type as DependencyType)) return null;
|
|
519
|
+
if (lag !== undefined && typeof lag !== 'string') return null;
|
|
520
|
+
return rows.map((row) =>
|
|
521
|
+
row.key !== action.key
|
|
522
|
+
? row
|
|
523
|
+
: {
|
|
524
|
+
...row,
|
|
525
|
+
...(taskId === undefined ? {} : { taskId: taskId as Id | null }),
|
|
526
|
+
...(type === undefined ? {} : { type: type as DependencyType }),
|
|
527
|
+
...(lag === undefined ? {} : { lag: lag }),
|
|
528
|
+
error: null,
|
|
529
|
+
},
|
|
530
|
+
);
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
case 'removeDependency': {
|
|
534
|
+
const removed = draft[action.side].find((row) => row.key === action.key);
|
|
535
|
+
if (!removed) return false;
|
|
536
|
+
const dependencyId = removed.dependencyId;
|
|
537
|
+
const withRemoved =
|
|
538
|
+
dependencyId === null ? draft : { ...draft, removed: [...draft.removed, dependencyId] };
|
|
539
|
+
draft = withRemoved;
|
|
540
|
+
return updateSide(action.side, (rows) => rows.filter((row) => row.key !== action.key));
|
|
541
|
+
}
|
|
542
|
+
case 'save':
|
|
543
|
+
return save();
|
|
544
|
+
case 'close':
|
|
545
|
+
draft = null;
|
|
546
|
+
context.show();
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
return false;
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
return { current, open, act };
|
|
553
|
+
}
|