ht-flow.js 13 KB

1
  1. !function(N){"use strict";var D="ht",i=N[D],c=i.Default,Y=Math,M=Y.PI,e=2*M,x=Y.sin,k=Y.cos,o=Y.atan2,O=Y.sqrt,K=Y.max,y=Y.floor,n=(Y.round,Y.ceil),B=Y.abs,Z=i.Shape,S=i.Edge,f=i.List,F=i.Style,_=i.graph,d=c.getInternal(),v=d.ui(),H=null,r="__segmentLengths",E="__lineTotalLength",V="__linePoints",w="__distance0",u="flow.count",h="flow.step",J="flow.element.max",b="flow.element.count",a="flow.element.min",A="flow.element.space",s="flow.element.autorotate",I="flow.element.background",z="flow.element.shadow.max",$="flow.element.shadow.min",C="flow.element.shadow.begincolor",G="flow.element.shadow.endcolor",R="flow.element.shadow.visible",j="flow.element.image",X="flow",U="prototype",L=_.GraphView[U],Q=i.Data[U],T=v.DataUI[U],m=v.ShapeUI[U],g=v.EdgeUI[U],q=i.DataModel[U],t=m._80o,P=g._80o,p=q.prepareRemove,W=g._79o,l=m._79o,xc=L.setDataModel,eo=function(k){return document.createElement(k)};L.calculatePointLength=function(A,Q,f){var e=this,x=e.getDataUI(A);f==H&&(f=.1);var o=Ee(x),Y=[];if(o){for(var p=o.length,L=0;p>L;L++){var W=o[L];W._as&&(W=W._as);for(var j=W[0],d=1;d<W.length;d++)Y.push([j,W[d]]),j=W[d]}for(var m=[],L=0;L<Y.length;L++){var h=Sl(Y[L][0],Y[L][1],Q);m.push(h)}var R=m.slice(0);R.sort(function(Y,a){return Y.z>a.z?1:Y.z<a.z?-1:0});var r=R[0];if(r.z<f){for(var I=m.indexOf(r),Z=0,L=0;I>=L;L++)Z+=I>L?c.getDistance(Y[L][0],Y[L][1]):c.getDistance(Y[L][0],r);return Z}}},c.calculatePointLength=function(t,a,z,y){y==H&&(y=.1);var T=Ee(H,t,a),a=[];if(T){for(var u=T.length,x=0;u>x;x++){var U=T[x];U._as&&(U=U._as);for(var A=U[0],J=1;J<U.length;J++)a.push([A,U[J]]),A=U[J]}for(var _=[],x=0;x<a.length;x++){var g=Sl(a[x][0],a[x][1],z);_.push(g)}var i=_.slice(0);i.sort(function(c,W){return c.z>W.z?1:c.z<W.z?-1:0});var Q=i[0];if(Q.z<y){for(var f=_.indexOf(Q),q=0,x=0;f>=x;x++)q+=f>x?c.getDistance(a[x][0],a[x][1]):c.getDistance(a[x][0],Q);return q}}},L.calculateClosestPoint=function(O,I){var A=this,y=A.getDataUI(O),q=Ee(y),f=[];if(q){for(var v=q.length,k=0;v>k;k++){var c=q[k];c._as&&(c=c._as);for(var g=c[0],j=1;j<c.length;j++)f.push([g,c[j]]),g=c[j]}for(var M=[],k=0;k<f.length;k++){var P=Sl(f[k][0],f[k][1],I);M.push(P)}return M.sort(function(e,h){return e.z>h.z?1:e.z<h.z?-1:0}),M[0]}},c.calculateClosestPoint=function(D,Z,c){var q=Ee(H,D,Z),Z=[];if(q){for(var $=q.length,r=0;$>r;r++){var p=q[r];p._as&&(p=p._as);for(var y=p[0],Q=1;Q<p.length;Q++)Z.push([y,p[Q]]),y=p[Q]}for(var f=[],r=0;r<Z.length;r++){var j=Sl(Z[r][0],Z[r][1],c);f.push(j)}return f.sort(function(x,n){return x.z>n.z?1:x.z<n.z?-1:0}),f[0]}},L.getPercentAngle=function(X,z){var U=this,t=U.getDataUI(X),H=X.getRotation?X.getRotation():0,b=Ee(t);if(b)if(0===z){var C=b[0][0],y=b[0][1];H+=o(y.y-C.y,y.x-C.x)}else if(100===z){b=b[b.length-1];var C=b[b.length-2],y=b[b.length-1];H+=o(y.y-C.y,y.x-C.x)}else{for(var q=0,f=[],F=b.length,Z=0;F>Z;Z++){var T=b[Z],x=Ql(T);q+=x,f.push(x)}for(var Y=q*z/100,e=vg(Y,f),B=0,p=0;e>p;p++)B+=f[p];Y-=B;for(var i=zo(b[e],Y),j=b[e],k=0,G=0,l=0;l<j.length-1;l++){var v=j[l],c=j[l+1],n=c.x-v.x,Q=c.y-v.y,M=O(n*n+Q*Q);if(k+=M,k>Y){G=l;break}}var L=j[G];H+=o(i.y-L.y,i.x-L.x)}return H},c.getPercentAngle=function(R,F,d){var g=0,_=Ee(H,R,F);if(_)if(0===d){var h=_[0][0],z=_[0][1];g+=o(z.y-h.y,z.x-h.x)}else if(100===d){_=_[_.length-1];var h=_[_.length-2],z=_[_.length-1];g+=o(z.y-h.y,z.x-h.x)}else{for(var D=0,r=[],l=_.length,x=0;l>x;x++){var k=_[x],M=Ql(k);D+=M,r.push(M)}for(var v=D*d/100,i=vg(v,r),t=0,U=0;i>U;U++)t+=r[U];v-=t;for(var Z=zo(_[i],v),Y=_[i],G=0,q=0,L=0;L<Y.length-1;L++){var y=Y[L],T=Y[L+1],c=T.x-y.x,K=T.y-y.y,w=O(c*c+K*K);if(G+=w,G>v){q=L;break}}var j=Y[q];g+=o(Z.y-j.y,Z.x-j.x)}return g},L.calculateLength=function(y){var P=this,k=P.getDataUI(y),F=Ee(k),I=0;if(F)if(Array.isArray(F[0]))for(var D=F.length,Q=0;D>Q;Q++){var L=F[Q],a=Ql(L);I+=a}else I=Ql(F);return I},c.calculateLength=function(v,L){var v=Ee(H,v,L),n=0;if(v)if(Array.isArray(v[0]))for(var X=v.length,m=0;X>m;m++){var h=v[m],O=Ql(h);n+=O}else n=Ql(v);return n};var Sl=c.calculateClosestPointOnLine=function(S,l,i){var v=S.x,G=S.y,T=l.x,M=l.y,V=i.x,e=i.y,I={},j=T-v,w=M-G,L=Math.sqrt(j*j+w*w),n=j/L,f=w/L,r=(-v+V)*n+(-G+e)*f;return I.x=v+r*n,I.y=G+r*f,hc(I,S,l)||(I.x=Math.abs(I.x-S.x)<Math.abs(I.x-l.x)?S.x:l.x,I.y=Math.abs(I.y-S.y)<Math.abs(I.y-l.y)?S.y:l.y),j=V-I.x,w=e-I.y,I.z=Math.sqrt(j*j+w*w),I},hc=function(A,L,G){return A.x>=Math.min(L.x,G.x)&&A.x<=Math.max(L.x,G.x)&&A.y>=Math.min(L.y,G.y)&&A.y<=Math.max(L.y,G.y)},Ql=function(r){for(var Q=0,C=r.length-1,Z=0;C>Z;Z++){var z=r[Z],W=r[Z+1],R=W.x-z.x,x=W.y-z.y,v=O(R*R+x*x);Q+=v}return Q},zo=function(N,t){for(var S=0,V=0,g=0,M=N.length-1,s=0;M>s;s++){var Z=N[s],P=N[s+1],C=P.x-Z.x,H=P.y-Z.y;if(g=O(C*C+H*H),S+=g,S>t){S-=g,V=s;break}}var U=N[V],p=N[V+1],J=o(p.y-U.y,p.x-U.x),d=t-S,H=x(J)*d,C=k(J)*d;return{x:U.x+C,y:U.y+H}},co=function(E,H,R,f){var z=k(f),h=x(f),s=z*H-h*R,u=h*H+z*R;return E?{x:E.x+s,y:E.y+u}:{x:s,y:u}},yi=function(s,K){s[r]=s[E]=s[V]=K[w]=H},Tm=function(S,$,Y,P,p,W){var O,d,D,y,G,h,T,E,I,U,F,R=[];if(B(P)>e&&(P=e),G=n(B(P)/(M/4)),O=P/G,d=-O,D=-Y,G>0){h=S+k(Y)*p,T=$+x(-Y)*W,R.push({x:h,y:T});for(var j=0;G>j;j++)D+=d,y=D-d/2,E=S+k(D)*p,I=$+x(D)*W,U=S+k(y)*(p/k(d/2)),F=$+x(y)*(W/k(d/2)),R.push({x:U,y:F}),R.push({x:E,y:I})}return R},Ee=function(V,u,D){if(u==H){var J=V._data;if(J instanceof Z){if(u=J.getPoints(),D=J.getSegments(),(!D||0===D.size())&&u){D=new i.List([1]);for(var c=1;c<u.size();c++)D.add(2)}}else if(J instanceof S){var q=V._78o;if(q){var M=q.type,l=q.points,v=q.segments,s=q._4O;if(!M||l){var N=q.sourcePoint,E=N.x,G=N.y,m=q.targetPoint,$=m.x,U=m.y;if(M)v?(u=new f({x:E,y:G}),u.addAll(l),u.add({x:$,y:U}),D=new f(v._as)):(u=new f({x:E,y:G}),D=new f([1]),l.each(function(Y){u.add(Y),D.add(2)}),u.add({x:$,y:U}),D.add(2));else if(q.looped){u=new f(Tm(E,G,0,e,q.radius,q.radius)),D=new f([1]);for(var c=0;c<(u.size()-1)/2;c++)D.add(3)}else u=new f,q.center?(u.add({x:q.c1.x,y:q.c1.y}),u.add({x:E,y:G}),u.add({x:$,y:U}),u.add({x:q.c2.x,y:q.c2.y}),D=new f([1,2,2,2])):(u.add({x:E,y:G}),u.add({x:$,y:U}),D=new f([1,2]))}else if(s)if(u=new f(s.points._as),s.segments)D=new f(s.segments._as);else{D=new f([1]);for(var c=1;c<s.points.size();c++)D.add(2)}}}}if(u){if(Array.isArray(u)&&(u=new f(u)),"number"==typeof u.get(0)){for(var r=new i.List,c=0;c<u.size();c+=2)r.add({x:u.get(c),y:u.get(c+1)});u=r}if(!D){D=[];for(var c=0;c<u.size();c++)0===c?D.push(1):D.push(2)}Array.isArray(D)&&(D=new f(D));for(var y=d.toPointsArray(u._as,D._as,50),Q=y.length,I=[],c=0;Q>c;c++){var P=y[c];P.length>1&&I.push(P)}return I}},zn=function(B){var g=B._data,I=Ee(B);if(I){g.s("flow.reverse")&&(I.reverse(),I.forEach(function(_){_.reverse()}));for(var h=0,e=[],u=I.length,Z=0;u>Z;Z++){var m=I[Z],q=Ql(m);h+=q,e.push(q)}if(g[r]=e,g[E]=h,g[V]=I,g instanceof S){var J=c.unionPoint(I),n=J.x+J.width/2,w=J.y+J.height/2;g.$10e={x:n,y:w}}Pc(B,!0)}},Pc=(c.getPercentPositionOnPoints=function(L,E,w){if(L){var j=Ee(H,L,E);if(j){var Y;if(0===w)Y=j[0][0];else if(100===w)j=j[j.length-1],Y=j[j.length-1];else{for(var F=0,W=[],A=j.length,n=0;A>n;n++){var B=j[n],Z=Ql(B);F+=Z,W.push(Z)}for(var k=F*w/100,z=vg(k,W),a=0,m=0;z>m;m++)a+=W[m];k-=a,Y=zo(j[z],k)}return Y}}},L.getPercentPosition=function($,F){var e=this,w=e.getDataUI($),c=Ee(w);if(c){var T;if(0===F)T=c[0][0];else if(100===F)c=c[c.length-1],T=c[c.length-1];else{for(var x=0,B=[],f=c.length,N=0;f>N;N++){var n=c[N],y=Ql(n);x+=y,B.push(y)}for(var J=x*F/100,Z=vg(J,B),l=0,H=0;Z>H;H++)l+=B[H];J-=l,T=zo(c[Z],J)}return T}},function(W,L){var N=W._data,M=N[E],o=N.s(u),t=N.s(h),v=0,B=N[r],n=N.s(J),m=N.s(a),R=N.s(b),U=(n-m)/(R-1),O=[];if(B){if(1===R)O.push(n);else if(2===R)O.push(n),O.push(m);else{if(!(R>2))return;O.push(n);for(var j=R-2;j>0;j--)O.push(m+U*j);O.push(m)}var s=0,V=0;O.forEach(function(v){R-1>s&&(V+=N.getFlowElementSpace(v)),s++}),V+=(n+m)/2,v=(M-o*V+V)/o;var X=W[w];for(null==X&&(X=0),L||(X+=t);X>M+V;){var Q=W._overCount;Q?Q++:Q=1,Q>=o&&(Q=null),W._overCount=Q,N.s("flow.autoreverse")?Q?X-=v+V:(X=0,N.s("flow.reverse",!N.s("flow.reverse"))):X-=v+V}W[w]=X}}),Qp=function(N){var d=N.data,t=this.dm();if(d&&"add"===N.kind){var O=t.$3e;O&&d.s(X)&&O.indexOf(d)<0&&O.push(d)}"clear"===N.kind&&(t.$3e=[])},Bf=function(u){var $=u.property,p=u.data,V=u.newValue,n=this.dm().$3e;if(n&&"s:flow"===$)if(V)n.indexOf(p)<0&&n.push(p);else for(var J=n.length,y=0;J>y;y++)if(n[y]===p){n.splice(y,1);break}},vg=vg=function(v,x){for(var F=0,k=x.length,l=0;k>l;l++){var U=x[l];if(F+=U,F>v)return l}return l},Mm=function(M){var v=this,B=v._data,W=B[E],N=B[r],l=B[V],Q=B.s(u),Y=0,Z=v[w],U=B.s(J),I=B.s(a),F=B.s(b),p=B.s($),D=B.s(z),n=B.s(s),S=(D-p)/(F-1),G=(U-I)/(F-1),t=B.getRotation?B.getRotation():0,A=B.getPosition?B.p():B.$10e,L=[],m=[];if(Z!=H){if(1===F)L.push(U);else if(2===F)L.push(U),L.push(I);else{if(!(F>2))return;L.push(U);for(var y=F-2;y>0;y--)L.push(I+G*y);L.push(I)}if(1===F)m.push(D);else if(2===F)m.push(D),m.push(p);else{if(!(F>2))return;m.push(D);for(var y=F-2;y>0;y--)m.push(p+S*y);m.push(p)}var g=0,j=0;L.forEach(function(x){F-1>g&&(j+=B.getFlowElementSpace(x)),g++}),j+=(U+I)/2,Y=(W-Q*j+j)/Q,M.save();for(var y=0;Q>y;y++){var X=Z,c=0,e=v._overCount,q=0;B.s("flow.autoreverse")&&e&&e>Q-(y+1)||(X-=y*(Y+j),g=0,L.forEach(function(D){var p=X-c;if(p>=0&&W>p){var h=!0,P=vg(p,N);q=0;for(var k=0;P>k;k++)q+=N[k];if(p-=q,h){var H=zo(l[P],p),C=t;if(n){for(var $=l[P],e=0,b=0,V=0;V<$.length-1;V++){var F=$[V],a=$[V+1],x=a.x-F.x,R=a.y-F.y,Q=O(x*x+R*R);if(e+=Q,e>p){b=V;break}}var J=$[b];C+=o(H.y-J.y,H.x-J.x)}t&&(H=co(A,H.x-A.x,H.y-A.y,t)),v.$5e(M,H,D,m[g],C)}}c+=B.getFlowElementSpace(L[g]),g++}))}M.restore()}},jp=function(){var m=this,k=m._data,O=k.s(J),u=!1,v=H;if(m._6I||(u=!0),v=k instanceof S?W.call(m):l.call(m),k.s(X)&&u){var C=k.s(z),b=k.s(R);b&&C>O&&(O=C),O>0&&c.grow(v,n(O/2)),zn(m)}return!k.s(X)&&u&&yi(k,m),v};Q.getFlowElementSpace=function(){return this.s(A)},m._79o=jp,g._79o=jp,F[J]==H&&(F[J]=7),F[a]==H&&(F[a]=0),F[u]==H&&(F[u]=1),F[h]==H&&(F[h]=3),F[b]==H&&(F[b]=10),F[A]==H&&(F[A]=3.5),F[s]==H&&(F[s]=!1),F[I]==H&&(F[I]="rgba(255, 255, 114, 0.4)"),F[C]==H&&(F[C]="rgba(255, 255, 0, 0.3)"),F[G]==H&&(F[G]="rgba(255, 255, 0, 0)"),F[R]==H&&(F[R]=1),F[z]==H&&(F[z]=22),F[$]==H&&(F[$]=4),q.prepareRemove=function(Z){p.call(this,Z);var E=Z._dataModel,R=E.$3e;if(R)for(var o=R.length,T=0;o>T;T++)if(R[T]===Z){R.splice(T,1);break}},L.setDataModel=function(n){var t=this,h=t._dataModel;if(h!==n){h&&(h.umm(Qp,t),h.umd(Bf,t),h.$3e=[]),n.mm(Qp,t),n.md(Bf,t);var s=n.$3e=[];n.each(function(z){z.s(X)&&s.indexOf(z)<0&&s.push(z)}),xc.call(t,n)}},L.setFlowInterval=function(e){var m=this,K=m.$11e;m.$11e=e,m.fp("flowInterval",K,e),m.$7e!=H&&(clearInterval(m.$7e),delete m.$7e,m.enableFlow(e))},L.getFlowInterval=function(){return this.$11e},L.$9e=function(){var Z,j,k,X=this,l=X.tx(),I=X.ty(),q=X.getZoom(),J=X.getWidth(),H=X.getHeight(),o={x:-l/q,y:-I/q,width:J/q,height:H/q},b=X.dm().$3e,R=X._56I,x=new f;if(b.forEach(function(C){R[C.getId()]&&(Z=X.getDataUI(C),Z&&(k=Z._79o(),k&&x.add(k)))}),0!==x.size()&&(x.each(function(v){c.intersectsRect(o,v)&&(j=c.unionRect(j,v))}),j&&(j&&(c.grow(j,K(1,1/q)),j.x=y(j.x*q)/q,j.y=y(j.y*q)/q,j.width=n(j.width*q)/q,j.height=n(j.height*q)/q,j=c.intersection(o,j)),j))){var z=X._canvas.getContext("2d");z.save(),z.lineCap=c.lineCap,z.lineJoin=c.lineJoin,d.translateAndScale(z,l,I,q),z.beginPath(),z.rect(j.x,j.y,j.width,j.height),z.clip(),z.clearRect(j.x,j.y,j.width,j.height),X.$6e(z,j),z.restore()}},L.$6e=function(J,v){var y,I,u=this;u._93db(J),u.each(function(f){u._56I[f._id]&&(y=u.getDataUI(f),y&&(I=y._79o(),(!v||c.intersectsRect(v,I))&&y._42(J)))}),u._92db(J)},L.enableFlow=function(P){var q=this,W=q.dm(),M=W.$3e;q.$7e==H&&(M.forEach(function(T){var j=q.getDataUI(T);zn(j)}),q.$7e=setInterval(function(){W.$3e.forEach(function(G){Pc(q.getDataUI(G))}),q.$9e()},P||q.$11e||50))},L.disableFlow=function(){var W=this;clearInterval(W.$7e),delete W.$7e;var t=W.dm().$3e;t&&W.$9e()},T.$5e=function(X,Z,i,z,u){var V=this,_=V._data,g=V.gv,$=_.s(I),v=_.s(C),K=_.s(G),l=_.s(R),U=g.$8e,o=_.s(j);if(U==H&&(U=g.$8e={}),X.beginPath(),o!=H){var B=c.getImage(o),a=i/2;X.translate(Z.x,Z.y),X.rotate(u),X.translate(-Z.x,-Z.y),c.drawImage(X,B,Z.x-a,Z.y-a,i,i,_),X.translate(Z.x,Z.y),X.rotate(-u),X.translate(-Z.x,-Z.y)}else X.fillStyle=$,X.arc(Z.x,Z.y,i/2,0,e,!0),X.fill();if(l){var p=22,T=p+"_"+v+"_"+K,L=U[T];if(L==H){var Y=eo("canvas");d.setCanvas(Y,p,p);var Q=Y.getContext("2d"),s=p/2,q=s,t=s;d.translateAndScale(Q,0,0,1),Q.beginPath();var O=Q.createRadialGradient(q,t,0,q,q,s);O.addColorStop(0,v),O.addColorStop(1,K),Q.fillStyle=O,Q.arc(q,t,s,0,e,!0),Q.fill(),L=U[T]=Y}var a=z/2;c.drawImage(X,L,Z.x-a,Z.y-a,z,z,_)}},g._80o=function($){P.call(this,$);var R=this,x=R._data,q=R.gv;x.s(X)&&q.$7e!=H&&Mm.call(R,$)},m._80o=function(R){t.call(this,R);var k=this,Z=k._data,g=k.gv;Z.s(X)&&g.$7e!=H&&Mm.call(k,R)}}("undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:this,Object);