@nodeject/ui-components
Version:
UI library for non-trivial components
104 lines (103 loc) • 2.68 kB
JavaScript
import cytoscape from 'cytoscape';
export var toCytoscape = function (graph) {
return cytoscape({
headless: true,
elements: graph
});
};
/**
* Returns a node's previous sibling
* @param node
* @returns {Cy.NodeCollection}
*/
export var getPreviousSibling = function (node) {
var nodeId = node.id();
if (hasSiblings(node)) {
var mySiblingsAndCallingNode = siblingsAndCallingNode(node);
for (var i = 0; mySiblingsAndCallingNode.length - 1; i++) {
if (nodeId == mySiblingsAndCallingNode[i].id()) {
if (i > 0) {
return mySiblingsAndCallingNode[i - 1];
}
else {
return null;
}
}
}
}
else {
return null;
}
};
/**
* Returns a node's next sibling
* @param node
* @returns {Cy.NodeCollection}
*/
export var getNextSibling = function (node) {
var nodeId = node.id();
if (hasSiblings(node)) {
var mySiblingsAndCallingNode = siblingsAndCallingNode(node);
for (var i = 0; siblingsAndCallingNode.length - 1; i++) {
if (nodeId == mySiblingsAndCallingNode[i].id()) {
if (i < mySiblingsAndCallingNode.length - 1) {
return mySiblingsAndCallingNode[i + 1];
}
else {
return null;
}
}
}
}
else {
return null;
}
};
/**
* Returns the next node in the tree
* @param node
*/
export var getNextNode = function (node) {
return hasChildren(node) ? node.children().first() : getNextSibling(node);
};
/**
* Returns a node's parent's children
* @param node
* @returns {Cy.NodeCollection}
*/
export var siblingsAndCallingNode = function (node) {
if (hasParent(node)) {
return node.parent().children();
}
else {
return node;
}
};
/**
* Returns true if node has siblings
* @param node
*/
export var hasSiblings = function (node) {
return node.siblings().length > 0;
};
/**
* Returns true if node has children
* @param node
*/
export var hasChildren = function (node) {
return node.children().length > 0;
};
/**
* Returns true if node has a parent
* @param node
*/
export var hasParent = function (node) {
return node.parent().length > 0;
};
/**
* Returns true if node is Org Style, false if it's List Style
* @param node
*/
export var isOrgStyle = function (node) {
return typeof node === 'number' ? node === 0 : node.data().layoutStyle === 0;
};