JSFiddle - React, Tailwind, and code Playground
by superboggly
HTML
<script src="http://underscorejs.org/underscore-min.js"></script>
JavaScript
var linkGraph = (function() {
var nodes = {};
var safety = 0;
function addEdge(fromId, toId, link) {
var node = nodes[fromId];
if(!node) {
node = {id: fromId, p: null, visit: 0, friends: {}};
nodes[fromId] = node;
}
// Currently only one link per widget pair
node.friends[toId] = link;
}
function removeEdge(fromId, toId) {
var node = nodes[fromId];
if(node) {
delete node.friends[toId];
if($.isEmptyObject(node.friends)) delete nodes[fromId];
}
}
function hasCycle() {
}
function _initTraversal() {
_(nodes).forEach(function(d) {
d.p = null;
d.visit = 0;
});
}
function _bfs(startWidgetId, linkCallback, cycleCallback) {
_initTraversal();
var start = nodes[startWidgetId];
var toProcess = [["#root",start, null]];
safety = 0;
while(toProcess.length > 0) {
if(safety > 1000) { return false;}
safety++;
var edge = toProcess.shift();
var parentId = edge[0];
var widget = edge[1];
var link = edge[2];
if(link) {
linkCallback(parentId, widget.id, link, widget.visit > 0);
}
if(widget.visit > 0) {
// do not humour cycles
var cont = cycleCallback(parentId, widget.id);
if(!cont) return;
// Remove symmetric call from toProcess list
toProcess = _(toProcess).reject(function(d) { return d[0] == widget.id && d[1].id == parentId; });
continue;
}
widget.visit++;
widget.p = parentId;
_(widget.friends).forEach(function(link,targetId,k) {
var target = nodes[targetId];
...