gantt-lib 0.146.1 → 0.146.3

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.
@@ -447,6 +447,7 @@ function clampTaskRangeForIncomingFS(task, proposedStart, proposedEnd, allTasks,
447
447
  }
448
448
  function recalculateIncomingLags(task, newStartDate, newEndDate, allTasks, businessDays = false, weekendPredicate, taskById) {
449
449
  if (!task.dependencies) return [];
450
+ if (task.fixedStartDate || task.fixedEndDate) return task.dependencies;
450
451
  return task.dependencies.map((dep) => {
451
452
  const predecessor = taskById?.get(dep.taskId) ?? allTasks.find((candidate) => candidate.id === dep.taskId);
452
453
  if (!predecessor) {
@@ -470,6 +471,53 @@ function recalculateIncomingLags(task, newStartDate, newEndDate, allTasks, busin
470
471
  });
471
472
  }
472
473
 
474
+ // src/core/scheduling/fixedDates.ts
475
+ function resolveFixedDates(task, range, parent, businessDays = false, weekendPredicate, preserveDuration = true) {
476
+ for (const value of [task.fixedStartDate, task.fixedEndDate]) {
477
+ if (value && (!/^\d{4}-\d{2}-\d{2}$/.test(value) || Number.isNaN(Date.parse(value)) || new Date(value).toISOString().slice(0, 10) !== value)) {
478
+ throw new Error(`Invalid fixed date on task ${task.id}`);
479
+ }
480
+ }
481
+ const fixedStart = task.fixedStartDate ? parseDateOnly(task.fixedStartDate) : void 0;
482
+ const fixedEnd = task.fixedEndDate ? parseDateOnly(task.fixedEndDate) : void 0;
483
+ if (!fixedStart && !fixedEnd) return range;
484
+ if (fixedStart && fixedEnd && (fixedStart > fixedEnd || task.type === "milestone" && +fixedStart !== +fixedEnd)) {
485
+ throw new Error(`Fixed dates conflict on task ${task.id}`);
486
+ }
487
+ if (task.type === "milestone") {
488
+ const date = fixedStart ?? fixedEnd;
489
+ return { start: date, end: date };
490
+ }
491
+ if (fixedStart && fixedEnd) return { start: fixedStart, end: fixedEnd };
492
+ if (!parent && preserveDuration) {
493
+ const duration = getTaskDuration(task.startDate, task.endDate, businessDays, weekendPredicate);
494
+ if (fixedStart) return { start: fixedStart, end: buildTaskRangeFromStart(fixedStart, duration, businessDays, weekendPredicate).end };
495
+ return { start: buildTaskRangeFromEnd(fixedEnd, duration, businessDays, weekendPredicate).start, end: fixedEnd };
496
+ }
497
+ if (fixedStart) return { start: fixedStart, end: range.end < fixedStart ? fixedStart : range.end };
498
+ return { start: range.start > fixedEnd ? fixedEnd : range.start, end: fixedEnd };
499
+ }
500
+ function resolveParentFixedDates(task, range, getTask, businessDays = false, weekendPredicate) {
501
+ if (!task.fixedStartDate && !task.fixedEndDate) return range;
502
+ let { start, end } = range;
503
+ for (const dep of task.dependencies ?? []) {
504
+ const targetStart = dep.type === "FS" || dep.type === "SS";
505
+ if (targetStart ? task.fixedStartDate : task.fixedEndDate) continue;
506
+ const predecessor = getTask(dep.taskId);
507
+ if (!predecessor) continue;
508
+ const anchor = predecessor.type === "milestone" ? predecessor.fixedStartDate ?? predecessor.fixedEndDate : void 0;
509
+ const { predStart, predEnd } = normalizePredecessorDates({
510
+ ...predecessor,
511
+ startDate: anchor ?? predecessor.fixedStartDate ?? predecessor.startDate,
512
+ endDate: anchor ?? predecessor.fixedEndDate ?? predecessor.endDate
513
+ }, parseDateOnly);
514
+ const date = calculateSuccessorDate(predStart, predEnd, dep.type, getDependencyLag(dep), businessDays, weekendPredicate, task.type);
515
+ if (targetStart && date > start) start = date;
516
+ if (!targetStart && date > end) end = date;
517
+ }
518
+ return resolveFixedDates(task, { start, end }, true, businessDays, weekendPredicate);
519
+ }
520
+
473
521
  // src/core/scheduling/cascade.ts
474
522
  function parseCascadeDateInput(date) {
475
523
  if (date instanceof Date) {
@@ -658,8 +706,11 @@ function getTransitiveCascadeChain(changedTaskId, allTasks, firstLevelLinkTypes)
658
706
  }
659
707
  return chain;
660
708
  }
661
- function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays = false, weekendPredicate, context = createCascadeContext(allTasks)) {
709
+ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays = false, weekendPredicate, context = createCascadeContext(allTasks), rootIsRollup = false) {
662
710
  const { childrenByParentId, dependentsByPredecessorId, taskById } = context;
711
+ const originalMovedTask = taskById.get(movedTask.id) ?? movedTask;
712
+ const sameDelta = +newStart - +parseDateOnly(originalMovedTask.startDate) === +newEnd - +parseDateOnly(originalMovedTask.endDate);
713
+ ({ start: newStart, end: newEnd } = resolveFixedDates(originalMovedTask, { start: newStart, end: newEnd }, childrenByParentId.has(movedTask.id), businessDays, weekendPredicate, sameDelta));
663
714
  const normalizedMovedTask = normalizeTaskDependencyLags({
664
715
  ...movedTask,
665
716
  startDate: newStart.toISOString().split("T")[0],
@@ -672,7 +723,12 @@ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays =
672
723
  updatedDates.set(movedTask.id, { start: newStart, end: newEnd });
673
724
  const resultMap = /* @__PURE__ */ new Map();
674
725
  resultMap.set(movedTask.id, normalizedMovedTask);
675
- const queue = [[movedTask.id, "direct"]];
726
+ const getCurrentTask = (id) => {
727
+ const task = taskById.get(id);
728
+ const range = updatedDates.get(id);
729
+ return task && range ? { ...task, startDate: range.start, endDate: range.end } : task;
730
+ };
731
+ const queue = [[movedTask.id, rootIsRollup ? "parent-recalc" : "direct"]];
676
732
  const childShifted = /* @__PURE__ */ new Set();
677
733
  let iterations = 0;
678
734
  const MAX_ITERATIONS = allTasks.length * 3;
@@ -681,7 +737,7 @@ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays =
681
737
  const [currentId, arrivalMode] = queue.shift();
682
738
  const { start: currStart, end: currEnd } = updatedDates.get(currentId);
683
739
  const currentOriginal = taskById.get(currentId);
684
- if (arrivalMode !== "parent-recalc") {
740
+ if (arrivalMode !== "parent-recalc" && !(arrivalMode === "dependency" && (currentOriginal.fixedStartDate || currentOriginal.fixedEndDate))) {
685
741
  const children = childrenByParentId.get(currentId) ?? [];
686
742
  for (const child of children) {
687
743
  if (childShifted.has(child.id) || child.locked) continue;
@@ -710,6 +766,7 @@ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays =
710
766
  childNewStart = new Date(childOrigStart.getTime() + startDeltaMs);
711
767
  childNewEnd = new Date(childOrigEnd.getTime() + endDeltaMs);
712
768
  }
769
+ ({ start: childNewStart, end: childNewEnd } = resolveFixedDates(child, { start: childNewStart, end: childNewEnd }, childrenByParentId.has(child.id), businessDays, weekendPredicate));
713
770
  const prev = updatedDates.get(child.id);
714
771
  if (prev && prev.start.getTime() === childNewStart.getTime() && prev.end.getTime() === childNewEnd.getTime()) {
715
772
  continue;
@@ -740,8 +797,10 @@ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays =
740
797
  if (updatedDates.has(sib.id)) return updatedDates.get(sib.id);
741
798
  return { start: new Date(sib.startDate), end: new Date(sib.endDate) };
742
799
  });
743
- const minStart = new Date(Math.min(...siblingPositions.map((p) => p.start.getTime())));
744
- const maxEnd = new Date(Math.max(...siblingPositions.map((p) => p.end.getTime())));
800
+ const { start: minStart, end: maxEnd } = resolveParentFixedDates(parent, {
801
+ start: new Date(Math.min(...siblingPositions.map((p) => p.start.getTime()))),
802
+ end: new Date(Math.max(...siblingPositions.map((p) => p.end.getTime())))
803
+ }, getCurrentTask, businessDays, weekendPredicate);
745
804
  const prev = updatedDates.get(parentId);
746
805
  if (!prev || prev.start.getTime() !== minStart.getTime() || prev.end.getTime() !== maxEnd.getTime()) {
747
806
  updatedDates.set(parentId, { start: minStart, end: maxEnd });
@@ -797,6 +856,15 @@ function universalCascade(movedTask, newStart, newEnd, allTasks, businessDays =
797
856
  weekendPredicate
798
857
  ));
799
858
  }
859
+ const dependentChildren = childrenByParentId.get(task.id);
860
+ if (dependentChildren?.length && (task.fixedStartDate || task.fixedEndDate)) {
861
+ const positions = dependentChildren.map((child) => updatedDates.get(child.id) ?? { start: parseDateOnly(child.startDate), end: parseDateOnly(child.endDate) });
862
+ ({ start: succNewStart, end: succNewEnd } = resolveParentFixedDates(task, {
863
+ start: new Date(Math.min(...positions.map((position) => +position.start))),
864
+ end: new Date(Math.max(...positions.map((position) => +position.end)))
865
+ }, getCurrentTask, businessDays, weekendPredicate));
866
+ }
867
+ ({ start: succNewStart, end: succNewEnd } = resolveFixedDates(task, { start: succNewStart, end: succNewEnd }, !!dependentChildren?.length, businessDays, weekendPredicate));
800
868
  const prev = updatedDates.get(task.id);
801
869
  if (prev && prev.start.getTime() === succNewStart.getTime() && prev.end.getTime() === succNewEnd.getTime()) {
802
870
  continue;
@@ -826,22 +894,23 @@ function getChildren(parentId, tasks) {
826
894
  function isTaskParent(taskId, tasks) {
827
895
  return tasks.some((t) => t.parentId === taskId);
828
896
  }
829
- function computeParentDates(parentId, tasks) {
897
+ function computeParentDates(parentId, tasks, businessDays = false, weekendPredicate) {
830
898
  const children = getChildren(parentId, tasks);
831
899
  if (children.length === 0) {
832
- const parent = tasks.find((t) => t.id === parentId);
833
- const start = parent ? new Date(parent.startDate) : /* @__PURE__ */ new Date();
834
- const end = parent ? new Date(parent.endDate) : /* @__PURE__ */ new Date();
835
- return { startDate: start, endDate: end };
900
+ const parent2 = tasks.find((t) => t.id === parentId);
901
+ const start = parent2 ? new Date(parent2.startDate) : /* @__PURE__ */ new Date();
902
+ const end = parent2 ? new Date(parent2.endDate) : /* @__PURE__ */ new Date();
903
+ const range2 = parent2 ? resolveFixedDates(parent2, { start, end }, false) : { start, end };
904
+ return { startDate: range2.start, endDate: range2.end };
836
905
  }
837
906
  const startDates = children.map((c) => new Date(c.startDate));
838
907
  const endDates = children.map((c) => new Date(c.endDate));
839
908
  const minTime = Math.min(...startDates.map((d) => d.getTime()));
840
909
  const maxTime = Math.max(...endDates.map((d) => d.getTime()));
841
- return {
842
- startDate: new Date(minTime),
843
- endDate: new Date(maxTime)
844
- };
910
+ const parent = tasks.find((t) => t.id === parentId);
911
+ const range = { start: new Date(minTime), end: new Date(maxTime) };
912
+ const resolved = parent ? resolveParentFixedDates(parent, range, (id) => tasks.find((task) => task.id === id), businessDays, weekendPredicate) : range;
913
+ return { startDate: resolved.start, endDate: resolved.end };
845
914
  }
846
915
  function computeParentProgress(parentId, tasks) {
847
916
  const children = getChildren(parentId, tasks);
@@ -972,13 +1041,14 @@ function moveTaskWithCascade(taskId, newStart, snapshot, options) {
972
1041
  }
973
1042
  const businessDays = options?.businessDays ?? false;
974
1043
  const weekendPredicate = options?.weekendPredicate;
975
- const newRange = moveTaskRange(
1044
+ let newRange = moveTaskRange(
976
1045
  task.startDate,
977
1046
  task.endDate,
978
1047
  newStart,
979
1048
  businessDays,
980
1049
  weekendPredicate
981
1050
  );
1051
+ newRange = resolveFixedDates(task, newRange, snapshot.some((t) => t.parentId === task.id), businessDays, weekendPredicate);
982
1052
  const updatedDependencies = recalculateIncomingLags(
983
1053
  task,
984
1054
  newRange.start,
@@ -1033,6 +1103,7 @@ function resizeTaskWithCascade(taskId, anchor, newDate, snapshot, options) {
1033
1103
  } else {
1034
1104
  newRange = { start: newDate, end: originalEnd };
1035
1105
  }
1106
+ newRange = resolveFixedDates(task, newRange, snapshot.some((t) => t.parentId === task.id), businessDays, weekendPredicate, false);
1036
1107
  const updatedDependencies = recalculateIncomingLags(
1037
1108
  task,
1038
1109
  newRange.start,
@@ -1123,6 +1194,14 @@ function recalculateTaskFromDependencies(taskId, snapshot, options) {
1123
1194
  changedIds: [task.id]
1124
1195
  };
1125
1196
  }
1197
+ const fixedParent = !!(task.fixedStartDate || task.fixedEndDate) && snapshot.some((t) => t.parentId === task.id);
1198
+ if (fixedParent) {
1199
+ const rollup = computeParentDates(task.id, snapshot, businessDays, weekendPredicate);
1200
+ constrainedStart = rollup.startDate;
1201
+ constrainedEnd = rollup.endDate;
1202
+ } else {
1203
+ ({ start: constrainedStart, end: constrainedEnd } = resolveFixedDates(task, { start: constrainedStart, end: constrainedEnd }, false, businessDays, weekendPredicate));
1204
+ }
1126
1205
  const updatedDependencies = recalculateIncomingLags(
1127
1206
  task,
1128
1207
  constrainedStart,
@@ -1149,7 +1228,9 @@ function recalculateTaskFromDependencies(taskId, snapshot, options) {
1149
1228
  constrainedEnd,
1150
1229
  snapshot,
1151
1230
  businessDays,
1152
- weekendPredicate
1231
+ weekendPredicate,
1232
+ void 0,
1233
+ fixedParent
1153
1234
  );
1154
1235
  const resultMap = /* @__PURE__ */ new Map();
1155
1236
  resultMap.set(recalculatedTask.id, recalculatedTask);
@@ -1163,6 +1244,100 @@ function recalculateTaskFromDependencies(taskId, snapshot, options) {
1163
1244
  };
1164
1245
  }
1165
1246
  function recalculateProjectSchedule(snapshot, options) {
1247
+ if (snapshot.some((task) => task.fixedStartDate || task.fixedEndDate)) {
1248
+ return recalculateFixedComponents(snapshot, options);
1249
+ }
1250
+ return recalculateProjectScheduleLegacy(snapshot, options);
1251
+ }
1252
+ function recalculateFixedComponents(snapshot, options) {
1253
+ const businessDays = options?.businessDays ?? false;
1254
+ const weekendPredicate = options?.weekendPredicate;
1255
+ const initial = new Map(snapshot.map((task) => [task.id, task]));
1256
+ const next = new Map(snapshot.map((task) => [task.id, task]));
1257
+ const children = /* @__PURE__ */ new Map();
1258
+ const outgoing = new Map(snapshot.map((task) => [task.id, /* @__PURE__ */ new Set()]));
1259
+ const neighbours = new Map(snapshot.map((task) => [task.id, /* @__PURE__ */ new Set()]));
1260
+ const connect = (from, to, ordered = true) => {
1261
+ if (!initial.has(from) || !initial.has(to)) return;
1262
+ if (ordered) outgoing.get(from).add(to);
1263
+ neighbours.get(from).add(to);
1264
+ neighbours.get(to).add(from);
1265
+ };
1266
+ for (const task of snapshot) {
1267
+ if (task.parentId && initial.has(task.parentId)) {
1268
+ const list = children.get(task.parentId) ?? [];
1269
+ list.push(task.id);
1270
+ children.set(task.parentId, list);
1271
+ connect(task.id, task.parentId);
1272
+ }
1273
+ }
1274
+ for (const task of snapshot) {
1275
+ for (const dependency of task.dependencies ?? []) {
1276
+ const predecessor = initial.get(dependency.taskId);
1277
+ const fixedSource = predecessor?.type === "milestone" ? predecessor.fixedStartDate ?? predecessor.fixedEndDate : dependency.type === "FS" || dependency.type === "FF" ? predecessor?.fixedEndDate : predecessor?.fixedStartDate;
1278
+ const targetFixed = dependency.type === "FS" || dependency.type === "SS" ? task.fixedStartDate : task.fixedEndDate;
1279
+ const derivedParent = children.has(task.id) && !task.fixedStartDate && !task.fixedEndDate;
1280
+ connect(dependency.taskId, task.id, !fixedSource && !targetFixed && !derivedParent);
1281
+ }
1282
+ }
1283
+ const affected = new Set(snapshot.filter((task) => task.fixedStartDate || task.fixedEndDate).map((task) => task.id));
1284
+ const pending = [...affected];
1285
+ for (let i = 0; i < pending.length; i++) {
1286
+ for (const id of neighbours.get(pending[i])) if (!affected.has(id)) {
1287
+ affected.add(id);
1288
+ pending.push(id);
1289
+ }
1290
+ }
1291
+ const indegree = new Map([...affected].map((id) => [id, 0]));
1292
+ for (const id of affected) for (const successor of outgoing.get(id)) {
1293
+ indegree.set(successor, indegree.get(successor) + 1);
1294
+ }
1295
+ const queue = [...affected].filter((id) => indegree.get(id) === 0);
1296
+ for (let i = 0; i < queue.length; i++) {
1297
+ const id = queue[i];
1298
+ const task = next.get(id);
1299
+ const childIds = children.get(id);
1300
+ if (!task.locked) {
1301
+ let range = { start: parseDateOnly(task.startDate), end: parseDateOnly(task.endDate) };
1302
+ if (childIds?.length) {
1303
+ range = {
1304
+ start: new Date(Math.min(...childIds.map((child) => +parseDateOnly(next.get(child).startDate)))),
1305
+ end: new Date(Math.max(...childIds.map((child) => +parseDateOnly(next.get(child).endDate))))
1306
+ };
1307
+ range = resolveParentFixedDates(task, range, (key) => next.get(key), businessDays, weekendPredicate);
1308
+ } else if (task.dependencies?.length) {
1309
+ const duration = getTaskDuration(task.startDate, task.endDate, businessDays, weekendPredicate);
1310
+ let selected;
1311
+ for (const dependency of task.dependencies) {
1312
+ const predecessor = next.get(dependency.taskId);
1313
+ if (!predecessor) continue;
1314
+ const milestoneAnchor = predecessor.type === "milestone" ? predecessor.fixedStartDate ?? predecessor.fixedEndDate : void 0;
1315
+ const { predStart, predEnd } = normalizePredecessorDates({
1316
+ ...predecessor,
1317
+ startDate: milestoneAnchor ?? predecessor.fixedStartDate ?? predecessor.startDate,
1318
+ endDate: milestoneAnchor ?? predecessor.fixedEndDate ?? predecessor.endDate
1319
+ }, parseDateOnly);
1320
+ const date = calculateSuccessorDate(predStart, predEnd, dependency.type, getDependencyLag(dependency), businessDays, weekendPredicate, task.type);
1321
+ const candidate = dependency.type === "FS" || dependency.type === "SS" ? buildTaskRangeFromStart(date, duration, businessDays, weekendPredicate) : buildTaskRangeFromEnd(date, duration, businessDays, weekendPredicate);
1322
+ if (!selected || candidate.start > selected.start || +candidate.start === +selected.start && candidate.end > selected.end) selected = candidate;
1323
+ }
1324
+ if (selected) range = selected;
1325
+ }
1326
+ range = resolveFixedDates(task, range, !!childIds?.length, businessDays, weekendPredicate);
1327
+ next.set(id, { ...task, startDate: toIsoDate(range.start), endDate: toIsoDate(range.end) });
1328
+ }
1329
+ for (const successor of outgoing.get(id)) {
1330
+ indegree.set(successor, indegree.get(successor) - 1);
1331
+ if (indegree.get(successor) === 0) queue.push(successor);
1332
+ }
1333
+ }
1334
+ if (queue.length !== affected.size) throw new Error("Fixed date schedule contains a hierarchy/dependency cycle");
1335
+ for (const task of recalculateProjectScheduleLegacy(snapshot, options).changedTasks) {
1336
+ if (!affected.has(task.id)) next.set(task.id, task);
1337
+ }
1338
+ return createChangedResult(snapshot, [...next.values()]);
1339
+ }
1340
+ function recalculateProjectScheduleLegacy(snapshot, options) {
1166
1341
  const businessDays = options?.businessDays ?? false;
1167
1342
  const weekendPredicate = options?.weekendPredicate;
1168
1343
  const workingMap = new Map(snapshot.map((task) => [task.id, { ...task }]));
@@ -1286,12 +1461,13 @@ function reflowTasksOnModeSwitch(sourceTasks, toBusinessDays, weekendPredicate)
1286
1461
  true,
1287
1462
  weekendPredicate
1288
1463
  ) : buildTaskRangeFromStart(start, duration, false);
1289
- task.startDate = toISO(range.start);
1290
- task.endDate = toISO(range.end);
1464
+ const resolved = resolveFixedDates(task, range, false, toBusinessDays, weekendPredicate);
1465
+ task.startDate = toISO(resolved.start);
1466
+ task.endDate = toISO(resolved.end);
1291
1467
  }
1292
1468
  for (const task of tasks) {
1293
1469
  if (!isTaskParent(task.id, tasks)) continue;
1294
- const { startDate, endDate } = computeParentDates(task.id, tasks);
1470
+ const { startDate, endDate } = computeParentDates(task.id, tasks, toBusinessDays, weekendPredicate);
1295
1471
  task.startDate = toISO(startDate);
1296
1472
  task.endDate = toISO(endDate);
1297
1473
  }
@@ -2479,6 +2655,10 @@ function scaleTaskSubtreeDuration(parentId, targetDuration, snapshot, options) {
2479
2655
  const { context: ctx, space } = built;
2480
2656
  const solved = solveScale(ctx, space, targetDuration);
2481
2657
  const finalTasks = buildFinalTasks(ctx, solved.state, solved.layout, space);
2658
+ const fixedConflicts = [...finalTasks.values()].filter((task) => task.fixedStartDate && isoOfTaskDate(task.startDate) !== task.fixedStartDate || task.fixedEndDate && isoOfTaskDate(task.endDate) !== task.fixedEndDate);
2659
+ if (fixedConflicts.length) {
2660
+ return scaleFailure("FIXED_DATE_CONFLICT", fixedConflicts.map((task) => task.id));
2661
+ }
2482
2662
  const externalPolicy = resolvedOptions.externalDependencyPolicy ?? "subtree-only";
2483
2663
  let externalChanged = [];
2484
2664
  if (externalPolicy === "subtree-only") {