Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- g1 = (function(g) { g1 = new g; g1.addNode('n1'); g1.addNode('n2'); g1.addNode('n3'); g1.addLink('l12', 'n1', 'n2'); g1.addLink('l21', 'n2', 'n1'); g1.addLink('l13', 'n1', 'n3'); return g1 })((function() {
- var
- r = function() { return (new Date).getTime() + Math.ceil(Math.random()*10000); },
- grep = function(a, c) { var r=[], i; for(i=0;i<a.length; ++i) { if (c.apply(a[i], [])) r.push(a[i]) } return r; },
- proxy = function(ctx, f) { var args = Array.prototype.slice.apply(arguments, [1]); return function(){ return f.apply(ctx,args); } },
- init = function(o,id) {
- this.owner = o;
- if (typeof id === 'undefined') { id = r(); }
- this.id = id;
- },
- n = function(o, id, data){
- init.apply(this, [o, id]);
- this.data = data;
- },
- l = function(o, id, fromNodeId, toNodeId, data){
- init.apply(this, [o, id]);
- this.fromNodeId=fromNodeId; this.toNodeId=toNodeId; this.data=data;
- },
- g = function(){
- };g.l=l;g.n=n;
- n.prototype = (function(p){
- p.addLink = function(toNodeId, data) { this.owner.addLink(this.id, data) }
- p.getLinksFrom = function() { return this.owner.linksFrom(this.id) }
- p.getLinksTo = function() { return this.owner.linksTo(this.id) }
- })({});
- g.prototype = (function(p){
- var nodes = [], links = [];
- p.addNode = function(id, data){ nodes.push(new n(this,id,data)); }
- p.printNodes = function() { console.log(nodes) }
- p.addLink = function(id, fromNodeId, toNodeId, data) { links.push(new l(this,id,fromNodeId, toNodeId, data)) }
- p.printLinks = function() { console.log(links) }
- p.linksFrom = function(nodeId) { return grep(links, function() { return this.fromNodeId === nodeId; }) }
- p.linksTo = function(nodeId) { return grep(links, function() { return this.toNodeId === nodeId; }) }
- p.getNode = function(nodeId) { return nodes[nodeId] }
- p.getLink = function(linkId) { return links[linkId] }
- return p;
- })({});
- return g;
- })())
Advertisement
Add Comment
Please, Sign In to add comment