@salmonui/gantt 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +43 -2
- package/dist/gantt/Gantt.d.ts +5 -0
- package/dist/gantt/Gantt.d.ts.map +1 -0
- package/dist/gantt/Gantt.js +516 -0
- package/dist/gantt/Gantt.js.map +1 -0
- package/dist/gantt/adaptive.d.ts +8 -0
- package/dist/gantt/adaptive.d.ts.map +1 -0
- package/dist/gantt/adaptive.js +38 -0
- package/dist/gantt/adaptive.js.map +1 -0
- package/dist/gantt/api.d.ts +9 -0
- package/dist/gantt/api.d.ts.map +1 -0
- package/dist/gantt/api.js +181 -0
- package/dist/gantt/api.js.map +1 -0
- package/dist/gantt/chart.d.ts +70 -0
- package/dist/gantt/chart.d.ts.map +1 -0
- package/dist/gantt/chart.js +580 -0
- package/dist/gantt/chart.js.map +1 -0
- package/dist/gantt/columns.d.ts +9 -0
- package/dist/gantt/columns.d.ts.map +1 -0
- package/dist/gantt/columns.js +139 -0
- package/dist/gantt/columns.js.map +1 -0
- package/dist/gantt/export.d.ts +53 -0
- package/dist/gantt/export.d.ts.map +1 -0
- package/dist/gantt/export.js +241 -0
- package/dist/gantt/export.js.map +1 -0
- package/dist/gantt/fields.d.ts +42 -0
- package/dist/gantt/fields.d.ts.map +1 -0
- package/dist/gantt/fields.js +235 -0
- package/dist/gantt/fields.js.map +1 -0
- package/dist/gantt/instance.d.ts +214 -0
- package/dist/gantt/instance.d.ts.map +1 -0
- package/dist/gantt/instance.js +1029 -0
- package/dist/gantt/instance.js.map +1 -0
- package/dist/gantt/interact.d.ts +7 -0
- package/dist/gantt/interact.d.ts.map +1 -0
- package/dist/gantt/interact.js +315 -0
- package/dist/gantt/interact.js.map +1 -0
- package/dist/gantt/loading.d.ts +21 -0
- package/dist/gantt/loading.d.ts.map +1 -0
- package/dist/gantt/loading.js +81 -0
- package/dist/gantt/loading.js.map +1 -0
- package/dist/gantt/locale.d.ts +161 -0
- package/dist/gantt/locale.d.ts.map +1 -0
- package/dist/gantt/locale.js +145 -0
- package/dist/gantt/locale.js.map +1 -0
- package/dist/gantt/menu-items.d.ts +8 -0
- package/dist/gantt/menu-items.d.ts.map +1 -0
- package/dist/gantt/menu-items.js +16 -0
- package/dist/gantt/menu-items.js.map +1 -0
- package/dist/gantt/menu-run.d.ts +5 -0
- package/dist/gantt/menu-run.d.ts.map +1 -0
- package/dist/gantt/menu-run.js +69 -0
- package/dist/gantt/menu-run.js.map +1 -0
- package/dist/gantt/modules.d.ts +19 -0
- package/dist/gantt/modules.d.ts.map +1 -0
- package/dist/gantt/modules.js +8 -0
- package/dist/gantt/modules.js.map +1 -0
- package/dist/gantt/popups.d.ts +23 -0
- package/dist/gantt/popups.d.ts.map +1 -0
- package/dist/gantt/popups.js +158 -0
- package/dist/gantt/popups.js.map +1 -0
- package/dist/gantt/registry-lazy.d.ts +4 -0
- package/dist/gantt/registry-lazy.d.ts.map +1 -0
- package/dist/gantt/registry-lazy.js +54 -0
- package/dist/gantt/registry-lazy.js.map +1 -0
- package/dist/gantt/registry-static.d.ts +4 -0
- package/dist/gantt/registry-static.d.ts.map +1 -0
- package/dist/gantt/registry-static.js +13 -0
- package/dist/gantt/registry-static.js.map +1 -0
- package/dist/gantt/remote.d.ts +16 -0
- package/dist/gantt/remote.d.ts.map +1 -0
- package/dist/gantt/remote.js +178 -0
- package/dist/gantt/remote.js.map +1 -0
- package/dist/gantt/resource-view.d.ts +57 -0
- package/dist/gantt/resource-view.d.ts.map +1 -0
- package/dist/gantt/resource-view.js +123 -0
- package/dist/gantt/resource-view.js.map +1 -0
- package/dist/gantt/toolbar.d.ts +11 -0
- package/dist/gantt/toolbar.d.ts.map +1 -0
- package/dist/gantt/toolbar.js +166 -0
- package/dist/gantt/toolbar.js.map +1 -0
- package/dist/gantt/tooltip.d.ts +7 -0
- package/dist/gantt/tooltip.d.ts.map +1 -0
- package/dist/gantt/tooltip.js +180 -0
- package/dist/gantt/tooltip.js.map +1 -0
- package/dist/gantt/types.d.ts +625 -0
- package/dist/gantt/types.d.ts.map +1 -0
- package/dist/gantt/types.js +7 -0
- package/dist/gantt/types.js.map +1 -0
- package/dist/gantt.css +5593 -0
- package/dist/gantt.d.ts +6 -0
- package/dist/gantt.d.ts.map +1 -0
- package/dist/gantt.js +13 -0
- package/dist/gantt.js.map +1 -0
- package/dist/internal/timeline/project/calendar.d.ts +50 -0
- package/dist/internal/timeline/project/calendar.d.ts.map +1 -0
- package/dist/internal/timeline/project/calendar.js +312 -0
- package/dist/internal/timeline/project/calendar.js.map +1 -0
- package/dist/internal/timeline/project/critical.d.ts +34 -0
- package/dist/internal/timeline/project/critical.d.ts.map +1 -0
- package/dist/internal/timeline/project/critical.js +135 -0
- package/dist/internal/timeline/project/critical.js.map +1 -0
- package/dist/internal/timeline/project/dependencies.d.ts +16 -0
- package/dist/internal/timeline/project/dependencies.d.ts.map +1 -0
- package/dist/internal/timeline/project/dependencies.js +87 -0
- package/dist/internal/timeline/project/dependencies.js.map +1 -0
- package/dist/internal/timeline/project/engine.d.ts +118 -0
- package/dist/internal/timeline/project/engine.d.ts.map +1 -0
- package/dist/internal/timeline/project/engine.js +1907 -0
- package/dist/internal/timeline/project/engine.js.map +1 -0
- package/dist/internal/timeline/project/format.d.ts +22 -0
- package/dist/internal/timeline/project/format.d.ts.map +1 -0
- package/dist/internal/timeline/project/format.js +41 -0
- package/dist/internal/timeline/project/format.js.map +1 -0
- package/dist/internal/timeline/project/history.d.ts +27 -0
- package/dist/internal/timeline/project/history.d.ts.map +1 -0
- package/dist/internal/timeline/project/history.js +74 -0
- package/dist/internal/timeline/project/history.js.map +1 -0
- package/dist/internal/timeline/project/resources.d.ts +26 -0
- package/dist/internal/timeline/project/resources.d.ts.map +1 -0
- package/dist/internal/timeline/project/resources.js +109 -0
- package/dist/internal/timeline/project/resources.js.map +1 -0
- package/dist/internal/timeline/project/types.d.ts +99 -0
- package/dist/internal/timeline/project/types.d.ts.map +1 -0
- package/dist/internal/timeline/project/types.js +6 -0
- package/dist/internal/timeline/project/types.js.map +1 -0
- package/dist/internal/timeline/scale.d.ts +60 -0
- package/dist/internal/timeline/scale.d.ts.map +1 -0
- package/dist/internal/timeline/scale.js +156 -0
- package/dist/internal/timeline/scale.js.map +1 -0
- package/package.json +55 -4
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
// The Gantt instance: one
|
|
2
|
+
// per component, created from the initial props and synced in a layout
|
|
3
|
+
// effect (the DataGridInstance model). It owns the project engine, the
|
|
4
|
+
// grid-pane rows (data rows with the computed roles written), the time
|
|
5
|
+
// scale and zoom, the undo history and the chart DOM targets; React reads it
|
|
6
|
+
// through `useSyncExternalStore` on `version`. Drag and scroll transients
|
|
7
|
+
// never pass through here as React state.
|
|
8
|
+
import { formatMessage, localeFirstDay } from '@salmonui/core/internal/dates/index';
|
|
9
|
+
import { gridEvent } from '@salmonui/core/internal/events';
|
|
10
|
+
import { createProjectEngine } from '../internal/timeline/project/engine.js';
|
|
11
|
+
import { peekGanttModules, readGanttModules } from '#gantt-modules';
|
|
12
|
+
import { createScale, fitLevel, nearestLevel, VIEW_MODES, ZOOM_LEVELS } from '../internal/timeline/scale.js';
|
|
13
|
+
import { DAY } from '@salmonui/core/internal/timeline/axis';
|
|
14
|
+
import { dateOf, flatten, resolveFields, toTaskInput, wallOf, writeRow } from './fields.js';
|
|
15
|
+
import { GANTT_DEFAULT_LOCALE_TEXT } from './locale.js';
|
|
16
|
+
import { RESOURCE_KEY, ROW_KEY, TASK_KEY } from './fields.js';
|
|
17
|
+
const APPLIED = Object.freeze({ status: 'applied' });
|
|
18
|
+
const rejected = (reason) => Object.freeze({ status: 'rejected', reason });
|
|
19
|
+
/** Zoom ladder of the props (custom `zoomLevels`, else the built-in levels with the `timeline` tiers at the configured level). */
|
|
20
|
+
function levelsOf(props) {
|
|
21
|
+
if (!props.zoomLevels?.length)
|
|
22
|
+
return ZOOM_LEVELS;
|
|
23
|
+
return props.zoomLevels.map((l, i) => ({
|
|
24
|
+
level: i,
|
|
25
|
+
top: l.top ? { unit: l.top.unit, count: l.top.count ?? 1 } : null,
|
|
26
|
+
bottom: { unit: l.bottom.unit, count: l.bottom.count ?? 1 },
|
|
27
|
+
unitWidth: l.unitWidth ?? 33,
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
/** The level of the `timeline` prop (mode preset, explicit tiers, unit width). */
|
|
31
|
+
function timelineLevel(props) {
|
|
32
|
+
const t = props.timeline;
|
|
33
|
+
const base = VIEW_MODES[t?.mode ?? 'week'];
|
|
34
|
+
return {
|
|
35
|
+
level: t?.topTier || t?.bottomTier || t?.unitWidth || (t?.mode && t.mode !== 'week') ? -1 : base.level,
|
|
36
|
+
top: t?.topTier === null ? null : t?.topTier ? { unit: t.topTier.unit, count: t.topTier.count ?? 1 } : base.top,
|
|
37
|
+
bottom: t?.bottomTier ? { unit: t.bottomTier.unit, count: t.bottomTier.count ?? 1 } : base.bottom,
|
|
38
|
+
unitWidth: t?.unitWidth ?? base.unitWidth,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Two zoom levels with the same tiers and unit width (an inline `timeline` object is a new reference every render). */
|
|
42
|
+
function sameLevel(a, b) {
|
|
43
|
+
return a.level === b.level && a.unitWidth === b.unitWidth && a.bottom.unit === b.bottom.unit && a.bottom.count === b.bottom.count && (a.top === null ? b.top === null : b.top !== null && a.top.unit === b.top.unit && a.top.count === b.top.count);
|
|
44
|
+
}
|
|
45
|
+
// A pure memo of `Intl` objects, not render state: every duration cell formats a number.
|
|
46
|
+
const numberFormats = {};
|
|
47
|
+
/** Numbers in the digits and decimal separator of `locale`; an invalid tag uses `en-US`. */
|
|
48
|
+
function numbersOf(locale = 'en-US') {
|
|
49
|
+
try {
|
|
50
|
+
return (numberFormats[locale] ??= new Intl.NumberFormat(locale, { useGrouping: false }));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return numbersOf();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export class GanttInstance {
|
|
57
|
+
props;
|
|
58
|
+
fields;
|
|
59
|
+
ctx;
|
|
60
|
+
engine;
|
|
61
|
+
history = null;
|
|
62
|
+
detachHistory = null;
|
|
63
|
+
/** Data as given (flattened) and the rows the grid shows. */
|
|
64
|
+
flat = { rows: [], ids: [], parents: [] };
|
|
65
|
+
source = new Map();
|
|
66
|
+
rows = [];
|
|
67
|
+
/** The rows the grid receives: `rows`, or the resource view rows. */
|
|
68
|
+
gridRows = [];
|
|
69
|
+
rowById = new Map();
|
|
70
|
+
/** Resource view models per version (resource-view.ts). */
|
|
71
|
+
resourceCache = null;
|
|
72
|
+
resourceIds = new Set();
|
|
73
|
+
/** The array last sent to `onDataChange` (a round trip through `data` is not a reload). */
|
|
74
|
+
emitted = null;
|
|
75
|
+
loadedData = null;
|
|
76
|
+
levels;
|
|
77
|
+
level;
|
|
78
|
+
zoomIndex;
|
|
79
|
+
scale;
|
|
80
|
+
/** Extra time added by `extendTimeline` / infinite scroll (ms before and after the project range). */
|
|
81
|
+
extendBefore = 0;
|
|
82
|
+
extendAfter = 0;
|
|
83
|
+
critical = null;
|
|
84
|
+
loading = false;
|
|
85
|
+
/** Bumped when remote rows join the engine: the grid's start and finish cells read the computed dates. */
|
|
86
|
+
adopted = 0;
|
|
87
|
+
/** Tasks with a remote write in flight. */
|
|
88
|
+
pending = new Set();
|
|
89
|
+
/** Remote writer of a writable source (`#gantt-modules` `remote`). */
|
|
90
|
+
remote = null;
|
|
91
|
+
/** Lazy parents whose children are loading (skeleton row). */
|
|
92
|
+
loadingParents = new Set();
|
|
93
|
+
/** Adaptive layout: the container is under the breakpoint; the pane shown. */
|
|
94
|
+
narrow = false;
|
|
95
|
+
pane = 'grid';
|
|
96
|
+
version = 0;
|
|
97
|
+
listeners = new Set();
|
|
98
|
+
gridApi = null;
|
|
99
|
+
chartRoot = null;
|
|
100
|
+
chartScroll = null;
|
|
101
|
+
/** Dialog state (lazy dialog chunk). */
|
|
102
|
+
dialog = null;
|
|
103
|
+
/** A pending conflict from a user edit (built-in conflict dialog). */
|
|
104
|
+
conflict = null;
|
|
105
|
+
splitterPosition = null;
|
|
106
|
+
/** The public API (set by the component). */
|
|
107
|
+
api = null;
|
|
108
|
+
/** The chart date under the last context menu (split position). */
|
|
109
|
+
menuDate = null;
|
|
110
|
+
/** Critical path toggled from the toolbar without the `criticalPath` prop. */
|
|
111
|
+
criticalToggled = false;
|
|
112
|
+
/** Date formatters of the current locale (set by the component). */
|
|
113
|
+
formatters = null;
|
|
114
|
+
constructor(props) {
|
|
115
|
+
this.props = props;
|
|
116
|
+
this.fields = resolveFields(props.taskFields);
|
|
117
|
+
this.ctx = this.mapContext(props);
|
|
118
|
+
this.engine = createProjectEngine(this.engineOptions(props));
|
|
119
|
+
this.levels = levelsOf(props);
|
|
120
|
+
const initial = props.zoom ?? props.defaultZoom;
|
|
121
|
+
this.level = initial !== undefined ? this.levels[Math.max(0, Math.min(this.levels.length - 1, initial))] : timelineLevel(props);
|
|
122
|
+
this.zoomIndex = initial ?? nearestLevel(this.level, this.levels);
|
|
123
|
+
this.load(props.data);
|
|
124
|
+
this.scale = this.buildScale();
|
|
125
|
+
}
|
|
126
|
+
// ------------------------------------------------------------------
|
|
127
|
+
// Store
|
|
128
|
+
// ------------------------------------------------------------------
|
|
129
|
+
subscribe = (listener) => {
|
|
130
|
+
this.listeners.add(listener);
|
|
131
|
+
return () => this.listeners.delete(listener);
|
|
132
|
+
};
|
|
133
|
+
getVersion = () => this.version;
|
|
134
|
+
emit() {
|
|
135
|
+
this.version++;
|
|
136
|
+
for (const l of this.listeners)
|
|
137
|
+
l();
|
|
138
|
+
}
|
|
139
|
+
// ------------------------------------------------------------------
|
|
140
|
+
// Props
|
|
141
|
+
// ------------------------------------------------------------------
|
|
142
|
+
mapContext(props) {
|
|
143
|
+
return { fields: this.fields, timeZone: props.timeZone, resourceIdField: props.resourceFields?.id ?? 'id', resourceUnitField: props.resourceFields?.unit ?? 'unit' };
|
|
144
|
+
}
|
|
145
|
+
wall(value) {
|
|
146
|
+
return wallOf(value, this.props.timeZone);
|
|
147
|
+
}
|
|
148
|
+
date(wall) {
|
|
149
|
+
return dateOf(wall, this.props.timeZone);
|
|
150
|
+
}
|
|
151
|
+
exceptionsOf(list) {
|
|
152
|
+
const out = [];
|
|
153
|
+
for (const h of list ?? []) {
|
|
154
|
+
const s = this.wall(h.start);
|
|
155
|
+
if (s === null)
|
|
156
|
+
continue;
|
|
157
|
+
const e = this.wall(h.end ?? h.start) ?? s;
|
|
158
|
+
const day = Math.floor(s / DAY) * DAY;
|
|
159
|
+
out.push({ start: day, end: Math.floor(e / DAY) * DAY + DAY, ...(h.ranges ? { ranges: h.ranges } : {}) });
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
engineOptions(props) {
|
|
164
|
+
const o = {
|
|
165
|
+
workingTime: props.workingTime,
|
|
166
|
+
holidays: this.exceptionsOf(props.holidays),
|
|
167
|
+
calendars: props.calendars?.map((c) => ({ id: c.id, workingTime: c.workingTime, exceptions: this.exceptionsOf(c.exceptions) })),
|
|
168
|
+
projectCalendar: props.projectCalendar,
|
|
169
|
+
taskMode: props.taskMode,
|
|
170
|
+
autoSchedule: props.autoSchedule,
|
|
171
|
+
projectStart: props.projectStart === undefined ? undefined : this.wall(props.projectStart),
|
|
172
|
+
projectEnd: props.projectEnd === undefined ? undefined : this.wall(props.projectEnd),
|
|
173
|
+
durationUnit: props.durationUnit,
|
|
174
|
+
workUnit: props.workUnit,
|
|
175
|
+
taskType: props.taskType,
|
|
176
|
+
dependencyTypes: props.dependencyTypes,
|
|
177
|
+
validateDependencies: props.validateDependencies,
|
|
178
|
+
validateManualTasks: props.validateManualTasks,
|
|
179
|
+
parentDependencies: props.parentDependencies,
|
|
180
|
+
dependencyLag: props.dependencyLag,
|
|
181
|
+
unscheduledTasks: props.unscheduledTasks,
|
|
182
|
+
};
|
|
183
|
+
for (const k of Object.keys(o))
|
|
184
|
+
if (o[k] === undefined)
|
|
185
|
+
delete o[k];
|
|
186
|
+
return o;
|
|
187
|
+
}
|
|
188
|
+
historyToken = 0;
|
|
189
|
+
/**
|
|
190
|
+
* Undo history: a lazy chunk attached after mount (the
|
|
191
|
+
* component's effect), so a page without `undo` never loads it and the
|
|
192
|
+
* server render does no work for it.
|
|
193
|
+
*/
|
|
194
|
+
setupHistory(undo) {
|
|
195
|
+
const token = ++this.historyToken;
|
|
196
|
+
const off = () => {
|
|
197
|
+
this.detachHistory?.();
|
|
198
|
+
this.detachHistory = null;
|
|
199
|
+
this.history = null;
|
|
200
|
+
};
|
|
201
|
+
off();
|
|
202
|
+
if (!undo)
|
|
203
|
+
return off;
|
|
204
|
+
void import('../internal/timeline/project/history.js').then((m) => {
|
|
205
|
+
if (token !== this.historyToken)
|
|
206
|
+
return;
|
|
207
|
+
const opts = undo === true ? {} : undo;
|
|
208
|
+
this.history = m.createHistory(opts.limit ?? 50);
|
|
209
|
+
this.detachHistory = m.attachHistory(this.engine, this.history, opts.actions ? { actions: opts.actions } : {});
|
|
210
|
+
this.emit();
|
|
211
|
+
});
|
|
212
|
+
return () => {
|
|
213
|
+
if (token === this.historyToken) {
|
|
214
|
+
this.historyToken++;
|
|
215
|
+
off();
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
idOf = (row) => String(row[this.fields.id]);
|
|
220
|
+
/** Full load of `data`. */
|
|
221
|
+
load(data) {
|
|
222
|
+
this.loadedData = data;
|
|
223
|
+
this.remote?.dispose();
|
|
224
|
+
const mod = peekGanttModules().remote;
|
|
225
|
+
this.remote = mod?.writableSource(data) ? mod.createRemoteWriter(this) : null;
|
|
226
|
+
if (!Array.isArray(data)) {
|
|
227
|
+
// A remote source: the grid pane reads it; the chart follows the rows the grid loads.
|
|
228
|
+
this.flat = { rows: [], ids: [], parents: [] };
|
|
229
|
+
this.engine.load([]);
|
|
230
|
+
this.rebuildRows(null);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.flat = flatten(data, this.fields, this.idOf);
|
|
234
|
+
this.source = new Map();
|
|
235
|
+
const tasks = [];
|
|
236
|
+
for (let i = 0; i < this.flat.rows.length; i++) {
|
|
237
|
+
const id = this.flat.ids[i];
|
|
238
|
+
const row = this.flat.rows[i];
|
|
239
|
+
this.source.set(id, row);
|
|
240
|
+
tasks.push(toTaskInput(row, id, this.flat.parents[i] ?? null, this.ctx));
|
|
241
|
+
}
|
|
242
|
+
this.engine.load(tasks);
|
|
243
|
+
this.history?.clear();
|
|
244
|
+
if (this.props.criticalPath)
|
|
245
|
+
this.computeCritical();
|
|
246
|
+
this.rebuildRows(null);
|
|
247
|
+
}
|
|
248
|
+
/** Remote rows the grid loaded: added to the engine with their computed dates. */
|
|
249
|
+
adoptRemoteRows(rows, parentOf) {
|
|
250
|
+
let added = false;
|
|
251
|
+
for (const row of rows) {
|
|
252
|
+
const id = this.idOf(row);
|
|
253
|
+
if (this.source.has(id))
|
|
254
|
+
continue;
|
|
255
|
+
this.source.set(id, row);
|
|
256
|
+
this.engine.add(toTaskInput(row, id, parentOf(row), this.ctx));
|
|
257
|
+
added = true;
|
|
258
|
+
}
|
|
259
|
+
// Adopted rows are the source's state, not a change to write back.
|
|
260
|
+
this.remote?.discard();
|
|
261
|
+
if (added) {
|
|
262
|
+
this.adopted++;
|
|
263
|
+
this.rebuildRows(null);
|
|
264
|
+
this.scale = this.buildScale();
|
|
265
|
+
this.emit();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** Rebuilds the grid rows (all, or the changed ids only). */
|
|
269
|
+
rebuildRows(changed) {
|
|
270
|
+
const tasks = this.engine.getTasks();
|
|
271
|
+
const out = new Array(tasks.length);
|
|
272
|
+
const next = new Map();
|
|
273
|
+
let same = tasks.length === this.rows.length;
|
|
274
|
+
for (let i = 0; i < tasks.length; i++) {
|
|
275
|
+
const t = tasks[i];
|
|
276
|
+
const prev = this.rowById.get(t.id);
|
|
277
|
+
let row;
|
|
278
|
+
if (prev && changed && !changed.has(t.id))
|
|
279
|
+
row = prev;
|
|
280
|
+
else {
|
|
281
|
+
const base = this.source.get(t.id) ?? prev;
|
|
282
|
+
const schedule = this.engine.getSchedule(t.id);
|
|
283
|
+
row = base && schedule ? writeRow(base, schedule, t.parentId, this.ctx, prev) : prev;
|
|
284
|
+
}
|
|
285
|
+
if (!row)
|
|
286
|
+
continue;
|
|
287
|
+
out[i] = row;
|
|
288
|
+
next.set(t.id, row);
|
|
289
|
+
if (same && this.rows[i] !== row)
|
|
290
|
+
same = false;
|
|
291
|
+
}
|
|
292
|
+
this.rowById = next;
|
|
293
|
+
if (!same)
|
|
294
|
+
this.rows = out.filter(Boolean);
|
|
295
|
+
this.rebuildGridRows(!same);
|
|
296
|
+
}
|
|
297
|
+
/** Resource view: the grid rows grouped by resource. */
|
|
298
|
+
resourceMode() {
|
|
299
|
+
return !!this.props.resourceView && Array.isArray(this.props.data);
|
|
300
|
+
}
|
|
301
|
+
gridRowsFor = null;
|
|
302
|
+
rebuildGridRows(force) {
|
|
303
|
+
// The resource view module comes from `#gantt-modules`; the component suspends until it is loaded.
|
|
304
|
+
const mod = this.resourceMode() ? peekGanttModules().resources : undefined;
|
|
305
|
+
const on = !!mod;
|
|
306
|
+
const idField = this.props.resourceFields?.id ?? 'id';
|
|
307
|
+
this.resourceIds = new Set((this.props.resources ?? []).map((r) => String(r[idField])));
|
|
308
|
+
const k = this.gridRowsFor;
|
|
309
|
+
if (!force && k && k.rows === this.rows && k.resources === this.props.resources && k.on === on)
|
|
310
|
+
return;
|
|
311
|
+
this.gridRowsFor = { rows: this.rows, resources: this.props.resources, on };
|
|
312
|
+
this.gridRows = mod ? mod.buildResourceRows(this, (id) => this.rowById.get(id)) : this.rows;
|
|
313
|
+
}
|
|
314
|
+
isResource(id) {
|
|
315
|
+
return this.resourceIds.has(id);
|
|
316
|
+
}
|
|
317
|
+
/** Grid row key (the task id, or the resource view key). */
|
|
318
|
+
keyOf = (row) => {
|
|
319
|
+
const k = row[ROW_KEY];
|
|
320
|
+
return k === undefined ? this.idOf(row) : String(k);
|
|
321
|
+
};
|
|
322
|
+
/** Task id of a grid row; null for a resource row. */
|
|
323
|
+
taskIdOf(row) {
|
|
324
|
+
const r = row;
|
|
325
|
+
if (r[ROW_KEY] === undefined)
|
|
326
|
+
return this.idOf(row);
|
|
327
|
+
return r[TASK_KEY] === undefined ? null : String(r[TASK_KEY]);
|
|
328
|
+
}
|
|
329
|
+
/** Task id of a grid row key (resource view keys `id@resource`). */
|
|
330
|
+
taskIdOfKey(key) {
|
|
331
|
+
if (!this.resourceMode())
|
|
332
|
+
return key;
|
|
333
|
+
if (key.startsWith('__res:'))
|
|
334
|
+
return null;
|
|
335
|
+
if (this.engine.getTask(key))
|
|
336
|
+
return key;
|
|
337
|
+
const at = key.lastIndexOf('@');
|
|
338
|
+
return at > 0 ? key.slice(0, at) : key;
|
|
339
|
+
}
|
|
340
|
+
/** Resource group of a grid row in the resource view. */
|
|
341
|
+
groupOf(row) {
|
|
342
|
+
const v = row[RESOURCE_KEY];
|
|
343
|
+
return typeof v === 'string' ? v : undefined;
|
|
344
|
+
}
|
|
345
|
+
syncProps(props) {
|
|
346
|
+
const prev = this.props;
|
|
347
|
+
this.props = props;
|
|
348
|
+
let changed = false;
|
|
349
|
+
if (props.taskFields !== prev.taskFields || props.timeZone !== prev.timeZone || props.resourceFields !== prev.resourceFields) {
|
|
350
|
+
this.fields = resolveFields(props.taskFields);
|
|
351
|
+
this.ctx = this.mapContext(props);
|
|
352
|
+
}
|
|
353
|
+
const engineKeys = ['workingTime', 'holidays', 'calendars', 'projectCalendar', 'taskMode', 'autoSchedule', 'projectStart', 'projectEnd', 'durationUnit', 'workUnit', 'taskType', 'dependencyTypes', 'validateDependencies', 'validateManualTasks', 'parentDependencies', 'dependencyLag', 'unscheduledTasks', 'timeZone'];
|
|
354
|
+
if (engineKeys.some((k) => props[k] !== prev[k])) {
|
|
355
|
+
this.engine.setOptions(this.engineOptions(props));
|
|
356
|
+
changed = true;
|
|
357
|
+
}
|
|
358
|
+
if (props.data !== this.loadedData && props.data !== this.emitted) {
|
|
359
|
+
this.load(props.data);
|
|
360
|
+
changed = true;
|
|
361
|
+
}
|
|
362
|
+
else if (props.data === this.emitted)
|
|
363
|
+
this.loadedData = props.data;
|
|
364
|
+
else if (changed || props.taskFields !== prev.taskFields)
|
|
365
|
+
this.rebuildRows(null);
|
|
366
|
+
if (props.resourceView !== prev.resourceView || props.resources !== prev.resources || props.overAllocation !== prev.overAllocation || props.taskbarOverlap !== prev.taskbarOverlap || props.multiTaskbar !== prev.multiTaskbar) {
|
|
367
|
+
this.rebuildGridRows(true);
|
|
368
|
+
changed = true;
|
|
369
|
+
}
|
|
370
|
+
if (props.criticalPath !== prev.criticalPath || (changed && props.criticalPath)) {
|
|
371
|
+
this.critical = null;
|
|
372
|
+
if (props.criticalPath)
|
|
373
|
+
this.computeCritical();
|
|
374
|
+
changed = true;
|
|
375
|
+
}
|
|
376
|
+
if (props.zoomLevels !== prev.zoomLevels)
|
|
377
|
+
this.levels = levelsOf(props);
|
|
378
|
+
if (props.zoom !== undefined && props.zoom !== prev.zoom && props.zoom !== this.zoomIndex) {
|
|
379
|
+
this.applyZoom(props.zoom, 'api', false);
|
|
380
|
+
changed = true;
|
|
381
|
+
}
|
|
382
|
+
else if (props.timeline !== prev.timeline && props.zoom === undefined) {
|
|
383
|
+
// Only a different level resets the zoom; an equal one (a re-created object) keeps zoomIn / zoomOut / zoomToFit.
|
|
384
|
+
const next = timelineLevel(props);
|
|
385
|
+
if (!sameLevel(next, timelineLevel(prev))) {
|
|
386
|
+
this.level = next;
|
|
387
|
+
this.zoomIndex = nearestLevel(this.level, this.levels);
|
|
388
|
+
}
|
|
389
|
+
changed = true;
|
|
390
|
+
}
|
|
391
|
+
if (changed || props.projectStart !== prev.projectStart || props.projectEnd !== prev.projectEnd || props.timeline?.weekStart !== prev.timeline?.weekStart || props.locale !== prev.locale) {
|
|
392
|
+
this.scale = this.buildScale();
|
|
393
|
+
changed = true;
|
|
394
|
+
}
|
|
395
|
+
if (changed)
|
|
396
|
+
this.emit();
|
|
397
|
+
}
|
|
398
|
+
// ------------------------------------------------------------------
|
|
399
|
+
// Text
|
|
400
|
+
// ------------------------------------------------------------------
|
|
401
|
+
text = (key, params) => {
|
|
402
|
+
const lt = this.props.localeText;
|
|
403
|
+
return formatMessage(lt?.[key] ?? GANTT_DEFAULT_LOCALE_TEXT[key], params, this.props.locale ?? 'en-US');
|
|
404
|
+
};
|
|
405
|
+
/** Duration and work text: the count in the locale's digits and decimal separator (`parseDuration` reads it back). */
|
|
406
|
+
units(unit, value) {
|
|
407
|
+
const count = Math.round(value * 100) / 100;
|
|
408
|
+
return this.text(unit, { count }).replace(String(count), numbersOf(this.props.locale).format(count));
|
|
409
|
+
}
|
|
410
|
+
/** `'3 days'`. */
|
|
411
|
+
formatDuration(id) {
|
|
412
|
+
const s = this.engine.getSchedule(String(id));
|
|
413
|
+
if (!s)
|
|
414
|
+
return '';
|
|
415
|
+
return this.units(s.durationUnit, s.duration);
|
|
416
|
+
}
|
|
417
|
+
/** Parses `'3 days'`, `'2h'`, `'1.5'` with the localized unit words and the locale's digits; a bare number takes `durationUnit`. */
|
|
418
|
+
parseDuration(text) {
|
|
419
|
+
// The locale's digits (9 to 0, one character each) as Latin ones.
|
|
420
|
+
const digits = [...numbersOf(this.props.locale).format(9876543210)].reverse();
|
|
421
|
+
const m = /^\s*(-?\d+(?:[.,٫]\d+)?)\s*(\S*)\s*$/u.exec(text.replace(/./gu, (c) => String(digits.indexOf(c)).replace('-1', c)));
|
|
422
|
+
if (!m)
|
|
423
|
+
return null;
|
|
424
|
+
const value = Number.parseFloat(m[1].replace(/[,٫]/, '.'));
|
|
425
|
+
const word = (m[2] ?? '').toLowerCase();
|
|
426
|
+
if (!word)
|
|
427
|
+
return { value, unit: this.props.durationUnit ?? 'day' };
|
|
428
|
+
for (const unit of ['minute', 'hour', 'day']) {
|
|
429
|
+
const words = this.text(`${unit}Units`).toLowerCase().split(',').map((w) => w.trim());
|
|
430
|
+
if (words.includes(word))
|
|
431
|
+
return { value, unit };
|
|
432
|
+
}
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
/** Work in `workUnit`. */
|
|
436
|
+
formatWork(id) {
|
|
437
|
+
const s = this.engine.getSchedule(String(id));
|
|
438
|
+
if (!s)
|
|
439
|
+
return '';
|
|
440
|
+
return this.units(this.props.workUnit ?? 'hour', s.work);
|
|
441
|
+
}
|
|
442
|
+
// ------------------------------------------------------------------
|
|
443
|
+
// Scale and zoom
|
|
444
|
+
// ------------------------------------------------------------------
|
|
445
|
+
weekStart() {
|
|
446
|
+
return this.props.timeline?.weekStart ?? localeFirstDay(this.props.locale ?? 'en-US');
|
|
447
|
+
}
|
|
448
|
+
projectRange() {
|
|
449
|
+
const r = this.engine.projectRange();
|
|
450
|
+
if (r)
|
|
451
|
+
return r;
|
|
452
|
+
// No tasks: the project start, else a fixed anchor (never today: SSR determinism).
|
|
453
|
+
const s = this.wall(this.props.projectStart) ?? Date.UTC(2026, 0, 1);
|
|
454
|
+
return { start: s, end: s + 7 * DAY };
|
|
455
|
+
}
|
|
456
|
+
buildScale() {
|
|
457
|
+
const r = this.projectRange();
|
|
458
|
+
return createScale(r.start - this.extendBefore, r.end + this.extendAfter, this.level, this.weekStart());
|
|
459
|
+
}
|
|
460
|
+
applyZoom(index, source, notify = true) {
|
|
461
|
+
const i = Math.max(0, Math.min(this.levels.length - 1, Math.round(index)));
|
|
462
|
+
const prev = this.zoomIndex;
|
|
463
|
+
if (notify && this.props.onZoomChange) {
|
|
464
|
+
const e = gridEvent('zoomChange', source, { level: i, previous: prev });
|
|
465
|
+
this.props.onZoomChange(e);
|
|
466
|
+
if (e.defaultPrevented)
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
// A controlled `zoom` waits for the prop.
|
|
470
|
+
if (notify && this.props.zoom !== undefined)
|
|
471
|
+
return;
|
|
472
|
+
this.zoomIndex = i;
|
|
473
|
+
this.level = this.levels[i];
|
|
474
|
+
this.rescale();
|
|
475
|
+
}
|
|
476
|
+
/** Rebuilds the scale keeping the date at the chart's inline start in view. */
|
|
477
|
+
rescale() {
|
|
478
|
+
const el = this.chartScroll;
|
|
479
|
+
const anchor = el ? this.scale.start + Math.abs(el.scrollLeft) / this.scale.pxPerMs : null;
|
|
480
|
+
this.scale = this.buildScale();
|
|
481
|
+
this.emit();
|
|
482
|
+
if (el && anchor !== null) {
|
|
483
|
+
const x = Math.max(0, (anchor - this.scale.start) * this.scale.pxPerMs);
|
|
484
|
+
queueMicrotask(() => this.setScrollLeft(x));
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
zoomToFit(source) {
|
|
488
|
+
const r = this.projectRange();
|
|
489
|
+
const width = this.chartScroll?.clientWidth ?? 800;
|
|
490
|
+
let level = fitLevel(r.start, r.end, width, this.levels);
|
|
491
|
+
// Whole top-tier units widen the scale: shrink the unit width until it fits.
|
|
492
|
+
for (let i = 0; i < 4; i++) {
|
|
493
|
+
const w = createScale(r.start, r.end, level, this.weekStart()).width;
|
|
494
|
+
if (w <= width)
|
|
495
|
+
break;
|
|
496
|
+
level = { ...level, unitWidth: Math.max(4, Math.floor((level.unitWidth * width) / w)) };
|
|
497
|
+
}
|
|
498
|
+
this.level = level;
|
|
499
|
+
this.zoomIndex = this.level.level;
|
|
500
|
+
this.extendBefore = 0;
|
|
501
|
+
this.extendAfter = 0;
|
|
502
|
+
this.props.onZoomChange?.(gridEvent('zoomChange', source, { level: this.zoomIndex, previous: this.zoomIndex }));
|
|
503
|
+
this.scale = this.buildScale();
|
|
504
|
+
this.emit();
|
|
505
|
+
queueMicrotask(() => this.setScrollLeft(0));
|
|
506
|
+
}
|
|
507
|
+
extendTimeline(side) {
|
|
508
|
+
const step = (this.chartScroll?.clientWidth ?? 600) / this.scale.pxPerMs;
|
|
509
|
+
if (side === 'start')
|
|
510
|
+
this.extendBefore += step;
|
|
511
|
+
else
|
|
512
|
+
this.extendAfter += step;
|
|
513
|
+
const before = this.scale.start;
|
|
514
|
+
const el = this.chartScroll;
|
|
515
|
+
const left = el ? Math.abs(el.scrollLeft) : 0;
|
|
516
|
+
this.scale = this.buildScale();
|
|
517
|
+
this.emit();
|
|
518
|
+
if (side === 'start')
|
|
519
|
+
queueMicrotask(() => this.setScrollLeft(left + (before - this.scale.start) * this.scale.pxPerMs));
|
|
520
|
+
}
|
|
521
|
+
setScrollLeft(x) {
|
|
522
|
+
const el = this.chartScroll;
|
|
523
|
+
if (!el)
|
|
524
|
+
return;
|
|
525
|
+
const rtl = this.isRtl();
|
|
526
|
+
el.scrollLeft = rtl ? -x : x;
|
|
527
|
+
}
|
|
528
|
+
isRtl() {
|
|
529
|
+
const el = this.chartRoot;
|
|
530
|
+
return !!el && getComputedStyle(el).direction === 'rtl';
|
|
531
|
+
}
|
|
532
|
+
scrollToDate(date) {
|
|
533
|
+
const w = this.wall(date);
|
|
534
|
+
if (w === null)
|
|
535
|
+
return;
|
|
536
|
+
this.setScrollLeft(Math.max(0, (w - this.scale.start) * this.scale.pxPerMs));
|
|
537
|
+
}
|
|
538
|
+
// ------------------------------------------------------------------
|
|
539
|
+
// Critical path
|
|
540
|
+
// ------------------------------------------------------------------
|
|
541
|
+
criticalLoading = false;
|
|
542
|
+
/**
|
|
543
|
+
* Marks the critical tasks. The backward pass is a `#gantt-modules` module
|
|
544
|
+
* (the component waits for it with `criticalPath`); a toolbar toggle or an
|
|
545
|
+
* API call before it loaded requests it and marks the bars when it arrives.
|
|
546
|
+
*/
|
|
547
|
+
computeCritical() {
|
|
548
|
+
const impl = peekGanttModules().critical?.computeCritical;
|
|
549
|
+
if (impl)
|
|
550
|
+
this.critical = this.engine.computeCriticalPath(impl).critical;
|
|
551
|
+
else
|
|
552
|
+
this.loadCritical();
|
|
553
|
+
}
|
|
554
|
+
loadCritical() {
|
|
555
|
+
if (this.criticalLoading)
|
|
556
|
+
return;
|
|
557
|
+
this.criticalLoading = true;
|
|
558
|
+
void Promise.resolve(readGanttModules(['critical'])).then(() => {
|
|
559
|
+
this.criticalLoading = false;
|
|
560
|
+
if (this.props.criticalPath || this.criticalToggled) {
|
|
561
|
+
this.computeCritical();
|
|
562
|
+
this.emit();
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
// ------------------------------------------------------------------
|
|
567
|
+
// Commits
|
|
568
|
+
// ------------------------------------------------------------------
|
|
569
|
+
/** Applies an engine result: rows, critical path, scale, events. */
|
|
570
|
+
commit(result, action, ids, source, silent = false) {
|
|
571
|
+
const changed = result ? result.changed : null;
|
|
572
|
+
this.rebuildRows(changed);
|
|
573
|
+
if (this.props.criticalPath)
|
|
574
|
+
this.computeCritical();
|
|
575
|
+
const r = this.projectRange();
|
|
576
|
+
if (r.start < this.scale.start || r.end > this.scale.end)
|
|
577
|
+
this.scale = this.buildScale();
|
|
578
|
+
this.emit();
|
|
579
|
+
if (silent)
|
|
580
|
+
return;
|
|
581
|
+
this.remote?.committed(changed, action, ids);
|
|
582
|
+
const rows = ids.map((id) => this.rowById.get(id)).filter((x) => x !== undefined);
|
|
583
|
+
if (this.props.onTaskChange || this.props.onDataChange) {
|
|
584
|
+
const event = gridEvent('taskChange', source, { action, taskIds: ids, rows });
|
|
585
|
+
this.props.onTaskChange?.(event);
|
|
586
|
+
if (this.props.onDataChange) {
|
|
587
|
+
const data = this.outputData();
|
|
588
|
+
this.emitted = data;
|
|
589
|
+
this.props.onDataChange(data, event);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
/** The rows in the shape of the input (`children` nesting kept for nested data). */
|
|
594
|
+
outputData() {
|
|
595
|
+
const childKey = this.fields.children;
|
|
596
|
+
const tasks = this.engine.getTasks();
|
|
597
|
+
const parentField = this.fields.parentId;
|
|
598
|
+
if (!childKey) {
|
|
599
|
+
return tasks.map((t) => {
|
|
600
|
+
const row = { ...this.rowById.get(t.id) };
|
|
601
|
+
delete row['__salmonParentId'];
|
|
602
|
+
row[parentField] = t.parentId;
|
|
603
|
+
return row;
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
const kids = new Map();
|
|
607
|
+
const built = new Map();
|
|
608
|
+
for (const t of tasks) {
|
|
609
|
+
const row = { ...this.rowById.get(t.id) };
|
|
610
|
+
delete row['__salmonParentId'];
|
|
611
|
+
built.set(t.id, row);
|
|
612
|
+
let list = kids.get(t.parentId);
|
|
613
|
+
if (!list)
|
|
614
|
+
kids.set(t.parentId, (list = []));
|
|
615
|
+
list.push(row);
|
|
616
|
+
}
|
|
617
|
+
for (const [id, row] of built) {
|
|
618
|
+
const list = kids.get(id);
|
|
619
|
+
if (list)
|
|
620
|
+
row[childKey] = list;
|
|
621
|
+
}
|
|
622
|
+
return kids.get(null) ?? [];
|
|
623
|
+
}
|
|
624
|
+
/** Before-event of a task change; false when prevented. */
|
|
625
|
+
before(action, ids, source) {
|
|
626
|
+
if (!this.props.onBeforeTaskChange)
|
|
627
|
+
return true;
|
|
628
|
+
const rows = ids.map((id) => this.rowById.get(id)).filter((x) => x !== undefined);
|
|
629
|
+
const e = gridEvent('beforeTaskChange', source, { action, taskIds: ids, rows });
|
|
630
|
+
this.props.onBeforeTaskChange(e);
|
|
631
|
+
return !e.defaultPrevented;
|
|
632
|
+
}
|
|
633
|
+
editable() {
|
|
634
|
+
return !this.props.readOnly && this.props.editing !== false;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* A user-level task update through the engine with dependency validation: conflicts go to `onScheduleConflict` or the built-in dialog.
|
|
638
|
+
*/
|
|
639
|
+
update(id, changes, source, origin = 'api') {
|
|
640
|
+
if (!this.engine.getTask(id))
|
|
641
|
+
return rejected(`unknown task "${id}"`);
|
|
642
|
+
if (!this.before('update', [id], source))
|
|
643
|
+
return rejected('prevented');
|
|
644
|
+
const validate = origin !== 'api' && this.props.validateDependencies !== false;
|
|
645
|
+
const result = this.engine.batch('update', () => this.engine.update(id, changes, { source: origin, validate }));
|
|
646
|
+
if (result.conflicts.length && validate && !result.changed.size) {
|
|
647
|
+
this.raiseConflict(id, changes, result.conflicts, source, origin);
|
|
648
|
+
return Object.freeze({ status: 'conflict', reason: 'dependency' });
|
|
649
|
+
}
|
|
650
|
+
// Data-only fields (title, notes, ...) change the row even when the schedule does not.
|
|
651
|
+
this.commit({ ...result, changed: new Set([...result.changed, id]) }, 'update', [id], source);
|
|
652
|
+
return APPLIED;
|
|
653
|
+
}
|
|
654
|
+
raiseConflict(id, changes, conflicts, source, origin) {
|
|
655
|
+
let settled = false;
|
|
656
|
+
const apply = (choice) => {
|
|
657
|
+
if (settled)
|
|
658
|
+
return;
|
|
659
|
+
settled = true;
|
|
660
|
+
this.conflict = null;
|
|
661
|
+
if (choice === 'cancel') {
|
|
662
|
+
this.emit();
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const result = this.engine.batch('update', () => {
|
|
666
|
+
if (choice === 'removeLink') {
|
|
667
|
+
const task = this.engine.getTask(id);
|
|
668
|
+
const drop = new Set(conflicts.map((c) => c.predecessorId));
|
|
669
|
+
this.engine.setDependencies(id, (task?.predecessors ?? []).filter((d) => !drop.has(d.id)));
|
|
670
|
+
}
|
|
671
|
+
return this.engine.update(id, changes, { source: origin, validate: false });
|
|
672
|
+
});
|
|
673
|
+
this.commit(result, 'update', [id], source);
|
|
674
|
+
};
|
|
675
|
+
if (this.props.onScheduleConflict) {
|
|
676
|
+
let chosen = null;
|
|
677
|
+
const e = gridEvent('scheduleConflict', source, {
|
|
678
|
+
conflicts: conflicts.map((c) => ({ ...c, task: this.rowById.get(c.taskId) ?? null })),
|
|
679
|
+
resolve: (choice) => {
|
|
680
|
+
chosen = choice;
|
|
681
|
+
apply(choice);
|
|
682
|
+
},
|
|
683
|
+
});
|
|
684
|
+
this.props.onScheduleConflict(e);
|
|
685
|
+
if (chosen !== null || e.defaultPrevented)
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
this.conflict = { conflicts, apply };
|
|
689
|
+
this.emit();
|
|
690
|
+
}
|
|
691
|
+
// ------------------------------------------------------------------
|
|
692
|
+
// Lookup
|
|
693
|
+
// ------------------------------------------------------------------
|
|
694
|
+
getRow(id) {
|
|
695
|
+
return this.rowById.get(String(id));
|
|
696
|
+
}
|
|
697
|
+
info(id) {
|
|
698
|
+
const s = this.engine.getSchedule(String(id));
|
|
699
|
+
const t = this.engine.getTask(String(id));
|
|
700
|
+
if (!s || !t)
|
|
701
|
+
return undefined;
|
|
702
|
+
return infoOf(s, t, this);
|
|
703
|
+
}
|
|
704
|
+
// ------------------------------------------------------------------
|
|
705
|
+
// Actions
|
|
706
|
+
// ------------------------------------------------------------------
|
|
707
|
+
newId() {
|
|
708
|
+
let max = 0;
|
|
709
|
+
let numeric = true;
|
|
710
|
+
for (const t of this.engine.getTasks()) {
|
|
711
|
+
const n = Number(t.id);
|
|
712
|
+
if (Number.isFinite(n))
|
|
713
|
+
max = Math.max(max, n);
|
|
714
|
+
else
|
|
715
|
+
numeric = false;
|
|
716
|
+
}
|
|
717
|
+
if (numeric)
|
|
718
|
+
return String(max + 1);
|
|
719
|
+
let i = this.engine.getTasks().length + 1;
|
|
720
|
+
while (this.engine.getTask(`task-${i}`))
|
|
721
|
+
i++;
|
|
722
|
+
return `task-${i}`;
|
|
723
|
+
}
|
|
724
|
+
addTask(task, options, source) {
|
|
725
|
+
const f = this.fields;
|
|
726
|
+
const raw = { ...task };
|
|
727
|
+
const id = raw[f.id] !== undefined && raw[f.id] !== null ? String(raw[f.id]) : this.newId();
|
|
728
|
+
if (this.engine.getTask(id))
|
|
729
|
+
return rejected(`duplicate task id "${id}"`);
|
|
730
|
+
if (!this.before('add', [id], source))
|
|
731
|
+
return rejected('prevented');
|
|
732
|
+
const position = options?.position ?? (typeof this.props.editing === 'object' ? this.props.editing.newRowPosition : undefined) ?? 'top';
|
|
733
|
+
const target = options?.targetId !== undefined ? String(options.targetId) : (this.selectedId() ?? null);
|
|
734
|
+
const targetTask = target ? this.engine.getTask(target) : undefined;
|
|
735
|
+
const tasks = this.engine.getTasks();
|
|
736
|
+
let parentId = null;
|
|
737
|
+
let index = position === 'bottom' ? tasks.length : 0;
|
|
738
|
+
if (targetTask && (position === 'above' || position === 'below' || position === 'child')) {
|
|
739
|
+
const at = tasks.findIndex((t) => t.id === target);
|
|
740
|
+
if (position === 'child') {
|
|
741
|
+
parentId = target;
|
|
742
|
+
index = at + 1;
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
parentId = targetTask.parentId;
|
|
746
|
+
index = position === 'above' ? at : at + 1 + this.descendantCount(target);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (raw[f.id] === undefined) {
|
|
750
|
+
const first = tasks[0] ? this.source.get(tasks[0].id) : undefined;
|
|
751
|
+
raw[f.id] = typeof first?.[f.id] === 'number' && Number.isFinite(Number(id)) ? Number(id) : id;
|
|
752
|
+
}
|
|
753
|
+
if (raw[f.title] === undefined)
|
|
754
|
+
raw[f.title] = this.text('newTask');
|
|
755
|
+
if (raw[f.start] === undefined && raw[f.end] === undefined) {
|
|
756
|
+
const ref = targetTask ? this.engine.getSchedule(target)?.start : null;
|
|
757
|
+
raw[f.start] = this.date(ref ?? this.projectRange().start);
|
|
758
|
+
}
|
|
759
|
+
if (raw[f.duration] === undefined && raw[f.end] === undefined)
|
|
760
|
+
raw[f.duration] = raw[f.milestone] === true ? 0 : 1;
|
|
761
|
+
const row = raw;
|
|
762
|
+
this.source.set(id, row);
|
|
763
|
+
this.engine.batch('add', () => this.engine.add(toTaskInput(row, id, parentId, this.ctx), { index }));
|
|
764
|
+
this.commit(null, 'add', [id], source);
|
|
765
|
+
return APPLIED;
|
|
766
|
+
}
|
|
767
|
+
descendantCount(id) {
|
|
768
|
+
let n = 0;
|
|
769
|
+
const tasks = this.engine.getTasks();
|
|
770
|
+
const inside = new Set([id]);
|
|
771
|
+
const at = tasks.findIndex((t) => t.id === id);
|
|
772
|
+
for (let i = at + 1; i < tasks.length; i++) {
|
|
773
|
+
const t = tasks[i];
|
|
774
|
+
if (t.parentId === null || !inside.has(t.parentId))
|
|
775
|
+
break;
|
|
776
|
+
inside.add(t.id);
|
|
777
|
+
n++;
|
|
778
|
+
}
|
|
779
|
+
return n;
|
|
780
|
+
}
|
|
781
|
+
deleteTasks(ids, source) {
|
|
782
|
+
const list = ids.map(String).filter((id) => this.engine.getTask(id));
|
|
783
|
+
if (!list.length)
|
|
784
|
+
return rejected('no tasks');
|
|
785
|
+
if (!this.before('delete', list, source))
|
|
786
|
+
return rejected('prevented');
|
|
787
|
+
// Source rows are kept: undo can bring the tasks back.
|
|
788
|
+
this.engine.batch('delete', () => this.engine.remove(list));
|
|
789
|
+
this.commit(null, 'delete', list, source);
|
|
790
|
+
return APPLIED;
|
|
791
|
+
}
|
|
792
|
+
changeTaskId(id, newId, source) {
|
|
793
|
+
const from = String(id);
|
|
794
|
+
const to = String(newId);
|
|
795
|
+
if (!this.engine.getTask(from))
|
|
796
|
+
return rejected(`unknown task "${from}"`);
|
|
797
|
+
if (this.engine.getTask(to))
|
|
798
|
+
return rejected(`duplicate task id "${to}"`);
|
|
799
|
+
const row = this.source.get(from);
|
|
800
|
+
this.engine.batch('changeId', () => this.engine.changeId(from, to));
|
|
801
|
+
if (row) {
|
|
802
|
+
this.source.delete(from);
|
|
803
|
+
this.source.set(to, { ...row, [this.fields.id]: typeof row[this.fields.id] === 'number' ? Number(to) : to });
|
|
804
|
+
}
|
|
805
|
+
this.commit(null, 'update', [to], source);
|
|
806
|
+
return APPLIED;
|
|
807
|
+
}
|
|
808
|
+
/** Writes changed data fields into the source row and the engine (api.updateTask, dialog saves, grid edits). */
|
|
809
|
+
updateFields(id, changes, source, origin = 'api') {
|
|
810
|
+
const key = String(id);
|
|
811
|
+
const base = this.source.get(key) ?? this.rowById.get(key);
|
|
812
|
+
const task = this.engine.getTask(key);
|
|
813
|
+
if (!base || !task)
|
|
814
|
+
return rejected(`unknown task "${key}"`);
|
|
815
|
+
const merged = { ...base, ...changes };
|
|
816
|
+
const fields = Object.keys(changes);
|
|
817
|
+
const next = toTaskInput(merged, key, task.parentId, this.ctx);
|
|
818
|
+
const f = this.fields;
|
|
819
|
+
const engineChanges = {};
|
|
820
|
+
for (const role of Object.keys(f)) {
|
|
821
|
+
if (role === 'children' || role === 'id' || role === 'parentId')
|
|
822
|
+
continue;
|
|
823
|
+
if (!fields.includes(f[role]))
|
|
824
|
+
continue;
|
|
825
|
+
if (role in next)
|
|
826
|
+
engineChanges[role] = next[role];
|
|
827
|
+
}
|
|
828
|
+
// An end edit recomputes the duration; a start edit keeps it (the end follows).
|
|
829
|
+
if (fields.includes(f['end']) && !fields.includes(f['duration']))
|
|
830
|
+
engineChanges['duration'] = null;
|
|
831
|
+
if (fields.includes(f['start']) && !fields.includes(f['end']) && !fields.includes(f['duration']))
|
|
832
|
+
engineChanges['end'] = null;
|
|
833
|
+
if (fields.includes(f['duration']) && !fields.includes(f['end']))
|
|
834
|
+
engineChanges['end'] = null;
|
|
835
|
+
if (fields.includes(f['parentId'])) {
|
|
836
|
+
const p = changes[f['parentId']];
|
|
837
|
+
engineChanges['parentId'] = p === null || p === undefined || p === '' ? null : String(p);
|
|
838
|
+
}
|
|
839
|
+
const prevSource = this.source.get(key);
|
|
840
|
+
this.source.set(key, merged);
|
|
841
|
+
const r = this.update(key, engineChanges, source, origin);
|
|
842
|
+
if (r.status !== 'applied') {
|
|
843
|
+
if (prevSource)
|
|
844
|
+
this.source.set(key, prevSource);
|
|
845
|
+
return r;
|
|
846
|
+
}
|
|
847
|
+
return r;
|
|
848
|
+
}
|
|
849
|
+
run(action, ids, source, fn) {
|
|
850
|
+
if (!ids.length)
|
|
851
|
+
return rejected('no tasks');
|
|
852
|
+
if (!this.before(action, ids, source))
|
|
853
|
+
return rejected('prevented');
|
|
854
|
+
const result = this.engine.batch(action, fn);
|
|
855
|
+
this.commit(result ?? null, action, ids, source);
|
|
856
|
+
return APPLIED;
|
|
857
|
+
}
|
|
858
|
+
dependencies(taskId, deps, action, source) {
|
|
859
|
+
const id = String(taskId);
|
|
860
|
+
const task = this.engine.getTask(id);
|
|
861
|
+
if (!task)
|
|
862
|
+
return rejected(`unknown task "${id}"`);
|
|
863
|
+
const previous = task.predecessors ?? [];
|
|
864
|
+
const row = this.rowById.get(id) ?? null;
|
|
865
|
+
const p = this.props;
|
|
866
|
+
if (p.onBeforeDependencyChange) {
|
|
867
|
+
const e = gridEvent('beforeDependencyChange', source, { action, taskId: id, task: row, dependencies: deps, previous });
|
|
868
|
+
p.onBeforeDependencyChange(e);
|
|
869
|
+
if (e.defaultPrevented)
|
|
870
|
+
return rejected('prevented');
|
|
871
|
+
}
|
|
872
|
+
const result = this.engine.batch('dependency', () => this.engine.setDependencies(id, deps));
|
|
873
|
+
this.writePredecessors(id);
|
|
874
|
+
this.commit(result, 'update', [id], source);
|
|
875
|
+
p.onDependencyChange?.(gridEvent('dependencyChange', source, { action, taskId: id, task: this.rowById.get(id) ?? null, dependencies: deps, previous }));
|
|
876
|
+
return APPLIED;
|
|
877
|
+
}
|
|
878
|
+
/** Keeps the source row's predecessor field in the input's format (string or array). */
|
|
879
|
+
writePredecessors(id) {
|
|
880
|
+
const row = this.source.get(id);
|
|
881
|
+
const task = this.engine.getTask(id);
|
|
882
|
+
if (!row || !task)
|
|
883
|
+
return;
|
|
884
|
+
const f = this.fields.predecessors;
|
|
885
|
+
const prev = row[f];
|
|
886
|
+
const deps = task.predecessors ?? [];
|
|
887
|
+
const value = Array.isArray(prev) ? deps.map((d) => ({ ...d })) : formatDeps(deps);
|
|
888
|
+
this.source.set(id, { ...row, [f]: value });
|
|
889
|
+
}
|
|
890
|
+
/** Toolbar critical path toggle (`'criticalPath'` item). */
|
|
891
|
+
toggleCriticalPath() {
|
|
892
|
+
const on = this.critical !== null || (this.criticalToggled && !peekGanttModules().critical);
|
|
893
|
+
this.criticalToggled = !on;
|
|
894
|
+
if (this.criticalToggled)
|
|
895
|
+
this.computeCritical();
|
|
896
|
+
else
|
|
897
|
+
this.critical = null;
|
|
898
|
+
this.emit();
|
|
899
|
+
}
|
|
900
|
+
/** Saves an exported file in the browser (toolbar export items). */
|
|
901
|
+
download(file) {
|
|
902
|
+
const doc = this.chartRoot?.ownerDocument;
|
|
903
|
+
if (!doc)
|
|
904
|
+
return;
|
|
905
|
+
const blob = new Blob([file.data], { type: file.mimeType });
|
|
906
|
+
const url = URL.createObjectURL(blob);
|
|
907
|
+
const a = doc.createElement('a');
|
|
908
|
+
a.href = url;
|
|
909
|
+
a.download = file.fileName;
|
|
910
|
+
a.click();
|
|
911
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
912
|
+
}
|
|
913
|
+
/** `onBeforeDialogOpen`; false when prevented. */
|
|
914
|
+
dialogBefore(mode, id) {
|
|
915
|
+
if (!this.props.onBeforeDialogOpen)
|
|
916
|
+
return true;
|
|
917
|
+
const e = gridEvent('beforeDialogOpen', 'api', { mode, taskId: id, task: id ? (this.rowById.get(id) ?? null) : null });
|
|
918
|
+
this.props.onBeforeDialogOpen(e);
|
|
919
|
+
return !e.defaultPrevented;
|
|
920
|
+
}
|
|
921
|
+
/** A source row replaced from outside (a reverted remote write). */
|
|
922
|
+
setSourceRow(id, row) {
|
|
923
|
+
this.source.set(id, row);
|
|
924
|
+
}
|
|
925
|
+
/** After undo / redo: predecessor fields of the source rows follow the engine. */
|
|
926
|
+
syncSourceFromEngine() {
|
|
927
|
+
for (const t of this.engine.getTasks()) {
|
|
928
|
+
this.writePredecessors(t.id);
|
|
929
|
+
this.writeResources(t.id);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
/** Keeps the source row's resource assignments in step with the engine (undo of a resource view drop). */
|
|
933
|
+
writeResources(id) {
|
|
934
|
+
const row = this.source.get(id);
|
|
935
|
+
const list = this.engine.getTask(id)?.resources ?? [];
|
|
936
|
+
const f = this.fields.resources;
|
|
937
|
+
if (!row || (row[f] === undefined && !list.length))
|
|
938
|
+
return;
|
|
939
|
+
const prev = Array.isArray(row[f]) ? row[f] : [];
|
|
940
|
+
const idField = this.ctx.resourceIdField;
|
|
941
|
+
const unitField = this.ctx.resourceUnitField;
|
|
942
|
+
const idOf = (v) => String(v !== null && typeof v === 'object' ? v[idField] : v);
|
|
943
|
+
if (prev.length === list.length && prev.every((v, i) => idOf(v) === list[i]?.id))
|
|
944
|
+
return;
|
|
945
|
+
const numeric = (this.props.resources ?? []).some((r) => typeof r[idField] === 'number');
|
|
946
|
+
this.source.set(id, { ...row, [f]: list.map((a) => ({ [idField]: numeric ? Number(a.id) : a.id, [unitField]: a.unit })) });
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Taskbar dropped on another row: in the resource view the task
|
|
950
|
+
* moves from the `from` resource to the target row's resource (the
|
|
951
|
+
* "Unassigned" row removes it); otherwise it moves below the target, or
|
|
952
|
+
* into it when the target is a parent.
|
|
953
|
+
*/
|
|
954
|
+
dropOnRow(id, target, from, dates, source) {
|
|
955
|
+
if (this.resourceMode()) {
|
|
956
|
+
const to = target.group;
|
|
957
|
+
if (to === undefined || to === from)
|
|
958
|
+
return rejected('same resource');
|
|
959
|
+
const idField = this.ctx.resourceIdField;
|
|
960
|
+
const unitField = this.ctx.resourceUnitField;
|
|
961
|
+
const current = this.engine.getTask(id)?.resources ?? [];
|
|
962
|
+
const moved = current.find((a) => a.id === from);
|
|
963
|
+
const list = current.filter((a) => a.id !== from && a.id !== to);
|
|
964
|
+
if (to !== '')
|
|
965
|
+
list.push({ id: to, unit: moved?.unit ?? 100 });
|
|
966
|
+
const numeric = (this.props.resources ?? []).some((r) => typeof r[idField] === 'number');
|
|
967
|
+
const changes = { [this.fields.resources]: list.map((a) => ({ [idField]: numeric ? Number(a.id) : a.id, [unitField]: a.unit })) };
|
|
968
|
+
if (this.engine.getSchedule(id)?.start !== dates.start)
|
|
969
|
+
changes[this.fields.start] = this.date(dates.start);
|
|
970
|
+
return this.updateFields(id, changes, source, 'edit');
|
|
971
|
+
}
|
|
972
|
+
const targetId = this.taskIdOfKey(target.key);
|
|
973
|
+
if (!targetId || targetId === id)
|
|
974
|
+
return rejected('same row');
|
|
975
|
+
return this.run('move', [id], source, () => {
|
|
976
|
+
this.engine.update(id, { start: dates.start }, { source: 'taskbar', validate: false });
|
|
977
|
+
const t = this.engine.getSchedule(targetId);
|
|
978
|
+
this.engine.move([id], targetId, t?.parent ? 'child' : 'below');
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
// ------------------------------------------------------------------
|
|
982
|
+
// Selection (taskbar click selects its row)
|
|
983
|
+
// ------------------------------------------------------------------
|
|
984
|
+
selectedId() {
|
|
985
|
+
const ids = this.gridApi?.getSelectedRowIds?.() ?? [];
|
|
986
|
+
const first = ids[0];
|
|
987
|
+
if (first !== undefined)
|
|
988
|
+
return this.taskIdOfKey(String(first));
|
|
989
|
+
const cell = this.gridApi?.getFocusedCell?.();
|
|
990
|
+
return cell ? this.taskIdOfKey(String(cell.rowId)) : null;
|
|
991
|
+
}
|
|
992
|
+
/** Data-grid tree expansion is the source of row visibility. */
|
|
993
|
+
visibleRows() {
|
|
994
|
+
return this.gridApi ? this.gridApi.getVisibleRows() : this.rows;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
/** `'2FS+3d'` form of dependencies. */
|
|
998
|
+
export function formatDeps(deps) {
|
|
999
|
+
return deps
|
|
1000
|
+
.map((d) => {
|
|
1001
|
+
const type = d.type.toUpperCase();
|
|
1002
|
+
const lag = d.lag ? `${d.lag > 0 ? '+' : ''}${d.lag}${d.lagUnit === 'day' ? 'd' : d.lagUnit === 'hour' ? 'h' : 'm'}` : '';
|
|
1003
|
+
return `${d.id}${type === 'FS' && !lag ? '' : type}${lag}`;
|
|
1004
|
+
})
|
|
1005
|
+
.join(',');
|
|
1006
|
+
}
|
|
1007
|
+
export function infoOf(s, t, g) {
|
|
1008
|
+
return Object.freeze({
|
|
1009
|
+
id: s.id,
|
|
1010
|
+
parentId: s.parentId,
|
|
1011
|
+
level: s.level,
|
|
1012
|
+
start: s.start === null ? null : g.date(s.start),
|
|
1013
|
+
end: s.end === null ? null : g.date(s.end),
|
|
1014
|
+
duration: s.duration,
|
|
1015
|
+
durationUnit: s.durationUnit,
|
|
1016
|
+
progress: s.progress,
|
|
1017
|
+
milestone: s.milestone,
|
|
1018
|
+
parent: s.parent,
|
|
1019
|
+
manual: s.manual,
|
|
1020
|
+
unscheduled: s.unscheduled,
|
|
1021
|
+
work: s.work,
|
|
1022
|
+
wbs: s.wbs,
|
|
1023
|
+
critical: g.critical?.has(s.id) ?? s.critical,
|
|
1024
|
+
totalFloat: s.totalFloat / 3_600_000,
|
|
1025
|
+
predecessors: t.predecessors ?? [],
|
|
1026
|
+
segments: s.segments ? s.segments.map((x) => ({ start: g.date(x.start), end: g.date(x.end) })) : null,
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
//# sourceMappingURL=instance.js.map
|