suyiScreen/public/htWeb/lib/plugin/ht-forcelayout.js

1 line
6.4 KiB
JavaScript

!function(l,s){"use strict";var y="ht",_=y+".layout.",$=l[y]||module.parent.exports.ht,B=$.List,j=$.DataModel,W=$.Node,M=$.Edge,E=$.Group,L=Math,G=L.sqrt,I=L.random,D=L.max,m=L.min,w=function(v){return v*v};$.Default.getInternal().addMSMap({ms_force:function(_){_._interval=50,_._stepCount=10,_._motionLimit=.01,_._edgeRepulsion=.65,_._nodeRepulsion=.65,_._damper=1,_._maxMotion=0,_._motionRatio=0,_.init=function(n){var o=this;n instanceof j?o.dm=n:o.gv=n,o._nodeMap={},o._nodes=new B,o._edges=new B},_.start=function(){var J=this,X=J.gv;if(!J._timer){var h=J.cdm=X?X.dm():J.dm;h.mm(J.handleDataModelChange,J),h.md(J.handleDataPropertyChange,J),X&&X.mp(J.handleGV,J),h.each(function(U){if(J.isVisible(U)&&J.isLayoutable(U)&&U instanceof W)if(J instanceof p){var u=U.p3();U.p3([u[0]+I(),u[1]+I(),u[2]+I()])}else u=U.p(),U.p(u.x+I(),u.y+I())}),J._timer=setInterval(function(){J.relax()},J._interval),J._damper=1}},_.stop=function(){var E=this;E._timer&&(E.cdm.umm(E.handleDataModelChange,E),E.cdm.umd(E.handleDataPropertyChange,E),E.gv&&E.gv.ump(E.handleGV,E),clearInterval(E._timer),delete E._timer,delete E.cdm)},_.handleGV=function(W){var v=this;if("dataModel"===W.property){var E=W.oldValue,m=W.newValue;E&&(E.umm(v.handleDataModelChange,v),E.umd(v.handleDataPropertyChange,v)),this.cdm=m,m.mm(v.handleDataModelChange,v),m.md(v.handleDataPropertyChange,v)}},_.relax=function(){var i=this;if(!(i._damper<.1&&i._maxMotion<i._motionLimit)){this.cdm.each(function(_){i.isVisible(_)&&(_ instanceof M?i.addEdge(_):_ instanceof W&&i.addNode(_))});for(var J,c,m=0,A=i._edges,a=i._nodes,_=a.size();m<i._stepCount;m++){for(A.each(i.relaxEdge,i),J=0;_>J;J++)for(c=0;_>c;c++)i.relaxNode(a.get(J),a.get(c));i.moveNode()}i._isAdjusting=1,a.each(function(N){N.fix||(N.p?N.v.p3(N.p):N.v.p(N.x,N.y))}),delete i._isAdjusting,i._nodeMap={},a.clear(),A.clear(),i.onRelaxed()}},_.onRelaxed=function(){},_.isRunning=function(){return!!this._timer},_.isVisible=function(U){return U.s("layoutable")===!1?!1:this.gv?this.gv.isVisible(U):!0},_.isLayoutable=function(x){if(x.s("layoutable")===!1)return!1;if(x instanceof E)return!1;var l=this;return l.gv?l.gv.isMovable(x)&&!l.gv.isSelected(x):!(l.cdm||l.dm).sm().co(x)},_.getNodeRepulsion=function(){return this._nodeRepulsion},_.setNodeRepulsion=function(X){this._nodeRepulsion=X,this._damper=1},_.getEdgeRepulsion=function(){return this._edgeRepulsion},_.setEdgeRepulsion=function(y){this._edgeRepulsion=y,this._damper=1},_.getStepCount=function(){return this._stepCount},_.setStepCount=function(r){this._stepCount=r,this._damper=1},_.getInterval=function(){return this._interval},_.setInterval=function(l){var q=this;q._interval!==l&&(q._interval=l,q._timer&&(clearInterval(q._timer),q._timer=setInterval(function(){q.relax()},l)))},_.handleDataPropertyChange=function(s){!this._isAdjusting&&this.isVisible(s.data)&&(this._damper=1)},_.handleDataModelChange=function(){this._damper=1},_.damp=function(){var w=this._maxMotion,F=this._damper;this._motionRatio<=.001&&((.2>w||w>1&&.9>F)&&F>.01?this._damper-=.01:.4>w&&F>.003?this._damper-=.003:F>1e-4&&(this._damper-=1e-4)),w<this._motionLimit&&(this._damper=0)}}}),$.layout.ForceLayout=function(T){this.init(T)},$.Default.def(_+"ForceLayout",s,{ms_force:1,getLimitBounds:function(){return this._limitBounds},setLimitBounds:function(X){this._limitBounds=X,this._damper=1},getNodeSize:function(J){var d=this.gv;return d&&d.getDataUIBounds?d.getDataUIBounds(J):J.getRect()},addNode:function(p){var l=this,x=l._nodeMap[p._id];if(x)return x;var Q=p.p();x={v:p,x:Q.x,y:Q.y,dx:0,dy:0,fix:!l.isLayoutable(p),s:l.getNodeSize(p)};var t=x.s,d=G(w(t.width)+w(t.height))*l._nodeRepulsion;return x.r=1>d?100:d,l._nodeMap[p._id]=x,l._nodes.add(x),x},addEdge:function(t){if(t._40I&&t._41I){var M=this,H=M.addNode(t._40I),i=M.addNode(t._41I),x={s:H,t:i};i=i.s,H=H.s;var _=i.width+H.width,E=i.height+H.height;x.length=G(_*_+E*E)*M._edgeRepulsion,x.length<=0&&(x.length=100),M._edges.add(x)}},relaxEdge:function(M){var Y=M.t,V=M.s,N=Y.x-V.x,R=Y.y-V.y,n=G(N*N+R*R),L=100*M.length,O=.25*N/L*n,S=.25*R/L*n;Y.dx=Y.dx-O,Y.dy=Y.dy-S,V.dx=V.dx+O,V.dy=V.dy+S},relaxNode:function(B,G){if(B!==G){var Y=0,o=0,c=B.x-G.x,h=B.y-G.y,P=c*c+h*h;0===P?(Y=I(),o=I()):36e4>P&&(Y=c/P,o=h/P);var e=B.r*G.r/400;Y*=e,o*=e,B.dx+=Y,B.dy+=o,G.dx-=Y,G.dy-=o}},moveNode:function(){var K=this,V=K._limitBounds,g=K._maxMotion,h=0,H=K._damper;K._nodes.each(function(o){if(!o.fix){var U=o.dx*H,S=o.dy*H;if(o.dx=U/2,o.dy=S/2,h=D(G(U*U+S*S),h),o.x+=D(-40,m(40,U)),o.y+=D(-40,m(40,S)),V){o.x<V.x&&(o.x=V.x,K.adjust(1,0)),o.y<V.y&&(o.y=V.y,K.adjust(0,1));var Y=o.s;o.x+Y.width>V.x+V.width&&(o.x=V.x+V.width-Y.width,K.adjust(-1,0)),o.y+Y.height>V.y+V.height&&(o.y=V.y+V.height-Y.height,K.adjust(0,-1))}}}),K._maxMotion=h,K._motionRatio=h>0?g/h-1:0,K.damp()},adjust:function(c,H){var $=this._limitBounds;this._nodes.each(function(R){c>0?(!$||R.x+R.s.width+c<$.x+$.width)&&(R.x+=c):(!$||R.x+c>$.x)&&(R.x+=c),H>0?(!$||R.y+R.s.height+H<$.y+$.height)&&(R.y+=H):(!$||R.y+H>$.y)&&(R.y+=H)})}});var p=$.layout.Force3dLayout=function(W){this.init(W)};$.Default.def(_+"Force3dLayout",s,{ms_force:1,getNodeSize3d:function(M){return M.s3()},addNode:function(N){var x=this,n=x._nodeMap[N._id];if(n)return n;n={v:N,p:N.p3(),d:[0,0,0],fix:!x.isLayoutable(N),s:x.getNodeSize3d(N)};var o=n.s,m=$.Default.getDistance(o)*x._nodeRepulsion;return n.r=1>m?100:m,x._nodeMap[N._id]=n,x._nodes.add(n),n},addEdge:function(T){if(T._40I&&T._41I){var K=this,B=K.addNode(T._40I),$=K.addNode(T._41I),n={s:B,t:$};$=$.s,B=B.s,n.length=G(w($[0]+B[0])+w($[1]+B[1])+w($[2]+B[2]))*K._edgeRepulsion,n.length<=0&&(n.length=100),K._edges.add(n)}},relaxEdge:function(V){var A=V.t.p,z=V.s.p,q=V.t.d,U=V.s.d,J=A[0]-z[0],P=A[1]-z[1],B=A[2]-z[2],r=G(J*J+P*P+B*B),E=100*V.length,$=.25*J/E*r,h=.25*P/E*r,c=.25*B/E*r;q[0]-=$,q[1]-=h,q[2]-=c,U[0]+=$,U[1]+=h,U[2]+=c},relaxNode:function(f,i){if(f!==i){var C=f.p,b=i.p,P=0,Q=0,c=0,O=C[0]-b[0],o=C[1]-b[1],k=C[2]-b[2],j=O*O+o*o+k*k;0===j?(P=I(),Q=I(),c=I()):216e6>j&&(P=O/j,Q=o/j,c=k/j);var s=f.r*i.r/400,V=f.d,N=i.d;P*=s,Q*=s,c*=s,V[0]+=P,V[1]+=Q,V[2]+=c,N[0]-=P,N[1]-=Q,N[2]-=c}},moveNode:function(){var M=this,j=M._maxMotion,U=0,w=M._damper;M._nodes.each(function(O){if(!O.fix){var x=O.p,W=O.d,F=W[0]*w,X=W[1]*w,N=W[2]*w;W[0]=F/2,W[1]=X/2,W[2]=N/2,U=D(G(F*F+X*X+N*N),U),x[0]+=D(-40,m(40,F)),x[1]+=D(-40,m(40,X)),x[2]+=D(-40,m(40,N))}}),M._maxMotion=U,M._motionRatio=U>0?j/U-1:0,M.damp()}})}("undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:(0,eval)("this"),Object);