@receptron/graphai_vue_cytoscape 0.0.1
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/README.md +0 -0
- package/lib/cytoscope.d.ts +7 -0
- package/lib/cytoscope.js +295 -0
- package/package.json +49 -0
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ref, ComputedRef } from "vue";
|
|
2
|
+
import { GraphData, NodeState } from "graphai";
|
|
3
|
+
export declare const useCytoscope: (selectedGraph: ComputedRef<GraphData> | Ref<GraphData>) => {
|
|
4
|
+
cytoscopeRef: Ref<any>;
|
|
5
|
+
updateCytoscope: (nodeId: string, state: NodeState) => Promise<void>;
|
|
6
|
+
resetCytoscope: () => void;
|
|
7
|
+
};
|
package/lib/cytoscope.js
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
var _a;
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.useCytoscope = void 0;
|
|
44
|
+
var vue_1 = require("vue");
|
|
45
|
+
var graphai_1 = require("graphai");
|
|
46
|
+
var cytoscape_1 = __importDefault(require("cytoscape"));
|
|
47
|
+
var cytoscape_klay_1 = __importDefault(require("cytoscape-klay"));
|
|
48
|
+
cytoscape_1.default.use(cytoscape_klay_1.default);
|
|
49
|
+
var layout = "klay";
|
|
50
|
+
var colorPriority = "#f80";
|
|
51
|
+
var colorStatic = "#88f";
|
|
52
|
+
var calcNodeWidth = function (label) {
|
|
53
|
+
if (label === null || label === undefined) {
|
|
54
|
+
return "50px";
|
|
55
|
+
}
|
|
56
|
+
return Math.max(50, label.length * 8) + "px";
|
|
57
|
+
};
|
|
58
|
+
var cyStyle = [
|
|
59
|
+
{
|
|
60
|
+
selector: "node",
|
|
61
|
+
style: {
|
|
62
|
+
"background-color": "data(color)",
|
|
63
|
+
label: "data(id)",
|
|
64
|
+
shape: function (ele) { return (ele.data("isStatic") ? "rectangle" : "roundrectangle"); },
|
|
65
|
+
width: function (ele) { return calcNodeWidth(ele.data("id")); },
|
|
66
|
+
color: "#fff",
|
|
67
|
+
height: "30px",
|
|
68
|
+
"text-valign": "center",
|
|
69
|
+
"text-halign": "center",
|
|
70
|
+
"font-size": "12px",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
selector: "edge",
|
|
75
|
+
style: {
|
|
76
|
+
width: 3,
|
|
77
|
+
"line-color": "#888",
|
|
78
|
+
"target-arrow-color": "#888",
|
|
79
|
+
"target-arrow-shape": "triangle",
|
|
80
|
+
"curve-style": "straight",
|
|
81
|
+
"text-background-color": "#ffffff",
|
|
82
|
+
"text-background-opacity": 0.8,
|
|
83
|
+
"text-background-shape": "rectangle",
|
|
84
|
+
"font-size": "10px",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
selector: "edge[label]",
|
|
89
|
+
style: {
|
|
90
|
+
label: "data(label)",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
selector: "edge[isUpdate]",
|
|
95
|
+
style: {
|
|
96
|
+
color: "#ddd",
|
|
97
|
+
"line-color": "#ddd",
|
|
98
|
+
"line-style": "dashed",
|
|
99
|
+
"curve-style": "unbundled-bezier",
|
|
100
|
+
"target-arrow-color": "#ddd",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
var colorMap = (_a = {},
|
|
105
|
+
_a[graphai_1.NodeState.Waiting] = "#888",
|
|
106
|
+
_a[graphai_1.NodeState.Completed] = "#000",
|
|
107
|
+
_a[graphai_1.NodeState.Executing] = "#0f0",
|
|
108
|
+
_a["executing-server"] = "#FFC0CB",
|
|
109
|
+
_a[graphai_1.NodeState.Queued] = "#ff0",
|
|
110
|
+
_a[graphai_1.NodeState.Injected] = "#00f",
|
|
111
|
+
_a[graphai_1.NodeState.TimedOut] = "#f0f",
|
|
112
|
+
_a[graphai_1.NodeState.Failed] = "#f00",
|
|
113
|
+
_a[graphai_1.NodeState.Skipped] = "#0ff",
|
|
114
|
+
_a);
|
|
115
|
+
var parseInput = function (input) {
|
|
116
|
+
// WARNING: Assuming the first character is always ":"
|
|
117
|
+
var ids = input.slice(1).split(".");
|
|
118
|
+
var source = ids.shift() || "";
|
|
119
|
+
var label = ids.length ? ids.join(".") : undefined;
|
|
120
|
+
return { source: source, label: label };
|
|
121
|
+
};
|
|
122
|
+
var cytoscapeFromGraph = function (graph_data) {
|
|
123
|
+
var elements = Object.keys(graph_data.nodes).reduce(function (tmp, nodeId) {
|
|
124
|
+
var node = graph_data.nodes[nodeId];
|
|
125
|
+
var isStatic = "value" in node;
|
|
126
|
+
var cyNode = {
|
|
127
|
+
data: {
|
|
128
|
+
id: nodeId,
|
|
129
|
+
color: isStatic ? colorStatic : colorMap[graphai_1.NodeState.Waiting],
|
|
130
|
+
isStatic: isStatic,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
tmp.nodes.push(cyNode);
|
|
134
|
+
tmp.map[nodeId] = cyNode;
|
|
135
|
+
if ("inputs" in node) {
|
|
136
|
+
// computed node
|
|
137
|
+
var inputs = Array.isArray(node.inputs) ? node.inputs : Object.values(node.inputs || {});
|
|
138
|
+
(inputs !== null && inputs !== void 0 ? inputs : []).forEach(function (input) {
|
|
139
|
+
var _a = parseInput(input), source = _a.source, label = _a.label;
|
|
140
|
+
tmp.edges.push({
|
|
141
|
+
data: {
|
|
142
|
+
source: source,
|
|
143
|
+
target: nodeId,
|
|
144
|
+
label: label,
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if ("update" in node && node.update) {
|
|
150
|
+
// static node
|
|
151
|
+
var _a = parseInput(node.update), source = _a.source, label = _a.label;
|
|
152
|
+
tmp.edges.push({
|
|
153
|
+
data: {
|
|
154
|
+
source: source,
|
|
155
|
+
target: nodeId,
|
|
156
|
+
isUpdate: true,
|
|
157
|
+
label: label,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return tmp;
|
|
162
|
+
}, { nodes: [], edges: [], map: {} });
|
|
163
|
+
return { elements: elements };
|
|
164
|
+
};
|
|
165
|
+
var useCytoscope = function (selectedGraph) {
|
|
166
|
+
var cy = null;
|
|
167
|
+
var cytoscopeData = (0, vue_1.ref)(cytoscapeFromGraph(selectedGraph.value));
|
|
168
|
+
var cytoscopeRef = (0, vue_1.ref)();
|
|
169
|
+
var updateCytoscope = function (nodeId, state) { return __awaiter(void 0, void 0, void 0, function () {
|
|
170
|
+
var elements, graph, nodeData;
|
|
171
|
+
var _a;
|
|
172
|
+
return __generator(this, function (_b) {
|
|
173
|
+
switch (_b.label) {
|
|
174
|
+
case 0:
|
|
175
|
+
if (!(state === graphai_1.NodeState.Completed || state === graphai_1.NodeState.Waiting)) return [3 /*break*/, 2];
|
|
176
|
+
return [4 /*yield*/, (0, graphai_1.sleep)(100)];
|
|
177
|
+
case 1:
|
|
178
|
+
_b.sent();
|
|
179
|
+
_b.label = 2;
|
|
180
|
+
case 2:
|
|
181
|
+
elements = cytoscopeData.value.elements;
|
|
182
|
+
elements.map[nodeId].data.color = colorMap[state];
|
|
183
|
+
graph = selectedGraph.value;
|
|
184
|
+
nodeData = graph.nodes[nodeId];
|
|
185
|
+
if ("agent" in nodeData && state === graphai_1.NodeState.Queued && ((_a = nodeData.priority) !== null && _a !== void 0 ? _a : 0) > 0) {
|
|
186
|
+
// computed node
|
|
187
|
+
elements.map[nodeId].data.color = colorPriority;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
if ("value" in nodeData && state === graphai_1.NodeState.Waiting) {
|
|
191
|
+
// static node
|
|
192
|
+
elements.map[nodeId].data.color = colorStatic;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
cytoscopeData.value = { elements: elements };
|
|
196
|
+
if (!(state === graphai_1.NodeState.Injected)) return [3 /*break*/, 4];
|
|
197
|
+
return [4 /*yield*/, (0, graphai_1.sleep)(100)];
|
|
198
|
+
case 3:
|
|
199
|
+
_b.sent();
|
|
200
|
+
elements.map[nodeId].data.color = colorStatic;
|
|
201
|
+
cytoscopeData.value = { elements: elements };
|
|
202
|
+
_b.label = 4;
|
|
203
|
+
case 4: return [2 /*return*/];
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}); };
|
|
207
|
+
var createCytoscope = function () {
|
|
208
|
+
try {
|
|
209
|
+
cy = (0, cytoscape_1.default)({
|
|
210
|
+
container: cytoscopeRef.value,
|
|
211
|
+
style: cyStyle,
|
|
212
|
+
layout: {
|
|
213
|
+
name: layout,
|
|
214
|
+
// fit: true,
|
|
215
|
+
// padding: 30,
|
|
216
|
+
// avoidOverlap: true,
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
cy.on("mouseup", storePositions);
|
|
220
|
+
cy.on("touchend", storePositions);
|
|
221
|
+
// cy.on("select", "node", callback);
|
|
222
|
+
// cy.on("select", "edge", callback);
|
|
223
|
+
//store.commit("setCytoscape", cy);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
console.error(error);
|
|
227
|
+
// store.commit("setCytoscape", null);
|
|
228
|
+
// error_msg.value = `${error}`;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
var updateGraphData = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
232
|
+
var name_1;
|
|
233
|
+
return __generator(this, function (_a) {
|
|
234
|
+
switch (_a.label) {
|
|
235
|
+
case 0:
|
|
236
|
+
if (!cy) return [3 /*break*/, 2];
|
|
237
|
+
cy.elements().remove();
|
|
238
|
+
cy.add(cytoscopeData.value.elements);
|
|
239
|
+
name_1 = cytoscopeData.value.elements.nodes.reduce(function (prevName, node) {
|
|
240
|
+
if (node.position) {
|
|
241
|
+
return "preset";
|
|
242
|
+
}
|
|
243
|
+
return prevName;
|
|
244
|
+
}, layout);
|
|
245
|
+
console.log("layout", name_1);
|
|
246
|
+
cy.layout({ name: name_1 }).run();
|
|
247
|
+
cy.fit();
|
|
248
|
+
if (!(name_1 === layout)) return [3 /*break*/, 2];
|
|
249
|
+
return [4 /*yield*/, (0, graphai_1.sleep)(400)];
|
|
250
|
+
case 1:
|
|
251
|
+
_a.sent();
|
|
252
|
+
storePositions();
|
|
253
|
+
_a.label = 2;
|
|
254
|
+
case 2: return [2 /*return*/];
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}); };
|
|
258
|
+
var storePositions = function () {
|
|
259
|
+
console.log("storePositions");
|
|
260
|
+
if (cy) {
|
|
261
|
+
cy.nodes().forEach(function (cynode) {
|
|
262
|
+
var id = cynode.id();
|
|
263
|
+
var pos = cynode.position();
|
|
264
|
+
var node = cytoscopeData.value.elements.map[id];
|
|
265
|
+
node.position = pos;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
var resetCytoscope = function () {
|
|
270
|
+
var elements = cytoscopeData.value.elements;
|
|
271
|
+
Object.keys(elements.map).forEach(function (nodeId) {
|
|
272
|
+
var graph = selectedGraph.value;
|
|
273
|
+
var nodeData = graph.nodes[nodeId];
|
|
274
|
+
elements.map[nodeId].data.color = "value" in nodeData ? colorStatic : colorMap[graphai_1.NodeState.Waiting];
|
|
275
|
+
});
|
|
276
|
+
cytoscopeData.value = { elements: elements };
|
|
277
|
+
};
|
|
278
|
+
(0, vue_1.watch)(cytoscopeData, function () {
|
|
279
|
+
console.log("updated");
|
|
280
|
+
updateGraphData();
|
|
281
|
+
});
|
|
282
|
+
(0, vue_1.watch)(selectedGraph, function () {
|
|
283
|
+
cytoscopeData.value = cytoscapeFromGraph(selectedGraph.value);
|
|
284
|
+
});
|
|
285
|
+
(0, vue_1.onMounted)(function () {
|
|
286
|
+
createCytoscope();
|
|
287
|
+
updateGraphData();
|
|
288
|
+
});
|
|
289
|
+
return {
|
|
290
|
+
cytoscopeRef: cytoscopeRef,
|
|
291
|
+
updateCytoscope: updateCytoscope,
|
|
292
|
+
resetCytoscope: resetCytoscope,
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
exports.useCytoscope = useCytoscope;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@receptron/graphai_vue_cytoscape",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "lib/cytoscape.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"./lib"
|
|
7
|
+
],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "vite",
|
|
10
|
+
"build": "tsc --esModuleInterop --declaration --outDir lib/ src/composables/cytoscape.ts",
|
|
11
|
+
"build_old": "run-p type-check \"build-only {@}\" --",
|
|
12
|
+
"preview": "vite preview",
|
|
13
|
+
"build-only": "vite build",
|
|
14
|
+
"type-check": "vue-tsc --build --force",
|
|
15
|
+
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
|
|
16
|
+
"format": "prettier --write src/"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"cytoscape": "^3.29.2",
|
|
20
|
+
"cytoscape-klay": "^3.1.4"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"graphai": "^0.5.1",
|
|
24
|
+
"vue": "^3.4.21",
|
|
25
|
+
"@graphai/vanilla": "^0.0.3",
|
|
26
|
+
"@babel/types": "^7.24.7",
|
|
27
|
+
"@rushstack/eslint-patch": "^1.8.0",
|
|
28
|
+
"@tsconfig/node20": "^20.1.4",
|
|
29
|
+
"@types/node": "^20.12.5",
|
|
30
|
+
"@vitejs/plugin-vue": "^5.0.4",
|
|
31
|
+
"@vue/eslint-config-prettier": "^9.0.0",
|
|
32
|
+
"@vue/eslint-config-typescript": "^13.0.0",
|
|
33
|
+
"@vue/tsconfig": "^0.5.1",
|
|
34
|
+
"autoprefixer": "^10.4.19",
|
|
35
|
+
"eslint": "^8.57.0",
|
|
36
|
+
"eslint-plugin-vue": "^9.23.0",
|
|
37
|
+
"npm-run-all2": "^6.1.2",
|
|
38
|
+
"postcss": "^8.4.38",
|
|
39
|
+
"prettier": "^3.2.5",
|
|
40
|
+
"tailwindcss": "^3.4.4",
|
|
41
|
+
"typescript": "~5.4.0",
|
|
42
|
+
"vite": "^5.2.13",
|
|
43
|
+
"vue-tsc": "^2.0.11"
|
|
44
|
+
},
|
|
45
|
+
"types": "./lib/cytoscape.d.ts",
|
|
46
|
+
"directories": {
|
|
47
|
+
"lib": "lib"
|
|
48
|
+
}
|
|
49
|
+
}
|