jsPlumb-0.0.4.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * jsPlumb 0.0.4-RC1
  3. *
  4. * contains fix for line disappearing when x or y delta was less than line width (bug 1).
  5. *
  6. */
  7. if (!Array.prototype.indexOf) {
  8. Array.prototype.indexOf = function( v, b, s ) {
  9. for( var i = +b || 0, l = this.length; i < l; i++ ) {
  10. if( this[i]===v || s && this[i]==v ) { return i; }
  11. }
  12. return -1;
  13. };
  14. }
  15. (function() {
  16. var connections = {};
  17. var offsets = [];
  18. var sizes = [];
  19. var jsPlumb = window.jsPlumb = {
  20. connectorClass : '_jsPlumb_connector',
  21. endpointClass : '_jsPlumb_endpoint',
  22. DEFAULT_PAINT_STYLE : { lineWidth : 10, strokeStyle : "red" },
  23. DEFAULT_ENDPOINT_STYLE : { fillStyle : null }, // meaning it will be derived from the stroke style of the connector.
  24. DEFAULT_DRAG_OPTIONS : { },
  25. DEFAULT_CONNECTOR : null,
  26. DEFAULT_ENDPOINT : null,
  27. DEFAULT_ENDPOINTS : null, // new in 0.0.4, the ability to specify diff. endpoints. DEFAULT_ENDPOINT is here for backwards compatibility.
  28. DEFAULT_NEW_CANVAS_SIZE : 1200, // only used for IE; a canvas needs a size before the init call to excanvas (for some reason. no idea why.)
  29. /**
  30. * Places you can anchor a connection to. You can write your own one of these; you
  31. * just need to provide a 'compute' method and an 'orientation'. so you'd say something like this:
  32. *
  33. * jsPlumb.Anchors.MY_ANCHOR = {
  34. * compute : function(xy, wh) { return some mathematics on those variables; },
  35. * orientation : [ox, oy]
  36. * };
  37. *
  38. * compute takes the [x,y] position of the top left corner of the anchored element,
  39. * and the element's [width,height] (all in pixels), and returns where the anchor should
  40. * be located.
  41. *
  42. * the 'orientation' array (returned here as [ox,oy]) indicates the general direction a connection from the anchor
  43. * should go in, if possible. it is an [x,y] matrix where a value of 0 means no preference,
  44. * -1 means go in a negative direction for the given axis, and 1 means go in a positive
  45. * direction. so consider a TOP_CENTER anchor: the orientation matrix for it is [0,-1],
  46. * meaning connections naturally want to go upwards on screen. in a Bezier implementation, for example,
  47. * the curve would start out going in that direction, before bending towards the target anchor.
  48. */
  49. Anchors :
  50. {
  51. TOP_CENTER : { compute : function(xy,wh, txy, twh) { return [ xy[0] + (wh[0]/2), xy[1] ]; }, orientation:[0,-1] },
  52. BOTTOM_CENTER : { compute : function(xy,wh, txy, twh) { return [ xy[0] + (wh[0]/2), xy[1] + wh[1] ]; }, orientation:[0,1] },
  53. LEFT_MIDDLE : { compute : function(xy,wh, txy, twh) { return [ xy[0], xy[1] + (wh[1]/2) ]; }, orientation:[-1,0] },
  54. RIGHT_MIDDLE : { compute : function(xy,wh, txy, twh) { return [ xy[0] + wh[0], xy[1] + (wh[1]/2) ]; }, orientation:[1,0] },
  55. CENTER : { compute : function(xy, wh, txy, twh) { return [xy[0] + (wh[0] / 2), xy[1] + (wh[1]) / 2]; }, orientation:[0,0] },
  56. TOP_RIGHT : { compute : function(xy,wh, txy, twh) { return [xy[0] + wh[0], xy[1]]; }, orientation:[0,-1] },
  57. BOTTOM_RIGHT : { compute : function(xy,wh, txy, twh) { return [xy[0] + wh[0], xy[1] + wh[1]]; }, orientation:[0,1] },
  58. TOP_LEFT : { compute : function(xy,wh, txy, twh) { return [xy[0], xy[1]]; }, orientation:[0,-1] },
  59. BOTTOM_LEFT : { compute : function(xy,wh, txy, twh) { return [xy[0], xy[1] + wh[1]]; }, orientation:[0,1] }
  60. },
  61. /**
  62. * Types of connectors, eg. Straight line, bezier.
  63. */
  64. Connectors :
  65. {
  66. /**
  67. * A Connector is given a Canvas context and a source xy and target xy.
  68. * those coordinates are relative to the canvas's position.
  69. * @param ctx
  70. * @param sourceXY
  71. * @param targetXY
  72. */
  73. Straight : {
  74. /**
  75. * Computes the new size and position of the canvas.
  76. * @param sourceAnchor Absolute position on screen of the source object's anchor.
  77. * @param targetAnchor Absolute position on screen of the target object's anchor.
  78. * @param positionMatrix Indicates the relative positions of the left,top of the
  79. * two plumbed objects. so [0,0] indicates that the source is to the left of, and
  80. * above, the target. [1,0] means the source is to the right and above. [0,1] means
  81. * the source is to the left and below. [1,1] means the source is to the right
  82. * and below. this is used to figure out which direction to draw the connector in.
  83. * @returns an array of positioning information. the first two values are
  84. * the [left, top] absolute position the canvas should be placed on screen. the
  85. * next two values are the [width,height] the canvas should be. after that each
  86. * Connector can put whatever it likes into the array:it will be passed back in
  87. * to the paint call. This particular function stores the origin and destination of
  88. * the line it is going to draw. a more involved implementation, like a Bezier curve,
  89. * would store the control point info in this array too.
  90. */
  91. compute : function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth) {
  92. $("#log").html("");
  93. var s = "sourcePos, targetPos : " + sourcePos + " : " + targetPos + "<br/>";
  94. var w = Math.abs(sourcePos[0] - targetPos[0]);
  95. var h = Math.abs(sourcePos[1] - targetPos[1]);
  96. var widthAdjusted = false, heightAdjusted = false;
  97. s += "original w = " + w + "; h = " + h + "<br/>";
  98. if (w < lineWidth) { w = lineWidth; widthAdjusted = true; }
  99. if (h < lineWidth) { h = lineWidth; heightAdjusted = true; }
  100. s += " w = " + w + "; h = " + h + "<br/>";
  101. // these are padding to ensure the whole connector line appears
  102. var xo = 0.45 * w, yo = 0.45 * h;
  103. s += "xoffset = " + xo + "; yoffset = " + yo + "<br/>";
  104. // these are padding to ensure the whole connector line appears
  105. w *= 1.9; h *=1.9;
  106. s += "adjusted w, h " + w + "," + h + "<br/>";
  107. var x = Math.min(sourcePos[0], targetPos[0]) - xo;
  108. var y = Math.min(sourcePos[1], targetPos[1]) - yo;
  109. // here we check to see if the delta was very small and so the line in
  110. // one direction can be considered straight.
  111. var sx = widthAdjusted ? (w - lineWidth) / 2 : sourcePos[0] < targetPos[0] ? w-xo : xo;
  112. var sy = heightAdjusted ? (h - lineWidth) / 2 : sourcePos[1] < targetPos[1] ? h-yo : yo;
  113. var tx = widthAdjusted ? (w - lineWidth) / 2 : sourcePos[0] < targetPos[0] ? xo : w-xo;
  114. var ty = heightAdjusted ? (h - lineWidth) / 2 : sourcePos[1] < targetPos[1] ? yo : h-yo;
  115. var retVal = [ x, y, w, h, sx, sy, tx, ty ];
  116. s += " lineWidth : " + lineWidth + "<br/>";
  117. s += " retVal : " + retVal;
  118. $("#log").html(s);
  119. // return [canvasX, canvasY, canvasWidth, canvasHeight,
  120. // sourceX, sourceY, targetX, targetY]
  121. return retVal;
  122. },
  123. paint : function(dimensions, ctx)
  124. {
  125. ctx.beginPath();
  126. ctx.moveTo(dimensions[4], dimensions[5]);
  127. ctx.lineTo(dimensions[6], dimensions[7]);
  128. ctx.stroke();
  129. }
  130. },
  131. Bezier : function(curviness) {
  132. var self = this;
  133. this.majorAnchor = curviness || 150;
  134. this.minorAnchor = 10;
  135. this._findControlPoint = function(point, anchor1Position, anchor2Position, anchor1, anchor2) {
  136. var p = [];
  137. var ma = self.majorAnchor, mi = self.minorAnchor;
  138. if (anchor1.orientation[0] == 0) // X
  139. p.push(anchor1Position[0] < anchor2Position[0] ? point[0] + mi : point[0] - mi);
  140. else p.push(point[0] - (ma * anchor1.orientation[0]));
  141. if (anchor1.orientation[1] == 0) // Y
  142. p.push(anchor1Position[1] < anchor2Position[1] ? point[1] + mi : point[1] - mi);
  143. else p.push(point[1] + (ma * anchor2.orientation[1]));
  144. return p;
  145. };
  146. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth)
  147. {
  148. var w = Math.abs(sourcePos[0] - targetPos[0]), h = Math.abs(sourcePos[1] - targetPos[1]);
  149. var canvasX = Math.min(sourcePos[0], targetPos[0]), canvasY = Math.min(sourcePos[1], targetPos[1]);
  150. var sx = sourcePos[0] < targetPos[0] ? w : 0, sy = sourcePos[1] < targetPos[1] ? h : 0;
  151. var tx = sourcePos[0] < targetPos[0] ? 0 : w, ty = sourcePos[1] < targetPos[1] ? 0 : h;
  152. var CP = self._findControlPoint([sx,sy], sourcePos, targetPos, sourceAnchor, targetAnchor);
  153. var CP2 = self._findControlPoint([tx,ty], targetPos, sourcePos, targetAnchor, sourceAnchor);
  154. var minx1 = Math.min(sx,tx); var minx2 = Math.min(CP[0], CP2[0]); var minx = Math.min(minx1,minx2);
  155. var maxx1 = Math.max(sx,tx); var maxx2 = Math.max(CP[0], CP2[0]); var maxx = Math.max(maxx1,maxx2);
  156. if (maxx > w) w = maxx;
  157. if (minx < 0) {
  158. canvasX += minx; var ox = Math.abs(minx);
  159. w += ox; CP[0] += ox; sx += ox; tx +=ox; CP2[0] += ox;
  160. }
  161. var miny1 = Math.min(sy,ty); var miny2 = Math.min(CP[1], CP2[1]); var miny = Math.min(miny1,miny2);
  162. var maxy1 = Math.max(sy,ty); var maxy2 = Math.max(CP[1], CP2[1]); var maxy = Math.max(maxy1,maxy2);
  163. if (maxy > h) h = maxy;
  164. if (miny < 0) {
  165. canvasY += miny; var oy = Math.abs(miny);
  166. h += oy; CP[1] += oy; sy += oy; ty +=oy; CP2[1] += oy;
  167. }
  168. // return [ canvasx, canvasy, canvasWidth, canvasHeight,
  169. // sourceX, sourceY, targetX, targetY,
  170. // controlPoint1_X, controlPoint1_Y, controlPoint2_X, controlPoint2_Y
  171. return [canvasX, canvasY, w, h, sx, sy, tx, ty, CP[0], CP[1], CP2[0], CP2[1] ];
  172. };
  173. this.paint = function(d, ctx) {
  174. ctx.beginPath();
  175. ctx.moveTo(d[4],d[5]);
  176. ctx.bezierCurveTo(d[8],d[9],d[10],d[11],d[6],d[7]);
  177. ctx.stroke();
  178. }
  179. }
  180. },
  181. /**
  182. * Types of endpoint UIs. we supply two - a circle of default radius 10px, and a rectangle of
  183. * default size 20x20. you can supply others of these if you want to - see the documentation
  184. * for a howto.
  185. */
  186. Endpoints : {
  187. Dot : {
  188. radius : 10,
  189. paint : function(anchorPoint, canvas, endpointStyle, connectorPaintStyle) {
  190. var radius = endpointStyle.radius || jsPlumb.Endpoints.Dot.radius;
  191. var x = anchorPoint[0] - radius;
  192. var y = anchorPoint[1] - radius;
  193. jsPlumb.sizeCanvas(canvas, x, y, radius * 2, radius * 2);
  194. var ctx = canvas.getContext('2d');
  195. var style = {};
  196. jsPlumb.applyPaintStyle(style, endpointStyle);
  197. if (style.fillStyle == null) style.fillStyle = connectorPaintStyle.strokeStyle;
  198. jsPlumb.applyPaintStyle(ctx, style);
  199. ctx.beginPath();
  200. ctx.arc(radius, radius, radius, 0, Math.PI*2, true);
  201. ctx.closePath();
  202. ctx.fill();
  203. }
  204. },
  205. Rectangle : {
  206. width : 20,
  207. height : 20,
  208. paint : function(anchorPoint, canvas, endpointStyle, connectorPaintStyle) {
  209. var width = endpointStyle.width || jsPlumb.Endpoints.Rectangle.width;
  210. var height = endpointStyle.height || jsPlumb.Endpoints.Rectangle.height;
  211. var x = anchorPoint[0] - (width/2);
  212. var y = anchorPoint[1] - (height/2);
  213. jsPlumb.sizeCanvas(canvas, x, y, width, height);
  214. var ctx = canvas.getContext('2d');
  215. var style = {};
  216. jsPlumb.applyPaintStyle(style, endpointStyle);
  217. if (style.fillStyle == null) style.fillStyle = connectorPaintStyle.strokeStyle;
  218. jsPlumb.applyPaintStyle(ctx, style);
  219. ctx.beginPath();
  220. ctx.rect(0, 0, width, height);
  221. ctx.closePath();
  222. ctx.fill();
  223. }
  224. }
  225. },
  226. connect : function(params) {
  227. var jpc = new jsPlumbConnection(params);
  228. var key = jpc.sourceId + "_" + jpc.targetId;
  229. connections[key] = jpc;
  230. var addToList = function(elId, jpc) {
  231. var l = connections[elId];
  232. if (l == null) {
  233. l = [];
  234. connections[elId] = l;
  235. }
  236. l.push(jpc);
  237. };
  238. // register this connection.
  239. addToList(jpc.sourceId, jpc);
  240. addToList(jpc.targetId, jpc);
  241. },
  242. getConnections : function(elId) {
  243. return connections[elId];
  244. },
  245. drag : function(element, ui) {
  246. var id = element.attr("id");
  247. var l = jsPlumb.getConnections(id);
  248. for (var i = 0; i < l.length; i++)
  249. l[i].paint(id, ui);
  250. },
  251. detach : function(sourceId, targetId) {
  252. var jpcs = connections[sourceId];
  253. var idx = -1;
  254. for (var i = 0; i < jpcs.length; i++) {
  255. if ((jpcs[i].sourceId == sourceId && jpcs[i].targetId == targetId) || (jpcs[i].targetId == sourceId && jpcs[i].sourceId == targetId)) {
  256. jsPlumb.removeCanvas(jpcs[i].canvas);
  257. if (jpcs[i].drawEndpoints) {
  258. jsPlumb.removeCanvas(jpcs[i].targetEndpointCanvas);
  259. jsPlumb.removeCanvas(jpcs[i].sourceEndpointCanvas);
  260. }
  261. idx = i;
  262. break;
  263. }
  264. }
  265. if (idx != -1)
  266. jpcs.splice(idx, 1);
  267. // todo - dragging? if no more connections for an object turn off dragging by default, but
  268. // allow an override on it?
  269. },
  270. detachAll : function(elId) {
  271. var jpcs = connections[elId];
  272. for (var i = 0; i < jpcs.length; i++) {
  273. jsPlumb.removeCanvas(jpcs[i].canvas);
  274. if (jpcs[i].drawEndpoints) {
  275. jsPlumb.removeCanvas(jpcs[i].targetEndpointCanvas);
  276. jsPlumb.removeCanvas(jpcs[i].sourceEndpointCanvas);
  277. }
  278. }
  279. connections[elId] = [];
  280. },
  281. hide : function(elId) {
  282. jsPlumb._setVisible(elId, "none");
  283. },
  284. show : function(elId) {
  285. jsPlumb._setVisible(elId, "block");
  286. },
  287. toggle : function(elId) {
  288. var jpcs = connections[elId];
  289. if (jpcs.length > 0)
  290. jsPlumb._setVisible(elId, "none" == jpcs[0].canvas.style.display ? "block" : "none");
  291. },
  292. _setVisible : function(elId, state) {
  293. var jpcs = connections[elId];
  294. for (var i = 0; i < jpcs.length; i++) {
  295. jpcs[i].canvas.style.display=state;
  296. if (jpcs[i].drawEndpoints) {
  297. jpcs[i].sourceEndpointCanvas.style.display=state;
  298. jpcs[i].targetEndpointCanvas.style.display=state;
  299. }
  300. }
  301. },
  302. /**
  303. * helper to create a canvas.
  304. * @param clazz optional class name for the canvas.
  305. */
  306. newCanvas : function(clazz) {
  307. var canvas = document.createElement("canvas");
  308. document.body.appendChild(canvas);
  309. canvas.style.position="absolute";
  310. if (clazz) { canvas.className=clazz; }
  311. if (/MSIE/.test(navigator.userAgent) && !window.opera) {
  312. // for IE we have to set a big canvas size. actually you can override this, too, if 1200 pixels
  313. // is not big enough for the biggest connector/endpoint canvas you have at startup.
  314. jsPlumb.sizeCanvas(canvas, 0, 0, jsPlumb.DEFAULT_NEW_CANVAS_SIZE, jsPlumb.DEFAULT_NEW_CANVAS_SIZE);
  315. canvas = G_vmlCanvasManager.initElement(canvas);
  316. }
  317. return canvas;
  318. },
  319. /**
  320. * helper to remove a canvas from the DOM.
  321. */
  322. removeCanvas : function(canvas) {
  323. if (canvas != null) document.body.removeChild(canvas);
  324. },
  325. /**
  326. * helper to size a canvas.
  327. */
  328. sizeCanvas : function(canvas, x, y, w, h) {
  329. canvas.style.height = h + "px"; canvas.height = h;
  330. canvas.style.width = w + "px"; canvas.width = w;
  331. canvas.style.left = x + "px"; canvas.style.top = y + "px";
  332. },
  333. /**
  334. * applies all the styles to the given context.
  335. * @param canvas
  336. * @param styles
  337. */
  338. applyPaintStyle : function(context, styles) {
  339. for (var i in styles) {
  340. context[i] = styles[i];
  341. }
  342. }
  343. };
  344. // ************** connection
  345. // ****************************************
  346. /**
  347. * allowed params:
  348. * source: source element (string or a jQuery element) (required)
  349. * target: target element (string or a jQuery element) (required)
  350. * anchors: optional array of anchor placements. defaults to BOTTOM_CENTER for source
  351. * and TOP_CENTER for target.
  352. */
  353. var jsPlumbConnection = window.jsPlumbConnection = function(params) {
  354. // ************** get the source and target and register the connection. *******************
  355. var self = this;
  356. // get source and target as jQuery objects
  357. this.source = (typeof params.source == 'string') ? $("#" + params.source) : params.source;
  358. this.target = (typeof params.target == 'string') ? $("#" + params.target) : params.target;
  359. this.sourceId = $(this.source).attr("id");
  360. this.targetId = $(this.target).attr("id");
  361. this.drawEndpoints = params.drawEndpoints != null ? params.drawEndpoints : true;
  362. this.endpointsOnTop = params.endpointsOnTop != null ? params.endpointsOnTop : true;
  363. // get anchor
  364. this.anchors = params.anchors || jsPlumb.DEFAULT_ANCHORS || [jsPlumb.Anchors.BOTTOM_CENTER, jsPlumb.Anchors.TOP_CENTER];
  365. // make connector
  366. this.connector = params.connector || jsPlumb.DEFAULT_CONNECTOR || new jsPlumb.Connectors.Bezier();
  367. this.paintStyle = params.paintStyle || jsPlumb.DEFAULT_PAINT_STYLE;
  368. // init endpoints
  369. this.endpoint = params.endpoint || jsPlumb.DEFAULT_ENDPOINT || jsPlumb.Endpoints.Dot;
  370. this.endpointStyle = params.endpointStyle || jsPlumb.DEFAULT_ENDPOINT_STYLE;
  371. offsets[this.sourceId] = this.source.offset();
  372. sizes[this.sourceId] = [this.source.outerWidth(), this.source.outerHeight()];
  373. offsets[this.targetId] = this.target.offset();
  374. sizes[this.targetId] = [this.target.outerWidth(), this.target.outerHeight()];
  375. // *************** create canvases on which the connection will be drawn ************
  376. var canvas = jsPlumb.newCanvas(jsPlumb.connectorClass);
  377. this.canvas = canvas;
  378. // create endpoint canvases
  379. if (this.drawEndpoints) {
  380. this.sourceEndpointCanvas = jsPlumb.newCanvas(jsPlumb.endpointClass);
  381. this.targetEndpointCanvas = jsPlumb.newCanvas(jsPlumb.endpointClass);
  382. // sit them on top of the underlying element?
  383. if (this.endpointsOnTop) {
  384. $(this.sourceEndpointCanvas).css("zIndex", this.source.css("zIndex") + 1);
  385. $(this.targetEndpointCanvas).css("zIndex", this.target.css("zIndex") + 1);
  386. }
  387. }
  388. // ************** store the anchors
  389. this.paint = function(elId, ui) {
  390. // if the moving object is not the source we must transpose the two references.
  391. var swap = !(elId == this.sourceId);
  392. var tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId;
  393. var tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  394. if (this.canvas.getContext) {
  395. var myOffset = ui.absolutePosition;
  396. offsets[elId] = myOffset;
  397. var myWH = sizes[elId];
  398. var ctx = canvas.getContext('2d');
  399. var otherOffset = offsets[tId];
  400. var otherWH = sizes[tId];
  401. var sAnchorP = this.anchors[sIdx].compute([myOffset.left, myOffset.top], myWH, [otherOffset.left, otherOffset.top], otherWH);
  402. var tAnchorP = this.anchors[tIdx].compute([otherOffset.left, otherOffset.top], otherWH, [myOffset.left, myOffset.top], myWH);
  403. var dim = this.connector.compute(sAnchorP, tAnchorP, this.anchors[sIdx], this.anchors[tIdx], this.paintStyle.lineWidth);
  404. jsPlumb.sizeCanvas(canvas, dim[0], dim[1], dim[2], dim[3]);
  405. jsPlumb.applyPaintStyle(ctx, this.paintStyle);
  406. // gradient experiment.
  407. if (this.paintStyle.gradient) {
  408. var g = swap ? ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]) : ctx.createLinearGradient(dim[6], dim[7], dim[4], dim[5]);
  409. for (var i = 0; i < this.paintStyle.gradient.length; i++)
  410. g.addColorStop(this.paintStyle.gradient[i][0],this.paintStyle.gradient[i][1]);
  411. ctx.strokeStyle = g;
  412. }
  413. this.connector.paint(dim, ctx);
  414. if (this.drawEndpoints) {
  415. // todo support endpoints here.
  416. var style = this.endpointStyle || this.paintStyle;
  417. var sourceCanvas = swap ? this.targetEndpointCanvas : this.sourceEndpointCanvas;
  418. var targetCanvas = swap ? this.sourceEndpointCanvas : this.targetEndpointCanvas;
  419. this.endpoint.paint(sAnchorP, sourceCanvas, style, this.paintStyle);
  420. this.endpoint.paint(tAnchorP, targetCanvas, style, this.paintStyle);
  421. }
  422. }
  423. };
  424. var draggable = params.draggable == null ? true : params.draggable;
  425. if (draggable) {
  426. var dragOptions = params.dragOptions || jsPlumb.DEFAULT_DRAG_OPTIONS;
  427. var dragCascade = dragOptions.drag || function(e,u) {};
  428. var initDrag = function(element, dragFunc) {
  429. var opts = {};
  430. for (var i in dragOptions) {
  431. opts[i] = dragOptions[i];
  432. }
  433. opts.drag = dragFunc;
  434. element.draggable(opts);
  435. };
  436. initDrag(this.source, function(event, ui) {
  437. jsPlumb.drag(self.source, ui);
  438. dragCascade(event, ui);
  439. });
  440. initDrag(this.target, function(event, ui) {
  441. jsPlumb.drag(self.target, ui);
  442. dragCascade(event, ui);
  443. });
  444. }
  445. var o = this.source.offset();
  446. this.paint(this.sourceId, {'absolutePosition': this.source.offset()});
  447. };
  448. })();
  449. // jQuery plugin code
  450. (function($){
  451. $.fn.plumb = function(options) {
  452. var defaults = { };
  453. var options = $.extend(defaults, options);
  454. return this.each(function()
  455. {
  456. var obj = $(this);
  457. var params = {};
  458. params.source = obj;
  459. for (var i in options) {
  460. params[i] = options[i];
  461. }
  462. jsPlumb.connect(params);
  463. });
  464. };
  465. $.fn.detach = function(options) {
  466. return this.each(function()
  467. {
  468. var id = $(this).attr("id");
  469. if (typeof options == 'string') options = [options];
  470. for (var i = 0; i < options.length; i++)
  471. jsPlumb.detach(id, options[i]);
  472. });
  473. };
  474. $.fn.detachAll = function(options) {
  475. return this.each(function()
  476. {
  477. var id = $(this).attr("id");
  478. jsPlumb.detachAll(id);
  479. });
  480. };
  481. })(jQuery);