@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,1164 @@
|
|
|
1
|
+
import { createProject } from '../data/project';
|
|
2
|
+
import { scheduleProject } from '../scheduling/schedule';
|
|
3
|
+
import { getTreeIndex } from '../data/tree';
|
|
4
|
+
import type { Id, Operation, ProjectState } from '../data/types';
|
|
5
|
+
import { createEmitter } from '../util/emitter';
|
|
6
|
+
import { isEqual } from '../util/equal';
|
|
7
|
+
import { QuartzioError } from '../util/errors';
|
|
8
|
+
import { createDataBinding } from './binding';
|
|
9
|
+
import { createHistory, type HistoryAction } from './history';
|
|
10
|
+
import { keyCommand } from './keyboard';
|
|
11
|
+
import {
|
|
12
|
+
clickRow,
|
|
13
|
+
NO_SELECTION,
|
|
14
|
+
pruneSelection,
|
|
15
|
+
selectIds,
|
|
16
|
+
selectionOf,
|
|
17
|
+
visibleRowOf,
|
|
18
|
+
type KeyModifiers,
|
|
19
|
+
type Selection,
|
|
20
|
+
type VisibleRows,
|
|
21
|
+
} from './selection';
|
|
22
|
+
import { resolveColumns, type ColumnInput, type ResolvedColumns } from './columns';
|
|
23
|
+
import type { TimeAxis } from '../timeaxis/timeAxis';
|
|
24
|
+
import { createDependencyView } from './dependencies';
|
|
25
|
+
import {
|
|
26
|
+
CLICK_TOLERANCE,
|
|
27
|
+
createInteraction,
|
|
28
|
+
type Interaction,
|
|
29
|
+
type InteractionView,
|
|
30
|
+
type TaskInteraction,
|
|
31
|
+
type TimelinePoint,
|
|
32
|
+
} from './interaction';
|
|
33
|
+
import { createNonWorkingView } from './nonWorking';
|
|
34
|
+
import { createRowsView, UNMEASURED_HEIGHT } from './rows';
|
|
35
|
+
import { tooltipFor, type TaskTooltip } from './tooltip';
|
|
36
|
+
import { createCellEditor, type EditorView } from './cellEditor';
|
|
37
|
+
import { barUnder } from './interaction';
|
|
38
|
+
import {
|
|
39
|
+
customized,
|
|
40
|
+
findItem,
|
|
41
|
+
menuHover,
|
|
42
|
+
menuKey,
|
|
43
|
+
taskMenuItems,
|
|
44
|
+
targetsOf,
|
|
45
|
+
timeAxisMenuItems,
|
|
46
|
+
zoomStep,
|
|
47
|
+
type MenuContext,
|
|
48
|
+
type MenuItem,
|
|
49
|
+
type MenuState,
|
|
50
|
+
type MenuTarget,
|
|
51
|
+
type TaskMenuItemId,
|
|
52
|
+
} from './menu';
|
|
53
|
+
import { addTask, indent, outdent, type AddWhere } from './taskActions';
|
|
54
|
+
import { createTaskEditor } from './taskEditor';
|
|
55
|
+
import { createListWidth, toListWidth } from './listWidth';
|
|
56
|
+
import { VIEW_PRESETS } from '../timeaxis/presets';
|
|
57
|
+
import type { Transaction } from '../data/transaction';
|
|
58
|
+
|
|
59
|
+
import { dateKind } from './editing';
|
|
60
|
+
import { createTimelineView, resolveTimeline, sameTimeline, type TimelineOptions } from './timeline';
|
|
61
|
+
import type {
|
|
62
|
+
GanttController,
|
|
63
|
+
GanttOptions,
|
|
64
|
+
Interactions,
|
|
65
|
+
ScrollRequest,
|
|
66
|
+
TodayLine,
|
|
67
|
+
ViewState,
|
|
68
|
+
Viewport,
|
|
69
|
+
} from './types';
|
|
70
|
+
|
|
71
|
+
export type {
|
|
72
|
+
GanttController,
|
|
73
|
+
GanttDataChange,
|
|
74
|
+
GanttOptions,
|
|
75
|
+
HeaderState,
|
|
76
|
+
Interactions,
|
|
77
|
+
ViewState,
|
|
78
|
+
Viewport,
|
|
79
|
+
} from './types';
|
|
80
|
+
|
|
81
|
+
const INITIAL_VIEWPORT: Viewport = { width: 0, height: 0, scrollLeft: 0, scrollTop: 0 };
|
|
82
|
+
const DEFAULT_ROW_HEIGHT = 44;
|
|
83
|
+
const DEFAULT_HEADER_ROW_HEIGHT = 28;
|
|
84
|
+
/** Where a menu opened with the keyboard goes, from the left edge of the chart. */
|
|
85
|
+
const KEYBOARD_MENU_X = 24;
|
|
86
|
+
|
|
87
|
+
/** The `scrollTop` that shows row `index`, or `null` when it shows (or nothing is measured). */
|
|
88
|
+
function revealTopOf(index: number, viewport: Viewport, rowHeight: number): number | null {
|
|
89
|
+
if (viewport.height <= 0) return null;
|
|
90
|
+
const top = index * rowHeight;
|
|
91
|
+
if (top < viewport.scrollTop) return top;
|
|
92
|
+
if (top + rowHeight > viewport.scrollTop + viewport.height) return top + rowHeight - viewport.height;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Options that shape the view (as opposed to data and callbacks), validated as a whole. */
|
|
97
|
+
interface ViewOptions {
|
|
98
|
+
timeline: TimelineOptions;
|
|
99
|
+
columnInput: readonly ColumnInput[] | undefined;
|
|
100
|
+
columns: ResolvedColumns;
|
|
101
|
+
rowHeight: number;
|
|
102
|
+
headerRowHeight: number;
|
|
103
|
+
showToday: boolean;
|
|
104
|
+
showNonWorkingTime: boolean;
|
|
105
|
+
taskDrag: boolean;
|
|
106
|
+
taskResize: boolean;
|
|
107
|
+
taskDragCreate: boolean;
|
|
108
|
+
progressDrag: boolean;
|
|
109
|
+
dependencyCreate: boolean;
|
|
110
|
+
/** The five switches above as one object, kept while they're unchanged. */
|
|
111
|
+
interactions: Interactions;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const VIEW_KEYS = [
|
|
115
|
+
'preset',
|
|
116
|
+
'startDate',
|
|
117
|
+
'endDate',
|
|
118
|
+
'locale',
|
|
119
|
+
'columns',
|
|
120
|
+
'rowHeight',
|
|
121
|
+
'headerRowHeight',
|
|
122
|
+
'showToday',
|
|
123
|
+
'showNonWorkingTime',
|
|
124
|
+
'taskDrag',
|
|
125
|
+
'taskResize',
|
|
126
|
+
'taskDragCreate',
|
|
127
|
+
'progressDrag',
|
|
128
|
+
'dependencyCreate',
|
|
129
|
+
] as const;
|
|
130
|
+
|
|
131
|
+
/** Modifiers straight from events (or plain JavaScript): anything that isn't an object means none. */
|
|
132
|
+
const modifiersOf = (value: unknown): KeyModifiers =>
|
|
133
|
+
typeof value === 'object' && value !== null ? value : {};
|
|
134
|
+
|
|
135
|
+
function toSwitch(value: unknown, fallback: boolean, name: string): boolean {
|
|
136
|
+
if (value === undefined || value === null) return fallback;
|
|
137
|
+
if (typeof value !== 'boolean') throw new QuartzioError(`Gantt options: "${name}" must be true or false.`);
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function toHeight(value: unknown, fallback: number, name: string): number {
|
|
142
|
+
if (value === undefined || value === null) return fallback;
|
|
143
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 8 || value > 500) {
|
|
144
|
+
throw new QuartzioError(`Gantt options: "${name}" must be a number from 8 to 500.`);
|
|
145
|
+
}
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Validates the view options, reusing unchanged parts of `previous`. Throws before anything changes. */
|
|
150
|
+
function resolveViewOptions(options: GanttOptions, previous?: ViewOptions): ViewOptions {
|
|
151
|
+
const resolved = resolveSwitches(options, previous);
|
|
152
|
+
const interactions: Interactions = {
|
|
153
|
+
drag: resolved.taskDrag,
|
|
154
|
+
resize: resolved.taskResize,
|
|
155
|
+
create: resolved.taskDragCreate,
|
|
156
|
+
progress: resolved.progressDrag,
|
|
157
|
+
link: resolved.dependencyCreate,
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
...resolved,
|
|
161
|
+
interactions:
|
|
162
|
+
previous && isEqual(previous.interactions, interactions) ? previous.interactions : interactions,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function resolveSwitches(options: GanttOptions, previous?: ViewOptions): Omit<ViewOptions, 'interactions'> {
|
|
167
|
+
const has = (key: keyof GanttOptions) => previous === undefined || key in options;
|
|
168
|
+
const timeline = resolveTimeline({
|
|
169
|
+
preset: has('preset') ? options.preset : previous?.timeline.preset,
|
|
170
|
+
startDate: has('startDate') ? options.startDate : previous?.timeline.startDate,
|
|
171
|
+
endDate: has('endDate') ? options.endDate : previous?.timeline.endDate,
|
|
172
|
+
locale: has('locale') ? options.locale : previous?.timeline.locale,
|
|
173
|
+
});
|
|
174
|
+
const columnInput = has('columns') ? options.columns : previous?.columnInput;
|
|
175
|
+
// Equal by value, so an inline array with the same columns (new on every React render) is no change.
|
|
176
|
+
// Value functions compare by identity: define columns with custom functions outside render.
|
|
177
|
+
const columns =
|
|
178
|
+
previous && (columnInput === previous.columnInput || isEqual(columnInput, previous.columnInput))
|
|
179
|
+
? previous.columns
|
|
180
|
+
: resolveColumns(columnInput ?? undefined);
|
|
181
|
+
return {
|
|
182
|
+
timeline: previous && sameTimeline(timeline, previous.timeline) ? previous.timeline : timeline,
|
|
183
|
+
columnInput,
|
|
184
|
+
columns,
|
|
185
|
+
rowHeight: has('rowHeight')
|
|
186
|
+
? toHeight(options.rowHeight, DEFAULT_ROW_HEIGHT, 'rowHeight')
|
|
187
|
+
: (previous?.rowHeight ?? DEFAULT_ROW_HEIGHT),
|
|
188
|
+
headerRowHeight: has('headerRowHeight')
|
|
189
|
+
? toHeight(options.headerRowHeight, DEFAULT_HEADER_ROW_HEIGHT, 'headerRowHeight')
|
|
190
|
+
: (previous?.headerRowHeight ?? DEFAULT_HEADER_ROW_HEIGHT),
|
|
191
|
+
showToday: has('showToday')
|
|
192
|
+
? toSwitch(options.showToday, true, 'showToday')
|
|
193
|
+
: (previous?.showToday ?? true),
|
|
194
|
+
showNonWorkingTime: has('showNonWorkingTime')
|
|
195
|
+
? toSwitch(options.showNonWorkingTime, true, 'showNonWorkingTime')
|
|
196
|
+
: (previous?.showNonWorkingTime ?? true),
|
|
197
|
+
taskDrag: has('taskDrag') ? toSwitch(options.taskDrag, true, 'taskDrag') : (previous?.taskDrag ?? true),
|
|
198
|
+
taskResize: has('taskResize')
|
|
199
|
+
? toSwitch(options.taskResize, true, 'taskResize')
|
|
200
|
+
: (previous?.taskResize ?? true),
|
|
201
|
+
taskDragCreate: has('taskDragCreate')
|
|
202
|
+
? toSwitch(options.taskDragCreate, true, 'taskDragCreate')
|
|
203
|
+
: (previous?.taskDragCreate ?? true),
|
|
204
|
+
progressDrag: has('progressDrag')
|
|
205
|
+
? toSwitch(options.progressDrag, true, 'progressDrag')
|
|
206
|
+
: (previous?.progressDrag ?? true),
|
|
207
|
+
dependencyCreate: has('dependencyCreate')
|
|
208
|
+
? toSwitch(options.dependencyCreate, true, 'dependencyCreate')
|
|
209
|
+
: (previous?.dependencyCreate ?? true),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
type CallbackOption =
|
|
214
|
+
| 'validateChange'
|
|
215
|
+
| 'onSelectionChange'
|
|
216
|
+
| 'taskMenuItems'
|
|
217
|
+
| 'timeAxisMenuItems'
|
|
218
|
+
| 'createTaskId'
|
|
219
|
+
| 'onPresetChange'
|
|
220
|
+
| 'onListWidthChange';
|
|
221
|
+
|
|
222
|
+
function toCallback<K extends CallbackOption>(value: unknown, name: K): GanttOptions[K] {
|
|
223
|
+
if (value === undefined || value === null) return undefined;
|
|
224
|
+
if (typeof value !== 'function') throw new QuartzioError(`Gantt options: "${name}" must be a function.`);
|
|
225
|
+
return value as GanttOptions[K];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Behaviour switches that don't change how anything looks. */
|
|
229
|
+
interface Behaviour {
|
|
230
|
+
taskTooltip: boolean;
|
|
231
|
+
cellEdit: boolean;
|
|
232
|
+
undoRedo: boolean;
|
|
233
|
+
multiSelect: boolean;
|
|
234
|
+
deleteKey: boolean;
|
|
235
|
+
taskMenu: boolean;
|
|
236
|
+
timeAxisMenu: boolean;
|
|
237
|
+
taskEdit: boolean;
|
|
238
|
+
validateChange: GanttOptions['validateChange'];
|
|
239
|
+
onSelectionChange: GanttOptions['onSelectionChange'];
|
|
240
|
+
taskMenuItems: GanttOptions['taskMenuItems'];
|
|
241
|
+
timeAxisMenuItems: GanttOptions['timeAxisMenuItems'];
|
|
242
|
+
createTaskId: GanttOptions['createTaskId'];
|
|
243
|
+
onPresetChange: GanttOptions['onPresetChange'];
|
|
244
|
+
onListWidthChange: GanttOptions['onListWidthChange'];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function resolveBehaviour(options: GanttOptions, previous?: Behaviour): Behaviour {
|
|
248
|
+
const has = (key: keyof GanttOptions) => previous === undefined || key in options;
|
|
249
|
+
return {
|
|
250
|
+
taskTooltip: has('taskTooltip')
|
|
251
|
+
? toSwitch(options.taskTooltip, true, 'taskTooltip')
|
|
252
|
+
: (previous?.taskTooltip ?? true),
|
|
253
|
+
cellEdit: has('cellEdit') ? toSwitch(options.cellEdit, true, 'cellEdit') : (previous?.cellEdit ?? true),
|
|
254
|
+
undoRedo: has('undoRedo') ? toSwitch(options.undoRedo, true, 'undoRedo') : (previous?.undoRedo ?? true),
|
|
255
|
+
multiSelect: has('multiSelect')
|
|
256
|
+
? toSwitch(options.multiSelect, true, 'multiSelect')
|
|
257
|
+
: (previous?.multiSelect ?? true),
|
|
258
|
+
deleteKey: has('deleteKey')
|
|
259
|
+
? toSwitch(options.deleteKey, true, 'deleteKey')
|
|
260
|
+
: (previous?.deleteKey ?? true),
|
|
261
|
+
validateChange: has('validateChange')
|
|
262
|
+
? toCallback(options.validateChange, 'validateChange')
|
|
263
|
+
: previous?.validateChange,
|
|
264
|
+
onSelectionChange: has('onSelectionChange')
|
|
265
|
+
? toCallback(options.onSelectionChange, 'onSelectionChange')
|
|
266
|
+
: previous?.onSelectionChange,
|
|
267
|
+
taskMenu: has('taskMenu') ? toSwitch(options.taskMenu, true, 'taskMenu') : (previous?.taskMenu ?? true),
|
|
268
|
+
timeAxisMenu: has('timeAxisMenu')
|
|
269
|
+
? toSwitch(options.timeAxisMenu, true, 'timeAxisMenu')
|
|
270
|
+
: (previous?.timeAxisMenu ?? true),
|
|
271
|
+
taskEdit: has('taskEdit') ? toSwitch(options.taskEdit, true, 'taskEdit') : (previous?.taskEdit ?? true),
|
|
272
|
+
taskMenuItems: has('taskMenuItems')
|
|
273
|
+
? toCallback(options.taskMenuItems, 'taskMenuItems')
|
|
274
|
+
: previous?.taskMenuItems,
|
|
275
|
+
timeAxisMenuItems: has('timeAxisMenuItems')
|
|
276
|
+
? toCallback(options.timeAxisMenuItems, 'timeAxisMenuItems')
|
|
277
|
+
: previous?.timeAxisMenuItems,
|
|
278
|
+
createTaskId: has('createTaskId')
|
|
279
|
+
? toCallback(options.createTaskId, 'createTaskId')
|
|
280
|
+
: previous?.createTaskId,
|
|
281
|
+
onPresetChange: has('onPresetChange')
|
|
282
|
+
? toCallback(options.onPresetChange, 'onPresetChange')
|
|
283
|
+
: previous?.onPresetChange,
|
|
284
|
+
onListWidthChange: has('onListWidthChange')
|
|
285
|
+
? toCallback(options.onListWidthChange, 'onListWidthChange')
|
|
286
|
+
: previous?.onListWidthChange,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const sameView = (a: ViewOptions, b: ViewOptions) =>
|
|
291
|
+
a.taskDrag === b.taskDrag &&
|
|
292
|
+
a.taskResize === b.taskResize &&
|
|
293
|
+
a.taskDragCreate === b.taskDragCreate &&
|
|
294
|
+
a.progressDrag === b.progressDrag &&
|
|
295
|
+
a.dependencyCreate === b.dependencyCreate &&
|
|
296
|
+
a.showToday === b.showToday &&
|
|
297
|
+
a.showNonWorkingTime === b.showNonWorkingTime &&
|
|
298
|
+
a.timeline === b.timeline &&
|
|
299
|
+
a.columns === b.columns &&
|
|
300
|
+
a.rowHeight === b.rowHeight &&
|
|
301
|
+
a.headerRowHeight === b.headerRowHeight;
|
|
302
|
+
|
|
303
|
+
export function createGantt(options: GanttOptions = {}): GanttController {
|
|
304
|
+
let view = resolveViewOptions(options);
|
|
305
|
+
let behaviour = resolveBehaviour(options);
|
|
306
|
+
const controlled = 'data' in options;
|
|
307
|
+
// Scheduling writes computed dates back into the data.
|
|
308
|
+
const project = createProject({}, { propagate: scheduleProject });
|
|
309
|
+
const initialPatch = project.load((controlled ? options.data : options.defaultData) ?? {});
|
|
310
|
+
const history = createHistory();
|
|
311
|
+
// Changes are recorded after they're committed, so the history in the state is brought up to date then.
|
|
312
|
+
const syncHistory = (): void => {
|
|
313
|
+
if (!batching && !destroyed && state.history !== history.state())
|
|
314
|
+
setState({ ...state, history: history.state() });
|
|
315
|
+
};
|
|
316
|
+
const binding = createDataBinding(project, controlled, options.data, options.onChange, initialPatch, {
|
|
317
|
+
confirm(actions) {
|
|
318
|
+
if (!behaviour.undoRedo) return;
|
|
319
|
+
history.confirm(actions);
|
|
320
|
+
syncHistory();
|
|
321
|
+
},
|
|
322
|
+
clear() {
|
|
323
|
+
history.clear();
|
|
324
|
+
syncHistory();
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
const timelineView = createTimelineView();
|
|
328
|
+
const rowsView = createRowsView();
|
|
329
|
+
const dependencyView = createDependencyView();
|
|
330
|
+
const nonWorkingView = createNonWorkingView();
|
|
331
|
+
const changes = createEmitter<ViewState>();
|
|
332
|
+
let collapsed: ReadonlySet<Id> = new Set();
|
|
333
|
+
let selection: Selection = NO_SELECTION;
|
|
334
|
+
let menu: MenuState | null = null;
|
|
335
|
+
// Where the renderer should scroll to (after zooming, or to show a new task); new object each time.
|
|
336
|
+
let scrollTo: ScrollRequest | null = null;
|
|
337
|
+
// A zoom overrides the preset option until the option changes (compared by value).
|
|
338
|
+
let zoomed = false;
|
|
339
|
+
let presetOption: unknown = options.preset;
|
|
340
|
+
const listWidth = createListWidth(toListWidth(options.listWidth), view.columns.state.totalWidth);
|
|
341
|
+
// A task just added from the menu: selected, shown and opened for editing once it's in the data.
|
|
342
|
+
let added: Id | null = null;
|
|
343
|
+
let reveal: Id | null = null;
|
|
344
|
+
// Its name is opened once the rows show it (after the state is derived).
|
|
345
|
+
let editAdded: Id | null = null;
|
|
346
|
+
const openAdded = (): void => {
|
|
347
|
+
const id = editAdded;
|
|
348
|
+
editAdded = null;
|
|
349
|
+
if (id !== null) editor.start(id, 'name');
|
|
350
|
+
};
|
|
351
|
+
// The selection last reported through onSelectionChange.
|
|
352
|
+
let announced = selection.ids;
|
|
353
|
+
// A press in the timeline that is a click as long as the pointer stays within the tolerance.
|
|
354
|
+
let press: { origin: TimelinePoint; id: Id | null; modifiers: KeyModifiers } | null = null;
|
|
355
|
+
let visible: VisibleRows | undefined;
|
|
356
|
+
let collapsedVersion = 0;
|
|
357
|
+
let destroyed = false;
|
|
358
|
+
// True while setOptions applies several changes, so they produce one state update instead of several.
|
|
359
|
+
let batching = false;
|
|
360
|
+
|
|
361
|
+
// The pointer over the timeline (for the tooltip), and the tooltip it shows.
|
|
362
|
+
let hoverPoint: TimelinePoint | null = null;
|
|
363
|
+
let tooltip: TaskTooltip | null = null;
|
|
364
|
+
let editorView: EditorView | undefined;
|
|
365
|
+
|
|
366
|
+
// The bar being dragged (a preview), and the task of every visible row (for hit testing).
|
|
367
|
+
let shownInteraction: TaskInteraction | null = null;
|
|
368
|
+
let interactionView: InteractionView | undefined;
|
|
369
|
+
// Created after the first state (it reads the view), so derive can't use it yet on that first call.
|
|
370
|
+
const interactionRef: { current?: Interaction } = {};
|
|
371
|
+
|
|
372
|
+
let todayCache: { axis: TimeAxis; line: TodayLine } | undefined;
|
|
373
|
+
const todayOn = (axis: TimeAxis): TodayLine | null => {
|
|
374
|
+
const now = Date.now();
|
|
375
|
+
if (!view.showToday || now < axis.start || now >= axis.end) return null;
|
|
376
|
+
// Whole pixels, and the same object while on the same pixel, so the line doesn't re-render on every update.
|
|
377
|
+
const x = Math.round(axis.dateToX(now));
|
|
378
|
+
if (todayCache?.axis !== axis || todayCache.line.x !== x) todayCache = { axis, line: { time: now, x } };
|
|
379
|
+
return todayCache.line;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// Deriving never throws: options are validated up front, and oversized ranges are cut short.
|
|
383
|
+
const derive = (viewport: Viewport, projectState: ProjectState): ViewState => {
|
|
384
|
+
const timeAxis = timelineView.axisFor(projectState, viewport.width, view.timeline);
|
|
385
|
+
const { rows, rowIndex, rowIds } = rowsView.rowsFor({
|
|
386
|
+
project: projectState,
|
|
387
|
+
collapsed,
|
|
388
|
+
collapsedVersion,
|
|
389
|
+
viewport,
|
|
390
|
+
rowHeight: view.rowHeight,
|
|
391
|
+
columns: view.columns,
|
|
392
|
+
locale: view.timeline.locale,
|
|
393
|
+
timeAxis,
|
|
394
|
+
selection,
|
|
395
|
+
});
|
|
396
|
+
visible = { rowIds, rowIndex, tree: getTreeIndex(projectState.tasks) };
|
|
397
|
+
if (reveal !== null) {
|
|
398
|
+
const index = rowIndex.get(reveal);
|
|
399
|
+
const top = index === undefined ? null : revealTopOf(index, viewport, rows.rowHeight);
|
|
400
|
+
if (top !== null) scrollTo = { top };
|
|
401
|
+
reveal = null;
|
|
402
|
+
}
|
|
403
|
+
if (menu?.kind === 'task' && !projectState.tasks.byId.has(menu.taskId as Id)) menu = null;
|
|
404
|
+
editorView = {
|
|
405
|
+
...visible,
|
|
406
|
+
project: projectState,
|
|
407
|
+
columns: view.columns,
|
|
408
|
+
dateField: dateKind(timeAxis.preset.timeResolution.unit),
|
|
409
|
+
};
|
|
410
|
+
const tooltipView = {
|
|
411
|
+
project: projectState,
|
|
412
|
+
timeAxis,
|
|
413
|
+
rows,
|
|
414
|
+
rowIds,
|
|
415
|
+
viewport,
|
|
416
|
+
locale: view.timeline.locale,
|
|
417
|
+
};
|
|
418
|
+
tooltip =
|
|
419
|
+
hoverPoint && behaviour.taskTooltip && !shownInteraction
|
|
420
|
+
? tooltipFor(tooltipView, hoverPoint, tooltip)
|
|
421
|
+
: null;
|
|
422
|
+
const next: ViewState = {
|
|
423
|
+
viewport,
|
|
424
|
+
project: projectState,
|
|
425
|
+
timeAxis,
|
|
426
|
+
header: timelineView.headerFor(timeAxis, viewport, view.headerRowHeight),
|
|
427
|
+
columns: view.columns.state,
|
|
428
|
+
rows,
|
|
429
|
+
dependencies: dependencyView.linesFor({
|
|
430
|
+
project: projectState,
|
|
431
|
+
timeAxis,
|
|
432
|
+
rows,
|
|
433
|
+
rowIndex,
|
|
434
|
+
}),
|
|
435
|
+
today: todayOn(timeAxis),
|
|
436
|
+
nonWorkingTime: nonWorkingView.spansFor(projectState, timeAxis, viewport, view.showNonWorkingTime),
|
|
437
|
+
interaction: shownInteraction,
|
|
438
|
+
interactions: view.interactions,
|
|
439
|
+
history: history.state(),
|
|
440
|
+
selection: selection.ids,
|
|
441
|
+
activeId: selection.active,
|
|
442
|
+
tooltip,
|
|
443
|
+
editing: editor.current(editorView),
|
|
444
|
+
menu,
|
|
445
|
+
taskEditor: taskEditor.current(editorView),
|
|
446
|
+
scrollTo,
|
|
447
|
+
list: listWidth.state(),
|
|
448
|
+
};
|
|
449
|
+
interactionView = {
|
|
450
|
+
project: projectState,
|
|
451
|
+
timeAxis,
|
|
452
|
+
rows,
|
|
453
|
+
rowIndex,
|
|
454
|
+
rowIds,
|
|
455
|
+
viewport,
|
|
456
|
+
locale: view.timeline.locale,
|
|
457
|
+
enabled: {
|
|
458
|
+
drag: view.taskDrag,
|
|
459
|
+
resize: view.taskResize,
|
|
460
|
+
create: view.taskDragCreate,
|
|
461
|
+
progress: view.progressDrag,
|
|
462
|
+
link: view.dependencyCreate,
|
|
463
|
+
},
|
|
464
|
+
// Read when asked, so a validator set later (or one closing over newer app state) is the one used.
|
|
465
|
+
validate: (change) => (behaviour.validateChange ? behaviour.validateChange(change) : true),
|
|
466
|
+
};
|
|
467
|
+
// A drag on a stale basis (another axis, a changed or hidden task, options turned off) is dropped before
|
|
468
|
+
// anything shows it.
|
|
469
|
+
if (interactionRef.current && !interactionRef.current.keep(interactionView)) {
|
|
470
|
+
shownInteraction = null;
|
|
471
|
+
return { ...next, interaction: null };
|
|
472
|
+
}
|
|
473
|
+
if (shownInteraction) tooltip = null;
|
|
474
|
+
return next;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const editor = createCellEditor({
|
|
478
|
+
view: () => editorView as EditorView,
|
|
479
|
+
enabled: () => behaviour.cellEdit,
|
|
480
|
+
plan: (fn) => project.plan(fn),
|
|
481
|
+
commit(fn) {
|
|
482
|
+
changing(() => binding.transact(fn));
|
|
483
|
+
},
|
|
484
|
+
validate: (change) => (behaviour.validateChange ? behaviour.validateChange(change) : true),
|
|
485
|
+
show: () => {
|
|
486
|
+
if (!batching && !destroyed) refresh();
|
|
487
|
+
},
|
|
488
|
+
focusRow(id) {
|
|
489
|
+
selection = selection.set.has(id)
|
|
490
|
+
? selectionOf(selection, selection.ids, id, selection.anchor)
|
|
491
|
+
: selectionOf(selection, [id], id, id);
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
const taskEditor = createTaskEditor({
|
|
496
|
+
view: () => editorView as EditorView,
|
|
497
|
+
enabled: () => behaviour.taskEdit,
|
|
498
|
+
plan: (fn) => project.plan(fn),
|
|
499
|
+
commit(fn) {
|
|
500
|
+
changing(() => binding.transact(fn));
|
|
501
|
+
},
|
|
502
|
+
show: () => {
|
|
503
|
+
if (!batching && !destroyed) refresh();
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
let state = derive(INITIAL_VIEWPORT, project.getState());
|
|
508
|
+
|
|
509
|
+
const drags = createInteraction({
|
|
510
|
+
view: () => interactionView as InteractionView,
|
|
511
|
+
show(next) {
|
|
512
|
+
shownInteraction = next;
|
|
513
|
+
setState({ ...state, interaction: next, tooltip: next ? null : state.tooltip });
|
|
514
|
+
},
|
|
515
|
+
commit(fn) {
|
|
516
|
+
try {
|
|
517
|
+
changing(() => binding.transact(fn));
|
|
518
|
+
} catch (error) {
|
|
519
|
+
// Expected: the data rejecting the change (a dependency cycle can't come from a drop, but a date the
|
|
520
|
+
// data doesn't accept could). Nothing changes and the bar goes back; a pointer handler must not throw.
|
|
521
|
+
if (!(error instanceof QuartzioError)) throw error;
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
});
|
|
525
|
+
interactionRef.current = drags;
|
|
526
|
+
|
|
527
|
+
const setState = (next: ViewState): void => {
|
|
528
|
+
state = next;
|
|
529
|
+
changes.emit(state);
|
|
530
|
+
};
|
|
531
|
+
const refresh = (): void => {
|
|
532
|
+
setState(derive(state.viewport, project.getState()));
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
const unsubscribeProject = project.subscribe(({ state: projectState }) => {
|
|
536
|
+
pruneCollapsed(projectState);
|
|
537
|
+
selection = pruneSelection(selection, projectState.tasks.byId);
|
|
538
|
+
const arrived = added !== null && projectState.tasks.byId.has(added) ? added : null;
|
|
539
|
+
added = null; // in this change, or not coming (the app didn't take it)
|
|
540
|
+
if (arrived !== null) {
|
|
541
|
+
selection = selectionOf(selection, [arrived], arrived, arrived);
|
|
542
|
+
reveal = arrived;
|
|
543
|
+
}
|
|
544
|
+
// Reported once the change is complete (see announce), not in the middle of committing it.
|
|
545
|
+
if (arrived !== null) editAdded = arrived;
|
|
546
|
+
if (!batching) {
|
|
547
|
+
setState(derive(state.viewport, projectState));
|
|
548
|
+
openAdded();
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Reports a changed selection. Called once an action is done (the data change reported and recorded), so an
|
|
554
|
+
* app callback that throws or changes things again can't leave a change half done.
|
|
555
|
+
*/
|
|
556
|
+
const announce = (): void => {
|
|
557
|
+
if (selection.ids === announced || destroyed) return;
|
|
558
|
+
const same =
|
|
559
|
+
selection.ids.length === announced.length && selection.ids.every((id, i) => id === announced[i]);
|
|
560
|
+
announced = selection.ids;
|
|
561
|
+
if (!same) behaviour.onSelectionChange?.(announced);
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
/** Runs a data change, then reports what it did to the selection (deleted tasks drop out). */
|
|
565
|
+
const changing = <T>(fn: () => T): T => {
|
|
566
|
+
const result = fn();
|
|
567
|
+
announce();
|
|
568
|
+
return result;
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
const setSelection = (next: Selection): void => {
|
|
572
|
+
if (next === selection) return;
|
|
573
|
+
selection = next;
|
|
574
|
+
refresh();
|
|
575
|
+
announce();
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
const visibleRows = (): VisibleRows => visible as VisibleRows;
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Saves an open edit whose field is no longer there to take the keys and presses (its row scrolled out of the
|
|
582
|
+
* rendered window, say); a value that can't be saved is dropped, as on blur.
|
|
583
|
+
*/
|
|
584
|
+
const closeEdit = (): void => {
|
|
585
|
+
if (!state.editing) return;
|
|
586
|
+
if (!editor.commit()) editor.cancel();
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const parentOf = (id: Id): Id | null => state.project.tasks.byId.get(id)?.parentId ?? null;
|
|
590
|
+
|
|
591
|
+
const menuContext = (target: MenuTarget): MenuContext => ({
|
|
592
|
+
target,
|
|
593
|
+
task: target.kind === 'task' ? (state.project.tasks.byId.get(target.id) ?? null) : null,
|
|
594
|
+
selection: selection.ids,
|
|
595
|
+
preset: view.timeline.preset,
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
/** A change from the task menu; the data refusing it changes nothing. */
|
|
599
|
+
const changeFromMenu = (fn: (tx: Transaction) => void): void => {
|
|
600
|
+
try {
|
|
601
|
+
changing(() => binding.transact(fn));
|
|
602
|
+
} catch (error) {
|
|
603
|
+
if (!(error instanceof QuartzioError)) throw error;
|
|
604
|
+
added = null;
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
const expand = (ids: readonly Id[]): void => {
|
|
609
|
+
if (ids.some((id) => collapsed.has(id)))
|
|
610
|
+
setCollapsed(new Set([...collapsed].filter((id) => !ids.includes(id))));
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const runTaskAction = (id: TaskMenuItemId, taskId: Id): void => {
|
|
614
|
+
const task = state.project.tasks.byId.get(taskId);
|
|
615
|
+
if (!task) return;
|
|
616
|
+
const targets = targetsOf(taskId, selection.ids, getTreeIndex(state.project.tasks));
|
|
617
|
+
const add = (where: AddWhere) => {
|
|
618
|
+
// A subtask of a collapsed parent would be hidden.
|
|
619
|
+
if (where === 'subtask') expand([taskId]);
|
|
620
|
+
changeFromMenu((tx) => {
|
|
621
|
+
added = addTask(tx, task, where, behaviour.createTaskId?.());
|
|
622
|
+
});
|
|
623
|
+
};
|
|
624
|
+
switch (id) {
|
|
625
|
+
case 'edit':
|
|
626
|
+
controller.openTaskEditor(taskId);
|
|
627
|
+
return;
|
|
628
|
+
case 'addTaskAbove': {
|
|
629
|
+
add('above');
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
case 'addTaskBelow': {
|
|
633
|
+
add('below');
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
case 'addSubtask': {
|
|
637
|
+
add('subtask');
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
case 'addMilestone': {
|
|
641
|
+
add('milestone');
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
case 'addSuccessor': {
|
|
645
|
+
add('successor');
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
case 'addPredecessor': {
|
|
649
|
+
add('predecessor');
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
case 'indent': {
|
|
653
|
+
let parents: Id[] = [];
|
|
654
|
+
const { tasks, dependencies } = state.project;
|
|
655
|
+
changeFromMenu((tx) => {
|
|
656
|
+
parents = indent(tx, targets, getTreeIndex(tasks), dependencies.byId.values());
|
|
657
|
+
});
|
|
658
|
+
expand(parents); // so the indented tasks stay in view
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
case 'outdent': {
|
|
662
|
+
changeFromMenu((tx) => {
|
|
663
|
+
outdent(tx, targets);
|
|
664
|
+
});
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
case 'convertToMilestone': {
|
|
668
|
+
changeFromMenu((tx) => {
|
|
669
|
+
tx.tasks.update(taskId, { duration: 0 });
|
|
670
|
+
});
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
case 'delete':
|
|
674
|
+
removeTasks(targets);
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
/** Undo or redo. If the data refuses it, the history no longer matches the data and is dropped. */
|
|
679
|
+
const replay = (operations: readonly Operation[], action: HistoryAction): boolean => {
|
|
680
|
+
try {
|
|
681
|
+
binding.apply(operations, () => action);
|
|
682
|
+
return true;
|
|
683
|
+
} catch (error) {
|
|
684
|
+
if (!(error instanceof QuartzioError)) throw error;
|
|
685
|
+
history.clear();
|
|
686
|
+
syncHistory();
|
|
687
|
+
return false;
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
const removeTasks = (ids: readonly Id[]): void => {
|
|
692
|
+
try {
|
|
693
|
+
changing(() =>
|
|
694
|
+
binding.transact((tx) => {
|
|
695
|
+
for (const id of ids) tx.tasks.remove(id);
|
|
696
|
+
}),
|
|
697
|
+
);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
if (!(error instanceof QuartzioError)) throw error;
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
const sameSet = (a: ReadonlySet<Id>, b: ReadonlySet<Id>) =>
|
|
704
|
+
a.size === b.size && [...a].every((id) => b.has(id));
|
|
705
|
+
|
|
706
|
+
const setCollapsed = (next: ReadonlySet<Id>): void => {
|
|
707
|
+
if (sameSet(next, collapsed)) return;
|
|
708
|
+
collapsed = next;
|
|
709
|
+
collapsedVersion++;
|
|
710
|
+
refresh();
|
|
711
|
+
// A cursor hidden by collapsing moves to the parent that hides it, so it stays in view.
|
|
712
|
+
const shown = visibleRowOf(selection.active, visibleRows());
|
|
713
|
+
const shownId = shown === undefined ? undefined : visibleRows().rowIds[shown];
|
|
714
|
+
if (shownId !== undefined && shownId !== selection.active) {
|
|
715
|
+
setSelection(selectionOf(selection, selection.ids, shownId, shownId));
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
/** Forgets collapsed tasks that were removed or lost their children, so they start expanded if reused. */
|
|
720
|
+
const pruneCollapsed = (projectState: ProjectState): void => {
|
|
721
|
+
if (collapsed.size === 0) return;
|
|
722
|
+
const tree = getTreeIndex(projectState.tasks);
|
|
723
|
+
const kept = new Set([...collapsed].filter((id) => !tree.isLeaf(id)));
|
|
724
|
+
if (kept.size !== collapsed.size) {
|
|
725
|
+
collapsed = kept;
|
|
726
|
+
collapsedVersion++;
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
const controller: GanttController = {
|
|
731
|
+
getState: () => state,
|
|
732
|
+
subscribe: (listener) => {
|
|
733
|
+
binding.start(); // a renderer took this controller into use: report the initial scheduling
|
|
734
|
+
return changes.subscribe(listener);
|
|
735
|
+
},
|
|
736
|
+
|
|
737
|
+
setOptions(next) {
|
|
738
|
+
if (destroyed) return;
|
|
739
|
+
// Validate everything first, so an invalid option changes nothing.
|
|
740
|
+
// The same preset as before on a zoomed chart (e.g. the same prop on the next render) keeps the zoom.
|
|
741
|
+
const keepZoom = zoomed && 'preset' in next && isEqual(next.preset, presetOption);
|
|
742
|
+
const { preset: _preset, ...withoutPreset } = next;
|
|
743
|
+
const effective = keepZoom ? withoutPreset : next;
|
|
744
|
+
const nextView = VIEW_KEYS.some((key) => key in effective) ? resolveViewOptions(effective, view) : view;
|
|
745
|
+
const nextBehaviour = resolveBehaviour(next, behaviour);
|
|
746
|
+
const nextListWidth = 'listWidth' in next ? toListWidth(next.listWidth) : undefined;
|
|
747
|
+
const before = project.getState();
|
|
748
|
+
|
|
749
|
+
const previousView = view;
|
|
750
|
+
view = nextView;
|
|
751
|
+
batching = true;
|
|
752
|
+
try {
|
|
753
|
+
if (controlled && 'data' in next) {
|
|
754
|
+
binding.setData(next.data);
|
|
755
|
+
// A task added from the menu comes with the next data, or not at all (the app didn't take it).
|
|
756
|
+
added = null;
|
|
757
|
+
}
|
|
758
|
+
} catch (error) {
|
|
759
|
+
view = previousView;
|
|
760
|
+
throw error;
|
|
761
|
+
} finally {
|
|
762
|
+
batching = false;
|
|
763
|
+
}
|
|
764
|
+
if ('onChange' in next) binding.setOnChange(next.onChange);
|
|
765
|
+
behaviour = nextBehaviour;
|
|
766
|
+
if ('preset' in next && !keepZoom) {
|
|
767
|
+
zoomed = false;
|
|
768
|
+
presetOption = next.preset;
|
|
769
|
+
}
|
|
770
|
+
if (menu && !(menu.kind === 'task' ? behaviour.taskMenu : behaviour.timeAxisMenu)) menu = null;
|
|
771
|
+
if (!behaviour.undoRedo) history.clear();
|
|
772
|
+
if (nextListWidth !== undefined) listWidth.setOption(nextListWidth);
|
|
773
|
+
if (nextView.columns !== previousView.columns)
|
|
774
|
+
listWidth.setColumnsWidth(nextView.columns.state.totalWidth);
|
|
775
|
+
// Recomputed from scratch: the texts depend on the options (locale, columns).
|
|
776
|
+
if (!sameView(nextView, previousView)) tooltip = null;
|
|
777
|
+
if (!behaviour.multiSelect && selection.ids.length > 1) {
|
|
778
|
+
const last = selection.ids.at(-1) as Id;
|
|
779
|
+
selection = selectionOf(selection, [last], selection.active, last);
|
|
780
|
+
}
|
|
781
|
+
if (
|
|
782
|
+
!sameView(nextView, previousView) ||
|
|
783
|
+
project.getState() !== before ||
|
|
784
|
+
state.history !== history.state() ||
|
|
785
|
+
state.selection !== selection.ids ||
|
|
786
|
+
(state.tooltip !== null && !behaviour.taskTooltip) ||
|
|
787
|
+
(state.editing !== null && !behaviour.cellEdit) ||
|
|
788
|
+
state.menu !== menu ||
|
|
789
|
+
state.list !== listWidth.state() ||
|
|
790
|
+
(state.taskEditor !== null && !behaviour.taskEdit)
|
|
791
|
+
) {
|
|
792
|
+
refresh();
|
|
793
|
+
}
|
|
794
|
+
openAdded();
|
|
795
|
+
announce();
|
|
796
|
+
},
|
|
797
|
+
|
|
798
|
+
setViewport(patch) {
|
|
799
|
+
if (destroyed) return;
|
|
800
|
+
const viewport = { ...state.viewport, ...patch };
|
|
801
|
+
const current = state.viewport;
|
|
802
|
+
if (
|
|
803
|
+
viewport.width === current.width &&
|
|
804
|
+
viewport.height === current.height &&
|
|
805
|
+
viewport.scrollLeft === current.scrollLeft &&
|
|
806
|
+
viewport.scrollTop === current.scrollTop
|
|
807
|
+
) {
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
// What's under a still pointer changes while scrolling; the tooltip goes until it moves again.
|
|
811
|
+
if (viewport.scrollLeft !== current.scrollLeft || viewport.scrollTop !== current.scrollTop) menu = null;
|
|
812
|
+
if (viewport.scrollLeft !== current.scrollLeft || viewport.scrollTop !== current.scrollTop)
|
|
813
|
+
hoverPoint = null;
|
|
814
|
+
setState(derive(viewport, state.project));
|
|
815
|
+
// A drag follows the pointer to what's under it now (this is how auto-scrolling moves it).
|
|
816
|
+
drags.follow();
|
|
817
|
+
},
|
|
818
|
+
|
|
819
|
+
transact(fn) {
|
|
820
|
+
if (destroyed) return null;
|
|
821
|
+
return changing(() => binding.transact(fn));
|
|
822
|
+
},
|
|
823
|
+
|
|
824
|
+
undo() {
|
|
825
|
+
const patch = destroyed || !behaviour.undoRedo || binding.awaiting() ? undefined : history.undoable();
|
|
826
|
+
if (!patch) return false;
|
|
827
|
+
return changing(() => replay(patch.inverse, { type: 'undo', patch }));
|
|
828
|
+
},
|
|
829
|
+
|
|
830
|
+
redo() {
|
|
831
|
+
const patch = destroyed || !behaviour.undoRedo || binding.awaiting() ? undefined : history.redoable();
|
|
832
|
+
if (!patch) return false;
|
|
833
|
+
return changing(() => replay(patch.operations, { type: 'redo', patch }));
|
|
834
|
+
},
|
|
835
|
+
|
|
836
|
+
hitTest: (point) => (destroyed ? null : drags.hitTest(point)),
|
|
837
|
+
pointerDown(point, modifiers) {
|
|
838
|
+
if (destroyed || !Number.isFinite(point.x) || !Number.isFinite(point.y)) return false;
|
|
839
|
+
const index = Math.floor(point.y / state.rows.rowHeight);
|
|
840
|
+
closeEdit();
|
|
841
|
+
if (menu) {
|
|
842
|
+
menu = null;
|
|
843
|
+
refresh();
|
|
844
|
+
}
|
|
845
|
+
press = { origin: point, id: visibleRows().rowIds[index] ?? null, modifiers: modifiersOf(modifiers) };
|
|
846
|
+
if (hoverPoint) {
|
|
847
|
+
hoverPoint = null;
|
|
848
|
+
refresh();
|
|
849
|
+
}
|
|
850
|
+
drags.pointerDown(point);
|
|
851
|
+
return true;
|
|
852
|
+
},
|
|
853
|
+
pointerMove(point) {
|
|
854
|
+
if (destroyed) return;
|
|
855
|
+
if (press && Math.hypot(point.x - press.origin.x, point.y - press.origin.y) >= CLICK_TOLERANCE)
|
|
856
|
+
press = null;
|
|
857
|
+
drags.pointerMove(point);
|
|
858
|
+
},
|
|
859
|
+
pointerUp(point) {
|
|
860
|
+
if (destroyed) return;
|
|
861
|
+
const click = press;
|
|
862
|
+
press = null;
|
|
863
|
+
drags.pointerUp(point);
|
|
864
|
+
if (!click) return;
|
|
865
|
+
// Below the rows: a plain click clears the selection, as in a file list.
|
|
866
|
+
if (click.id === null) {
|
|
867
|
+
if (!click.modifiers.ctrl && !click.modifiers.meta && !click.modifiers.shift) {
|
|
868
|
+
setSelection(selectionOf(selection, [], selection.active, selection.anchor));
|
|
869
|
+
}
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
setSelection(clickRow(selection, click.id, click.modifiers, visibleRows(), behaviour.multiSelect));
|
|
873
|
+
},
|
|
874
|
+
cancelInteraction() {
|
|
875
|
+
press = null;
|
|
876
|
+
return !destroyed && drags.cancel();
|
|
877
|
+
},
|
|
878
|
+
|
|
879
|
+
rowClick(id, modifiers) {
|
|
880
|
+
if (destroyed) return;
|
|
881
|
+
setSelection(clickRow(selection, id, modifiersOf(modifiers), visibleRows(), behaviour.multiSelect));
|
|
882
|
+
},
|
|
883
|
+
|
|
884
|
+
select(ids) {
|
|
885
|
+
if (destroyed || !Array.isArray(ids)) return;
|
|
886
|
+
setSelection(selectIds(selection, ids, state.project.tasks.byId, behaviour.multiSelect));
|
|
887
|
+
},
|
|
888
|
+
|
|
889
|
+
clearSelection() {
|
|
890
|
+
if (destroyed) return;
|
|
891
|
+
setSelection(selectionOf(selection, [], selection.active, selection.anchor));
|
|
892
|
+
},
|
|
893
|
+
|
|
894
|
+
keyDown(input) {
|
|
895
|
+
// Checked, as it comes straight from DOM events (or plain JavaScript).
|
|
896
|
+
if (destroyed || typeof (input as unknown) !== 'object' || (input as unknown) === null) return false;
|
|
897
|
+
closeEdit();
|
|
898
|
+
const height = state.viewport.height > 0 ? state.viewport.height : UNMEASURED_HEIGHT;
|
|
899
|
+
const command = keyCommand(input, {
|
|
900
|
+
...visibleRows(),
|
|
901
|
+
selection,
|
|
902
|
+
collapsed,
|
|
903
|
+
pageRows: Math.floor(height / state.rows.rowHeight),
|
|
904
|
+
multiSelect: behaviour.multiSelect,
|
|
905
|
+
deleteKey: behaviour.deleteKey,
|
|
906
|
+
undoRedo: behaviour.undoRedo,
|
|
907
|
+
cellEdit: behaviour.cellEdit,
|
|
908
|
+
});
|
|
909
|
+
if (!command) return false;
|
|
910
|
+
switch (command.type) {
|
|
911
|
+
case 'select':
|
|
912
|
+
setSelection(command.selection);
|
|
913
|
+
return true;
|
|
914
|
+
case 'expand':
|
|
915
|
+
controller.setExpanded(command.id, command.expanded);
|
|
916
|
+
return true;
|
|
917
|
+
case 'delete':
|
|
918
|
+
removeTasks(command.ids);
|
|
919
|
+
setSelection(command.selection);
|
|
920
|
+
return true;
|
|
921
|
+
case 'undo':
|
|
922
|
+
return controller.undo();
|
|
923
|
+
case 'redo':
|
|
924
|
+
return controller.redo();
|
|
925
|
+
case 'menu':
|
|
926
|
+
return controller.openMenu({ kind: 'task', id: command.id });
|
|
927
|
+
case 'edit': {
|
|
928
|
+
const started = editor.start(command.id);
|
|
929
|
+
announce(); // the cursor may have moved
|
|
930
|
+
return started;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
},
|
|
934
|
+
|
|
935
|
+
hover(point) {
|
|
936
|
+
if (destroyed) return;
|
|
937
|
+
const next = point && Number.isFinite(point.x) && Number.isFinite(point.y) ? point : null;
|
|
938
|
+
if (next === null && hoverPoint === null) return;
|
|
939
|
+
hoverPoint = next;
|
|
940
|
+
const shown =
|
|
941
|
+
next && behaviour.taskTooltip && !shownInteraction && interactionView
|
|
942
|
+
? tooltipFor(interactionView, next, tooltip)
|
|
943
|
+
: null;
|
|
944
|
+
tooltip = shown;
|
|
945
|
+
if (shown !== state.tooltip) setState({ ...state, tooltip: shown });
|
|
946
|
+
},
|
|
947
|
+
|
|
948
|
+
startEdit(id, columnId) {
|
|
949
|
+
if (destroyed) return false;
|
|
950
|
+
const started = editor.start(id, columnId);
|
|
951
|
+
announce(); // the cursor may have moved
|
|
952
|
+
return started;
|
|
953
|
+
},
|
|
954
|
+
editInput(text) {
|
|
955
|
+
if (!destroyed) editor.input(text);
|
|
956
|
+
},
|
|
957
|
+
editKeyDown(key) {
|
|
958
|
+
if (destroyed) return false;
|
|
959
|
+
const used = editor.keyDown(key);
|
|
960
|
+
announce(); // Tab may move the cursor
|
|
961
|
+
return used;
|
|
962
|
+
},
|
|
963
|
+
commitEdit: () => !destroyed && editor.commit(),
|
|
964
|
+
cancelEdit() {
|
|
965
|
+
if (!destroyed) editor.cancel();
|
|
966
|
+
},
|
|
967
|
+
|
|
968
|
+
openMenu(target, point) {
|
|
969
|
+
if (destroyed || typeof target !== 'object' || (target as unknown) === null) return false;
|
|
970
|
+
// Opened during a press (Ctrl-click on macOS): that press is not a click or a drag.
|
|
971
|
+
press = null;
|
|
972
|
+
drags.cancel();
|
|
973
|
+
const at = point && Number.isFinite(point.x) && Number.isFinite(point.y) ? point : null;
|
|
974
|
+
let items: readonly MenuItem[];
|
|
975
|
+
let y: number;
|
|
976
|
+
if (target.kind === 'task') {
|
|
977
|
+
const task = state.project.tasks.byId.get(target.id);
|
|
978
|
+
if (!behaviour.taskMenu || !task) return false;
|
|
979
|
+
// As in a file list: a row that isn't selected is selected alone; a selection that has it is kept.
|
|
980
|
+
const id = task.id;
|
|
981
|
+
selection = selection.set.has(id)
|
|
982
|
+
? selectionOf(selection, selection.ids, id, selection.anchor)
|
|
983
|
+
: selectionOf(selection, [id], id, id);
|
|
984
|
+
const tree = getTreeIndex(state.project.tasks);
|
|
985
|
+
const targets = targetsOf(id, selection.ids, tree);
|
|
986
|
+
const builtIn = taskMenuItems(task, targets, tree, parentOf, { edit: behaviour.taskEdit });
|
|
987
|
+
items = customized(builtIn, behaviour.taskMenuItems, menuContext(target));
|
|
988
|
+
const row = visibleRows().rowIndex.get(id) ?? 0;
|
|
989
|
+
const { header, rows, viewport } = state;
|
|
990
|
+
// Below the row, kept within the visible rows (the row may be scrolled out of view).
|
|
991
|
+
const below = header.height + (row + 1) * rows.rowHeight - viewport.scrollTop;
|
|
992
|
+
y = Math.max(header.height, Math.min(below, header.height + Math.max(viewport.height, 0)));
|
|
993
|
+
} else if ((target.kind as unknown) === 'timeAxis') {
|
|
994
|
+
if (!behaviour.timeAxisMenu) return false;
|
|
995
|
+
const builtIn = timeAxisMenuItems(view.timeline.preset);
|
|
996
|
+
items = customized(builtIn, behaviour.timeAxisMenuItems, menuContext(target));
|
|
997
|
+
y = state.header.height;
|
|
998
|
+
} else {
|
|
999
|
+
return false;
|
|
1000
|
+
}
|
|
1001
|
+
if (items.length === 0) return false;
|
|
1002
|
+
menu = {
|
|
1003
|
+
kind: target.kind,
|
|
1004
|
+
taskId: target.kind === 'task' ? target.id : null,
|
|
1005
|
+
x: at?.x ?? KEYBOARD_MENU_X,
|
|
1006
|
+
y: at?.y ?? y,
|
|
1007
|
+
items,
|
|
1008
|
+
// Opened with the keyboard: the first item is ready to be picked.
|
|
1009
|
+
active: at ? null : (items.find((item) => item.disabled !== true)?.id ?? null),
|
|
1010
|
+
submenu: null,
|
|
1011
|
+
};
|
|
1012
|
+
refresh();
|
|
1013
|
+
announce();
|
|
1014
|
+
return true;
|
|
1015
|
+
},
|
|
1016
|
+
|
|
1017
|
+
menuKeyDown(key) {
|
|
1018
|
+
if (destroyed || !menu || typeof key !== 'object' || (key as unknown) === null) return false;
|
|
1019
|
+
const result = menuKey(menu, key);
|
|
1020
|
+
if (!result) return false;
|
|
1021
|
+
if (result.pick !== undefined) {
|
|
1022
|
+
controller.menuAction(result.pick);
|
|
1023
|
+
return true;
|
|
1024
|
+
}
|
|
1025
|
+
menu = result.menu;
|
|
1026
|
+
refresh();
|
|
1027
|
+
return true;
|
|
1028
|
+
},
|
|
1029
|
+
|
|
1030
|
+
menuHover(id) {
|
|
1031
|
+
if (destroyed || !menu) return;
|
|
1032
|
+
const next = menuHover(menu, id);
|
|
1033
|
+
if (next === menu) return;
|
|
1034
|
+
menu = next;
|
|
1035
|
+
refresh();
|
|
1036
|
+
},
|
|
1037
|
+
|
|
1038
|
+
menuAction(id) {
|
|
1039
|
+
const open = menu;
|
|
1040
|
+
const item = open && !destroyed ? findItem(open.items, id) : undefined;
|
|
1041
|
+
if (!open || !item || item.disabled === true || item.items) return;
|
|
1042
|
+
menu = null;
|
|
1043
|
+
refresh();
|
|
1044
|
+
const target: MenuTarget =
|
|
1045
|
+
open.kind === 'task' ? { kind: 'task', id: open.taskId as Id } : { kind: 'timeAxis' };
|
|
1046
|
+
if (item.action) {
|
|
1047
|
+
item.action(menuContext(target));
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
if (target.kind === 'timeAxis') {
|
|
1051
|
+
if (id === 'zoomIn' || id === 'zoomOut') controller.zoom(id === 'zoomIn' ? 'in' : 'out');
|
|
1052
|
+
else if (id.startsWith('preset:')) controller.zoom(id.slice('preset:'.length));
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
runTaskAction(id as TaskMenuItemId, target.id);
|
|
1056
|
+
},
|
|
1057
|
+
|
|
1058
|
+
closeMenu() {
|
|
1059
|
+
if (destroyed || !menu) return;
|
|
1060
|
+
menu = null;
|
|
1061
|
+
refresh();
|
|
1062
|
+
},
|
|
1063
|
+
|
|
1064
|
+
zoom(target) {
|
|
1065
|
+
if (destroyed || typeof target !== 'string') return false;
|
|
1066
|
+
const current = view.timeline.preset;
|
|
1067
|
+
const next =
|
|
1068
|
+
target === 'in' || target === 'out'
|
|
1069
|
+
? zoomStep(current, target)
|
|
1070
|
+
: VIEW_PRESETS.find((preset) => preset.id === target);
|
|
1071
|
+
if (!next || next === current || isEqual(next, current)) return false;
|
|
1072
|
+
const { width, scrollLeft } = state.viewport;
|
|
1073
|
+
const middle = width > 0 ? state.timeAxis.xToDate(scrollLeft + width / 2) : null;
|
|
1074
|
+
view = resolveViewOptions({ preset: next }, view);
|
|
1075
|
+
zoomed = true;
|
|
1076
|
+
tooltip = null;
|
|
1077
|
+
refresh();
|
|
1078
|
+
if (middle !== null) {
|
|
1079
|
+
// Keep what was in the middle of the view there.
|
|
1080
|
+
scrollTo = { left: Math.max(0, Math.round(state.timeAxis.dateToX(middle) - width / 2)) };
|
|
1081
|
+
setState({ ...state, scrollTo });
|
|
1082
|
+
}
|
|
1083
|
+
behaviour.onPresetChange?.(next.id);
|
|
1084
|
+
return true;
|
|
1085
|
+
},
|
|
1086
|
+
|
|
1087
|
+
resizeList(action) {
|
|
1088
|
+
if (destroyed) return false;
|
|
1089
|
+
const result = listWidth.act(action);
|
|
1090
|
+
if (!result.used) return false;
|
|
1091
|
+
if (listWidth.state().resizing) {
|
|
1092
|
+
// Nothing else shows while the divider moves.
|
|
1093
|
+
menu = null;
|
|
1094
|
+
hoverPoint = null;
|
|
1095
|
+
tooltip = null;
|
|
1096
|
+
}
|
|
1097
|
+
if (state.list !== listWidth.state() || state.menu !== menu || state.tooltip !== tooltip)
|
|
1098
|
+
setState({ ...state, list: listWidth.state(), menu, tooltip });
|
|
1099
|
+
if (result.changed !== undefined) behaviour.onListWidthChange?.(result.changed);
|
|
1100
|
+
return true;
|
|
1101
|
+
},
|
|
1102
|
+
|
|
1103
|
+
openTaskEditor(id) {
|
|
1104
|
+
if (destroyed) return false;
|
|
1105
|
+
closeEdit();
|
|
1106
|
+
menu = null;
|
|
1107
|
+
return taskEditor.open(id);
|
|
1108
|
+
},
|
|
1109
|
+
|
|
1110
|
+
taskEditorAction(action) {
|
|
1111
|
+
if (destroyed) return false;
|
|
1112
|
+
const used = taskEditor.act(action);
|
|
1113
|
+
announce();
|
|
1114
|
+
return used;
|
|
1115
|
+
},
|
|
1116
|
+
|
|
1117
|
+
doubleClick(point) {
|
|
1118
|
+
if (destroyed || !interactionView || !Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
1119
|
+
return false;
|
|
1120
|
+
const hit = barUnder(interactionView, point);
|
|
1121
|
+
return hit ? controller.openTaskEditor(hit.taskId) : false;
|
|
1122
|
+
},
|
|
1123
|
+
|
|
1124
|
+
revealTop(id) {
|
|
1125
|
+
const index = destroyed ? undefined : visibleRows().rowIndex.get(id);
|
|
1126
|
+
return index === undefined ? null : revealTopOf(index, state.viewport, state.rows.rowHeight);
|
|
1127
|
+
},
|
|
1128
|
+
|
|
1129
|
+
toggle(id) {
|
|
1130
|
+
if (destroyed || getTreeIndex(state.project.tasks).isLeaf(id)) return;
|
|
1131
|
+
const next = new Set(collapsed);
|
|
1132
|
+
if (next.has(id)) next.delete(id);
|
|
1133
|
+
else next.add(id);
|
|
1134
|
+
setCollapsed(next);
|
|
1135
|
+
},
|
|
1136
|
+
|
|
1137
|
+
setExpanded(id, expanded) {
|
|
1138
|
+
if (destroyed || getTreeIndex(state.project.tasks).isLeaf(id) || collapsed.has(id) !== expanded) return;
|
|
1139
|
+
const next = new Set(collapsed);
|
|
1140
|
+
if (expanded) next.delete(id);
|
|
1141
|
+
else next.add(id);
|
|
1142
|
+
setCollapsed(next);
|
|
1143
|
+
},
|
|
1144
|
+
|
|
1145
|
+
expandAll() {
|
|
1146
|
+
if (destroyed) return;
|
|
1147
|
+
setCollapsed(new Set());
|
|
1148
|
+
},
|
|
1149
|
+
|
|
1150
|
+
collapseAll() {
|
|
1151
|
+
if (destroyed) return;
|
|
1152
|
+
const tree = getTreeIndex(state.project.tasks);
|
|
1153
|
+
setCollapsed(new Set(state.project.tasks.order.filter((id) => !tree.isLeaf(id))));
|
|
1154
|
+
},
|
|
1155
|
+
|
|
1156
|
+
destroy() {
|
|
1157
|
+
destroyed = true;
|
|
1158
|
+
binding.destroy();
|
|
1159
|
+
unsubscribeProject();
|
|
1160
|
+
changes.clear();
|
|
1161
|
+
},
|
|
1162
|
+
};
|
|
1163
|
+
return controller;
|
|
1164
|
+
}
|