jsPlumb-defaults-1.3.0-RC1.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * jsPlumb-defaults-1.3.0-RC1
  3. *
  4. * Copyright 2010 - 2011 Simon Porritt http://jsplumb.org
  5. *
  6. * Triple licensed under the MIT, GPL2 and Beer licenses.
  7. */
  8. (function() {
  9. /**
  10. *
  11. * Helper class to consume unused mouse events by components that are DOM elements and
  12. * are used by all of the different rendering modes.
  13. *
  14. */
  15. jsPlumb.DOMElementComponent = function(params) {
  16. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  17. // when render mode is canvas, these functions may be called by the canvas mouse handler.
  18. // this component is safe to pipe this stuff to /dev/null.
  19. this.mousemove =
  20. this.dblclick =
  21. this.click =
  22. this.mousedown =
  23. this.mouseup = function(e) { };
  24. };
  25. /**
  26. * Class: Connectors.Straight
  27. * The Straight connector draws a simple straight line between the two anchor points. It does not have any constructor parameters.
  28. */
  29. jsPlumb.Connectors.Straight = function() {
  30. var self = this;
  31. var currentPoints = null;
  32. var _m, _m2, _b, _dx, _dy, _theta, _theta2, _sx, _sy, _tx, _ty;
  33. /**
  34. * Computes the new size and position of the canvas.
  35. * @param sourceAnchor Absolute position on screen of the source object's anchor.
  36. * @param targetAnchor Absolute position on screen of the target object's anchor.
  37. * @param positionMatrix Indicates the relative positions of the left,top of the
  38. * two plumbed objects. so [0,0] indicates that the source is to the left of, and
  39. * above, the target. [1,0] means the source is to the right and above. [0,1] means
  40. * the source is to the left and below. [1,1] means the source is to the right
  41. * and below. this is used to figure out which direction to draw the connector in.
  42. * @returns an array of positioning information. the first two values are
  43. * the [left, top] absolute position the canvas should be placed on screen. the
  44. * next two values are the [width,height] the canvas should be. after that each
  45. * Connector can put whatever it likes into the array:it will be passed back in
  46. * to the paint call. This particular function stores the origin and destination of
  47. * the line it is going to draw. a more involved implementation, like a Bezier curve,
  48. * would store the control point info in this array too.
  49. */
  50. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth, minWidth) {
  51. var w = Math.abs(sourcePos[0] - targetPos[0]);
  52. var h = Math.abs(sourcePos[1] - targetPos[1]);
  53. var widthAdjusted = false, heightAdjusted = false;
  54. // these are padding to ensure the whole connector line appears
  55. var xo = 0.45 * w, yo = 0.45 * h;
  56. // these are padding to ensure the whole connector line appears
  57. w *= 1.9; h *=1.9;
  58. var x = Math.min(sourcePos[0], targetPos[0]) - xo;
  59. var y = Math.min(sourcePos[1], targetPos[1]) - yo;
  60. // minimum size is 2 * line Width if minWidth was not given.
  61. var calculatedMinWidth = Math.max(2 * lineWidth, minWidth);
  62. if (w < calculatedMinWidth) {
  63. w = calculatedMinWidth;
  64. x = sourcePos[0] + ((targetPos[0] - sourcePos[0]) / 2) - (calculatedMinWidth / 2);
  65. xo = (w - Math.abs(sourcePos[0]-targetPos[0])) / 2;
  66. }
  67. if (h < calculatedMinWidth) {
  68. h = calculatedMinWidth;
  69. y = sourcePos[1] + ((targetPos[1] - sourcePos[1]) / 2) - (calculatedMinWidth / 2);
  70. yo = (h - Math.abs(sourcePos[1]-targetPos[1])) / 2;
  71. }
  72. _sx = sourcePos[0] < targetPos[0] ? xo : w-xo;
  73. _sy = sourcePos[1] < targetPos[1] ? yo:h-yo;
  74. _tx = sourcePos[0] < targetPos[0] ? w-xo : xo;
  75. _ty = sourcePos[1] < targetPos[1] ? h-yo : yo;
  76. currentPoints = [ x, y, w, h, _sx, _sy, _tx, _ty ];
  77. _dx = _tx - _sx, _dy = (_ty - _sy);
  78. _m = _dy / _dx, _m2 = -1 / _m;
  79. _b = -1 * ((_m * _sx) - _sy);
  80. _theta = Math.atan(_m); _theta2 = Math.atan(_m2);
  81. return currentPoints;
  82. };
  83. /**
  84. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  85. * 0 to 1 inclusive. for the straight line connector this is simple maths. for Bezier, not so much.
  86. */
  87. this.pointOnPath = function(location) {
  88. var xp = _sx + (location * _dx);
  89. var yp = (_m == Infinity || _m == -Infinity) ? _sy + (location * (_ty - _sy)) : (_m * xp) + _b;
  90. return {x:xp, y:yp};
  91. };
  92. /**
  93. * returns the gradient of the connector at the given point - which for us is constant.
  94. */
  95. this.gradientAtPoint = function(location) { return _m; };
  96. /**
  97. * returns the point on the connector's path that is 'distance' along the length of the path from 'location', where
  98. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels.
  99. */
  100. this.pointAlongPathFrom = function(location, distance) {
  101. var p = self.pointOnPath(location);
  102. var orientation = distance > 0 ? 1 : -1;
  103. var y = Math.abs(distance * Math.sin(_theta));
  104. if (_sy > _ty) y = y * -1;
  105. var x = Math.abs(distance * Math.cos(_theta));
  106. if (_sx > _tx) x = x * -1;
  107. return {x:p.x + (orientation * x), y:p.y + (orientation * y)};
  108. };
  109. /**
  110. * calculates a line that is perpendicular to, and centered on, the path at 'distance' pixels from the given location.
  111. * the line is 'length' pixels long.
  112. */
  113. this.perpendicularToPathAt = function(location, length, distance) {
  114. var p = self.pointAlongPathFrom(location, distance);
  115. var m = self.gradientAtPoint(p.location);
  116. var _theta2 = Math.atan(-1 / m);
  117. var y = length / 2 * Math.sin(_theta2);
  118. var x = length / 2 * Math.cos(_theta2);
  119. return [{x:p.x + x, y:p.y + y}, {x:p.x - x, y:p.y - y}];
  120. };
  121. };
  122. /**
  123. * Class:Connectors.Bezier
  124. * This Connector draws a Bezier curve with two control points. You can provide a 'curviness' value which gets applied to jsPlumb's
  125. * internal voodoo machine and ends up generating locations for the two control points. See the constructor documentation below.
  126. */
  127. /**
  128. * Function:Constructor
  129. *
  130. * Parameters:
  131. * curviness - How 'curvy' you want the curve to be! This is a directive for the placement of control points, not endpoints of the curve, so your curve does not
  132. * actually touch the given point, but it has the tendency to lean towards it. The larger this value, the greater the curve is pulled from a straight line.
  133. * Optional; defaults to 150.
  134. *
  135. */
  136. jsPlumb.Connectors.Bezier = function(params) {
  137. var self = this;
  138. params = params || {};
  139. this.majorAnchor = params.curviness || 150;
  140. this.minorAnchor = 10;
  141. var currentPoints = null;
  142. this._findControlPoint = function(point, sourceAnchorPosition, targetAnchorPosition, sourceAnchor, targetAnchor) {
  143. // determine if the two anchors are perpendicular to each other in their orientation. we swap the control
  144. // points around if so (code could be tightened up)
  145. var soo = sourceAnchor.getOrientation(), too = targetAnchor.getOrientation();
  146. var perpendicular = soo[0] != too[0] || soo[1] == too[1];
  147. var p = [];
  148. var ma = self.majorAnchor, mi = self.minorAnchor;
  149. if (!perpendicular) {
  150. if (soo[0] == 0) // X
  151. p.push(sourceAnchorPosition[0] < targetAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  152. else p.push(point[0] - (ma * soo[0]));
  153. if (soo[1] == 0) // Y
  154. p.push(sourceAnchorPosition[1] < targetAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  155. else p.push(point[1] + (ma * too[1]));
  156. }
  157. else {
  158. if (too[0] == 0) // X
  159. p.push(targetAnchorPosition[0] < sourceAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  160. else p.push(point[0] + (ma * too[0]));
  161. if (too[1] == 0) // Y
  162. p.push(targetAnchorPosition[1] < sourceAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  163. else p.push(point[1] + (ma * soo[1]));
  164. }
  165. return p;
  166. };
  167. var _CP, _CP2, _sx, _tx, _ty, _sx, _sy, _canvasX, _canvasY, _w, _h;
  168. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth, minWidth)
  169. {
  170. lineWidth = lineWidth || 0;
  171. _w = Math.abs(sourcePos[0] - targetPos[0]) + lineWidth;
  172. _h = Math.abs(sourcePos[1] - targetPos[1]) + lineWidth;
  173. _canvasX = Math.min(sourcePos[0], targetPos[0])-(lineWidth/2);
  174. _canvasY = Math.min(sourcePos[1], targetPos[1])-(lineWidth/2);
  175. _sx = sourcePos[0] < targetPos[0] ? _w - (lineWidth/2): (lineWidth/2);
  176. _sy = sourcePos[1] < targetPos[1] ? _h - (lineWidth/2) : (lineWidth/2);
  177. _tx = sourcePos[0] < targetPos[0] ? (lineWidth/2) : _w - (lineWidth/2);
  178. _ty = sourcePos[1] < targetPos[1] ? (lineWidth/2) : _h - (lineWidth/2);
  179. _CP = self._findControlPoint([_sx,_sy], sourcePos, targetPos, sourceAnchor, targetAnchor);
  180. _CP2 = self._findControlPoint([_tx,_ty], targetPos, sourcePos, targetAnchor, sourceAnchor);
  181. var minx1 = Math.min(_sx,_tx); var minx2 = Math.min(_CP[0], _CP2[0]); var minx = Math.min(minx1,minx2);
  182. var maxx1 = Math.max(_sx,_tx); var maxx2 = Math.max(_CP[0], _CP2[0]); var maxx = Math.max(maxx1,maxx2);
  183. if (maxx > _w) _w = maxx;
  184. if (minx < 0) {
  185. _canvasX += minx; var ox = Math.abs(minx);
  186. _w += ox; _CP[0] += ox; _sx += ox; _tx +=ox; _CP2[0] += ox;
  187. }
  188. var miny1 = Math.min(_sy,_ty); var miny2 = Math.min(_CP[1], _CP2[1]); var miny = Math.min(miny1,miny2);
  189. var maxy1 = Math.max(_sy,_ty); var maxy2 = Math.max(_CP[1], _CP2[1]); var maxy = Math.max(maxy1,maxy2);
  190. if (maxy > _h) _h = maxy;
  191. if (miny < 0) {
  192. _canvasY += miny; var oy = Math.abs(miny);
  193. _h += oy; _CP[1] += oy; _sy += oy; _ty +=oy; _CP2[1] += oy;
  194. }
  195. if (minWidth && _w < minWidth) {
  196. var posAdjust = (minWidth - _w) / 2;
  197. _w = minWidth;
  198. _canvasX -= posAdjust; _sx = _sx + posAdjust ; _tx = _tx + posAdjust; _CP[0] = _CP[0] + posAdjust; _CP2[0] = _CP2[0] + posAdjust;
  199. }
  200. if (minWidth && _h < minWidth) {
  201. var posAdjust = (minWidth - _h) / 2;
  202. _h = minWidth;
  203. _canvasY -= posAdjust; _sy = _sy + posAdjust ; _ty = _ty + posAdjust; _CP[1] = _CP[1] + posAdjust; _CP2[1] = _CP2[1] + posAdjust;
  204. }
  205. currentPoints = [_canvasX, _canvasY, _w, _h, _sx, _sy, _tx, _ty, _CP[0], _CP[1], _CP2[0], _CP2[1] ];
  206. return currentPoints;
  207. };
  208. var _makeCurve = function() {
  209. return [
  210. { x:_sx, y:_sy },
  211. { x:_CP[0], y:_CP[1] },
  212. { x:_CP2[0], y:_CP2[1] },
  213. { x:_tx, y:_ty }
  214. ];
  215. };
  216. /**
  217. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  218. * 0 to 1 inclusive. for the straight line connector this is simple maths. for Bezier, not so much.
  219. */
  220. this.pointOnPath = function(location) {
  221. return jsBezier.pointOnCurve(_makeCurve(), location);
  222. };
  223. /**
  224. * returns the gradient of the connector at the given point.
  225. */
  226. this.gradientAtPoint = function(location) {
  227. return jsBezier.gradientAtPoint(_makeCurve(), location);
  228. };
  229. /**
  230. * for Bezier curves this method is a little tricky, cos calculating path distance algebraically is notoriously difficult.
  231. * this method is iterative, jumping forward .05% of the path at a time and summing the distance between this point and the previous
  232. * one, until the sum reaches 'distance'. the method may turn out to be computationally expensive; we'll see.
  233. * another drawback of this method is that if the connector gets quite long, .05% of the length of it is not necessarily smaller
  234. * than the desired distance, in which case the loop returns immediately and the arrow is mis-shapen. so a better strategy might be to
  235. * calculate the step as a function of distance/distance between endpoints.
  236. */
  237. this.pointAlongPathFrom = function(location, distance) {
  238. return jsBezier.pointAlongCurveFrom(_makeCurve(), location, distance);
  239. };
  240. /**
  241. * calculates a line that is perpendicular to, and centered on, the path at 'distance' pixels from the given location.
  242. * the line is 'length' pixels long.
  243. */
  244. this.perpendicularToPathAt = function(location, length, distance) {
  245. return jsBezier.perpendicularToCurveAt(_makeCurve(), location, length, distance);
  246. };
  247. };
  248. /**
  249. * Class: Connectors.Flowchart
  250. * Provides 'flowchart' connectors, consisting of vertical and horizontal line segments.
  251. */
  252. /**
  253. * Function: Constructor
  254. *
  255. * Parameters:
  256. * stub - minimum length for the stub at each end of the connector. defaults to 30 pixels.
  257. */
  258. jsPlumb.Connectors.Flowchart = function(params) {
  259. params = params || {};
  260. var self = this,
  261. minStubLength = params.stub || params.minStubLength /* bwds compat. */ || 30,
  262. segments = [],
  263. segmentGradients = [],
  264. segmentProportions = [],
  265. segmentLengths = [],
  266. segmentProportionalLengths = [],
  267. points = [],
  268. swapX,
  269. swapY,
  270. /**
  271. * recalculates the gradients of each segment, and the points at which the segments begin, proportional to the total length travelled
  272. * by all the segments that constitute the connector.
  273. */
  274. updateSegmentGradientsAndProportions = function(startX, startY, endX, endY) {
  275. var total = 0;
  276. for (var i = 0; i < segments.length; i++) {
  277. var sx = i == 0 ? startX : segments[i][2],
  278. sy = i == 0 ? startY : segments[i][3],
  279. ex = segments[i][0],
  280. ey = segments[i][1];
  281. segmentGradients[i] = sx == ex ? Infinity : 0;
  282. segmentLengths[i] = Math.abs(sx == ex ? ey - sy : ex - sx);
  283. total += segmentLengths[i];
  284. }
  285. var curLoc = 0;
  286. for (var i = 0; i < segments.length; i++) {
  287. segmentProportionalLengths[i] = segmentLengths[i] / total;
  288. segmentProportions[i] = [curLoc, (curLoc += (segmentLengths[i] / total)) ];
  289. }
  290. },
  291. appendSegmentsToPoints = function() {
  292. points.push(segments.length);
  293. for (var i = 0; i < segments.length; i++) {
  294. points.push(segments[i][0]);
  295. points.push(segments[i][1]);
  296. }
  297. },
  298. /**
  299. * helper method to add a segment.
  300. */
  301. addSegment = function(x, y, sx, sy, tx, ty) {
  302. var lx = segments.length == 0 ? sx : segments[segments.length - 1][0];
  303. var ly = segments.length == 0 ? sy : segments[segments.length - 1][1];
  304. segments.push([x, y, lx, ly]);
  305. },
  306. /**
  307. * returns [segment, proportion of travel in segment, segment index] for the segment
  308. * that contains the point which is 'location' distance along the entire path, where
  309. * 'location' is a decimal between 0 and 1 inclusive. in this connector type, paths
  310. * are made up of a list of segments, each of which contributes some fraction to
  311. * the total length.
  312. */
  313. findSegmentForLocation = function(location) {
  314. var idx = segmentProportions.length - 1, inSegmentProportion = 0;
  315. for (var i = 0; i < segmentProportions.length; i++) {
  316. if (segmentProportions[i][1] >= location) {
  317. idx = i;
  318. inSegmentProportion = (location - segmentProportions[i][0]) / segmentProportionalLengths[i];
  319. break;
  320. }
  321. }
  322. return { segment:segments[idx], proportion:inSegmentProportion, index:idx };
  323. };
  324. this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth, minWidth) {
  325. segments = [];
  326. segmentGradients = [];
  327. segmentProportionalLengths = [];
  328. segmentLengths = [];
  329. segmentProportionals = [];
  330. swapX = targetPos[0] < sourcePos[0];
  331. swapY = targetPos[1] < sourcePos[1];
  332. var lw = lineWidth || 1,
  333. offx = (lw / 2) + (minStubLength * 2),
  334. offy = (lw / 2) + (minStubLength * 2),
  335. so = sourceAnchor.orientation || sourceAnchor.getOrientation(),
  336. to = targetAnchor.orientation || targetAnchor.getOrientation(),
  337. x = swapX ? targetPos[0] : sourcePos[0],
  338. y = swapY ? targetPos[1] : sourcePos[1],
  339. w = Math.abs(targetPos[0] - sourcePos[0]) + 2*offx,
  340. h = Math.abs(targetPos[1] - sourcePos[1]) + 2*offy;
  341. if (w < minWidth) {
  342. offx += (minWidth - w) / 2;
  343. w = minWidth;
  344. }
  345. if (h < minWidth) {
  346. offy += (minWidth - h) / 2;
  347. h = minWidth;
  348. }
  349. var sx = swapX ? w-offx : offx,
  350. sy = swapY ? h-offy : offy,
  351. tx = swapX ? offx : w-offx ,
  352. ty = swapY ? offy : h-offy,
  353. startStubX = sx + (so[0] * minStubLength),
  354. startStubY = sy + (so[1] * minStubLength),
  355. endStubX = tx + (to[0] * minStubLength),
  356. endStubY = ty + (to[1] * minStubLength),
  357. midx = startStubX + ((endStubX - startStubX) / 2),
  358. midy = startStubY + ((endStubY - startStubY) / 2);
  359. x -= offx; y -= offy;
  360. points = [x, y, w, h, sx, sy, tx, ty], extraPoints = [];
  361. addSegment(startStubX, startStubY, sx, sy, tx, ty);
  362. if (so[0] == 0) {
  363. var startStubIsBeforeEndStub = startStubY < endStubY;
  364. // when start point's stub is less than endpoint's stub
  365. if (startStubIsBeforeEndStub) {
  366. addSegment(startStubX, midy, sx, sy, tx, ty);
  367. addSegment(midx, midy, sx, sy, tx, ty);
  368. addSegment(endStubX, midy, sx, sy, tx, ty);
  369. } else {
  370. // when start point's stub is greater than endpoint's stub
  371. addSegment(midx, startStubY, sx, sy, tx, ty);
  372. addSegment(midx, endStubY, sx, sy, tx, ty);
  373. }
  374. }
  375. else {
  376. var startStubIsBeforeEndStub = startStubX < endStubX;
  377. // when start point's stub is less than endpoint's stub
  378. if (startStubIsBeforeEndStub) {
  379. addSegment(midx, startStubY, sx, sy, tx, ty);
  380. addSegment(midx, midy, sx, sy, tx, ty);
  381. addSegment(midx, endStubY, sx, sy, tx, ty);
  382. } else {
  383. // when start point's stub is greater than endpoint's stub
  384. addSegment(startStubX, midy, sx, sy, tx, ty);
  385. addSegment(endStubX, midy, sx, sy, tx, ty);
  386. }
  387. }
  388. addSegment(endStubX, endStubY, sx, sy, tx, ty);
  389. addSegment(tx, ty, sx, sy, tx, ty);
  390. appendSegmentsToPoints();
  391. updateSegmentGradientsAndProportions(sx, sy, tx, ty);
  392. return points;
  393. };
  394. /**
  395. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  396. * 0 to 1 inclusive. for this connector we must first figure out which segment the given point lies in, and then compute the x,y position
  397. * from our knowledge of the segment's start and end points.
  398. */
  399. this.pointOnPath = function(location) {
  400. return self.pointAlongPathFrom(location, 0);
  401. };
  402. /**
  403. * returns the gradient of the connector at the given point; the gradient will be either 0 or Infinity, depending on the direction of the
  404. * segment the point falls in. segment gradients are calculated in the compute method.
  405. */
  406. this.gradientAtPoint = function(location) {
  407. return segmentGradients[findSegmentForLocation(location)["index"]];
  408. };
  409. /**
  410. * returns the point on the connector's path that is 'distance' along the length of the path from 'location', where
  411. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels. when you consider this concept from the point of view
  412. * of this connector, it starts to become clear that there's a problem with the overlay paint code: given that this connector makes several
  413. * 90 degree turns, it's entirely possible that an arrow overlay could be forced to paint itself around a corner, which would look stupid. this is
  414. * because jsPlumb uses this method (and pointOnPath) so determine the locations of the various points that go to make up an overlay. a better
  415. * solution would probably be to just use pointOnPath along with gradientAtPoint, and draw the overlay so that its axis ran along
  416. * a tangent to the connector. for straight line connectors this would obviously mean the overlay was painted directly on the connector, since a
  417. * tangent to a straight line is the line itself, which is what we want; for this connector, and for beziers, the results would probably be better. an additional
  418. * advantage is, of course, that there's less computation involved doing it that way.
  419. */
  420. this.pointAlongPathFrom = function(location, distance) {
  421. var s = findSegmentForLocation(location), seg = s.segment, p = s.proportion, sl = segmentLengths[s.index], m = segmentGradients[s.index];
  422. var e = {
  423. x : m == Infinity ? seg[2] : seg[2] > seg[0] ? seg[0] + ((1 - p) * sl) - distance : seg[2] + (p * sl) + distance,
  424. y : m == 0 ? seg[3] : seg[3] > seg[1] ? seg[1] + ((1 - p) * sl) - distance : seg[3] + (p * sl) + distance,
  425. segmentInfo : s
  426. };
  427. return e;
  428. };
  429. /**
  430. * calculates a line that is perpendicular to, and centered on, the path at 'distance' pixels from the given location.
  431. * the line is 'length' pixels long.
  432. */
  433. this.perpendicularToPathAt = function(location, length, distance) {
  434. var p = self.pointAlongPathFrom(location, distance);
  435. var m = segmentGradients[p.segmentInfo.index];
  436. var _theta2 = Math.atan(-1 / m);
  437. var y = length / 2 * Math.sin(_theta2);
  438. var x = length / 2 * Math.cos(_theta2);
  439. return [{x:p.x + x, y:p.y + y}, {x:p.x - x, y:p.y - y}];
  440. };
  441. };
  442. // ********************************* END OF CONNECTOR TYPES *******************************************************************
  443. // ********************************* ENDPOINT TYPES *******************************************************************
  444. /**
  445. * Class: Endpoints.Dot
  446. * A round endpoint, with default radius 10 pixels.
  447. */
  448. /**
  449. * Function: Constructor
  450. *
  451. * Parameters:
  452. *
  453. * radius - radius of the endpoint. defaults to 10 pixels.
  454. */
  455. jsPlumb.Endpoints.Dot = function(params) {
  456. var self = this;
  457. params = params || {};
  458. this.radius = params.radius || 10;
  459. this.defaultOffset = 0.5 * this.radius;
  460. this.defaultInnerRadius = this.radius / 3;
  461. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  462. var r = endpointStyle.radius || self.radius;
  463. var x = anchorPoint[0] - r;
  464. var y = anchorPoint[1] - r;
  465. return [ x, y, r * 2, r * 2, r ];
  466. };
  467. };
  468. /**
  469. * Class: Endpoints.Rectangle
  470. * A Rectangular Endpoint, with default size 20x20.
  471. */
  472. /**
  473. * Function: Constructor
  474. *
  475. * Parameters:
  476. *
  477. * width - width of the endpoint. defaults to 20 pixels.
  478. * height - height of the endpoint. defaults to 20 pixels.
  479. */
  480. jsPlumb.Endpoints.Rectangle = function(params) {
  481. var self = this;
  482. params = params || {};
  483. this.width = params.width || 20;
  484. this.height = params.height || 20;
  485. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  486. var width = endpointStyle.width || self.width;
  487. var height = endpointStyle.height || self.height;
  488. var x = anchorPoint[0] - (width/2);
  489. var y = anchorPoint[1] - (height/2);
  490. return [ x, y, width, height];
  491. };
  492. };
  493. /**
  494. * Class: Endpoints.Image
  495. * Draws an image as the Endpoint.
  496. */
  497. /**
  498. * Function: Constructor
  499. *
  500. * Parameters:
  501. *
  502. * src - location of the image to use.
  503. */
  504. jsPlumb.Endpoints.Image = function(params) {
  505. jsPlumb.DOMElementComponent.apply(this, arguments);
  506. var self = this, initialized = false;
  507. this.img = new Image();
  508. self.ready = false;
  509. this.img.onload = function() {
  510. self.ready = true;
  511. };
  512. this.img.src = params.src || params.url;
  513. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  514. self.anchorPoint = anchorPoint;
  515. if (self.ready) return [anchorPoint[0] - self.img.width / 2, anchorPoint[1] - self.img.height/ 2, self.img.width, self.img.height];
  516. else return [0,0,0,0];
  517. };
  518. self.canvas = document.createElement("img"), initialized = false;
  519. self.canvas.style["margin"] = 0;
  520. self.canvas.style["padding"] = 0;
  521. self.canvas.style["outline"] = 0;
  522. self.canvas.style["position"] = "absolute";
  523. self.canvas.className = jsPlumb.endpointClass;
  524. jsPlumb.appendElement(self.canvas, params.parent);
  525. self.attachListeners(self.canvas, self);
  526. var actuallyPaint = function(d, style, anchor) {
  527. if (!initialized) {
  528. self.canvas.setAttribute("src", self.img.src);
  529. initialized = true;
  530. }
  531. var width = self.img.width,
  532. height = self.img.height,
  533. x = self.anchorPoint[0] - (width/2),
  534. y = self.anchorPoint[1] - (height/2);
  535. jsPlumb.sizeCanvas(self.canvas, x, y, width, height);
  536. };
  537. this.paint = function(d, style, anchor) {
  538. if (self.ready) {
  539. actuallyPaint(d, style, anchor);
  540. }
  541. else {
  542. window.setTimeout(function() {
  543. self.paint(d, style, anchor);
  544. }, 200);
  545. }
  546. };
  547. };
  548. /**
  549. * Class: Endpoints.Blank
  550. * An Endpoint that paints nothing on the screen, and cannot be interacted with using the mouse. There are no constructor parameters for this Endpoint.
  551. */
  552. jsPlumb.Endpoints.Blank = function() {
  553. jsPlumb.DOMElementComponent.apply(this, arguments);
  554. this.compute = function() {
  555. return [0,0,0,0];
  556. };
  557. self.canvas = document.createElement("div");
  558. this.paint = function() { };
  559. };
  560. /**
  561. * Class: Endpoints.Triangle
  562. * A triangular Endpoint.
  563. */
  564. /**
  565. * Function: Constructor
  566. *
  567. * Parameters:
  568. *
  569. * width - width of the triangle's base. defaults to 55 pixels.
  570. * height - height of the triangle from base to apex. defaults to 55 pixels.
  571. */
  572. jsPlumb.Endpoints.Triangle = function(params) {
  573. params = params || { };
  574. params.width = params.width || 55;
  575. param.height = params.height || 55;
  576. this.width = params.width;
  577. this.height = params.height;
  578. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  579. var width = endpointStyle.width || self.width;
  580. var height = endpointStyle.height || self.height;
  581. var x = anchorPoint[0] - (width/2);
  582. var y = anchorPoint[1] - (height/2);
  583. return [ x, y, width, height ];
  584. };
  585. };
  586. // ********************************* END OF ENDPOINT TYPES *******************************************************************
  587. // ********************************* OVERLAY DEFINITIONS ***********************************************************************
  588. /**
  589. * Class: Overlays.Arrow
  590. *
  591. * An arrow overlay, defined by four points: the head, the two sides of the tail, and a 'foldback' point at some distance along the length
  592. * of the arrow that lines from each tail point converge into. The foldback point is defined using a decimal that indicates some fraction
  593. * of the length of the arrow and has a default value of 0.623. A foldback point value of 1 would mean that the arrow had a straight line
  594. * across the tail.
  595. */
  596. /**
  597. * Function: Constructor
  598. *
  599. * Parameters:
  600. *
  601. * length - distance in pixels from head to tail baseline. default 20.
  602. * width - width in pixels of the tail baseline. default 20.
  603. * fillStyle - style to use when filling the arrow. defaults to "black".
  604. * strokeStyle - style to use when stroking the arrow. defaults to null, which means the arrow is not stroked.
  605. * lineWidth - line width to use when stroking the arrow. defaults to 1, but only used if strokeStyle is not null.
  606. * foldback - distance (as a decimal from 0 to 1 inclusive) along the length of the arrow marking the point the tail points should fold back to. defaults to 0.623.
  607. * location - distance (as a decimal from 0 to 1 inclusive) marking where the arrow should sit on the connector. defaults to 0.5.
  608. * direction - indicates the direction the arrow points in. valid values are -1 and 1; 1 is default.
  609. */
  610. jsPlumb.Overlays.Arrow = function(params) {
  611. params = params || {};
  612. var self = this;
  613. this.length = params.length || 20;
  614. this.width = params.width || 20;
  615. this.id = params.id;
  616. this.connection = params.connection;
  617. var direction = (params.direction || 1) < 0 ? -1 : 1;
  618. var paintStyle = params.paintStyle || { lineWidth:1 };
  619. this.loc = params.location == null ? 0.5 : params.location;
  620. // how far along the arrow the lines folding back in come to. default is 62.3%.
  621. var foldback = params.foldback || 0.623;
  622. var _getFoldBackPoint = function(connector, loc) {
  623. if (foldback == 0.5) return connector.pointOnPath(loc);
  624. else {
  625. var adj = 0.5 - foldback; // we calculate relative to the center
  626. return connector.pointAlongPathFrom(loc, direction * self.length * adj);
  627. }
  628. };
  629. this.computeMaxSize = function() { return self.width * 1.5; };
  630. this.draw = function(connector, currentConnectionPaintStyle, connectorDimensions) {
  631. // this is the arrow head position
  632. var hxy = connector.pointAlongPathFrom(self.loc, direction * (self.length / 2));
  633. // this is the center of the tail
  634. var txy = connector.pointAlongPathFrom(self.loc, -1 * direction * (self.length / 2)), tx = txy.x, ty = txy.y;
  635. // this is the tail vector
  636. var tail = connector.perpendicularToPathAt(self.loc, self.width, -1 * direction * (self.length / 2));
  637. // this is the point the tail goes in to
  638. var cxy = _getFoldBackPoint(connector, self.loc);
  639. // if loc = 1, then hxy should be flush with the element, or if direction == -1, the tail midpoint.
  640. if (self.loc == 1) {
  641. var lxy = connector.pointOnPath(self.loc);
  642. // TODO determine why the 1.2.6 released version does not
  643. // use 'direction' in the two equations below, yet both
  644. // that and 1.3.0 still paint the arrows correctly.
  645. var dx = (lxy.x - hxy.x) * direction, dy = (lxy.y - hxy.y) * direction;
  646. cxy.x += dx; cxy.y += dy;
  647. txy.x += dx; txy.y += dy;
  648. tail[0].x += dx; tail[0].y += dy;
  649. tail[1].x += dx; tail[1].y += dy;
  650. hxy.x += dx; hxy.y += dy;
  651. }
  652. // if loc = 0, then tail midpoint should be flush with the element, or, if direction == -1, hxy should be.
  653. if (self.loc == 0) {
  654. var lxy = connector.pointOnPath(self.loc);
  655. var tailMid = foldback > 1 ? cxy : {
  656. x:tail[0].x + ((tail[1].x - tail[0].x) / 2),
  657. y:tail[0].y + ((tail[1].y - tail[0].y) / 2)
  658. };
  659. var dx = (lxy.x - tailMid.x) * direction, dy = (lxy.y - tailMid.y) * direction;
  660. cxy.x += dx; cxy.y += dy;
  661. txy.x += dx; txy.y += dy;
  662. tail[0].x += dx; tail[0].y += dy;
  663. tail[1].x += dx; tail[1].y += dy;
  664. hxy.x += dx; hxy.y += dy;
  665. }
  666. var minx = Math.min(hxy.x, tail[0].x, tail[1].x);
  667. var maxx = Math.max(hxy.x, tail[0].x, tail[1].x);
  668. var miny = Math.min(hxy.y, tail[0].y, tail[1].y);
  669. var maxy = Math.max(hxy.y, tail[0].y, tail[1].y);
  670. var d = { hxy:hxy, tail:tail, cxy:cxy },
  671. strokeStyle = paintStyle.strokeStyle || currentConnectionPaintStyle.strokeStyle,
  672. fillStyle = paintStyle.fillStyle || currentConnectionPaintStyle.strokeStyle,
  673. lineWidth = paintStyle.lineWidth || currentConnectionPaintStyle.lineWidth;
  674. self.paint(connector, d, lineWidth, strokeStyle, fillStyle, connectorDimensions);
  675. return [ minx, maxx, miny, maxy];
  676. };
  677. };
  678. /**
  679. * Class: Overlays.PlainArrow
  680. *
  681. * A basic arrow. This is in fact just one instance of the more generic case in which the tail folds back on itself to some
  682. * point along the length of the arrow: in this case, that foldback point is the full length of the arrow. so it just does
  683. * a 'call' to Arrow with foldback set appropriately.
  684. */
  685. /**
  686. * Function: Constructor
  687. * See <Overlays.Arrow> for allowed parameters for this overlay.
  688. */
  689. jsPlumb.Overlays.PlainArrow = function(params) {
  690. params = params || {};
  691. var p = jsPlumb.extend(params, {foldback:1});
  692. jsPlumb.Overlays.Arrow.call(this, p);
  693. };
  694. /**
  695. * Class: Overlays.Diamond
  696. *
  697. * A diamond. Like PlainArrow, this is a concrete case of the more generic case of the tail points converging on some point...it just
  698. * happens that in this case, that point is greater than the length of the the arrow.
  699. *
  700. * this could probably do with some help with positioning...due to the way it reuses the Arrow paint code, what Arrow thinks is the
  701. * center is actually 1/4 of the way along for this guy. but we don't have any knowledge of pixels at this point, so we're kind of
  702. * stuck when it comes to helping out the Arrow class. possibly we could pass in a 'transpose' parameter or something. the value
  703. * would be -l/4 in this case - move along one quarter of the total length.
  704. */
  705. /**
  706. * Function: Constructor
  707. * See <Overlays.Arrow> for allowed parameters for this overlay.
  708. */
  709. jsPlumb.Overlays.Diamond = function(params) {
  710. params = params || {};
  711. var l = params.length || 40;
  712. var p = jsPlumb.extend(params, {length:l/2, foldback:2});
  713. jsPlumb.Overlays.Arrow.call(this, p);
  714. };
  715. /**
  716. * Class: Overlays.Label
  717. * A Label overlay. For all different renderer types (SVG/Canvas/VML), jsPlumb draws a Label overlay as a styled DIV. Version 1.3.0 of jsPlumb
  718. * introduced the ability to set css classes on the label; this is now the preferred way for you to style a label. The 'labelStyle' parameter
  719. * is still supported in 1.3.0 but its usage is deprecated. Under the hood, jsPlumb just turns that object into a bunch of CSS directive that it
  720. * puts on the Label's 'style' attribute, so the end result is the same.
  721. */
  722. /**
  723. * Function: Constructor
  724. *
  725. * Parameters:
  726. * cssClass - optional css class string to append to css class. This string is appended "as-is", so you can of course have multiple classes
  727. * defined. This parameter is preferred to using labelStyle, borderWidth and borderStyle.
  728. * label - the label to paint. May be a string or a function that returns a string. Nothing will be painted if your label is null or your
  729. * label function returns null. empty strings _will_ be painted.
  730. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  731. * labelStyle - (deprecated) js object containing style instructions for the label. defaults to jsPlumb.Defaults.LabelStyle.
  732. * borderWidth - (deprecated) width of a border to paint. defaults to zero.
  733. * borderStyle - (deprecated) strokeStyle to use when painting the border, if necessary.
  734. *
  735. */
  736. jsPlumb.Overlays.Label = function(params) {
  737. jsPlumb.DOMElementComponent.apply(this, arguments);
  738. this.labelStyle = params.labelStyle || jsPlumb.Defaults.LabelStyle;
  739. this.labelStyle.font = this.labelStyle.font || "12px sans-serif";
  740. this.label = params.label;
  741. this.connection = params.connection;
  742. this.id = params.id;
  743. var self = this;
  744. var labelWidth = null, labelHeight = null, labelText = null, labelPadding = null;
  745. this.location = params.location || 0.5;
  746. this.cachedDimensions = null; // setting on 'this' rather than using closures uses a lot less memory. just don't monkey with it!
  747. var initialised = false,
  748. labelText = null,
  749. div = document.createElement("div");
  750. div.style["position"] = "absolute";
  751. div.style["textAlign"] = "center";
  752. div.style["cursor"] = "pointer";
  753. div.style["font"] = self.labelStyle.font;
  754. div.style["color"] = self.labelStyle.color || "black";
  755. if (self.labelStyle.fillStyle) div.style["background"] = self.labelStyle.fillStyle;//_convertStyle(self.labelStyle.fillStyle, true);
  756. if (self.labelStyle.borderWidth > 0) {
  757. var dStyle = self.labelStyle.borderStyle ? self.labelStyle.borderStyle/*_convertStyle(self.labelStyle.borderStyle, true)*/ : "black";
  758. div.style["border"] = self.labelStyle.borderWidth + "px solid " + dStyle;
  759. }
  760. if (self.labelStyle.padding) div.style["padding"] = self.labelStyle.padding;
  761. var clazz = params["_jsPlumb"].overlayClass + " " +
  762. (self.labelStyle.cssClass ? self.labelStyle.cssClass :
  763. params.cssClass ? params.cssClass : "");
  764. div.className = clazz;
  765. jsPlumb.appendElement(div, params.connection.parent);
  766. jsPlumb.getId(div);
  767. self.attachListeners(div, self);
  768. this.paint = function(connector, d, connectorDimensions) {
  769. if (!initialised) {
  770. connector.appendDisplayElement(div);
  771. self.attachListeners(div, connector);
  772. initialised = true;
  773. }
  774. div.style.left = (connectorDimensions[0] + d.minx) + "px";
  775. div.style.top = (connectorDimensions[1] + d.miny) + "px";
  776. };
  777. this.getTextDimensions = function(connector) {
  778. labelText = typeof self.label == 'function' ? self.label(self) : self.label;
  779. div.innerHTML = labelText.replace(/\r\n/g, "<br/>");
  780. var de = jsPlumb.CurrentLibrary.getElementObject(div),
  781. s = jsPlumb.CurrentLibrary.getSize(de);
  782. return {width:s[0], height:s[1]};
  783. };
  784. this.computeMaxSize = function(connector) {
  785. var td = self.getTextDimensions(connector);
  786. return td.width ? Math.max(td.width, td.height) * 1.5 : 0;
  787. };
  788. this.draw = function(connector, currentConnectionPaintStyle, connectorDimensions) {
  789. var td = self.getTextDimensions(connector);
  790. if (td.width != null) {
  791. var cxy = connector.pointOnPath(self.location);
  792. var minx = cxy.x - (td.width / 2);
  793. var miny = cxy.y - (td.height / 2);
  794. self.paint(connector, {
  795. minx:minx,
  796. miny:miny,
  797. td:td,
  798. cxy:cxy
  799. }, connectorDimensions);
  800. return [minx, minx+td.width, miny, miny+td.height];
  801. }
  802. else return [0,0,0,0];
  803. };
  804. };
  805. // ********************************* END OF OVERLAY DEFINITIONS ***********************************************************************
  806. // ********************************* OVERLAY CANVAS RENDERERS***********************************************************************
  807. // ********************************* END OF OVERLAY CANVAS RENDERERS ***********************************************************************
  808. })();