Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Last active December 18, 2015 21:18
Show Gist options
  • Save swvitaliy/5845866 to your computer and use it in GitHub Desktop.
Save swvitaliy/5845866 to your computer and use it in GitHub Desktop.
;(function (ns) {
var gid = (function (i) {
return function () {
return i++;
}
})(0);
function Node(graph, val) {
this.gid = gid();
this.graph = graph;
this.val = val;
this.inputs = [];
this.outputs = [];
}
function deleteItem(arr, i) {
return arr.slice(0, i).concat(arr.slice(i + 1));
}
Node.prototype.resolveNode = function (node, fn) {
return fn.call(this, this.graph.getNode(node));
};
Node.nodeMethod = function (fn) {
return function (node) {
return this.resolveNode(node, function (node) {
return fn.call(this, node);
});
};
};
Node.prototype.addLinkTo = Node.prototype.linkTo =
Node.nodeMethod(function (node) {
node.inputs.push(this);
this.outputs.push(node);
return node;
});
Node.prototype.addLinkFrom = Node.prototype.linkFrom =
function (node) {
return node.addLinkTo(this);
};
Node.prototype.existsLinkTo = Node.nodeMethod(function (node) {
return this.outputs.indexOf(node) >= 0;
});
Node.prototype.existsLinkFrom = function (node) {
return node.existsLinkTo(this);
};
Node.prototype.removeLinkTo = Node.nodeMethod(function (node) {
var i = this.output.indexOf(node);
var j = node.input.indexOf(this);
if (i >= 0 && j >= 0) {
this.outputs = deleteItem(this.outputs, i);
this.inputs = deleteItem(this.inputs, j);
}
return this;
});
Node.prototype.removeLinkFrom = function (node) {
return node.removeLinkTo(this);
};
Node.prototype.removeAllLinks = Node.prototype.isolate =
function (listName) {
if (!listName) {
this.removeAllLinks('inputs');
this.removeAllLinks('outputs');
return this;
}
var n2, k;
var n2ListName = listName === 'inputs' ? 'outputs' : 'inputs';
for (var j = 0; j < this[listName].length; j++) {
n2 = this[listName][j];
k = n2[n2ListName].indexOf(this);
if (k < 0) {
throw {
from: "removeAllLinks",
data: { list: n2[n2ListName], ctx: this }
};
}
n2[n2ListName] = deleteItem(n2[n2ListName], k);
}
return this;
};
function Graph() {
this.keys = {};
this.nodes = [];
}
Graph.prototype.resolveNode = function (node, fn) {
return fn.call(this, this.getNode(node));
};
Graph.nodeMethod = Node.nodeMethod;
Graph.prototype.getNode = Graph.prototype.get =
function (input) {
return typeof input === 'object' ? input : this.nodes[this.keys[input]];
};
Graph.prototype.pushNode = Graph.prototype.push =
function (node, key) {
this.keys[key] = this.nodes.length;
this.nodes.push(node);
return node;
};
Graph.prototype.createNode = Graph.prototype.create =
function (val, key) {
return this.pushNode(new Node(this, val), key);
};
Graph.prototype.removeNode = Graph.prototype.remove =
Graph.nodeMethod(function (node) {
var i = this.nodes.indexOf(node);
if (i >= 0) {
node.removeAllLinks();
deleteItem(this.nodes, index);
}
return this;
});
Graph.prototype.existsNode = Graph.prototype.exists =
Graph.nodeMethod(function (node) {
return this.nodes.indexOf(node) >= 0;
});
ns.Graph = Graph;
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment