pivot-editor/index.html

206 lines
5.9 KiB
HTML
Raw Normal View History

2021-05-08 13:51:19 -04:00
<div id="app"></div>
2021-05-11 06:26:18 -04:00
2021-05-26 22:54:50 -04:00
<!-- N -->
2021-05-09 20:26:40 -04:00
<script>
2021-05-08 13:51:19 -04:00
2021-05-09 20:26:40 -04:00
var N =
{
2021-05-09 14:19:13 -04:00
ID:{
Walk:0,
Instance:0
},
2021-05-11 23:18:08 -04:00
2021-05-11 06:26:18 -04:00
Create(inMeta)
2021-05-05 21:53:04 -04:00
{
2021-05-11 06:26:18 -04:00
return {
2021-05-09 14:19:13 -04:00
ID:{
Walk:0,
Instance:N.ID.Instance++
2021-05-11 06:26:18 -04:00
},
Meta:inMeta||{},
Link:{}
2021-05-05 21:53:04 -04:00
};
},
2021-05-14 07:11:23 -04:00
Connect(inNodeMajor, inNodeMinor, inKey, inUnique)
2021-05-05 21:53:04 -04:00
{
2021-05-14 07:11:23 -04:00
if(inUnique) // bail if the nodes are already connected
{
let check = N.Step(inNodeMajor, inKey, true);
if(check)
{
2021-05-20 19:27:29 -04:00
if(check.indexOf(inNodeMinor) !== -1)
2021-05-14 07:11:23 -04:00
{
return;
}
}
}
2021-05-11 23:18:08 -04:00
N.Step(inNodeMajor, inKey, true, true).push(inNodeMinor);
N.Step(inNodeMinor, inKey, false, true).push(inNodeMajor);
2021-05-08 07:03:41 -04:00
},
2021-05-09 20:26:40 -04:00
Disconnect(inNodeMajor, inNodeMinor, inKey)
2021-05-08 07:03:41 -04:00
{
2021-05-09 20:26:40 -04:00
let remove = (inArray, inMatch) => inArray.findIndex( (inMember, inIndex, inArray) => (inMember === inMatch) ? inArray.splice(inIndex, 1) : false );
// if no specific child was passed
2021-05-09 20:26:40 -04:00
if(inNodeMinor === null)
{
// get all the children
2021-05-14 07:11:23 -04:00
let check = N.Step(inNodeMajor, inKey);
if(!check){ return; }
// go down to each child ...
check.forEach( inNodeMinor =>
2021-05-09 14:19:13 -04:00
{
let connections = inNodeMinor.Link[inKey];
remove( connections.Get, inNodeMajor); // ... and remove any reference to the parent
// if after the remove operation, this child has no connections on inKey, scrub the key
if(!connections.Set.length && !connections.Get.length)
2021-05-09 20:26:40 -04:00
{
delete inNodeMinor.Link[inKey];
2021-05-09 20:26:40 -04:00
}
2021-05-09 14:19:13 -04:00
});
// we just wiped out all outgoing connections to the parent, if incoming connections are empty too we can purge the key there as well
2021-05-09 20:26:40 -04:00
if(inNodeMajor.Link[inKey].Get.length == 0)
{
delete inNodeMajor.Link[inKey];
}
2021-05-09 14:19:13 -04:00
return;
}
// if no specific parent was passed
2021-05-09 20:26:40 -04:00
if(inNodeMajor === null)
{
// get all the parents
let check = N.Step(inNodeMinor, inKey, false);
if(!check){ return; }
// go up to each parent ...
check.forEach( inNodeMajor =>
2021-05-09 14:19:13 -04:00
{
let connections = inNodeMajor.Link[inKey];
remove( connections.Set, inNodeMinor); // ... and remove any reference to the child
// if after the remove operation, this parent has no connections on inKey, scrub the key
if( !connections.Set.length && !connections.Get.length )
2021-05-09 20:26:40 -04:00
{
delete inNodeMajor.Link[inKey];
2021-05-09 20:26:40 -04:00
}
2021-05-09 14:19:13 -04:00
});
// we just wiped out all incoming connections to the child, if outgoing connections are empty too we can purge the key there as well
2021-05-09 20:26:40 -04:00
if(inNodeMinor.Link[inKey].Set.length == 0)
{
delete inNodeMinor.Link[inKey];
}
2021-05-09 14:19:13 -04:00
return;
}
// if a specific parent and child were passed
2021-05-09 20:26:40 -04:00
if(inNodeMajor.Link[inKey].Set.length == 1)
{
delete inNodeMajor.Link[inKey];
}
else
{
remove(inNodeMajor.Link[inKey].Set, inNodeMinor);
}
if(inNodeMinor.Link[inKey].Get.length == 1)
{
delete inNodeMinor.Link[inKey];
}
else
{
remove(inNodeMinor.Link[inKey].Get, inNodeMajor);
}
2021-05-11 23:18:08 -04:00
},
Step(inNode, inKey, inForward, inForceCreate)
{
let connectionGroup = inNode.Link[inKey];
if(!connectionGroup)
{
if(inForceCreate === true)
2021-05-11 23:18:08 -04:00
{
inNode.Link[inKey] = connectionGroup = {Get:[], Set:[]};
}
else
{
return false;
}
}
return (inForward === undefined || inForward === true) ? connectionGroup.Set : connectionGroup.Get;
2021-05-29 11:07:12 -04:00
2021-05-11 23:18:08 -04:00
2021-05-05 21:53:04 -04:00
},
2021-05-15 07:30:52 -04:00
Walk(inIterator, inNode, inKey, inForward, inTerminal)
2021-05-05 21:53:04 -04:00
{
2021-05-11 06:26:18 -04:00
let array = N.Step(inNode, inKey, inForward);
2021-05-15 07:30:52 -04:00
if(!array.length && inTerminal)
2021-05-05 21:53:04 -04:00
{
2021-05-15 07:30:52 -04:00
return inTerminal(inNode);
}
2021-05-13 19:54:42 -04:00
2021-05-15 07:30:52 -04:00
for(let i=0; i<array.length; i++)
{
2021-05-05 21:53:04 -04:00
let next = array[i];
2021-05-09 14:19:13 -04:00
if(next.ID.Walk !== N.ID.Walk)
2021-05-05 21:53:04 -04:00
{
2021-05-09 14:19:13 -04:00
next.ID.Walk = N.ID.Walk;
2021-05-28 20:52:56 -04:00
//console.log("processing", next.Meta)
2021-05-05 21:53:04 -04:00
let results = inIterator(next);
if(results !== false)
{
2021-05-15 07:30:52 -04:00
N.Walk(inIterator, next, inKey, inForward, inTerminal);
2021-05-05 21:53:04 -04:00
}
2021-05-28 20:52:56 -04:00
else
{
//console.log("routine exited");
}
}
else
{
//console.log("id collision");
2021-05-05 21:53:04 -04:00
}
}
2021-05-11 23:18:08 -04:00
},
Path(inArray, inNode, inKey, inForward)
{
var current = inNode;
var direction = inForward||true;
for(let i=0; i<inArray.length; i++)
{
current = N.Step(current, inKey, direction)[inArray[i]];
}
return current;
2021-05-09 20:26:40 -04:00
}
};
2021-05-11 23:18:08 -04:00
</script>
2021-05-09 20:26:40 -04:00
2021-05-26 22:54:50 -04:00
2021-05-29 11:07:12 -04:00
<script>
let root = N.Create("root");
let grow = node =>
2021-05-26 22:54:50 -04:00
{
2021-05-29 11:07:12 -04:00
let c1 = N.Create(node.Meta+"-1");
let c2 = N.Create(node.Meta+"-2");
N.Connect(node, c1, "parent");
N.Connect(node, c2, "parent");
};
var depth = 0;
let recurse = node =>
2021-05-26 22:54:50 -04:00
{
2021-05-29 11:07:12 -04:00
depth++;
grow(node);
if(depth < 2)
2021-05-26 22:54:50 -04:00
{
2021-05-29 11:07:12 -04:00
N.ID.Walk++;
N.Walk(recurse, node, "parent", true);
2021-05-26 22:54:50 -04:00
}
2021-05-29 11:07:12 -04:00
return false;
2021-05-28 19:11:15 -04:00
}
2021-05-29 11:07:12 -04:00
recurse(root);
2021-05-28 19:11:15 -04:00
2021-05-24 22:52:55 -04:00
</script>