SwVitaliy

Untitled

Jul 31st, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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() {
  2. var
  3. r = function() { return (new Date).getTime() + Math.ceil(Math.random()*10000); },
  4. 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; },
  5. proxy = function(ctx, f) { var args = Array.prototype.slice.apply(arguments, [1]); return function(){ return f.apply(ctx,args); } },
  6. init = function(o,id) {
  7. this.owner = o;
  8. if (typeof id === 'undefined') { id = r(); }
  9. this.id = id;
  10. },
  11. n = function(o, id, data){
  12. init.apply(this, [o, id]);
  13. this.data = data;
  14. },
  15. l = function(o, id, fromNodeId, toNodeId, data){
  16. init.apply(this, [o, id]);
  17. this.fromNodeId=fromNodeId; this.toNodeId=toNodeId; this.data=data;
  18. },
  19. g = function(){  
  20.  
  21. };g.l=l;g.n=n;
  22. n.prototype = (function(p){
  23. p.addLink = function(toNodeId, data) { this.owner.addLink(this.id, data) }
  24. p.getLinksFrom = function() { return this.owner.linksFrom(this.id) }
  25. p.getLinksTo = function() { return this.owner.linksTo(this.id) }
  26. })({});
  27. g.prototype = (function(p){
  28. var nodes = [], links = [];
  29. p.addNode = function(id, data){ nodes.push(new n(this,id,data)); }
  30. p.printNodes = function() { console.log(nodes) }
  31. p.addLink = function(id, fromNodeId, toNodeId, data) { links.push(new l(this,id,fromNodeId, toNodeId, data)) }
  32. p.printLinks = function() { console.log(links) }
  33. p.linksFrom = function(nodeId) { return grep(links, function() { return this.fromNodeId === nodeId; }) }
  34. p.linksTo = function(nodeId) { return grep(links, function() { return this.toNodeId === nodeId; }) }
  35. p.getNode = function(nodeId) { return nodes[nodeId] }
  36. p.getLink = function(linkId) { return links[linkId] }
  37. return p;
  38. })({});
  39.  
  40. return g;
  41.  
  42.  })())
Advertisement
Add Comment
Please, Sign In to add comment