jsPlumb-0.0.4-RC1.js 23 KB

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