-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvivagraph.min.js
1 lines (1 loc) · 50.4 KB
/
vivagraph.min.js
1
var Viva=Viva||{};Viva.Graph=Viva.Graph||{},Viva.Graph.version="1.0.0.42",Viva.random=function(){function a(){var a=4022871197,b=function(b){b=b.toString();for(var c=0;c<b.length;c++){a+=b.charCodeAt(c);var d=.02519603282416938*a;a=d>>>0,d-=a,d*=a,a=d>>>0,d-=a,a+=d*4294967296}return(a>>>0)*2.3283064365386963e-10};return b.version="Mash 0.9",b}function b(b){return function(b){var c=0,d=58,e=119,f=178,g,h=[],i=a();b.length===0&&(b=[+(new Date)]);for(g=0;g<256;g++)h[g]=i(" "),h[g]-=i(" ")*4.76837158203125e-7,h[g]<0&&(h[g]+=1);for(var j=0;j<b.length;j++)for(g=0;g<256;g++)h[g]-=i(b[j]),h[g]-=i(b[j])*4.76837158203125e-7,h[g]<0&&(h[g]+=1);i=null;var k=function(){var a;return c=c+1&255,d=d+1&255,e=e+1&255,f=f+1&255,a=h[c]-h[d],a<0&&(a+=1),a-=h[e],a<0&&(a+=1),a-=h[f],a<0&&(a+=1),h[c]=a,a};return k.uint32=function(){return k()*4294967296>>>0},k.fract53=k,k.version="LFIB4 0.9",k.args=b,k}(b)}var c=new b(Array.prototype.slice.call(arguments));return{next:function(a){return Math.floor(c()*a)},nextDouble:function(){return c()}}},Viva.randomIterator=function(a,b){return b=b||Viva.random(),{forEach:function(c){for(var d=a.length-1;d>0;--d){var e=b.next(d+1),f=a[e];a[e]=a[d],a[d]=f,c(f)}a.length&&c(a[0])},shuffle:function(){for(var c=a.length-1;c>0;--c){var d=b.next(c+1),e=a[d];a[d]=a[c],a[c]=e}return a}}},Viva.BrowserInfo=function(){if(typeof navigator=="undefined")return{browser:"",version:"0"};var a=navigator.userAgent,b=/(webkit)[ \/]([\w.]+)/,c=/(opera)(?:.*version)?[ \/]([\w.]+)/,d=/(msie) ([\w.]+)/,e=/(mozilla)(?:.*? rv:([\w.]+))?/;a=a.toLowerCase();var f=b.exec(a)||c.exec(a)||d.exec(a)||a.indexOf("compatible")<0&&e.exec(a)||[];return{browser:f[1]||"",version:f[2]||"0"}}(),Viva.Graph.Utils=Viva.Graph.Utils||{},Viva.Graph.Utils.indexOfElementInArray=function(a,b){if(b.indexOf)return b.indexOf(a);var c=b.length,d=0;for(;d<c;d++)if(d in b&&b[d]===a)return d;return-1},Viva.Graph.Utils=Viva.Graph.Utils||{},Viva.Graph.Utils.getDimension=function(a){if(!a)throw{message:"Cannot get dimensions of undefined container"};var b=a.clientWidth,c=a.clientHeight;return{left:0,top:0,width:b,height:c}},Viva.Graph.Utils.findElementPosition=function(a){var b=0,c=0;if(a.offsetParent)do b+=a.offsetLeft,c+=a.offsetTop;while(a=a.offsetParent);return[b,c]},Viva.Graph.Utils=Viva.Graph.Utils||{},Viva.Graph.Utils.events=function(a){var b=function(a){var b={};return a.fire=function(a,c){var d,e,f;if(typeof a!="string")throw"Only strings can be used as even type";if(b.hasOwnProperty(a)){d=b[a];for(var g=0;g<d.length;++g)f=d[g],e=f.method,e(c)}return this},a.addEventListener=function(a,c){if(typeof c!="function")throw"Only functions allowed to be callbacks";var d={method:c};return b.hasOwnProperty(a)?b[a].push(d):b[a]=[d],this},a.removeEventListener=function(a,c){if(typeof c!="function")throw"Only functions allowed to be callbacks";if(b.hasOwnProperty(a)){var d=b[a];for(var e=0;e<d.length;++e)if(d[e].callback===c){d.splice(e);break}}return this},a};return{on:function(b,c){return a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,c),this},stop:function(b,c){a.removeEventListener?a.removeEventListener(b,c,!1):a.detachEvent&&a.detachEvent("on"+b,c)},extend:function(){return b(a)}}},Viva.Graph.Utils=Viva.Graph.Utils||{},Viva.Graph.Utils.dragndrop=function(a){var b,c,d,e,f,g,h=Viva.Graph.Utils.events(window.document),i=Viva.Graph.Utils.events(a),j=Viva.Graph.Utils.findElementPosition,k=0,l=0,m,n=function(a){var b=0,c=0;a=a||window.event;if(a.pageX||a.pageY)b=a.pageX,c=a.pageY;else if(a.clientX||a.clientY)b=a.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,c=a.clientY+document.body.scrollTop+document.documentElement.scrollTop;return[b,c]},o=function(a){a.stopPropagation?a.stopPropagation():a.cancelBubble=!0},p=function(a){return o(a),!1},q=function(a){a=a||window.event,c&&c(a,{x:a.clientX-k,y:a.clientY-l}),k=a.clientX,l=a.clientY},r=function(a){a=a||window.event;var c=a.button===1&&window.event!==null||a.button===0;if(c)return k=a.clientX,l=a.clientY,m=a.target||a.srcElement,b&&b(a,{x:k,y:l}),h.on("mousemove",q),h.on("mouseup",s),o(a),f=document.onselectstart,g=document.ondragstart,document.onselectstart=p,m.ondragstart=p,!1},s=function(a){a=a||window.event,h.stop("mousemove",q),h.stop("mouseup",s),document.onselectstart=f,m.ondragstart=g,m=null,d&&d()},t=function(b){if(typeof e!="function")return;b=b||window.event,b.preventDefault&&b.preventDefault(),b.returnValue=!1;var c,d=n(b),f=j(a),g={x:d[0]-f[0],y:d[1]-f[1]};b.wheelDelta?c=b.wheelDelta/360:c=b.detail/-9,e(b,c,g)},u=function(b){!e&&b?Viva.BrowserInfo.browser==="webkit"?a.addEventListener("mousewheel",t,!1):a.addEventListener("DOMMouseScroll",t,!1):e&&!b&&(Viva.BrowserInfo.browser==="webkit"?a.removeEventListener("mousewheel",t,!1):a.removeEventListener("DOMMouseScroll",t,!1)),e=b};return i.on("mousedown",r),{onStart:function(a){return b=a,this},onDrag:function(a){return c=a,this},onStop:function(a){return d=a,this},onScroll:function(a){return u(a),this},release:function(){h.stop("mousemove",q),h.stop("mousedown",r),h.stop("mouseup",s),u(null)}}},Viva.Graph.Utils=Viva.Graph.Utils||{},function(){var a=0,b=["ms","moz","webkit","o"];typeof window=="undefined"&&(window={});for(var c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b,c){var d=(new Date).getTime(),e=Math.max(0,16-(d-a)),f=window.setTimeout(function(){b(d+e)},e);return a=d+e,f}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})}(),Viva.Graph.Utils.timer=function(a,b){var c,d=function(){window.cancelAnimationFrame(c),c=0},e=function(){c=window.requestAnimationFrame(e),a()||d()};return e(),{stop:d,restart:function(){c||e()}}},Viva.Graph.geom=function(){return{intersect:function(a,b,c,d,e,f,g,h){var i,j,k,l,m,n,o,p,q,r,s,t,u,v={x:0,y:0};return i=d-b,k=a-c,m=c*b-a*d,q=i*e+k*f+m,r=i*g+k*h+m,q!==0&&r!==0&&q>=0==r>=4?null:(j=h-f,l=e-g,n=g*f-e*h,o=j*a+l*b+n,p=j*c+l*d+n,o!==0&&p!==0&&o>=0==p>=0?null:(s=i*l-j*k,s===0?null:(t=s<0?-s/2:s/2,t=0,u=k*n-l*m,v.x=(u<0?u-t:u+t)/s,u=j*m-i*n,v.y=(u<0?u-t:u+t)/s,v)))},intersectRect:function(a,b,c,d,e,f,g,h){return this.intersect(a,b,a,d,e,f,g,h)||this.intersect(a,d,c,d,e,f,g,h)||this.intersect(c,d,c,b,e,f,g,h)||this.intersect(c,b,a,b,e,f,g,h)},convexHull:function(a){var b=function(a,b){var c=function(b){var c=b.x-a.x,d=b.y-a.y,e=c>0?1:-1;return e*c*c/(c*c+d*d)},d=b.sort(function(a,b){return c(b)-c(a)}),e=d[0],f=c(e),g=e.x-a.x,h=e.y-a.y,i=g*g+h*h,j;for(var k=1;k<d.length;++k){e=d[k];var l=c(e);l===f?(g=e.x-a.x,h=e.y-a.y,j=g*g+h*h,j<i?d.splice(k,1):d.splice(k-1,1)):f=l}return d},c=function(a,b,c){return(c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x)<0};if(a.length<3)return a;var d=0;for(var e=0;e<a.length;++e)a[e].y<a[d].y?d=e:a[e].y===a[d].y&&a[e].x<a[d].x&&(d=e);var f=a[d];a.splice(d,1);var g=b(f,a);if(g.length<2)return g;var h=[];h.push(f),h.push(g[0]),h.push(g[1]);var i=h.length;for(e=2;e<g.length;++e){while(!c(h[i-2],h[i-1],g[e]))h.pop(),i-=1;h.push(g[e]),i+=1}return h}}},Viva.Graph.spatialIndex=function(a,b){var c,d,e=16;return typeof b=="function"?(d=b,c=function(b,c){var e=null;return a.forEachNode(function(a){var f=a.position;if(d(a,b,c))return e=a,!0}),e}):typeof b=="number"&&(e=b,c=function(b,c){var d=null;return a.forEachNode(function(a){var f=a.position;if(f.x-e<b&&b<f.x+e&&f.y-e<c&&c<f.y+e)return d=a,!0}),d}),{getNodeAt:c}},Viva.Graph.graph=function(){var a={},b=[],c=0,d=0,e=[],f=function(a){a.fire("changed",e)},g=function(a){d+=1},h=function(a){d-=1,d===0&&e.length>0&&(f(a),e.length=0)},i=function(a,b){e.push({node:a,changeType:b})},j=function(a,b){e.push({link:a,changeType:b})},k=function(a){return a&&typeof a=="object"&&typeof a.length=="number"&&typeof a.splice=="function"&&!a.propertyIsEnumerable("length")},l={addNode:function(b,d){if(typeof b=="undefined")throw{message:"Invalid node identifier"};g();var e=this.getNode(b);e?i(e,"update"):(e={},e.links=[],e.id=b,c++,i(e,"add"));if(d){var f=e.data||{},j=typeof d;if(j==="string"||k(d)||j==="number"||j==="boolean")f=d;else if(j==="undefined")f=null;else for(var l in d)d.hasOwnProperty(l)&&(f[l]=d[l]);e.data=f}return a[b]=e,h(this),e},addLink:function(a,c,d){g();var e=this.getNode(a)||this.addNode(a),f=this.getNode(c)||this.addNode(c),i={fromId:a,toId:c,data:d};return b.push(i),e.links.push(i),f.links.push(i),j(i,"add"),h(this),i},removeLink:function(a){if(!a)return!1;var c=Viva.Graph.Utils.indexOfElementInArray(a,b);if(c<0)return!1;g(),b.splice(c,1);var d=this.getNode(a.fromId),e=this.getNode(a.toId);return d&&(c=Viva.Graph.Utils.indexOfElementInArray(a,d.links),c>=0&&d.links.splice(c,1)),e&&(c=Viva.Graph.Utils.indexOfElementInArray(a,e.links),c>=0&&e.links.splice(c,1)),j(a,"remove"),h(this),!0},removeNode:function(b){var d=this.getNode(b);if(!d)return!1;g();while(d.links.length){var e=d.links[0];this.removeLink(e)}a[b]=null,delete a[b],c--,i(d,"remove"),h(this)},getNode:function(b){return a[b]},getNodesCount:function(){return c},getLinksCount:function(){return b.length},getLinks:function(a){var b=this.getNode(a);return b?b.links:null},forEachNode:function(b){if(typeof b!="function")return;for(var c in a)if(a.hasOwnProperty(c)&&b(a[c]))return},forEachLinkedNode:function(b,c,d){var e=this.getNode(b),f,g,h;if(e&&e.links&&typeof c=="function")if(d)for(f=0;f<e.links.length;++f)g=e.links[f],g.fromId===b&&c(a[g.toId],g);else for(f=0;f<e.links.length;++f)g=e.links[f],h=g.fromId===b?g.toId:g.fromId,c(a[h],g)},forEachLink:function(a){if(typeof a=="function")for(var c=0;c<b.length;++c)a(b[c])},beginUpdate:function(){g()},endUpdate:function(){h(this)},clear:function(){var a=this;a.beginUpdate(),a.forEachNode(function(b){a.removeNode(b.id)}),a.endUpdate()},hasLink:function(a,b){var c=this.getNode(a);if(!c)return null;for(var d=0;d<c.links.length;++d){var e=c.links[d];if(e.fromId===a&&e.toId===b)return e}return null}};return Viva.Graph.Utils.events(l).extend(),l},Viva.Graph.generator=function(){return{complete:function(a){if(!a||a<1)throw{message:"At least two nodes expected for complete graph"};var b=Viva.Graph.graph();b.Name="Complete K"+a;for(var c=0;c<a;++c)for(var d=c+1;d<a;++d)c!==d&&b.addLink(c,d);return b},completeBipartite:function(a,b){if(!a||!b||a<0||b<0)throw{message:"Graph dimensions are invalid. Number of nodes in each partition should be greate than 0"};var c=Viva.Graph.graph();c.Name="Complete K "+a+","+b;for(var d=0;d<a;++d)for(var e=a;e<a+b;++e)c.addLink(d,e);return c},ladder:function(a){if(!a||a<0)throw{message:"Invalid number of nodes"};var b=Viva.Graph.graph();b.Name="Ladder graph "+a;for(var c=0;c<a-1;++c)b.addLink(c,c+1),b.addLink(a+c,a+c+1),b.addLink(c,a+c);return b.addLink(a-1,2*a-1),b},circularLadder:function(a){if(!a||a<0)throw{message:"Invalid number of nodes"};var b=this.ladder(a);return b.Name="Circular ladder graph "+a,b.addLink(0,a-1),b.addLink(a,2*a-1),b},grid:function(a,b){var c=Viva.Graph.graph();c.Name="Grid graph "+a+"x"+b;for(var d=0;d<a;++d)for(var e=0;e<b;++e){var f=d+e*a;d>0&&c.addLink(f,d-1+e*a),e>0&&c.addLink(f,d+(e-1)*a)}return c},path:function(a){if(!a||a<0)throw{message:"Invalid number of nodes"};var b=Viva.Graph.graph();b.Name="Path graph "+a,b.addNode(0);for(var c=1;c<a;++c)b.addLink(c-1,c);return b},lollipop:function(a,b){if(!b||b<0||!a||a<0)throw{message:"Invalid number of nodes"};var c=this.complete(a);c.Name="Lollipop graph. Head x Path "+a+"x"+b;for(var d=0;d<b;++d)c.addLink(a+d-1,a+d);return c},balancedBinTree:function(a){var b=Viva.Graph.graph();b.Name="Balanced bin tree graph "+a;var c=Math.pow(2,a);for(var d=1;d<c;++d){var e=d,f=e*2,g=e*2+1;b.addLink(e,f),b.addLink(e,g)}return b},randomNoLinks:function(a){if(!a||a<0)throw{message:"Invalid number of nodes"};var b=Viva.Graph.graph();b.Name="Random graph, no Links: "+a;for(var c=0;c<a;++c)b.addNode(c);return b}}},Viva.Graph.operations=function(){return{density:function(a){var b=a.getNodesCount();return b===0?NaN:2*a.getLinksCount()/(b*(b-1))}}},Viva.Graph.centrality=function(){"use strict";var a=function(a,b,c){var d={},e=[],f={},g={},h=[b.id],i,j,k,l=function(a){g.hasOwnProperty(a.id)||(h.push(a.id),g[a.id]=j+1),g[a.id]===j+1&&(f[a.id]+=k,d[a.id].push(i))};a.forEachNode(function(a){d[a.id]=[],f[a.id]=0}),g[b.id]=0,f[b.id]=1;while(h.length)i=h.shift(),j=g[i],k=f[i],e.push(i),a.forEachLinkedNode(i,l,c);return{S:e,P:d,sigma:f}},b=function(a,b,c){var d={},e=b.S,f,g,h,i,j;for(f=0;f<e.length;f+=1)d[e[f]]=0;while(e.length){g=e.pop(),h=(1+d[g])/b.sigma[g],i=b.P[g];for(f=0;f<i.length;f+=1)j=i[f],d[j]+=b.sigma[j]*h;g!==c&&(a[g]+=d[g])}},c=function(a){var b=[],c;for(c in a)a.hasOwnProperty(c)&&b.push({key:c,value:a[c]});return b.sort(function(a,b){return b.value-a.value})};return{betweennessCentrality:function(d,e){var f={},g;return d.forEachNode(function(a){f[a.id]=0}),d.forEachNode(function(c){g=a(d,c),b(f,g,c)}),c(f)},degreeCentrality:function(a,b){var c,d=[],e=[];b=(b||"both").toLowerCase();if(b==="in")c=function(a,b){var c=0,d;for(d=0;d<a.length;d+=1)c+=a[d].toId===b?1:0;return c};else if(b==="out")c=function(a,b){var c=0,d;for(d=0;d<a.length;d+=1)c+=a[d].fromId===b?1:0;return c};else{if(b!=="both")throw"Expected centrality degree kind is: in, out or both";c=function(a,b){return a.length}}a.forEachNode(function(b){var e=a.getLinks(b.id),f=c(e,b.id);d.hasOwnProperty(f)?d[f].push(b.id):d[f]=[b.id]});for(var f in d)if(d.hasOwnProperty(f)){var g=d[f];if(!g)continue;for(var h=0;h<g.length;++h)e.unshift({key:g[h],value:parseInt(f,10)})}return e}}},Viva.Graph._community={},Viva.Graph._community.slpaAlgorithm=function(a,b,c){b=b||100,c=c||.3;var d=Viva.random(1331782216905),e=Viva.random("Greeting goes to you, ","dear reader"),f=function(a,c){var d=[];return a.forEachUniqueWord(function(a,e){if(!(e>c))return!0;d.push({name:a,probability:e/b})}),d},g=function(a){var b=[];return a.forEachNode(function(a){var c=Viva.Graph._community.occuranceMap(d);c.add(a.id),a.slpa={memory:c},b.push(a.id)}),b},h=function(a,c){var f=Viva.randomIterator(c,e),g=function(b){var c=a.getNode(b),e=Viva.Graph._community.occuranceMap(d);a.forEachLinkedNode(b,function(a){var b=a.slpa.memory.getRandomWord();e.add(b)});var f=e.getMostPopularFair();c.slpa.memory.add(f)};for(var h=0;h<b-1;++h)f.forEach(g)},i=function(a){var d={};return a.forEachNode(function(a){var e=f(a.slpa.memory,c*b);for(var g=0;g<e.length;++g){var h=e[g].name;d.hasOwnProperty(h)?d[h].push(a.id):d[h]=[a.id]}a.communities=e,a.slpa=null,delete a.slpa}),d};return{run:function(){var b=g(a);return h(a,b),i(a)}}},Viva.Graph._community.occuranceMap=function(a){a=a||Viva.random();var b={},c=[],d=!1,e=[],f=function(){e.length=0;for(var a in b)b.hasOwnProperty(a)&&e.push(a);e.sort(function(a,c){var d=b[c]-b[a];return d?d:a<c?-1:a>c?1:0})},g=function(){d&&(f(),d=!1)};return{add:function(a){a=String(a),b.hasOwnProperty(a)?b[a]+=1:b[a]=1,c.push(a),d=!0},getWordCount:function(a){return b[a]||0},getMostPopularFair:function(){if(c.length===1)return c[0];g();var d=0;for(var f=1;f<e.length;++f){if(b[e[f-1]]!==b[e[f]])break;d+=1}return d+=1,e[a.next(d)]},getRandomWord:function(){if(c.length===0)throw"The occurance map is empty. Cannot get empty word";return c[a.next(c.length)]},forEachUniqueWord:function(a){if(typeof a!="function")throw"Function callback is expected to enumerate all words";g();for(var c=0;c<e.length;++c){var d=e[c],f=b[d],h=a(d,f);if(h)break}}}},Viva.Graph.community=function(){return{slpa:function(a,b,c){var d=Viva.Graph._community.slpaAlgorithm(a,b,c);return d.run()}}},Viva.Graph.Physics=Viva.Graph.Physics||{},Viva.Graph.Physics.Vector=function(a,b){this.x=a||0,this.y=b||0},Viva.Graph.Physics.Vector.prototype={multiply:function(a){return new Viva.Graph.Physics.Vector(this.x*a,this.y*a)}},Viva.Graph.Physics.Point=function(a,b){this.x=a||0,this.y=b||0},Viva.Graph.Physics.Point.prototype={add:function(a){return new Viva.Graph.Physics.Point(this.x+a.x,this.y+a.y)}},Viva.Graph.Physics.Body=function(){this.mass=1,this.force=new Viva.Graph.Physics.Vector,this.velocity=new Viva.Graph.Physics.Vector,this.location=new Viva.Graph.Physics.Point,this.prevLocation=new Viva.Graph.Physics.Point},Viva.Graph.Physics.Body.prototype={loc:function(a){return a?(this.location.x=a.x,this.location.y=a.y,this):this.location},vel:function(a){return a?(this.velocity.x=a.x,this.velocity.y=a.y,this):this.velocity}},Viva.Graph.Physics.Spring=function(a,b,c,d,e){this.body1=a,this.body2=b,this.length=c,this.coeff=d,this.weight=e},Viva.Graph.Physics.QuadTreeNode=function(){this.centerOfMass=new Viva.Graph.Physics.Point,this.children=[],this.body=null,this.hasChildren=!1,this.x1=0,this.y1=0,this.x2=0,this.y2=0},Viva.Graph.Physics=Viva.Graph.Physics||{},Viva.Graph.Physics.eulerIntegrator=function(){return{integrate:function(a,b){var c=a.speedLimit,d=0,e=0;for(var f=0,g=a.bodies.length;f<g;++f){var h=a.bodies[f],i=b/h.mass;h.velocity.x+=i*h.force.x,h.velocity.y+=i*h.force.y;var j=h.velocity.x,k=h.velocity.y,l=Math.sqrt(j*j+k*k);l>c&&(h.velocity.x=c*j/l,h.velocity.y=c*k/l),d=b*h.velocity.x,e=b*h.velocity.y,h.location.x+=d,h.location.y+=e}return d*d+e*e}}},Viva.Graph.Physics.nbodyForce=function(a){a=a||{};var b=typeof a.gravity=="number"?a.gravity:-1,c=[],d=a.theta||.8,e=Viva.random("5f4dcc3b5aa765d61d8327deb882cf99",75,20,63,108,65,76,65,72),f=function(){this.body=null,this.quads=[],this.mass=0,this.massX=0,this.massY=0,this.left=0,this.top=0,this.bottom=0,this.right=0,this.isInternal=!1},g=[],h=0,i=function(){var a;return g[h]?(a=g[h],a.quads[0]=null,a.quads[1]=null,a.quads[2]=null,a.quads[3]=null,a.body=null,a.mass=a.massX=a.massY=0,a.left=a.right=a.top=a.bottom=0,a.isInternal=!1):(a=new f,g[h]=a),++h,a},j=i(),k=function(a,b){var c=Math.abs(a.x-b.x),d=Math.abs(a.y-b.y);return c<.01&&d<.01},l=function(a){var b=[{node:j,body:a}];while(b.length){var c=b.shift(),d=c.node,f=c.body;if(d.isInternal){var g=f.location.x,h=f.location.y;d.mass=d.mass+f.mass,d.massX=d.massX+f.mass*g,d.massY=d.massY+f.mass*h;var l=0,m=d.left,n=(d.right+m)/2,o=d.top,p=(d.bottom+o)/2;if(g>n){l+=1;var q=m;m=n,n+=n-q}if(h>p){l+=2;var r=o;o=p,p+=p-r}var s=d.quads[l];s||(s=i(),s.left=m,s.top=o,s.right=n,s.bottom=p,d.quads[l]=s),b.unshift({node:s,body:f})}else if(d.body){var t=d.body;d.body=null,d.isInternal=!0;if(k(t.location,f.location)){var u,v;do{var w=e.nextDouble()*2*Math.PI,x=(d.right-d.left)*.006*Math.cos(w),y=(d.bottom-d.top)*.006*Math.sin(w);u=t.location.x+x,v=t.location.y+y}while(u<d.left||u>d.right||v<d.top||v>d.bottom);t.location.x=u,t.location.y=v}b.unshift({node:d,body:t}),b.unshift({node:d,body:f})}else d.body=f}},m=function(a){var f=c,g,h,i,k,l=1,m=0,n=1;f[0]=j;while(l){var o=f[m],p=o.body;l-=1,m+=1,p&&p!==a?(h=p.location.x-a.location.x,i=p.location.y-a.location.y,k=Math.sqrt(h*h+i*i),k===0&&(h=(e.nextDouble()-.5)/50,i=(e.nextDouble()-.5)/50,k=Math.sqrt(h*h+i*i)),g=b*p.mass*a.mass/(k*k*k),a.force.x=a.force.x+g*h,a.force.y=a.force.y+g*i):(h=o.massX/o.mass-a.location.x,i=o.massY/o.mass-a.location.y,k=Math.sqrt(h*h+i*i),k===0&&(h=(e.nextDouble()-.5)/50,i=(e.nextDouble()-.5)/50,k=Math.sqrt(h*h+i*i)),(o.right-o.left)/k<d?(g=b*o.mass*a.mass/(k*k*k),a.force.x=a.force.x+g*h,a.force.y=a.force.y+g*i):(o.quads[0]&&(f[n]=o.quads[0],l+=1,n+=1),o.quads[1]&&(f[n]=o.quads[1],l+=1,n+=1),o.quads[2]&&(f[n]=o.quads[2],l+=1,n+=1),o.quads[3]&&(f[n]=o.quads[3],l+=1,n+=1)))}},n=function(a){var b=Number.MAX_VALUE,c=Number.MAX_VALUE,d=Number.MIN_VALUE,e=Number.MIN_VALUE,f,g=a.bodies,k=g.length;f=k;while(f--){var m=g[f].location.x,n=g[f].location.y;m<b&&(b=m),m>d&&(d=m),n<c&&(c=n),n>e&&(e=n)}var o=d-b,p=e-c;o>p?e=c+o:d=b+p,h=0,j=i(),j.left=b,j.right=d,j.top=c,j.bottom=e,f=k;while(f--)l(g[f],j)};return{insert:l,init:n,update:m,options:function(a){return a?(typeof a.gravity=="number"&&(b=a.gravity),typeof a.theta=="number"&&(d=a.theta),this):{gravity:b,theta:d}}}},Viva.Graph.Physics.nbodyForceBrute=function(a){a=a||{};var b=typeof a.gravity=="number"?a.gravity:-1,c=[],d=Viva.random("don't use this"),e=function(a){a.force.x=0,a.force.y=0;for(var e=0;e<c.length;++e){var f=c[e];if(f!==a){var g=f.location.x-a.location.x,h=f.location.y-a.location.y,i=Math.sqrt(g*g+h*h);i===0&&(g=(d.nextDouble()-.5)/50,h=(d.nextDouble()-.5)/50,i=Math.sqrt(g*g+h*h));var j=b*f.mass*a.mass/(i*i*i);a.force.x=a.force.x+j*g,a.force.y=a.force.y+j*h}}};return{insert:function(){},init:function(a){c=a.bodies},update:e,options:function(a){return a?(typeof a.gravity=="number"&&(b=a.gravity),this):{gravity:b}}}},Viva.Graph.Physics.dragForce=function(a){a=a||{};var b={coeff:a.coeff||.01};return{init:function(a){},update:function(a){a.force.x-=b.coeff*a.velocity.x,a.force.y-=b.coeff*a.velocity.y},options:function(a){return a?(typeof a.coeff=="number"&&(b.coeff=a.coeff),this):b}}},Viva.Graph.Physics.springForce=function(a){a=a||{};var b={length:a.length||50,coeff:typeof a.coeff=="number"?a.coeff:22e-5},c=Viva.random("Random number 4.","Chosen by fair dice roll");return{init:function(a){},update:function(a){var d=a.body1,e=a.body2,f=a.length<0?b.length:a.length,g=e.location.x-d.location.x,h=e.location.y-d.location.y,i=Math.sqrt(g*g+h*h);i===0&&(g=(c.nextDouble()-.5)/50,h=(c.nextDouble()-.5)/50,i=Math.sqrt(g*g+h*h));var j=i-f,k=(!a.coeff||a.coeff<0?b.coeff:a.coeff)*j/i*a.weight;d.force.x+=k*g,d.force.y+=k*h,e.force.x+=-k*g,e.force.y+=-k*h},options:function(a){return a?(typeof a.length=="number"&&(b.length=a.length),typeof a.coeff=="number"&&(b.coeff=a.coeff),this):b}}},Viva.Graph.Physics=Viva.Graph.Physics||{},Viva.Graph.Physics.forceSimulator=function(a){var b=a||Viva.Graph.Physics.rungeKuttaIntegrator(),c=[],d=[],e=[],f=[];return{speedLimit:1,bodies:c,accumulate:function(){var a,b,g;a=e.length;while(a--)e[a].init(this);a=f.length;while(a--)f[a].init(this);a=c.length;while(a--){g=c[a],g.force.x=0,g.force.y=0;for(b=0;b<e.length;b++)e[b].update(g)}for(a=0;a<d.length;++a)for(b=0;b<f.length;b++)f[b].update(d[a])},run:function(a){return this.accumulate(),b.integrate(this,a)},addBody:function(a){if(!a)throw{message:"Cannot add null body to force simulator"};return c.push(a),a},removeBody:function(a){if(!a)return!1;var b=Viva.Graph.Utils.indexOfElementInArray(a,c);return b<0?!1:c.splice(b,1)},addSpring:function(a,b,c,e,f){if(!a||!b)throw{message:"Cannot add null spring to force simulator"};if(typeof c!="number")throw{message:"Spring length should be a number"};f=typeof f=="number"?f:1;var g=new Viva.Graph.Physics.Spring(a,b,c,e>=0?e:-1,f);return d.push(g),g},removeSpring:function(a){if(!a)return!1;var b=Viva.Graph.Utils.indexOfElementInArray(a,d);return b<0?!1:d.splice(b,1)},addBodyForce:function(a){if(!a)throw{message:"Cannot add mighty (unknown) force to the simulator"};e.push(a)},addSpringForce:function(a){if(!a)throw{message:"Cannot add unknown force to the simulator"};f.push(a)}}},Viva.Graph.Layout=Viva.Graph.Layout||{},Viva.Graph.Layout.forceDirected=function(a,b){var c=.001;if(!a)throw{message:"Graph structure cannot be undefined"};b=b||{};var d={springLength:typeof b.springLength=="number"?b.springLength:80,springCoeff:typeof b.springCoeff=="number"?b.springCoeff:2e-4,gravity:typeof b.gravity=="number"?b.gravity:-1.2,theta:typeof b.theta=="number"?b.theta:.8,dragCoeff:typeof b.dragCoeff=="number"?b.dragCoeff:.02},e=Viva.Graph.Physics.forceSimulator(Viva.Graph.Physics.eulerIntegrator()),f=Viva.Graph.Physics.nbodyForce({gravity:d.gravity,theta:d.theta}),g=Viva.Graph.Physics.springForce({length:d.springLength,coeff:d.springCoeff}),h=Viva.Graph.Physics.dragForce({coeff:d.dragCoeff}),i=!0,j={x1:0,y1:0,x2:0,y2:0},k=Viva.random("ted.com",103,114,101,97,116),l=function(b){var c=(j.x1+j.x2)/2,e=(j.y1+j.y2)/2,f=d.springLength;if(b.links&&b.links.length>0){var g=b.links[0],h=g.fromId!=b.id?a.getNode(g.fromId):a.getNode(g.toId);h.position&&(c=h.position.x,e=h.position.y)}return{x:c+k.next(f)-f/2,y:e+k.next(f)-f/2}},m=function(b){var c=b.force_directed_body;c.mass=1+a.getLinks(b.id).length/3},n=function(a){var b=a.force_directed_body;b||(a.position=a.position||l(a),b=new Viva.Graph.Physics.Body,a.force_directed_body=b,m(a),b.loc(a.position),e.addBody(b))},o=function(a){var b=a.force_directed_body;b&&(a.force_directed_body=null,delete a.force_directed_body,e.removeBody(b))},p=function(b){var c=a.getNode(b.fromId),d=a.getNode(b.toId);m(c),m(d),b.force_directed_spring=e.addSpring(c.force_directed_body,d.force_directed_body,-1,b.weight)},q=function(b){var c=b.force_directed_spring;if(c){var d=a.getNode(b.fromId),f=a.getNode(b.toId);d&&m(d),f&&m(f),b.force_directed_spring=null,delete b.force_directed_spring,e.removeSpring(c)}},r=function(){a.forEachNode(n),a.forEachLink(p)},s=function(a){return a?a.isPinned||a.data&&a.data.isPinned:!0},t=function(){var b=Number.MAX_VALUE,c=Number.MAX_VALUE,d=Number.MIN_VALUE,e=Number.MIN_VALUE;if(a.getNodesCount()===0)return;a.forEachNode(function(a){var f=a.force_directed_body;if(!f)return;s(a)&&f.loc(a.position),a.position.x=f.location.x,a.position.y=f.location.y,a.position.x<b&&(b=a.position.x),a.position.x>d&&(d=a.position.x),a.position.y<c&&(c=a.position.y),a.position.y>e&&(e=a.position.y)}),j.x1=b,j.x2=d,j.y1=c,j.y2=e};return e.addSpringForce(g),e.addBodyForce(f),e.addBodyForce(h),{run:function(a){a=a||50;for(var b=0;b<a;++b)this.step()},step:function(){i&&(r(),i=!1);var a=e.run(20);return t(),a<c},getGraphRect:function(){return j},addNode:function(a){n(a)},removeNode:function(a){o(a)},addLink:function(a){p(a)},removeLink:function(a){q(a)},springLength:function(a){return arguments.length===1?(g.options({length:a}),this):g.options().length},springCoeff:function(a){return arguments.length===1?(g.options({coeff:a}),this):g.options().coeff},gravity:function(a){return arguments.length===1?(f.options({gravity:a}),this):f.options().gravity},theta:function(a){return arguments.length===1?(f.options({theta:a}),this):f.options().theta},drag:function(a){return arguments.length===1?(h.options({coeff:a}),this):h.options().coeff}}},Viva.Graph.View=Viva.Graph.View||{},Viva.Graph.View.cssGraphics=function(){var a,b="OLD_IE",c,d,e=1,f=1,g=function(){var a=Viva.BrowserInfo.browser,c;switch(a){case"mozilla":c="Moz";break;case"webkit":c="webkit";break;case"opera":c="O";break;case"msie":var d=Viva.BrowserInfo.version.split(".")[0];if(!(d>8))return b;c="ms"}return c?c+"Transform":null}(),h=function(){return g===b?function(a,b,c,d){var e=Math.cos(d),f=Math.sin(d);d<0&&(d=2*Math.PI+d),d<Math.PI/2?(a.style.left=b+"px",a.style.top=c+"px"):d<Math.PI?(a.style.left=b-a.clientWidth*Math.cos(Math.PI-d),a.style.top=c):d<Math.PI+Math.PI/2?(a.style.left=b-a.clientWidth*Math.cos(Math.PI-d),a.style.top=c+a.clientWidth*Math.sin(Math.PI-d)):(a.style.left=b,a.style.top=c+a.clientWidth*Math.sin(Math.PI-d)),a.style.filter="progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand',M11="+e+", M12="+ -f+","+"M21="+f+", M22="+e+");"}:g?function(a,b,c,d){a.style.left=b+"px",a.style.top=c+"px",a.style[g]="rotate("+d+"rad)",a.style[g+"Origin"]="left"}:function(a,b,c,d){}}(),i=function(a){var b=document.createElement("div");return b.setAttribute("class","node"),b},j=function(a,b){a.style.left=b.x-5+"px",a.style.top=b.y-5+"px"},k=function(a,b,c){var d=b.x-c.x,e=b.y-c.y,f=Math.sqrt(d*d+e*e);a.style.height="1px",a.style.width=f+"px",h(a,c.x,c.y,Math.atan2(e,d))},l=function(a){var b=document.createElement("div");return b.setAttribute("class","link"),b},m=function(){if(a&&g&&g!==b){var h="matrix("+e+", 0, 0,"+f+","+c+","+d+")";a.style[g]=h}};return{node:function(a){return a&&typeof a!="function"?i(a):(i=a,this)},link:function(a){return a&&typeof a!="function"?l(a):(l=a,this)},graphCenterChanged:function(a,b){c=a,d=b,m()},translateRel:function(a,b){c+=a,d+=b,m()},scale:function(a,b){return 1},resetScale:function(){return this},beginRender:function(){},endRender:function(){},placeNode:function(a){return j=a,this},placeLink:function(a){return k=a,this},init:function(b){a=b,m()},initLink:function(b){a.childElementCount>0?a.insertBefore(b,a.firstChild):a.appendChild(b)},releaseLink:function(b){a.removeChild(b)},initNode:function(b){a.appendChild(b)},releaseNode:function(b){a.removeChild(b)},updateNodePosition:function(a,b){j(a,b)},updateLinkPosition:function(a,b,c){k(a,b,c)}}},Viva.Graph.svg=function(a){var b="http://www.w3.org/2000/svg",c="http://www.w3.org/1999/xlink",d=a;return typeof a=="string"&&(d=document.createElementNS(b,a)),d.vivagraph_augmented?d:(d.vivagraph_augmented=!0,d.attr=function(a,b){return arguments.length===2?(b!==null?d.setAttributeNS(null,a,b):d.removeAttributeNS(null,a),d):d.getAttributeNS(null,a)},d.append=function(a){var b=Viva.Graph.svg(a);return d.appendChild(b),b},d.text=function(a){return typeof a=="undefined"?d.textContent:(d.textContent=a,d)},d.link=function(a){return arguments.length===0?d.getAttributeNS(c,"xlink:href"):(d.setAttributeNS(c,"xlink:href",a),d)},d)},Viva.Graph.View=Viva.Graph.View||{},Viva.Graph.View.svgGraphics=function(){var a,b,c,d,e=1,f=function(a){return Viva.Graph.svg("rect").attr("width",10).attr("height",10).attr("fill","#00a2e8")},g=function(a,b){a.attr("x",b.x-5).attr("y",b.y-5)},h=function(a){return Viva.Graph.svg("line").attr("stroke","#999")},i=function(a,b,c){a.attr("x1",b.x).attr("y1",b.y).attr("x2",c.x).attr("y2",c.y)},j=function(a){a.fire("rescaled")},k=function(){if(a){var b="matrix("+e+", 0, 0,"+e+","+c+","+d+")";a.attr("transform",b)}},l={node:function(a){return a&&typeof a!="function"?f(a):(f=a,this)},link:function(a){return a&&typeof a!="function"?h(a):(h=a,this)},placeNode:function(a){return g=a,this},placeLink:function(a){return i=a,this},beginRender:function(){},endRender:function(){},graphCenterChanged:function(a,b){c=a,d=b,k()},translateRel:function(c,d){var e=b.createSVGPoint(),f=a.getCTM(),g=b.createSVGPoint().matrixTransform(f.inverse());e.x=c,e.y=d,e=e.matrixTransform(f.inverse()),e.x=(e.x-g.x)*f.a,e.y=(e.y-g.y)*f.d,f.e+=e.x,f.f+=e.y;var h="matrix("+f.a+", 0, 0,"+f.d+","+f.e+","+f.f+")";a.attr("transform",h)},scale:function(f,g){var h=b.createSVGPoint();h.x=g.x,h.y=g.y,h=h.matrixTransform(a.getCTM().inverse());var i=b.createSVGMatrix().translate(h.x,h.y).scale(f).translate(-h.x,-h.y),k=a.getCTM().multiply(i);e=k.a,c=k.e,d=k.f;var l="matrix("+k.a+", 0, 0,"+k.d+","+k.e+","+k.f+")";return a.attr("transform",l),j(this),e},resetScale:function(){e=1;var b="matrix(1, 0, 0, 1, 0, 0)";return a.attr("transform",b),j(this),this},init:function(c){b=Viva.Graph.svg("svg"),a=Viva.Graph.svg("g").attr("buffered-rendering","dynamic"),b.appendChild(a),c.appendChild(b),k()},initLink:function(b){if(!b)return;a.childElementCount>0?a.insertBefore(b,a.firstChild):a.appendChild(b)},releaseLink:function(b){a.removeChild(b)},initNode:function(b){a.appendChild(b)},releaseNode:function(b){a.removeChild(b)},updateNodePosition:function(a,b){g(a,b)},updateLinkPosition:function(a,b,c){i(a,b,c)},getSvgRoot:function(){return b}};return Viva.Graph.Utils.events(l).extend(),l},Viva.Graph.View.svgNodeFactory=function(a){var b="orange",c="#999",d=Viva.Graph.geom(),e=function(a,b){a.size={w:10,h:10},a.append("rect").attr("width",a.size.w).attr("height",a.size.h).attr("stroke","orange").attr("fill","orange")},f=function(a){return a.size};return{node:function(a){var b=Viva.Graph.svg("g");return e(b,a),b.nodeId=a.id,b},link:function(b){var d=a.getNode(b.fromId),e=d&&d.ui;if(e&&!e.linksContainer){var f=Viva.Graph.svg("path").attr("stroke",c);return e.linksContainer=f,f}return null},customContent:function(a,b){if(typeof a!="function"||typeof b!="function")throw"Two functions expected: contentCreator(nodeUI, node) and size(nodeUI)";e=a,f=b},placeNode:function(b,c){var e="",g=f(b);a.forEachLinkedNode(b.nodeId,function(a,h){if(!a.position||!a.ui)return;if(a.ui===b)return;if(h.fromId!==b.nodeId)return;var i=f(a.ui),j=a.position,k=d.intersectRect(c.x-g.w/2,c.y-g.h/2,c.x+g.w/2,c.y+g.h/2,c.x,c.y,j.x,j.y)||c,l=d.intersectRect(j.x-i.w/2,j.y-i.h/2,j.x+i.w/2,j.y+i.h/2,j.x,j.y,c.x,c.y)||j;e+="M"+Math.round(k.x)+" "+Math.round(k.y)+"L"+Math.round(l.x)+" "+Math.round(l.y)}),b.attr("transform","translate("+(c.x-g.w/2)+", "+(c.y-g.h/2)+")"),e!==""&&b.linksContainer&&b.linksContainer.attr("d",e)}}},Viva.Graph.webgl=function(a){var b=function(b,c){var d=a.createShader(c);a.shaderSource(d,b),a.compileShader(d);if(!a.getShaderParameter(d,a.COMPILE_STATUS)){var e=a.getShaderInfoLog(d);throw alert(e),e}return d};return{createProgram:function(c,d){var e=a.createProgram(),f=b(c,a.VERTEX_SHADER),g=b(d,a.FRAGMENT_SHADER);a.attachShader(e,f),a.attachShader(e,g),a.linkProgram(e);if(!a.getProgramParameter(e,a.LINK_STATUS)){var h=a.getShaderInfoLog(e);throw alert(h),h}return e},extendArray:function(a,b,c){if((b+1)*c>a.length){var d=new Float32Array(a.length*c*2);return d.set(a),d}return a},copyArrayPart:function(a,b,c,d){for(var e=0;e<d;++e)a[b+e]=a[c+e]},swapArrayPart:function(a,b,c,d){for(var e=0;e<d;++e){var f=a[b+e];a[b+e]=a[c+e],a[c+e]=f}},getLocations:function(b,c){var d={};for(var e=0;e<c.length;++e){var f=c[e],g=-1;if(f.indexOf("a_")===0){g=a.getAttribLocation(b,f);if(g===-1)throw"Program doesn't have required attribute: "+f;d[f.slice(2)]=g}else{if(f.indexOf("u_")!==0)throw"Couldn't figure out your intent. All uniforms should start with 'u_' prefix, and attributes with 'a_'";g=a.getUniformLocation(b,f);if(g===null)throw"Program doesn't have required uniform: "+f;d[f.slice(2)]=g}}return d},context:a}},Viva.Graph.View.WebglUtils=function(){},Viva.Graph.View.WebglUtils.prototype.parseColor=function(a){var b=10414335;if(typeof a=="string"&&a){a.length===4&&(a=a.replace(/([^#])/g,"$1$1"));if(a.length===9)b=parseInt(a.substr(1),16);else{if(a.length!==7)throw'Color expected in hex format with preceding "#". E.g. #00ff00. Got value: '+a;b=parseInt(a.substr(1),16)<<8|255}}else typeof a=="number"&&(b=a);return b},Viva.Graph.View._webglUtil=new Viva.Graph.View.WebglUtils,Viva.Graph.View.webglLine=function(a){return{color:Viva.Graph.View._webglUtil.parseColor(a)}},Viva.Graph.View.webglSquare=function(a,b){return{size:typeof a=="number"?a:10,color:Viva.Graph.View._webglUtil.parseColor(b)}},Viva.Graph.View.webglImage=function(a,b){return{_texture:0,_offset:0,size:typeof a=="number"?a:32,src:b}},Viva.Graph.View.webglNodeProgram=function(){var a=4,b=3*Float32Array.BYTES_PER_ELEMENT+Uint32Array.BYTES_PER_ELEMENT,c=["precision mediump float;","varying vec4 color;","void main(void) {"," gl_FragColor = color;","}"].join("\n"),d=["attribute vec3 a_vertexPos;","attribute vec4 a_color;","uniform vec2 u_screenSize;","uniform mat4 u_transform;","varying vec4 color;","void main(void) {"," gl_Position = u_transform * vec4(a_vertexPos.xy/u_screenSize, 0, 1);"," gl_PointSize = a_vertexPos.z * u_transform[0][0];"," color = a_color.abgr;","}"].join("\n"),e,f,g,h,i,j=new ArrayBuffer(16*b),k=new Float32Array(j),l=new Uint32Array(j),m=0,n,o,p,q,r=function(){if((m+1)*b>=j.byteLength){var a=new ArrayBuffer(j.byteLength*2),c=new Float32Array(a),d=new Uint32Array(a);d.set(l),k=c,l=d,j=a}};return{load:function(a){f=a,i=Viva.Graph.webgl(a),e=i.createProgram(d,c),f.useProgram(e),h=i.getLocations(e,["a_vertexPos","a_color","u_screenSize","u_transform"]),f.enableVertexAttribArray(h.vertexPos),f.enableVertexAttribArray(h.color),g=f.createBuffer()},position:function(b,c){var d=b.id,e=d*a;k[d*a]=c.x,k[d*a+1]=c.y,k[d*a+2]=b.size,l[d*a+3]=b.color},updateTransform:function(a){q=!0,p=a},updateSize:function(a,b){n=a,o=b,q=!0},createNode:function(a){r(),m+=1},removeNode:function(b){m>0&&(m-=1),b.id<m&&m>0&&i.copyArrayPart(l,b.id*a,m*a,a)},replaceProperties:function(a,b){},render:function(){f.useProgram(e),f.bindBuffer(f.ARRAY_BUFFER,g),f.bufferData(f.ARRAY_BUFFER,j,f.DYNAMIC_DRAW),q&&(q=!1,f.uniformMatrix4fv(h.transform,!1,p),f.uniform2f(h.screenSize,n,o)),f.vertexAttribPointer(h.vertexPos,3,f.FLOAT,!1,a*Float32Array.BYTES_PER_ELEMENT,0),f.vertexAttribPointer(h.color,4,f.UNSIGNED_BYTE,!0,a*Float32Array.BYTES_PER_ELEMENT,12),f.drawArrays(f.POINTS,0,m)}}},Viva.Graph.View.webglLinkProgram=function(){var a=6,b=2*(2*Float32Array.BYTES_PER_ELEMENT+Uint32Array.BYTES_PER_ELEMENT),c=["precision mediump float;","varying vec4 color;","void main(void) {"," gl_FragColor = color;","}"].join("\n"),d=["attribute vec2 a_vertexPos;","attribute vec4 a_color;","uniform vec2 u_screenSize;","uniform mat4 u_transform;","varying vec4 color;","void main(void) {"," gl_Position = u_transform * vec4(a_vertexPos/u_screenSize, 0.0, 1.0);"," color = a_color.abgr;","}"].join("\n"),e,f,g,h,i,j=0,k,l=new ArrayBuffer(16*b),m=new Float32Array(l),n=new Uint32Array(l),o,p,q,r,s=function(){if((j+1)*b>l.byteLength){var a=new ArrayBuffer(l.byteLength*2),c=new Float32Array(a),d=new Uint32Array(a);d.set(n),m=c,n=d,l=a}};return{load:function(a){f=a,h=Viva.Graph.webgl(a),e=h.createProgram(d,c),f.useProgram(e),i=h.getLocations(e,["a_vertexPos","a_color","u_screenSize","u_transform"]),f.enableVertexAttribArray(i.vertexPos),f.enableVertexAttribArray(i.color),g=f.createBuffer()},position:function(b,c,d){var e=b.id,f=e*a;m[f]=c.x,m[f+1]=c.y,n[f+2]=b.color,m[f+3]=d.x,m[f+4]=d.y,n[f+5]=b.color},createLink:function(a){s(),j+=1,k=a.id},removeLink:function(b){j>0&&(j-=1),b.id<j&&j>0&&h.copyArrayPart(n,b.id*a,j*a,a)},updateTransform:function(a){r=!0,q=a},updateSize:function(a,b){o=a,p=b,r=!0},render:function(){f.useProgram(e),f.bindBuffer(f.ARRAY_BUFFER,g),f.bufferData(f.ARRAY_BUFFER,l,f.DYNAMIC_DRAW),r&&(r=!1,f.uniformMatrix4fv(i.transform,!1,q),f.uniform2f(i.screenSize,o,p)),f.vertexAttribPointer(i.vertexPos,2,f.FLOAT,!1,3*Float32Array.BYTES_PER_ELEMENT,0),f.vertexAttribPointer(i.color,4,f.UNSIGNED_BYTE,!0,3*Float32Array.BYTES_PER_ELEMENT,8),f.drawArrays(f.LINES,0,j*2),k=j-1},bringToFront:function(b){k>b.id&&h.swapArrayPart(m,b.id*a,k*a,a),k>0&&(k-=1)},getFrontLinkId:function(){return k}}},Viva.Graph.View.Texture=function(a){this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.isDirty=!1,this.canvas.width=this.canvas.height=a},Viva.Graph.View.webglAtlas=function(a){var b=Math.sqrt(a||1024)<<0,c=b,d=1,e={},f,g=0,h=[],i=[],j,k=function(a){return a<<=0,a-=1,a|=a>>1,a|=a>>2,a|=a>>4,a|=a>>8,a|=a>>16,a+1},l=function(a){return(a&a-1)===0},m=function(){var a=new Viva.Graph.View.Texture(b*c);h.push(a)},n=function(c){var d=c/a<<0,e=c%a,f=e/b<<0,g=e%b;return{textureNumber:d,row:f,col:g}},o=function(){j.isDirty=!0,g=0,f=null},p=function(){f&&(clearTimeout(f),g+=1,f=null),g>10?o():f=setTimeout(o,400)},q=function(a,b){var d=h[a.textureNumber].canvas,e=h[b.textureNumber].ctx,f=b.col*c,g=b.row*c;e.drawImage(d,a.col*c,a.row*c,c,c,f,g,c,c),h[a.textureNumber].isDirty=!0,h[b.textureNumber].isDirty=!0},r=function(a,b,d){var f=n(a),g={offset:a};f.textureNumber>=h.length&&m();var j=h[f.textureNumber];j.ctx.drawImage(b,f.col*c,f.row*c,c,c),i[a]=b.src,e[b.src]=g,j.isDirty=!0,d(g)};if(!l(a))throw"Tiles per texture should be power of two.";return j={isDirty:!1,clearDirty:function(){this.isDirty=!1;for(var a=0;a<h.length;++a)h[a].isDirty=!1},remove:function(a){var b=e[a];if(!b)return!1;delete e[a],d-=1;if(d===b.offset)return!0;var c=n(b.offset),f=n(d);q(f,c);var g=e[i[d]];return g.offset=b.offset,i[b.offset]=i[d],p(),!0},getTextures:function(){return h},getCoordinates:function(a){return e[a]},load:function(a,b){if(e.hasOwnProperty(a))b(e[a]);else{var c=new Image,f=d,g=this;d+=1,c.crossOrigin="anonymous",c.onload=function(){p(),r(f,c,b)},c.src=a}}},j},Viva.Graph.View.webglImageNodeProgram=function(){var a=18,b=["precision mediump float;","varying vec4 color;","varying vec3 vTextureCoord;","uniform sampler2D u_sampler0;","uniform sampler2D u_sampler1;","uniform sampler2D u_sampler2;","uniform sampler2D u_sampler3;","void main(void) {"," if (vTextureCoord.z == 0.) {"," gl_FragColor = texture2D(u_sampler0, vTextureCoord.xy);"," } else if (vTextureCoord.z == 1.) {"," gl_FragColor = texture2D(u_sampler1, vTextureCoord.xy);"," } else if (vTextureCoord.z == 2.) {"," gl_FragColor = texture2D(u_sampler2, vTextureCoord.xy);"," } else if (vTextureCoord.z == 3.) {"," gl_FragColor = texture2D(u_sampler3, vTextureCoord.xy);"," } else { gl_FragColor = vec4(0, 1, 0, 1); }","}"].join("\n"),c=["attribute vec2 a_vertexPos;","attribute float a_customAttributes;","uniform vec2 u_screenSize;","uniform mat4 u_transform;","uniform float u_tilesPerTexture;","varying vec3 vTextureCoord;","void main(void) {"," gl_Position = u_transform * vec4(a_vertexPos/u_screenSize, 0, 1);","float corner = mod(a_customAttributes, 4.);","float tileIndex = mod(floor(a_customAttributes / 4.), u_tilesPerTexture);","float tilesPerRow = sqrt(u_tilesPerTexture);","float tileSize = 1./tilesPerRow;","float tileColumn = mod(tileIndex, tilesPerRow);","float tileRow = floor(tileIndex/tilesPerRow);","if(corner == 0.0) {"," vTextureCoord.xy = vec2(0, 1);","} else if(corner == 1.0) {"," vTextureCoord.xy = vec2(1, 1);","} else if(corner == 2.0) {"," vTextureCoord.xy = vec2(0, 0);","} else {"," vTextureCoord.xy = vec2(1, 0);","}","vTextureCoord *= tileSize;","vTextureCoord.x += tileColumn * tileSize;","vTextureCoord.y += tileRow * tileSize;","vTextureCoord.z = floor(floor(a_customAttributes / 4.)/u_tilesPerTexture);","}"].join("\n"),d=1024,e,f,g,h,i,j,k,l=0,m,n=new Float32Array(64),o,p,q,r,s=function(a,b){a.nativeObject&&h.deleteTexture(a.nativeObject);var c=h.createTexture();h.activeTexture(h["TEXTURE"+b]),h.bindTexture(h.TEXTURE_2D,c),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,h.RGBA,h.UNSIGNED_BYTE,a.canvas),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.LINEAR),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.LINEAR_MIPMAP_NEAREST),h.generateMipmap(h.TEXTURE_2D),h.uniform1i(k["sampler"+b],b),a.nativeObject=c},t=function(){if(e.isDirty){var a=e.getTextures();for(var b=0;b<a.length;++b)(a[b].isDirty||!a[b].nativeObject)&&s(a[b],b);e.clearDirty()}};return{load:function(a){h=a,j=Viva.Graph.webgl(a),d=1024,e=new Viva.Graph.View.webglAtlas(d),g=j.createProgram(c,b),h.useProgram(g),k=j.getLocations(g,["a_vertexPos","a_customAttributes","u_screenSize","u_transform","u_sampler0","u_sampler1","u_sampler2","u_sampler3","u_tilesPerTexture"]),h.uniform1f(k.tilesPerTexture,d),h.enableVertexAttribArray(k.vertexPos),h.enableVertexAttribArray(k.customAttributes),i=h.createBuffer()},position:function(b,c){var d=b.id*a;n[d+0]=c.x-b.size,n[d+1]=c.y-b.size,n[d+2]=b._offset*4+0,n[d+3]=c.x+b.size,n[d+4]=c.y-b.size,n[d+5]=b._offset*4+1,n[d+6]=c.x-b.size,n[d+7]=c.y+b.size,n[d+8]=b._offset*4+2,n[d+9]=c.x-b.size,n[d+10]=c.y+b.size,n[d+11]=b._offset*4+2,n[d+12]=c.x+b.size,n[d+13]=c.y-b.size,n[d+14]=b._offset*4+1,n[d+15]=c.x+b.size,n[d+16]=c.y+b.size,n[d+17]=b._offset*4+3},createNode:function(b){n=j.extendArray(n,l,a),l+=1;var c=e.getCoordinates(b.src);c?b._offset=c.offset:(b._offset=0,e.load(b.src,function(a){b._offset=a.offset}))},removeNode:function(b){l>0&&(l-=1),b.id<l&&l>0&&(b.src&&e.remove(b.src),j.copyArrayPart(n,b.id*a,l*a,a))},replaceProperties:function(a,b){b._offset=a._offset},updateTransform:function(a){r=!0,q=a},updateSize:function(a,b){o=a,p=b,r=!0},render:function(){h.useProgram(g),h.bindBuffer(h.ARRAY_BUFFER,i),h.bufferData(h.ARRAY_BUFFER,n,h.DYNAMIC_DRAW),r&&(r=!1,h.uniformMatrix4fv(k.transform,!1,q),h.uniform2f(k.screenSize,o,p)),h.vertexAttribPointer(k.vertexPos,2,h.FLOAT,!1,3*Float32Array.BYTES_PER_ELEMENT,0),h.vertexAttribPointer(k.customAttributes,1,h.FLOAT,!1,3*Float32Array.BYTES_PER_ELEMENT,8),t(),h.drawArrays(h.TRIANGLES,0,l*6)}}},Viva.Graph.View=Viva.Graph.View||{},Viva.Graph.View.webglGraphics=function(a){a=a||{},a.enableBlending=typeof a.enableBlending!="boolean"?!0:a.enableBlending;var b,c,d,e,f,g=0,h=0,i,j,k,l=[],m=[],n,o=Viva.Graph.View.webglLinkProgram(),p=Viva.Graph.View.webglNodeProgram(),q=function(a){return Viva.Graph.View.webglSquare()},r=function(a){return Viva.Graph.View.webglLine(3014898687)},s=function(){o.updateTransform(i),p.updateTransform(i)},t=function(){i=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]},u=function(){b&&c&&(e=c.width=Math.max(b.offsetWidth,1),f=c.height=Math.max(b.offsetHeight,1),d&&d.viewport(0,0,e,f),o&&o.updateSize(e,f),p&&p.updateSize(e,f))},v=function(a){var b=g++,c=q(a);return c.id=b,p.createNode(c),l[b]=a,c},w=function(a){var b=h++,c=r(a);return c.id=b,o.createLink(c),m[b]=a,c},x=function(a){a.fire("rescaled")},y={node:function(a){return a&&typeof a!="function"?v(a):(q=a,this)},link:function(a){return a&&typeof a!="function"?w(a):(r=a,this)},placeNode:function(a){return j=a,this},placeLink:function(a){return k=a,this},beginRender:function(){},endRender:function(){h>0&&o.render(),g>0&&p.render()},bringLinkToFront:function(a){var b=o.getFrontLinkId();o.bringToFront(a);if(b>a.id){var c=a.id,d=m[b];m[b]=m[c],m[b].ui.id=b,m[c]=d,m[c].ui.id=c}},graphCenterChanged:function(a,b){u()},translateRel:function(a,b){i[12]+=2*i[0]*a/e/i[0],i[13]-=2*i[5]*b/f/i[5],s()},scale:function(a,b){var c=2*b.x/e-1,d=1-2*b.y/f;return c-=i[12],d-=i[13],i[12]+=c*(1-a),i[13]+=d*(1-a),i[0]*=a,i[5]*=a,s(),x(this),i[0]},resetScale:function(){return t(),d&&(u(),s()),this},init:function(g){b=g,c=document.createElement("canvas"),u(),t(),b.appendChild(c),d=c.getContext("experimental-webgl");if(!d){var h="Could not initialize WebGL. Seems like the browser doesn't support it.";throw alert(h),h}a.enableBlending&&(d.blendFunc(d.SRC_ALPHA,d.ONE_MINUS_SRC_ALPHA),d.enable(d.BLEND)),o.load(d),o.updateSize(e,f),p.load(d),p.updateSize(e,f),s(),typeof n=="function"&&n(c)},isSupported:function(){var a=document.createElement("canvas"),b=a&&a.getContext&&a.getContext("experimental-webgl");return b},initLink:function(a){},releaseLink:function(a){h>0&&(h-=1),o.removeLink(a);var b=a.id;if(b<h){if(h===0||h===b)return;m[b]=m[h],m[b].ui.id=b}},initNode:function(a){},releaseNode:function(a){g>0&&(g-=1),p.removeNode(a);if(a.id<g){var b=a.id;if(g===0||g===b)return;var c=l[g],d=l[b];l[b]=c,l[b].ui.id=b,p.replaceProperties(d.ui,c.ui)}},updateNodePosition:function(a,b){j&&j(a,b),p.position(a,b)},updateLinkPosition:function(a,b,c){k&&k(a,b,c),o.position(a,b,c)},getGraphicsRoot:function(a){return typeof a=="function"&&(c?a(c):n=a),c},setNodeProgram:function(a){if(!d&&a){p=a;return}if(a)throw"Not implemented. Cannot swap shader on the fly... yet."},setLinkProgram:function(a){if(!d&&a){o=a;return}if(a)throw"Not implemented. Cannot swap shader on the fly... yet."},getGraphCoordinates:function(a){return a.x=2*a.x/e-1,a.y=1-2*a.y/f,a.x=(a.x-i[12])/i[0],a.y=(a.y-i[13])/i[5],a.x*=e,a.y*=f,a}};return Viva.Graph.Utils.events(y).extend(),y},Viva.Graph.webglInputEvents=function(a,b){var c=function(a,b,c){if(a.ui&&a.ui.size){var d=a.position,e=a.ui.size;return d.x-e<b&&b<d.x+e&&d.y-e<c&&c<d.y+e}return!0},d=Viva.Graph.spatialIndex(b,c),e,f,g,h,i=function(b){var c={x:0,y:0},i=null;b.addEventListener("mousemove",function(b){c.x=b.clientX,c.y=b.clientY,a.getGraphCoordinates(c);var g=d.getNodeAt(c.x,c.y);g&&i!==g?(i=g,typeof e=="function"&&e(i)):g===null&&i!==g&&(typeof f=="function"&&f(i),i=null)});var j=+(new Date);b.addEventListener("mousedown",function(b){var e=+(new Date);c.x=b.clientX,c.y=b.clientY,a.getGraphCoordinates(c);var f=d.getNodeAt(c.x,c.y);f?(e-j<400&&typeof h=="function"&&f===i?h(f,b):typeof g=="function"&&g(f,b),i=f):i=null,j=e})};return a.getGraphicsRoot(i),{mouseEnter:function(a){return e=a,this},mouseLeave:function(a){return f=a,this},click:function(a){return g=a,this},dblClick:function(a){return h=a,this}}},Viva.Graph.View=Viva.Graph.View||{},Viva.Graph.View.renderer=function(a,b){var c=30;b=b||{};var d=b.layout,e=b.graphics,f=b.container,g,h=!1,i=!0,j=0,k=0,l=!1,m=!1,n={x:0,y:0},o={offsetX:0,offsetY:0,scale:1},p=function(){f=f||document.body,d=d||Viva.Graph.Layout.forceDirected(a),e=e||Viva.Graph.View.svgGraphics(a,{container:f}),typeof b.renderLinks=="undefined"&&(b.renderLinks=!0),b.prerender=b.prerender||0},q={x:0,y:0,node:null},r={x:0,y:0,node:null},s={x:0,y:0},t=function(b){var c=a.getNode(b.fromId),d=a.getNode(b.toId);if(!c||!d)return;q.x=c.position.x,q.y=c.position.y,q.node=c,r.x=d.position.x,r.y=d.position.y,r.node=d,e.updateLinkPosition(b.ui,q,r)},u=function(a){s.x=a.position.x,s.y=a.position.y,e.updateNodePosition(a.ui,s)},v=function(){e.beginRender(),b.renderLinks&&!e.omitLinksRendering&&a.forEachLink(t),a.forEachNode(u),e.endRender()},w=function(){return l=d.step()&&!m,v(),!l},x=function(a){if(g){k+=a;return}a?(k+=a,g=Viva.Graph.Utils.timer(function(){return w()},c)):(j=0,k=0,g=Viva.Graph.Utils.timer(w,c))},y=function(){l=!1,g.restart()},z=function(){if(typeof b.prerender=="number"&&b.prerender>0)for(var a=0;a<b.prerender;++a)d.step();else d.step()},A=function(){var a=d.getGraphRect(),b=Viva.Graph.Utils.getDimension(f);n.x=n.y=0,o.offsetX=b.width/2-(a.x2+a.x1)/2,o.offsetY=b.height/2-(a.y2+a.y1)/2,e.graphCenterChanged(o.offsetX+n.x,o.offsetY+n.y),i=!1},B=function(a){var b=e.node(a);a.ui=b,e.initNode(b),d.addNode(a),u(a)},C=function(a){typeof a.ui!="undefined"&&(e.releaseNode(a.ui),a.ui=null,delete a.ui),d.removeNode(a)},D=function(a){var b=e.link(a);a.ui=b,e.initLink(b),e.omitLinksRendering||t(a)},E=function(a){typeof a.ui!="undefined"&&(e.releaseLink(a.ui),a.ui=null,delete a.ui)},F=function(a){var b=!1;a.events=Viva.Graph.Utils.dragndrop(a.ui).onStart(function(){b=a.isPinned,a.isPinned=!0,m=!0,y()}).onDrag(function(b,c){a.position.x+=c.x/o.scale,a.position.y+=c.y/o.scale,m=!0}).onStop(function(){a.isPinned=b,m=!1})},G=function(a){a.events&&(a.events.release(),a.events=null,delete a.events)},H=function(){e.init(f),a.forEachNode(B),b.renderLinks&&a.forEachLink(D)},I=function(b){var c=b.node;b.changeType==="add"?(B(c),F(c),i&&A()):b.changeType==="remove"?(G(c),C(c),a.getNodesCount()===0&&(i=!0)):b.changeType!=="update"},J=function(a){var c=a.link;a.changeType==="add"?(b.renderLinks&&D(c),d.addLink(c)):a.changeType==="remove"?(b.renderLinks&&E(c),d.removeLink(c)):a.changeType!=="update"},K=function(){Viva.Graph.Utils.events(window).on("resize",function(){A(),w()});var b=Viva.Graph.Utils.dragndrop(f);b.onDrag(function(a,b){n.x+=b.x,n.y+=b.y,e.translateRel(b.x,b.y),v()}),b.onScroll(function(a,b,c){var d=Math.pow(1.4,b<0?-0.2:.2);o.scale=e.scale(d,c),v()}),a.forEachNode(F),Viva.Graph.Utils.events(a).on("changed",function(a){for(var b=0;b<a.length;++b){var c=a[b];c.node?I(c):c.link&&J(c)}y()})};return{run:function(a){return h||(p(),z(),A(),H(),K(),h=!0),x(a),this},reset:function(){e.resetScale(),A(),o.scale=1},pause:function(){g.stop()},resume:function(){g.restart()},rerender:function(){return v(),this}}},Viva.Graph.serializer=function(){var a=function(){if(typeof JSON=="undefined"||!JSON.stringify||!JSON.parse)throw"JSON serializer is not defined."},b=function(a){return{id:a.id,data:a.data}},c=function(a){return{fromId:a.fromId,toId:a.toId,data:a.data}},d=function(a){return a},e=function(a){return a};return{storeToJSON:function(d,e,f){if(!d)throw"Graph is not defined";a(),e=e||b,f=f||c;var g={nodes:[],links:[]};return d.forEachNode(function(a){g.nodes.push(e(a))}),d.forEachLink(function(a){g.links.push(f(a))}),JSON.stringify(g)},loadFromJSON:function(b,c,f){if(typeof b!="string")throw"String expected in loadFromJSON() method";a(),c=c||d,f=f||e;var g=JSON.parse(b),h=Viva.Graph.graph();if(!g||!g.nodes||!g.links)throw"Passed json string does not represent valid graph";for(var i=0;i<g.nodes.length;++i){var j=c(g.nodes[i]);if(!j.hasOwnProperty("id"))throw"Graph node format is invalid. Node.id is missing";h.addNode(j.id,j.data)}for(i=0;i<g.links.length;++i){var k=f(g.links[i]);if(!k.hasOwnProperty("fromId")||!k.hasOwnProperty("toId"))throw"Graph link format is invalid. Both fromId and toId are required";h.addLink(k.fromId,k.toId,k.data)}return h}}}