@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,162 @@
|
|
|
1
|
+
import type { PlannedChange, Project } from '../data/project';
|
|
2
|
+
import { toProjectData } from '../data/serialize';
|
|
3
|
+
import type { Transaction } from '../data/transaction';
|
|
4
|
+
import type { Operation, Patch, ProjectData, ProjectInput, ProjectState } from '../data/types';
|
|
5
|
+
import type { History, HistoryAction } from './history';
|
|
6
|
+
import type { GanttDataChange } from './types';
|
|
7
|
+
|
|
8
|
+
export interface DataBinding {
|
|
9
|
+
/** Controlled mode: new data from the app. */
|
|
10
|
+
setData: (data: ProjectInput | undefined) => void;
|
|
11
|
+
transact: (fn: (tx: Transaction) => void) => Patch | null;
|
|
12
|
+
/** Applies operations as one change (undo and redo), recorded in the history as `action`. */
|
|
13
|
+
apply: (operations: readonly Operation[], action: (patch: Patch) => HistoryAction) => Patch | null;
|
|
14
|
+
/**
|
|
15
|
+
* Controlled mode: whether an edit reported in this synchronous run is still waiting for the app. The history
|
|
16
|
+
* doesn't show it yet, so undo and redo would pick the wrong step.
|
|
17
|
+
*/
|
|
18
|
+
awaiting: () => boolean;
|
|
19
|
+
setOnChange: (onChange: ((change: GanttDataChange) => void) | undefined) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Reports what scheduling changed in the initial data, after a microtask. Called when the controller gets
|
|
22
|
+
* its first subscriber: a renderer took it into use (React may create controllers it then throws away).
|
|
23
|
+
*/
|
|
24
|
+
start: () => void;
|
|
25
|
+
/** Stops reporting (a pending report of the initial scheduling is dropped). */
|
|
26
|
+
destroy: () => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Connects the project to the app's data: uncontrolled (the engine keeps edits) or controlled (edits are
|
|
31
|
+
* only reported, and shown once the app passes new data back in).
|
|
32
|
+
*
|
|
33
|
+
* Loaded data is shown completed (scheduled). What that changed is reported through onChange too, so the app
|
|
34
|
+
* can keep the scheduled data: right away for data passed in later; for the initial data (`initialPatch`) once
|
|
35
|
+
* `start` is called, after a microtask, since the controller is usually created during rendering.
|
|
36
|
+
*/
|
|
37
|
+
export function createDataBinding(
|
|
38
|
+
project: Project,
|
|
39
|
+
controlled: boolean,
|
|
40
|
+
initialData: ProjectInput | undefined,
|
|
41
|
+
initialOnChange: ((change: GanttDataChange) => void) | undefined,
|
|
42
|
+
initialPatch: Patch | null,
|
|
43
|
+
history: Pick<History, 'confirm' | 'clear'>,
|
|
44
|
+
): DataBinding {
|
|
45
|
+
let onChange = initialOnChange;
|
|
46
|
+
let currentData = initialData;
|
|
47
|
+
// Set once anything newer than the initial data was reported or loaded: its report is then out of date.
|
|
48
|
+
let superseded = false;
|
|
49
|
+
let destroyed = false;
|
|
50
|
+
const report = (change: GanttDataChange): void => {
|
|
51
|
+
superseded = true;
|
|
52
|
+
if (!destroyed) onChange?.(change);
|
|
53
|
+
};
|
|
54
|
+
// Controlled mode: the last change reported through onChange, relative to the committed state.
|
|
55
|
+
// If exactly its data comes back, the operations are replayed instead of re-normalizing everything.
|
|
56
|
+
// Its history actions take effect when the data comes back (the app accepted the change).
|
|
57
|
+
let pending:
|
|
58
|
+
{ state: ProjectState; data: ProjectData; operations: Operation[]; actions: HistoryAction[] } | undefined;
|
|
59
|
+
// Edits in the same synchronous run (e.g. two transact() calls in one event handler) build on each
|
|
60
|
+
// other. Later edits build on the last data passed in, so a change the app rejected (by not passing
|
|
61
|
+
// it back) is dropped instead of sneaking into the next one — like a controlled <input>.
|
|
62
|
+
let chaining = false;
|
|
63
|
+
|
|
64
|
+
/** Reports what completing loaded data changed; controlled, that data coming back is recognized. */
|
|
65
|
+
const reportLoaded = (patch: Patch): void => {
|
|
66
|
+
const data = toProjectData(project.getState());
|
|
67
|
+
if (controlled) pending = { state: project.getState(), data, operations: [], actions: [] };
|
|
68
|
+
report({ patch, data, source: 'load' });
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
let started = false;
|
|
72
|
+
|
|
73
|
+
/** One change: controlled, planned and reported; uncontrolled, applied and reported. */
|
|
74
|
+
const change = (
|
|
75
|
+
plan: (base: ProjectState | undefined) => PlannedChange | null,
|
|
76
|
+
apply: () => Patch | null,
|
|
77
|
+
action: (patch: Patch) => HistoryAction,
|
|
78
|
+
): Patch | null => {
|
|
79
|
+
if (controlled) {
|
|
80
|
+
const previous = chaining ? pending : undefined;
|
|
81
|
+
const planned = plan(previous?.state);
|
|
82
|
+
if (!planned) return null;
|
|
83
|
+
const done = action(planned.patch);
|
|
84
|
+
pending = {
|
|
85
|
+
state: planned.state,
|
|
86
|
+
data: toProjectData(planned.state),
|
|
87
|
+
operations: [...(previous?.operations ?? []), ...planned.patch.operations],
|
|
88
|
+
actions: [...(previous?.actions ?? []), done],
|
|
89
|
+
};
|
|
90
|
+
if (!chaining) {
|
|
91
|
+
chaining = true;
|
|
92
|
+
void Promise.resolve().then(() => {
|
|
93
|
+
chaining = false;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
report({ patch: planned.patch, data: pending.data, source: done.type });
|
|
97
|
+
return planned.patch;
|
|
98
|
+
}
|
|
99
|
+
const patch = apply();
|
|
100
|
+
if (patch) {
|
|
101
|
+
const done = action(patch);
|
|
102
|
+
history.confirm([done]);
|
|
103
|
+
report({ patch, data: project.toData(), source: done.type });
|
|
104
|
+
}
|
|
105
|
+
return patch;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
setData(data) {
|
|
110
|
+
if (data === currentData) return;
|
|
111
|
+
let patch: Patch | null = null;
|
|
112
|
+
const accepted = pending !== undefined && pending.data === data ? pending : undefined;
|
|
113
|
+
if (accepted) project.apply(accepted.operations);
|
|
114
|
+
else patch = project.load(data ?? {});
|
|
115
|
+
// Only after loading succeeded, so passing the same (fixed) data again is not skipped, and rejected data
|
|
116
|
+
// doesn't cancel the initial report.
|
|
117
|
+
superseded = true;
|
|
118
|
+
currentData = data;
|
|
119
|
+
pending = undefined;
|
|
120
|
+
// The reported changes happened; other data makes the history meaningless.
|
|
121
|
+
if (accepted) history.confirm(accepted.actions);
|
|
122
|
+
else history.clear();
|
|
123
|
+
if (patch) reportLoaded(patch);
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
transact(fn) {
|
|
127
|
+
return change(
|
|
128
|
+
(base) => project.plan(fn, base),
|
|
129
|
+
() => project.transact(fn),
|
|
130
|
+
(patch) => ({ type: 'edit', patch }),
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
apply(operations, action) {
|
|
135
|
+
return change(
|
|
136
|
+
(base) => project.planApply(operations, base),
|
|
137
|
+
() => project.apply(operations),
|
|
138
|
+
action,
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
awaiting() {
|
|
143
|
+
return chaining && pending !== undefined && pending.actions.length > 0;
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
setOnChange(next) {
|
|
147
|
+
onChange = next;
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
start() {
|
|
151
|
+
if (started || !initialPatch) return;
|
|
152
|
+
started = true;
|
|
153
|
+
void Promise.resolve().then(() => {
|
|
154
|
+
if (!superseded) reportLoaded(initialPatch);
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
destroy() {
|
|
159
|
+
destroyed = true;
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import type { PlannedChange } from '../data/project';
|
|
2
|
+
import type { Transaction } from '../data/transaction';
|
|
3
|
+
import type { TreeIndex } from '../data/tree';
|
|
4
|
+
import type { Id, ProjectState } from '../data/types';
|
|
5
|
+
import { QuartzioError } from '../util/errors';
|
|
6
|
+
import type { ResolvedColumns } from './columns';
|
|
7
|
+
import { taskDates } from './dates';
|
|
8
|
+
import { editKind, initialText, parseEdit, type CellEdit, type EditKind } from './editing';
|
|
9
|
+
import type { ProposedChange } from './interaction';
|
|
10
|
+
import type { KeyInput } from './keyboard';
|
|
11
|
+
import { rowKey } from './rows';
|
|
12
|
+
|
|
13
|
+
/** What the editor needs from the view. */
|
|
14
|
+
export interface EditorView {
|
|
15
|
+
readonly project: ProjectState;
|
|
16
|
+
readonly tree: TreeIndex;
|
|
17
|
+
readonly rowIds: readonly Id[];
|
|
18
|
+
readonly rowIndex: ReadonlyMap<Id, number>;
|
|
19
|
+
readonly columns: ResolvedColumns;
|
|
20
|
+
readonly dateField: 'date' | 'datetime';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface EditorContext {
|
|
24
|
+
readonly view: () => EditorView;
|
|
25
|
+
readonly enabled: () => boolean;
|
|
26
|
+
/** Plans a change without applying it (to validate what it would do); throws QuartzioError if refused. */
|
|
27
|
+
readonly plan: (fn: (tx: Transaction) => void) => PlannedChange | null;
|
|
28
|
+
/** Applies (or, controlled, reports) the change; throws QuartzioError if refused. */
|
|
29
|
+
readonly commit: (fn: (tx: Transaction) => void) => void;
|
|
30
|
+
readonly validate: (change: ProposedChange) => boolean | string;
|
|
31
|
+
/** The edit changed: show it. */
|
|
32
|
+
readonly show: () => void;
|
|
33
|
+
/** Editing starts on a row: move the cursor there. */
|
|
34
|
+
readonly focusRow: (id: Id) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CellEditor {
|
|
38
|
+
/** The open edit as the view shows it; drops an edit on a stale basis. The same object while unchanged. */
|
|
39
|
+
readonly current: (view: EditorView) => CellEdit | null;
|
|
40
|
+
readonly start: (id: Id, columnId?: string) => boolean;
|
|
41
|
+
readonly input: (text: string) => void;
|
|
42
|
+
readonly keyDown: (key: KeyInput) => boolean;
|
|
43
|
+
readonly commit: () => boolean;
|
|
44
|
+
readonly cancel: () => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface Open {
|
|
48
|
+
readonly taskId: Id;
|
|
49
|
+
readonly columnId: string;
|
|
50
|
+
readonly kind: EditKind;
|
|
51
|
+
readonly text: string;
|
|
52
|
+
/** The value's text: while `text` is still this, nothing was typed (and the field follows the value). */
|
|
53
|
+
readonly initial: string;
|
|
54
|
+
readonly error: string | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A cell of the task list, if it can be edited: the field it edits and how. */
|
|
58
|
+
function cellOf(view: EditorView, id: Id, columnIndex: number) {
|
|
59
|
+
const task = view.project.tasks.byId.get(id);
|
|
60
|
+
const column = view.columns.state.items[columnIndex];
|
|
61
|
+
const field = view.columns.fields[columnIndex] ?? null;
|
|
62
|
+
if (!task || !column || field === null || !view.rowIndex.has(id)) return null;
|
|
63
|
+
const dates = taskDates(task);
|
|
64
|
+
const kind = editKind(field, task, !view.tree.isLeaf(id), dates, view.dateField);
|
|
65
|
+
return kind ? { task, column, field, dates, kind } : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const columnIndexOf = (view: EditorView, columnId: string) =>
|
|
69
|
+
view.columns.state.items.findIndex((column) => column.id === columnId);
|
|
70
|
+
|
|
71
|
+
/** Editing cells in the task list. */
|
|
72
|
+
export function createCellEditor(context: EditorContext): CellEditor {
|
|
73
|
+
let open: Open | null = null;
|
|
74
|
+
let shown: CellEdit | null = null;
|
|
75
|
+
|
|
76
|
+
const current = (view: EditorView): CellEdit | null => {
|
|
77
|
+
const cell =
|
|
78
|
+
open && context.enabled() ? cellOf(view, open.taskId, columnIndexOf(view, open.columnId)) : null;
|
|
79
|
+
// Stale: the cell can't be edited now, or needs another kind of field (its text would not fit it).
|
|
80
|
+
if (!open || cell?.kind !== open.kind) {
|
|
81
|
+
open = null;
|
|
82
|
+
shown = null;
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (open.text === open.initial) {
|
|
86
|
+
// Nothing typed: show the value as it is now (changed elsewhere, or the app accepted an earlier edit).
|
|
87
|
+
const value = initialText(cell.field, cell.kind, cell.task, cell.dates, view.project.settings.timeZone);
|
|
88
|
+
if (value !== open.initial) open = { ...open, text: value, initial: value };
|
|
89
|
+
}
|
|
90
|
+
const { taskId, columnId, kind, text, error } = open;
|
|
91
|
+
if (
|
|
92
|
+
shown?.taskId !== taskId ||
|
|
93
|
+
shown.columnId !== columnId ||
|
|
94
|
+
shown.kind !== kind ||
|
|
95
|
+
shown.text !== text ||
|
|
96
|
+
shown.error !== error
|
|
97
|
+
) {
|
|
98
|
+
shown = { taskId, rowKey: rowKey(taskId), columnId, kind, text, error };
|
|
99
|
+
}
|
|
100
|
+
return shown;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const start = (id: Id, columnId?: string): boolean => {
|
|
104
|
+
const wasOpen = open !== null;
|
|
105
|
+
open = null;
|
|
106
|
+
const view = context.view();
|
|
107
|
+
let cell = null;
|
|
108
|
+
if (context.enabled()) {
|
|
109
|
+
if (columnId === undefined) {
|
|
110
|
+
for (let index = 0; index < view.columns.state.items.length && !cell; index++)
|
|
111
|
+
cell = cellOf(view, id, index);
|
|
112
|
+
} else if (typeof columnId === 'string') {
|
|
113
|
+
cell = cellOf(view, id, columnIndexOf(view, columnId));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (!cell) {
|
|
117
|
+
if (wasOpen) context.show();
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const text = initialText(cell.field, cell.kind, cell.task, cell.dates, view.project.settings.timeZone);
|
|
121
|
+
open = { taskId: id, columnId: cell.column.id, kind: cell.kind, text, initial: text, error: null };
|
|
122
|
+
context.focusRow(id);
|
|
123
|
+
context.show();
|
|
124
|
+
return true;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const refuse = (error: string): false => {
|
|
128
|
+
if (open) open = { ...open, error };
|
|
129
|
+
context.show();
|
|
130
|
+
return false;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const commit = (): boolean => {
|
|
134
|
+
if (!open) return false;
|
|
135
|
+
const view = context.view();
|
|
136
|
+
const cell = cellOf(view, open.taskId, columnIndexOf(view, open.columnId));
|
|
137
|
+
if (!cell) {
|
|
138
|
+
open = null;
|
|
139
|
+
context.show();
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
const { text } = open;
|
|
143
|
+
const close = (): true => {
|
|
144
|
+
open = null;
|
|
145
|
+
context.show();
|
|
146
|
+
return true;
|
|
147
|
+
};
|
|
148
|
+
if (text === open.initial) return close();
|
|
149
|
+
const parsed = parseEdit(cell.field, cell.kind, text, cell.task, cell.dates, view.project.settings);
|
|
150
|
+
if ('error' in parsed) return refuse(parsed.error);
|
|
151
|
+
try {
|
|
152
|
+
const planned = context.plan(parsed.change);
|
|
153
|
+
if (!planned) return close();
|
|
154
|
+
const after = planned.state.tasks.byId.get(cell.task.id);
|
|
155
|
+
const proposed = after ? parsed.propose(cell.task, taskDates(after)) : null;
|
|
156
|
+
if (proposed) {
|
|
157
|
+
let verdict: boolean | string;
|
|
158
|
+
try {
|
|
159
|
+
verdict = context.validate(proposed);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
// A validator that throws is a no, with its message.
|
|
162
|
+
verdict = error instanceof Error ? error.message : 'Not allowed.';
|
|
163
|
+
}
|
|
164
|
+
if (verdict !== true) return refuse(typeof verdict === 'string' ? verdict : 'Not allowed.');
|
|
165
|
+
}
|
|
166
|
+
const edited = open;
|
|
167
|
+
open = null;
|
|
168
|
+
try {
|
|
169
|
+
context.commit(parsed.change);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
open = edited;
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
context.show();
|
|
175
|
+
return true;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
// The data refusing the change (e.g. an unreasonable duration): shown in the field.
|
|
178
|
+
if (error instanceof QuartzioError) return refuse(error.message);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** The next (or previous) editable cell after the open one, across rows. */
|
|
184
|
+
const neighbour = (from: Open, step: 1 | -1): { id: Id; columnId: string } | null => {
|
|
185
|
+
const view = context.view();
|
|
186
|
+
const columns = view.columns.state.items.length;
|
|
187
|
+
let row = view.rowIndex.get(from.taskId);
|
|
188
|
+
let column = columnIndexOf(view, from.columnId);
|
|
189
|
+
if (row === undefined || column < 0) return null;
|
|
190
|
+
for (;;) {
|
|
191
|
+
column += step;
|
|
192
|
+
if (column < 0 || column >= columns) {
|
|
193
|
+
row += step;
|
|
194
|
+
column = step === 1 ? 0 : columns - 1;
|
|
195
|
+
}
|
|
196
|
+
const id = view.rowIds[row];
|
|
197
|
+
if (id === undefined) return null;
|
|
198
|
+
const cell = cellOf(view, id, column);
|
|
199
|
+
if (cell) return { id, columnId: cell.column.id };
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
current,
|
|
205
|
+
start,
|
|
206
|
+
commit,
|
|
207
|
+
input(text) {
|
|
208
|
+
if (!open || typeof text !== 'string') return;
|
|
209
|
+
open = { ...open, text, error: null };
|
|
210
|
+
context.show();
|
|
211
|
+
},
|
|
212
|
+
keyDown(key) {
|
|
213
|
+
if (!open || typeof key !== 'object' || (key as unknown) === null) return false;
|
|
214
|
+
switch (key.key) {
|
|
215
|
+
case 'Escape':
|
|
216
|
+
open = null;
|
|
217
|
+
context.show();
|
|
218
|
+
return true;
|
|
219
|
+
case 'Enter':
|
|
220
|
+
commit();
|
|
221
|
+
return true;
|
|
222
|
+
case 'Tab': {
|
|
223
|
+
const from = open;
|
|
224
|
+
if (commit()) {
|
|
225
|
+
const next = neighbour(from, key.shift === true ? -1 : 1);
|
|
226
|
+
if (next) start(next.id, next.columnId);
|
|
227
|
+
}
|
|
228
|
+
return true; // focus stays in the chart
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
},
|
|
233
|
+
cancel() {
|
|
234
|
+
if (!open) return;
|
|
235
|
+
open = null;
|
|
236
|
+
context.show();
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { TimeUnit } from '../util/time';
|
|
2
|
+
import { startOfUnit, type TimeZone } from '../util/zone';
|
|
3
|
+
|
|
4
|
+
const cache = new Map<string, Intl.DateTimeFormat | Intl.NumberFormat>();
|
|
5
|
+
|
|
6
|
+
function cached<F extends Intl.DateTimeFormat | Intl.NumberFormat>(key: string, create: () => F): F {
|
|
7
|
+
let formatter = cache.get(key) as F | undefined;
|
|
8
|
+
if (!formatter) cache.set(key, (formatter = create()));
|
|
9
|
+
return formatter;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function formatDate(time: number, zone: TimeZone, locale: string | undefined): string {
|
|
13
|
+
return cached(
|
|
14
|
+
`date|${locale ?? ''}|${zone}`,
|
|
15
|
+
() =>
|
|
16
|
+
new Intl.DateTimeFormat(
|
|
17
|
+
locale,
|
|
18
|
+
zone === 'local' ? { dateStyle: 'medium' } : { dateStyle: 'medium', timeZone: zone },
|
|
19
|
+
),
|
|
20
|
+
).format(time);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function formatDateTime(time: number, zone: TimeZone, locale: string | undefined): string {
|
|
24
|
+
return cached(
|
|
25
|
+
`datetime|${locale ?? ''}|${zone}`,
|
|
26
|
+
() =>
|
|
27
|
+
new Intl.DateTimeFormat(
|
|
28
|
+
locale,
|
|
29
|
+
zone === 'local'
|
|
30
|
+
? { dateStyle: 'medium', timeStyle: 'short' }
|
|
31
|
+
: { dateStyle: 'medium', timeStyle: 'short', timeZone: zone },
|
|
32
|
+
),
|
|
33
|
+
).format(time);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* End dates are exclusive: a task on 5–8 October ends at midnight on the 9th. Shown as a date, that reads
|
|
38
|
+
* as one day too many, so an end exactly at midnight is shown as the day before (like MS Project).
|
|
39
|
+
*/
|
|
40
|
+
export function formatEndDate(
|
|
41
|
+
start: number,
|
|
42
|
+
end: number,
|
|
43
|
+
zone: TimeZone,
|
|
44
|
+
locale: string | undefined,
|
|
45
|
+
): string {
|
|
46
|
+
const atMidnight = end > start && startOfUnit(end, 'day', zone) === end;
|
|
47
|
+
return formatDate(atMidnight ? end - 1 : end, zone, locale);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Units Intl.NumberFormat can spell out in every locale.
|
|
51
|
+
const INTL_UNITS: Partial<Record<TimeUnit, string>> = {
|
|
52
|
+
millisecond: 'millisecond',
|
|
53
|
+
second: 'second',
|
|
54
|
+
minute: 'minute',
|
|
55
|
+
hour: 'hour',
|
|
56
|
+
day: 'day',
|
|
57
|
+
week: 'week',
|
|
58
|
+
month: 'month',
|
|
59
|
+
year: 'year',
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export function formatDuration(value: number, unit: TimeUnit, locale: string | undefined): string {
|
|
63
|
+
const intlUnit = INTL_UNITS[unit];
|
|
64
|
+
if (intlUnit === undefined) {
|
|
65
|
+
// Quarters have no localized unit name.
|
|
66
|
+
const number = cached(
|
|
67
|
+
`number|${locale ?? ''}`,
|
|
68
|
+
() => new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }),
|
|
69
|
+
);
|
|
70
|
+
return `${number.format(value)} q`;
|
|
71
|
+
}
|
|
72
|
+
return cached(
|
|
73
|
+
`duration|${locale ?? ''}|${intlUnit}`,
|
|
74
|
+
() =>
|
|
75
|
+
new Intl.NumberFormat(locale, {
|
|
76
|
+
style: 'unit',
|
|
77
|
+
unit: intlUnit,
|
|
78
|
+
unitDisplay: 'long',
|
|
79
|
+
maximumFractionDigits: 2,
|
|
80
|
+
}),
|
|
81
|
+
).format(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function formatPercent(value: number, locale: string | undefined): string {
|
|
85
|
+
return cached(
|
|
86
|
+
`percent|${locale ?? ''}`,
|
|
87
|
+
() => new Intl.NumberFormat(locale, { style: 'percent', maximumFractionDigits: 0 }),
|
|
88
|
+
).format(value / 100);
|
|
89
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { workingMsPerUnit } from '../calendar/duration';
|
|
2
|
+
import type { WorkingCalendar } from '../calendar/workingCalendar';
|
|
3
|
+
import type { ProjectSettings, Task } from '../data/types';
|
|
4
|
+
import type { TaskDates } from './dates';
|
|
5
|
+
import { QuartzioError } from '../util/errors';
|
|
6
|
+
import { formatDate, formatDuration, formatEndDate, formatPercent } from './cells';
|
|
7
|
+
|
|
8
|
+
export type BuiltInColumnId = 'name' | 'startDate' | 'endDate' | 'duration' | 'percentDone';
|
|
9
|
+
|
|
10
|
+
/** What a cell's value function gets. */
|
|
11
|
+
export interface CellContext {
|
|
12
|
+
readonly task: Task;
|
|
13
|
+
/** Whether the task has children (its dates then span its descendants). */
|
|
14
|
+
readonly isParent: boolean;
|
|
15
|
+
/** Computed display dates, or `null` for unscheduled tasks. */
|
|
16
|
+
readonly dates: TaskDates | null;
|
|
17
|
+
readonly settings: ProjectSettings;
|
|
18
|
+
readonly locale: string | undefined;
|
|
19
|
+
/** The project calendar, e.g. to measure working time. Absent if it can't be used. */
|
|
20
|
+
readonly calendar: WorkingCalendar | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ColumnDefinition {
|
|
24
|
+
id: string;
|
|
25
|
+
title?: string;
|
|
26
|
+
/** Pixels. */
|
|
27
|
+
width?: number;
|
|
28
|
+
/** A built-in column to base this one on (its title, width and value). */
|
|
29
|
+
field?: BuiltInColumnId;
|
|
30
|
+
align?: 'start' | 'end';
|
|
31
|
+
/** Cell text. Must not throw (errors leave the cell empty). */
|
|
32
|
+
value?: (cell: CellContext) => string;
|
|
33
|
+
/**
|
|
34
|
+
* Whether cells can be edited (double-click, Enter/F2). Default `true` for columns with a `field` (the edit
|
|
35
|
+
* writes that field), and always `false` without one.
|
|
36
|
+
*/
|
|
37
|
+
editable?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ColumnInput = BuiltInColumnId | ColumnDefinition;
|
|
41
|
+
|
|
42
|
+
export interface Column {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly title: string;
|
|
45
|
+
readonly width: number;
|
|
46
|
+
/** Left edge within the task list. */
|
|
47
|
+
readonly x: number;
|
|
48
|
+
readonly align: 'start' | 'end';
|
|
49
|
+
/** The column that shows the tree (indentation and expand/collapse). */
|
|
50
|
+
readonly tree: boolean;
|
|
51
|
+
/** Its cells can be edited (some cells still can't, e.g. a parent's dates). */
|
|
52
|
+
readonly editable: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ColumnsState {
|
|
56
|
+
readonly items: readonly Column[];
|
|
57
|
+
/** Width of the task list. */
|
|
58
|
+
readonly totalWidth: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ResolvedColumns {
|
|
62
|
+
readonly state: ColumnsState;
|
|
63
|
+
readonly values: readonly ((cell: CellContext) => string)[];
|
|
64
|
+
/** The field each column edits, or `null` when it can't be edited. */
|
|
65
|
+
readonly fields: readonly (BuiltInColumnId | null)[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface BuiltIn {
|
|
69
|
+
title: string;
|
|
70
|
+
width: number;
|
|
71
|
+
align: 'start' | 'end';
|
|
72
|
+
value: (cell: CellContext) => string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const zoneOf = (cell: CellContext) => cell.settings.timeZone;
|
|
76
|
+
|
|
77
|
+
const BUILT_IN: Record<BuiltInColumnId, BuiltIn> = {
|
|
78
|
+
name: { title: 'Name', width: 240, align: 'start', value: ({ task }) => task.name },
|
|
79
|
+
startDate: {
|
|
80
|
+
title: 'Start',
|
|
81
|
+
width: 120,
|
|
82
|
+
align: 'start',
|
|
83
|
+
value: (cell) => (cell.dates ? formatDate(cell.dates.start, zoneOf(cell), cell.locale) : ''),
|
|
84
|
+
},
|
|
85
|
+
endDate: {
|
|
86
|
+
title: 'End',
|
|
87
|
+
width: 120,
|
|
88
|
+
align: 'start',
|
|
89
|
+
value: (cell) =>
|
|
90
|
+
cell.dates ? formatEndDate(cell.dates.start, cell.dates.end, zoneOf(cell), cell.locale) : '',
|
|
91
|
+
},
|
|
92
|
+
duration: {
|
|
93
|
+
title: 'Duration',
|
|
94
|
+
width: 100,
|
|
95
|
+
align: 'end',
|
|
96
|
+
value: ({ task, isParent, dates, settings, locale, calendar }) => {
|
|
97
|
+
// A parent's dates span its children, so its own duration would contradict the dates shown.
|
|
98
|
+
if (task.duration !== null && !isParent)
|
|
99
|
+
return formatDuration(task.duration, task.durationUnit, locale);
|
|
100
|
+
// Otherwise: the working time its dates span, in days.
|
|
101
|
+
if (!dates || !calendar) return '';
|
|
102
|
+
const days = calendar.workingTimeBetween(dates.start, dates.end) / workingMsPerUnit('day', settings);
|
|
103
|
+
return formatDuration(days, 'day', locale);
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
percentDone: {
|
|
107
|
+
title: '% Done',
|
|
108
|
+
width: 80,
|
|
109
|
+
align: 'end',
|
|
110
|
+
value: ({ task, locale }) => formatPercent(task.percentDone, locale),
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** A built-in column's text for a cell (also used by the task tooltip). */
|
|
115
|
+
export const builtInText = (field: BuiltInColumnId, cell: CellContext): string => BUILT_IN[field].value(cell);
|
|
116
|
+
|
|
117
|
+
export const DEFAULT_COLUMNS: readonly BuiltInColumnId[] = ['name', 'startDate', 'endDate', 'duration'];
|
|
118
|
+
|
|
119
|
+
const isBuiltIn = (value: unknown): value is BuiltInColumnId =>
|
|
120
|
+
typeof value === 'string' && Object.hasOwn(BUILT_IN, value);
|
|
121
|
+
|
|
122
|
+
/** Validates columns; throws QuartzioError for invalid definitions. */
|
|
123
|
+
export function resolveColumns(input: readonly ColumnInput[] = DEFAULT_COLUMNS): ResolvedColumns {
|
|
124
|
+
// Input may come from plain JavaScript, so check it as unknown.
|
|
125
|
+
const entries: unknown = input;
|
|
126
|
+
if (!Array.isArray(entries)) throw new QuartzioError('Gantt options: "columns" must be an array.');
|
|
127
|
+
const ids = new Set<string>();
|
|
128
|
+
const items: Column[] = [];
|
|
129
|
+
const values: ((cell: CellContext) => string)[] = [];
|
|
130
|
+
const fields: (BuiltInColumnId | null)[] = [];
|
|
131
|
+
let x = 0;
|
|
132
|
+
|
|
133
|
+
for (const entry of entries as readonly ColumnInput[]) {
|
|
134
|
+
if (typeof entry !== 'string' && (typeof entry !== 'object' || (entry as unknown) === null)) {
|
|
135
|
+
throw new QuartzioError('Every column must be a built-in column id or a column definition object.');
|
|
136
|
+
}
|
|
137
|
+
const definition: ColumnDefinition = typeof entry === 'string' ? { id: entry, field: entry } : entry;
|
|
138
|
+
if (typeof entry === 'string' && !isBuiltIn(entry)) {
|
|
139
|
+
throw new QuartzioError(
|
|
140
|
+
`Unknown column "${String(entry)}". Built-in columns: ${Object.keys(BUILT_IN).join(', ')}.`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const { id, field } = definition;
|
|
144
|
+
if (typeof id !== 'string' || id === '')
|
|
145
|
+
throw new QuartzioError('Every column needs a non-empty string id.');
|
|
146
|
+
if (ids.has(id)) throw new QuartzioError(`Column "${id}" is defined more than once.`);
|
|
147
|
+
if (field !== undefined && !isBuiltIn(field))
|
|
148
|
+
throw new QuartzioError(`Column "${id}": unknown field "${String(field)}".`);
|
|
149
|
+
const builtIn = field ? BUILT_IN[field] : undefined;
|
|
150
|
+
if (definition.value !== undefined && typeof definition.value !== 'function') {
|
|
151
|
+
throw new QuartzioError(`Column "${id}": "value" must be a function.`);
|
|
152
|
+
}
|
|
153
|
+
if (definition.title !== undefined && typeof definition.title !== 'string') {
|
|
154
|
+
throw new QuartzioError(`Column "${id}": "title" must be a string.`);
|
|
155
|
+
}
|
|
156
|
+
const editableInput: unknown = definition.editable;
|
|
157
|
+
if (editableInput !== undefined && typeof editableInput !== 'boolean') {
|
|
158
|
+
throw new QuartzioError(`Column "${id}": "editable" must be true or false.`);
|
|
159
|
+
}
|
|
160
|
+
const editable = field !== undefined && editableInput !== false;
|
|
161
|
+
const align: unknown = definition.align;
|
|
162
|
+
if (align !== undefined && align !== 'start' && align !== 'end') {
|
|
163
|
+
throw new QuartzioError(`Column "${id}": "align" must be "start" or "end".`);
|
|
164
|
+
}
|
|
165
|
+
const value = definition.value ?? builtIn?.value;
|
|
166
|
+
if (!value) throw new QuartzioError(`Column "${id}" needs a "field" or a "value" function.`);
|
|
167
|
+
const width = definition.width ?? builtIn?.width ?? 120;
|
|
168
|
+
if (typeof width !== 'number' || !Number.isFinite(width) || width <= 0 || width > 4000) {
|
|
169
|
+
throw new QuartzioError(`Column "${id}": width must be a number above 0 and at most 4000.`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
ids.add(id);
|
|
173
|
+
items.push({
|
|
174
|
+
id,
|
|
175
|
+
title: definition.title ?? builtIn?.title ?? id,
|
|
176
|
+
width,
|
|
177
|
+
x,
|
|
178
|
+
align: definition.align ?? builtIn?.align ?? 'start',
|
|
179
|
+
tree: field === 'name',
|
|
180
|
+
editable,
|
|
181
|
+
});
|
|
182
|
+
fields.push(editable ? field : null);
|
|
183
|
+
// A bug in a custom value function must not break rendering.
|
|
184
|
+
values.push((cell) => {
|
|
185
|
+
try {
|
|
186
|
+
const text: unknown = value(cell);
|
|
187
|
+
if (typeof text === 'string') return text;
|
|
188
|
+
// Numbers and booleans read fine as text; anything else (null, objects) becomes an empty cell.
|
|
189
|
+
return typeof text === 'number' || typeof text === 'boolean' ? String(text) : '';
|
|
190
|
+
} catch {
|
|
191
|
+
return '';
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
x += width;
|
|
195
|
+
}
|
|
196
|
+
return { state: { items, totalWidth: x }, values, fields };
|
|
197
|
+
}
|