forked from jsplumb/jsplumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsPlumb-0.0.3-min.js
1 lines (1 loc) · 11.7 KB
/
jsPlumb-0.0.3-min.js
1
Array.prototype.indexOf=function(v,b,s){for(var i=+b||0,l=this.length;i<l;i++){if(this[i]===v||s&&this[i]==v){return i;}}return-1;};(function(){var jsPlumb=window.jsPlumb={_connections:{},_offsets:[],_sizes:[],connectorClass:'_jsPlumb_connector',endpointClass:'_jsPlumb_endpoint',DEFAULT_PAINT_STYLE:{lineWidth:10,strokeStyle:"red"},DEFAULT_ENDPOINT_STYLE:{fillStyle:null},DEFAULT_DRAG_OPTIONS:{},DEFAULT_CONNECTOR:null,DEFAULT_ENDPOINT:null,DEFAULT_NEW_CANVAS_SIZE:1200,Anchors:{TOP_CENTER:{compute:function(xy,wh,txy,twh){return[xy[0]+(wh[0]/2),xy[1]];},orientation:[0,-1]},BOTTOM_CENTER:{compute:function(xy,wh,txy,twh){return[xy[0]+(wh[0]/2),xy[1]+wh[1]];},orientation:[0,1]},LEFT_MIDDLE:{compute:function(xy,wh,txy,twh){return[xy[0],xy[1]+(wh[1]/2)];},orientation:[-1,0]},RIGHT_MIDDLE:{compute:function(xy,wh,txy,twh){return[xy[0]+wh[0],xy[1]+(wh[1]/2)];},orientation:[1,0]},CENTER:{compute:function(xy,wh,txy,twh){return[xy[0]+(wh[0]/2),xy[1]+(wh[1])/2];},orientation:[0,0]},TOP_RIGHT:{compute:function(xy,wh,txy,twh){return[xy[0]+wh[0],xy[1]];},orientation:[0,-1]},BOTTOM_RIGHT:{compute:function(xy,wh,txy,twh){return[xy[0]+wh[0],xy[1]+wh[1]];},orientation:[0,1]},TOP_LEFT:{compute:function(xy,wh,txy,twh){return[xy[0],xy[1]];},orientation:[0,-1]},BOTTOM_LEFT:{compute:function(xy,wh,txy,twh){return[xy[0],xy[1]+wh[1]];},orientation:[0,1]}},Connectors:{STRAIGHT_LINE:{compute:function(sourcePos,targetPos,sourceAnchor,targetAnchor){var w=Math.abs(sourcePos[0]-targetPos[0]);var h=Math.abs(sourcePos[1]-targetPos[1]);var xo=0.25*w;var yo=0.25*h;w*=1.5;h*=1.5;return[Math.min(sourcePos[0],targetPos[0])-xo,Math.min(sourcePos[1],targetPos[1])-yo,w,h,sourcePos[0]<targetPos[0]?w-xo:xo,sourcePos[1]<targetPos[1]?h-yo:yo,sourcePos[0]<targetPos[0]?xo:w-xo,sourcePos[1]<targetPos[1]?yo:h-yo];},paint:function(dimensions,ctx){ctx.beginPath();ctx.moveTo(dimensions[4],dimensions[5]);ctx.lineTo(dimensions[6],dimensions[7]);ctx.stroke();}},ORG_CHART:{compute:function(sourcePos,targetPos,sourceAnchor,targetAnchor){var w=Math.abs(sourcePos[0]-targetPos[0]);var h=Math.abs(sourcePos[1]-targetPos[1]);var xo=0.25*w;var yo=0.25*h;w*=1.5;h*=1.5;return[Math.min(sourcePos[0],targetPos[0])-xo,Math.min(sourcePos[1],targetPos[1])-yo,w,h,sourcePos[0]<targetPos[0]?w-xo:xo,sourcePos[1]<targetPos[1]?h-yo:yo,sourcePos[0]<targetPos[0]?xo:w-xo,sourcePos[1]<targetPos[1]?yo:h-yo];},paint:function(dimensions,ctx){ctx.beginPath();ctx.moveTo(dimensions[4],dimensions[5]);ctx.lineTo(dimensions[6],dimensions[7]);ctx.stroke();}},BEZIER:{_findControlPoint:function(point,anchor1Position,anchor2Position,anchor1,anchor2){var p=[];if(anchor1.orientation[0]==0){var diff=anchor1Position[0]<anchor2Position[0]?point[0]+10:point[0]-10;p.push(diff);}else{p.push(point[0]-(150*anchor1.orientation[0]));}if(anchor1.orientation[1]==0){var diff=anchor1Position[1]<anchor2Position[1]?point[1]+10:point[1]-10;p.push(diff);}else p.push(point[1]+(150*anchor2.orientation[1]));return p;},compute:function(sourcePos,targetPos,sourceAnchor,targetAnchor){var w=Math.abs(sourcePos[0]-targetPos[0]);var h=Math.abs(sourcePos[1]-targetPos[1]);var canvasX=Math.min(sourcePos[0],targetPos[0]);var canvasY=Math.min(sourcePos[1],targetPos[1]);var sx=sourcePos[0]<targetPos[0]?w:0;var sy=sourcePos[1]<targetPos[1]?h:0;var tx=sourcePos[0]<targetPos[0]?0:w;var ty=sourcePos[1]<targetPos[1]?0:h;var CP=jsPlumb.Connectors.BEZIER._findControlPoint([sx,sy],sourcePos,targetPos,sourceAnchor,targetAnchor);var CP2=jsPlumb.Connectors.BEZIER._findControlPoint([tx,ty],targetPos,sourcePos,targetAnchor,sourceAnchor);var minx1=Math.min(sx,tx);var minx2=Math.min(CP[0],CP2[0]);var minx=Math.min(minx1,minx2);var maxx1=Math.max(sx,tx);var maxx2=Math.max(CP[0],CP2[0]);var maxx=Math.max(maxx1,maxx2);if(maxx>w)w=maxx;if(minx<0){canvasX+=minx;var ox=Math.abs(minx);w+=ox;CP[0]+=ox;sx+=ox;tx+=ox;CP2[0]+=ox;}var miny1=Math.min(sy,ty);var miny2=Math.min(CP[1],CP2[1]);var miny=Math.min(miny1,miny2);var maxy1=Math.max(sy,ty);var maxy2=Math.max(CP[1],CP2[1]);var maxy=Math.max(maxy1,maxy2);if(maxy>h)h=maxy;if(miny<0){canvasY+=miny;var oy=Math.abs(miny);h+=oy;CP[1]+=oy;sy+=oy;ty+=oy;CP2[1]+=oy;}return[canvasX,canvasY,w,h,sx,sy,tx,ty,CP[0],CP[1],CP2[0],CP2[1]];},paint:function(d,ctx){ctx.beginPath();ctx.moveTo(d[4],d[5]);ctx.bezierCurveTo(d[8],d[9],d[10],d[11],d[6],d[7]);ctx.stroke();}}},Endpoints:{DOT:{radius:10,paint:function(anchorPoint,canvas,endpointStyle,connectorPaintStyle){var radius=endpointStyle.radius||jsPlumb.Endpoints.DOT.radius;var x=anchorPoint[0]-radius;var y=anchorPoint[1]-radius;jsPlumb.sizeCanvas(canvas,x,y,radius*2,radius*2);var ctx=canvas.getContext('2d');var style={};jsPlumb.applyPaintStyle(style,endpointStyle);if(style.fillStyle==null)style.fillStyle=connectorPaintStyle.strokeStyle;jsPlumb.applyPaintStyle(ctx,style);ctx.beginPath();ctx.arc(radius,radius,radius,0,Math.PI*2,true);ctx.closePath();ctx.fill();}},RECTANGLE:{width:20,height:20,paint:function(anchorPoint,canvas,endpointStyle,connectorPaintStyle){var width=endpointStyle.width||jsPlumb.Endpoints.RECTANGLE.width;var height=endpointStyle.height||jsPlumb.Endpoints.RECTANGLE.height;var x=anchorPoint[0]-(width/2);var y=anchorPoint[1]-(height/2);jsPlumb.sizeCanvas(canvas,x,y,width,height);var ctx=canvas.getContext('2d');var style={};jsPlumb.applyPaintStyle(style,endpointStyle);if(style.fillStyle==null)style.fillStyle=connectorPaintStyle.strokeStyle;jsPlumb.applyPaintStyle(ctx,style);ctx.beginPath();ctx.rect(0,0,width,height);ctx.closePath();ctx.fill();}}},connect:function(params){var jpc=new jsPlumbConnection(params);var key=jpc.sourceId+"_"+jpc.targetId;jsPlumb._connections[key]=jpc;var addToList=function(elId,jpc){var l=jsPlumb._connections[elId];if(l==null){l=[];jsPlumb._connections[elId]=l;}l.push(jpc);};addToList(jpc.sourceId,jpc);addToList(jpc.targetId,jpc);},getConnections:function(elId){return jsPlumb._connections[elId];},drag:function(element,ui){var id=element.attr("id");var l=jsPlumb.getConnections(id);for(var i=0;i<l.length;i++)l[i].paint(id,ui);},detach:function(sourceId,targetId){var jpcs=jsPlumb._connections[sourceId];var idx=-1;for(var i=0;i<jpcs.length;i++){if((jpcs[i].sourceId==sourceId&&jpcs[i].targetId==targetId)||(jpcs[i].targetId==sourceId&&jpcs[i].sourceId==targetId)){jsPlumb.removeCanvas(jpcs[i].canvas);if(jpcs[i].drawEndpoints){jsPlumb.removeCanvas(jpcs[i].targetEndpointCanvas);jsPlumb.removeCanvas(jpcs[i].sourceEndpointCanvas);}idx=i;break;}}if(idx!=-1)jpcs.splice(idx,1);},detachAll:function(elId){var jpcs=jsPlumb._connections[elId];for(var i=0;i<jpcs.length;i++){jsPlumb.removeCanvas(jpcs[i].canvas);if(jpcs[i].drawEndpoints){jsPlumb.removeCanvas(jpcs[i].targetEndpointCanvas);jsPlumb.removeCanvas(jpcs[i].sourceEndpointCanvas);}}jsPlumb._connections[elId]=[];},hide:function(elId){jsPlumb._setVisible(elId,"none");},show:function(elId){jsPlumb._setVisible(elId,"block");},toggle:function(elId){var jpcs=jsPlumb._connections[elId];if(jpcs.length>0)jsPlumb._setVisible(elId,"none"==jpcs[0].canvas.style.display?"block":"none");},_setVisible:function(elId,state){var jpcs=jsPlumb._connections[elId];for(var i=0;i<jpcs.length;i++){jpcs[i].canvas.style.display=state;if(jpcs[i].drawEndpoints){jpcs[i].sourceEndpointCanvas.style.display=state;jpcs[i].targetEndpointCanvas.style.display=state;}}},newCanvas:function(clazz){var canvas=document.createElement("canvas");document.body.appendChild(canvas);canvas.style.position="absolute";if(clazz){canvas.className=clazz;}if(/MSIE/.test(navigator.userAgent)&&!window.opera){jsPlumb.sizeCanvas(canvas,0,0,jsPlumb.DEFAULT_NEW_CANVAS_SIZE,jsPlumb.DEFAULT_NEW_CANVAS_SIZE);canvas=G_vmlCanvasManager.initElement(canvas);}return canvas;},removeCanvas:function(canvas){if(canvas!=null)document.body.removeChild(canvas);},sizeCanvas:function(canvas,x,y,w,h){canvas.style.height=h+"px";canvas.height=h;canvas.style.width=w+"px";canvas.width=w;canvas.style.left=x+"px";canvas.style.top=y+"px";},applyPaintStyle:function(context,styles){for(var i in styles){context[i]=styles[i];}}};var jsPlumbConnection=window.jsPlumbConnection=function(params){var self=this;this.source=(typeof params.source=='string')?$("#"+params.source):params.source;this.target=(typeof params.target=='string')?$("#"+params.target):params.target;this.sourceId=$(this.source).attr("id");this.targetId=$(this.target).attr("id");this.drawEndpoints=params.drawEndpoints!=null?params.drawEndpoints:true;this.endpointsOnTop=params.endpointsOnTop!=null?params.endpointsOnTop:true;this.anchors=params.anchors||jsPlumb.DEFAULT_ANCHORS||[jsPlumb.Anchors.BOTTOM_CENTER,jsPlumb.Anchors.TOP_CENTER];this.connector=params.connector||jsPlumb.DEFAULT_CONNECTOR||jsPlumb.Connectors.BEZIER;this.paintStyle=params.paintStyle||jsPlumb.DEFAULT_PAINT_STYLE;this.endpoint=params.endpoint||jsPlumb.DEFAULT_ENDPOINT||jsPlumb.Endpoints.DOT;this.endpointStyle=params.endpointStyle||jsPlumb.DEFAULT_ENDPOINT_STYLE;jsPlumb._offsets[this.sourceId]=this.source.offset();jsPlumb._sizes[this.sourceId]=[this.source.outerWidth(),this.source.outerHeight()];jsPlumb._offsets[this.targetId]=this.target.offset();jsPlumb._sizes[this.targetId]=[this.target.outerWidth(),this.target.outerHeight()];var canvas=jsPlumb.newCanvas(jsPlumb.connectorClass);this.canvas=canvas;if(this.drawEndpoints){this.sourceEndpointCanvas=jsPlumb.newCanvas(jsPlumb.endpointClass);this.targetEndpointCanvas=jsPlumb.newCanvas(jsPlumb.endpointClass);if(this.endpointsOnTop){$(this.sourceEndpointCanvas).css("zIndex",this.source.css("zIndex")+1);$(this.targetEndpointCanvas).css("zIndex",this.target.css("zIndex")+1);}}this.paint=function(elId,ui){var swap=!(elId==this.sourceId);var tId=swap?this.sourceId:this.targetId,sId=swap?this.targetId:this.sourceId;var tIdx=swap?0:1,sIdx=swap?1:0;if(this.canvas.getContext){var myOffset=ui.absolutePosition;jsPlumb._offsets[elId]=myOffset;var myWH=jsPlumb._sizes[elId];var ctx=canvas.getContext('2d');var otherOffset=jsPlumb._offsets[tId];var otherWH=jsPlumb._sizes[tId];var sAnchorP=this.anchors[sIdx].compute([myOffset.left,myOffset.top],myWH,[otherOffset.left,otherOffset.top],otherWH);var tAnchorP=this.anchors[tIdx].compute([otherOffset.left,otherOffset.top],otherWH,[myOffset.left,myOffset.top],myWH);var dim=this.connector.compute(sAnchorP,tAnchorP,this.anchors[sIdx],this.anchors[tIdx]);jsPlumb.sizeCanvas(canvas,dim[0],dim[1],dim[2],dim[3]);jsPlumb.applyPaintStyle(ctx,this.paintStyle);this.connector.paint(dim,ctx);if(this.drawEndpoints){var style=this.endpointStyle||this.paintStyle;var sourceCanvas=swap?this.targetEndpointCanvas:this.sourceEndpointCanvas;var targetCanvas=swap?this.sourceEndpointCanvas:this.targetEndpointCanvas;this.endpoint.paint(sAnchorP,sourceCanvas,style,this.paintStyle);this.endpoint.paint(tAnchorP,targetCanvas,style,this.paintStyle);}}};var draggable=params.draggable==null?true:params.draggable;if(draggable){var dragOptions=params.dragOptions||jsPlumb.DEFAULT_DRAG_OPTIONS;var dragCascade=dragOptions.drag||function(e,u){};var initDrag=function(element,dragFunc){var opts={};for(var i in dragOptions){opts[i]=dragOptions[i];}opts.drag=dragFunc;element.draggable(opts);};initDrag(this.source,function(event,ui){jsPlumb.drag(self.source,ui);dragCascade(event,ui);});initDrag(this.target,function(event,ui){jsPlumb.drag(self.target,ui);dragCascade(event,ui);});}var o=this.source.offset();this.paint(this.sourceId,{'absolutePosition':this.source.offset()});};})();(function($){$.fn.plumb=function(options){var defaults={};var options=$.extend(defaults,options);return this.each(function(){var obj=$(this);var params={};params.source=obj;for(var i in options){params[i]=options[i];}jsPlumb.connect(params);});};$.fn.detach=function(options){return this.each(function(){var id=$(this).attr("id");if(typeof options=='string')options=[options];for(var i=0;i<options.length;i++)jsPlumb.detach(id,options[i]);});};$.fn.detachAll=function(options){return this.each(function(){var id=$(this).attr("id");jsPlumb.detachAll(id);});};})(jQuery);