const NODES_URL = "http://127.0.0.1:2310/nodes";
const PACKAGE_TYPE_URL = "http://127.0.0.1:2310/package_type"
const DEFAULT_LAYOUT = {
name: 'circle',
nodeSpacing: 5,
edgeLengthVal: 45,
animate: true,
randomize: false,
maxSimulationTime: 1500
};
Promise.all([
fetch('cy-style.json')
.then(function (res) {
return res.json();
})
])
.then(function (dataArray) {
const cy = window.cy = cytoscape({
container: document.getElementById('cy'),
style: dataArray[0],
elements: [],
layout: {name: 'circle'}
});
cy.on('dblclick', 'node', function (evt) {
const node = evt.target;
const node_id = node.id();
let x = evt.position.x;
let y = evt.position.y;
sendPostRequest(node_id, NODES_URL).then(async function (result) {
let json = await result.json();
toNodes(json).then(function (elements) {
let elements_id = cy.add(elements);
elements_id.layout({
name: 'circle',
nodeSpacing: 1,
edgeLengthVal: 45,
boundingBox: {x1: x, y1: y, x2: x, y2: y}, avoidOverlap: true, nodeDimensionsIncludeLabels: false, spacingFactor: undefined, radius: undefined, startAngle: 3 / 2 * Math.PI, animate: false, transform: function (node, position) {
return position;
} }).run();
})
toEdges(node_id, json).then(function (elements) {
cy.add(elements);
})
});
});
});
async function sendPostRequest(pcg, url) {
return await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(pcg)
});
}
async function toNodes(json) {
const elements = [];
for (let element of json) {
let color = "#008b02";
if (element[2] === "obsoleted") {
color = "#000000";
} else if (element[2] === "partly-obsoleted") {
color = "#fccb00";
} else if (element[2] === "renamed") {
color = "#004dcf";
}
elements.push({
"group": "nodes",
"data": {
"id": element[0],
"name": element[0],
"score": 1,
"query": true,
"gene": true
},
"style": {
"background-color": color,
},
"background-color": color,
"selected": true,
"selectable": true,
"grabbable": true,
});
}
return elements;
}
async function toEdges(from, to_array) {
const edges = [];
for (let to of to_array) {
let color = "#008b02"; let line_style = "solid"; if (to[1] === "build") {
color = "#004dcf";
} else if (to[1] === "test") {
color = "#abb8c3";
} else if (to[1] === "system-build") {
color = "#004dcf";
line_style = "dashed";
} else if (to[1] === "system-test") {
color = "#abb8c3";
line_style = "dashed";
}
edges.push({
"data": {
"id": from + to[0],
"source": from,
"target": to[0],
"weight": 0.1,
"arrow": "triangle"
},
"group": "edges",
"selectable": true,
"grabbed": false,
"grabbable": true,
"style": {
"line-color": color,
"line-style": line_style,
}
});
}
return edges;
}
function spawnPackage() {
const userInput = document.getElementById("spawn-input").value;
packageType(userInput).then(function (package_type) {
let color = "#008b02";
console.log(package_type);
if (package_type === "obsoleted") {
color = "#000000";
} else if (package_type === "partly-obsoleted") {
color = "#fccb00";
} else if (package_type === "renamed") {
color = "#004dcf";
}
window.cy.add([{
"group": "nodes",
"data": {
"id": userInput,
"name": userInput,
"score": 1,
"query": true,
"gene": true
},
"style": {
"background-color": color,
},
"selectable": true,
"grabbable": true,
}]).layout(DEFAULT_LAYOUT).run();
});
}
function packageType(pcg) {
return sendPostRequest(pcg, PACKAGE_TYPE_URL).then(async function (result) {
return await result.text();
})
}