-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.js
More file actions
114 lines (101 loc) · 3.26 KB
/
crud.js
File metadata and controls
114 lines (101 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// In-memory storage for Gantt tasks and links
// (simple demo: no DB, no ordering persistence)
let taskIdSeed = 6;
let linkIdSeed = 4;
const tasks = [
{ id: 1, text: "Project #1", start_date: "2025-04-20 00:00:00", duration: 6, progress: 0.4, parent: 0, open: true },
{ id: 2, text: "Task #1", start_date: "2025-04-21 00:00:00", duration: 3, progress: 0.3, parent: 1, open: true },
{ id: 3, text: "Task #2", start_date: "2025-04-24 00:00:00", duration: 4, progress: 0.6, parent: 1, open: true },
{ id: 4, text: "Subtask 2.1", start_date: "2025-04-24 00:00:00", duration: 2, progress: 0.2, parent: 3, open: true },
{ id: 5, text: "Subtask 2.2", start_date: "2025-04-26 00:00:00", duration: 2, progress: 0.0, parent: 3, open: true }
];
const links = [
{ id: 1, source: 2, target: 3, type: "0" }, // FS
{ id: 2, source: 3, target: 4, type: "0" },
{ id: 3, source: 4, target: 5, type: "0" }
];
function normalizeTask(payload, idOverride) {
// Keep only fields used in this demo; assume dates arrive already formatted as "%Y-%m-%d %H:%i:%s"
const t = {
id: idOverride ?? payload.id,
text: payload.text ?? "",
start_date: payload.start_date ?? "2025-04-20 00:00:00",
duration: Number(payload.duration ?? 1),
progress: Number(payload.progress ?? 0),
parent: Number(payload.parent ?? 0),
open: payload.open !== false
};
return t;
}
function normalizeLink(payload, idOverride) {
const l = {
id: idOverride ?? payload.id,
source: Number(payload.source),
target: Number(payload.target),
type: (payload.type ?? "0") + ""
};
return l;
}
// --- API
function getData() {
return { tasks, links };
}
// TASKS
function insertTask(raw) {
const id = ++taskIdSeed;
const item = normalizeTask(raw, id);
tasks.push(item);
return { action: "inserted", item, tid: id};
}
function updateTask(id, raw) {
const idx = tasks.findIndex(t => t.id == id);
if (idx === -1) return { action: "error" };
const updated = normalizeTask({ ...tasks[idx], ...raw }, Number(id));
tasks[idx] = updated;
return { action: "updated", item: updated };
}
function deleteTask(id) {
const numId = Number(id);
// remove task and its children (flat)
const toRemove = new Set([numId]);
// only one level for simplicity; enough for demo
tasks.forEach(t => { if (t.parent === numId) toRemove.add(t.id); });
// also remove any links connected to removed tasks
for (let i = links.length - 1; i >= 0; i--) {
if (toRemove.has(links[i].source) || toRemove.has(links[i].target)) {
links.splice(i, 1);
}
}
for (let i = tasks.length - 1; i >= 0; i--) {
if (toRemove.has(tasks[i].id)) tasks.splice(i, 1);
}
return { action: "deleted" };
}
// LINKS
function insertLink(raw) {
const id = ++linkIdSeed;
const item = normalizeLink(raw, id);
links.push(item);
return { action: "inserted", item, tid: id};
}
function updateLink(id, raw) {
const idx = links.findIndex(l => l.id == id);
if (idx === -1) return { action: "error" };
const updated = normalizeLink({ ...links[idx], ...raw }, Number(id));
links[idx] = updated;
return { action: "updated", item: updated };
}
function deleteLink(id) {
const idx = links.findIndex(l => l.id == id);
if (idx !== -1) links.splice(idx, 1);
return { action: "deleted" };
}
module.exports = {
getData,
insertTask,
updateTask,
deleteTask,
insertLink,
updateLink,
deleteLink
};