mxEdgeHandler.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxEdgeHandler
  7. *
  8. * Graph event handler that reconnects edges and modifies control points and
  9. * the edge label location. Uses <mxTerminalMarker> for finding and
  10. * highlighting new source and target vertices. This handler is automatically
  11. * created in <mxGraph.createHandler> for each selected edge.
  12. *
  13. * To enable adding/removing control points, the following code can be used:
  14. *
  15. * (code)
  16. * mxEdgeHandler.prototype.addEnabled = true;
  17. * mxEdgeHandler.prototype.removeEnabled = true;
  18. * (end)
  19. *
  20. * Note: This experimental feature is not recommended for production use.
  21. *
  22. * Constructor: mxEdgeHandler
  23. *
  24. * Constructs an edge handler for the specified <mxCellState>.
  25. *
  26. * Parameters:
  27. *
  28. * state - <mxCellState> of the cell to be handled.
  29. */
  30. function mxEdgeHandler(state)
  31. {
  32. if (state != null && state.shape != null)
  33. {
  34. this.state = state;
  35. this.init();
  36. // Handles escape keystrokes
  37. this.escapeHandler = mxUtils.bind(this, function(sender, evt)
  38. {
  39. var dirty = this.index != null;
  40. this.reset();
  41. if (dirty)
  42. {
  43. this.graph.cellRenderer.redraw(this.state, false, state.view.isRendering());
  44. }
  45. });
  46. this.state.view.graph.addListener(mxEvent.ESCAPE, this.escapeHandler);
  47. }
  48. };
  49. /**
  50. * Variable: graph
  51. *
  52. * Reference to the enclosing <mxGraph>.
  53. */
  54. mxEdgeHandler.prototype.graph = null;
  55. /**
  56. * Variable: state
  57. *
  58. * Reference to the <mxCellState> being modified.
  59. */
  60. mxEdgeHandler.prototype.state = null;
  61. /**
  62. * Variable: marker
  63. *
  64. * Holds the <mxTerminalMarker> which is used for highlighting terminals.
  65. */
  66. mxEdgeHandler.prototype.marker = null;
  67. /**
  68. * Variable: constraintHandler
  69. *
  70. * Holds the <mxConstraintHandler> used for drawing and highlighting
  71. * constraints.
  72. */
  73. mxEdgeHandler.prototype.constraintHandler = null;
  74. /**
  75. * Variable: error
  76. *
  77. * Holds the current validation error while a connection is being changed.
  78. */
  79. mxEdgeHandler.prototype.error = null;
  80. /**
  81. * Variable: shape
  82. *
  83. * Holds the <mxShape> that represents the preview edge.
  84. */
  85. mxEdgeHandler.prototype.shape = null;
  86. /**
  87. * Variable: bends
  88. *
  89. * Holds the <mxShapes> that represent the points.
  90. */
  91. mxEdgeHandler.prototype.bends = null;
  92. /**
  93. * Variable: labelShape
  94. *
  95. * Holds the <mxShape> that represents the label position.
  96. */
  97. mxEdgeHandler.prototype.labelShape = null;
  98. /**
  99. * Variable: cloneEnabled
  100. *
  101. * Specifies if cloning by control-drag is enabled. Default is true.
  102. */
  103. mxEdgeHandler.prototype.cloneEnabled = true;
  104. /**
  105. * Variable: addEnabled
  106. *
  107. * Specifies if adding bends by shift-click is enabled. Default is false.
  108. * Note: This experimental feature is not recommended for production use.
  109. */
  110. mxEdgeHandler.prototype.addEnabled = false;
  111. /**
  112. * Variable: removeEnabled
  113. *
  114. * Specifies if removing bends by shift-click is enabled. Default is false.
  115. * Note: This experimental feature is not recommended for production use.
  116. */
  117. mxEdgeHandler.prototype.removeEnabled = false;
  118. /**
  119. * Variable: dblClickRemoveEnabled
  120. *
  121. * Specifies if removing bends by double click is enabled. Default is false.
  122. */
  123. mxEdgeHandler.prototype.dblClickRemoveEnabled = false;
  124. /**
  125. * Variable: mergeRemoveEnabled
  126. *
  127. * Specifies if removing bends by dropping them on other bends is enabled.
  128. * Default is false.
  129. */
  130. mxEdgeHandler.prototype.mergeRemoveEnabled = false;
  131. /**
  132. * Variable: straightRemoveEnabled
  133. *
  134. * Specifies if removing bends by creating straight segments should be enabled.
  135. * If enabled, this can be overridden by holding down the alt key while moving.
  136. * Default is false.
  137. */
  138. mxEdgeHandler.prototype.straightRemoveEnabled = false;
  139. /**
  140. * Variable: virtualBendsEnabled
  141. *
  142. * Specifies if virtual bends should be added in the center of each
  143. * segments. These bends can then be used to add new waypoints.
  144. * Default is false.
  145. */
  146. mxEdgeHandler.prototype.virtualBendsEnabled = false;
  147. /**
  148. * Variable: virtualBendOpacity
  149. *
  150. * Opacity to be used for virtual bends (see <virtualBendsEnabled>).
  151. * Default is 20.
  152. */
  153. mxEdgeHandler.prototype.virtualBendOpacity = 20;
  154. /**
  155. * Variable: parentHighlightEnabled
  156. *
  157. * Specifies if the parent should be highlighted if a child cell is selected.
  158. * Default is false.
  159. */
  160. mxEdgeHandler.prototype.parentHighlightEnabled = false;
  161. /**
  162. * Variable: preferHtml
  163. *
  164. * Specifies if bends should be added to the graph container. This is updated
  165. * in <init> based on whether the edge or one of its terminals has an HTML
  166. * label in the container.
  167. */
  168. mxEdgeHandler.prototype.preferHtml = false;
  169. /**
  170. * Variable: allowHandleBoundsCheck
  171. *
  172. * Specifies if the bounds of handles should be used for hit-detection in IE
  173. * Default is true.
  174. */
  175. mxEdgeHandler.prototype.allowHandleBoundsCheck = true;
  176. /**
  177. * Variable: snapToTerminals
  178. *
  179. * Specifies if waypoints should snap to the routing centers of terminals.
  180. * Default is false.
  181. */
  182. mxEdgeHandler.prototype.snapToTerminals = false;
  183. /**
  184. * Variable: handleImage
  185. *
  186. * Optional <mxImage> to be used as handles. Default is null.
  187. */
  188. mxEdgeHandler.prototype.handleImage = null;
  189. /**
  190. * Variable: tolerance
  191. *
  192. * Optional tolerance for hit-detection in <getHandleForEvent>. Default is 0.
  193. */
  194. mxEdgeHandler.prototype.tolerance = 0;
  195. /**
  196. * Variable: outlineConnect
  197. *
  198. * Specifies if connections to the outline of a highlighted target should be
  199. * enabled. This will allow to place the connection point along the outline of
  200. * the highlighted target. Default is false.
  201. */
  202. mxEdgeHandler.prototype.outlineConnect = false;
  203. /**
  204. * Variable: manageLabelHandle
  205. *
  206. * Specifies if the label handle should be moved if it intersects with another
  207. * handle. Uses <checkLabelHandle> for checking and moving. Default is false.
  208. */
  209. mxEdgeHandler.prototype.manageLabelHandle = false;
  210. /**
  211. * Function: init
  212. *
  213. * Initializes the shapes required for this edge handler.
  214. */
  215. mxEdgeHandler.prototype.init = function()
  216. {
  217. this.graph = this.state.view.graph;
  218. this.marker = this.createMarker();
  219. this.constraintHandler = new mxConstraintHandler(this.graph);
  220. // Clones the original points from the cell
  221. // and makes sure at least one point exists
  222. this.points = [];
  223. // Uses the absolute points of the state
  224. // for the initial configuration and preview
  225. this.abspoints = this.getSelectionPoints(this.state);
  226. this.shape = this.createSelectionShape(this.abspoints);
  227. this.shape.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
  228. mxConstants.DIALECT_MIXEDHTML : mxConstants.DIALECT_SVG;
  229. this.shape.init(this.graph.getView().getOverlayPane());
  230. this.shape.pointerEvents = false;
  231. this.shape.setCursor(mxConstants.CURSOR_MOVABLE_EDGE);
  232. mxEvent.redirectMouseEvents(this.shape.node, this.graph, this.state);
  233. // Updates preferHtml
  234. this.preferHtml = this.state.text != null &&
  235. this.state.text.node.parentNode == this.graph.container;
  236. if (!this.preferHtml)
  237. {
  238. // Checks source terminal
  239. var sourceState = this.state.getVisibleTerminalState(true);
  240. if (sourceState != null)
  241. {
  242. this.preferHtml = sourceState.text != null &&
  243. sourceState.text.node.parentNode == this.graph.container;
  244. }
  245. if (!this.preferHtml)
  246. {
  247. // Checks target terminal
  248. var targetState = this.state.getVisibleTerminalState(false);
  249. if (targetState != null)
  250. {
  251. this.preferHtml = targetState.text != null &&
  252. targetState.text.node.parentNode == this.graph.container;
  253. }
  254. }
  255. }
  256. // Creates bends for the non-routed absolute points
  257. // or bends that don't correspond to points
  258. if (this.graph.getSelectionCount() < mxGraphHandler.prototype.maxCells ||
  259. mxGraphHandler.prototype.maxCells <= 0)
  260. {
  261. this.bends = this.createBends();
  262. if (this.isVirtualBendsEnabled())
  263. {
  264. this.virtualBends = this.createVirtualBends();
  265. }
  266. }
  267. // Adds a rectangular handle for the label position
  268. this.label = new mxPoint(this.state.absoluteOffset.x, this.state.absoluteOffset.y);
  269. this.labelShape = this.createLabelHandleShape();
  270. this.initBend(this.labelShape);
  271. this.labelShape.setCursor(mxConstants.CURSOR_LABEL_HANDLE);
  272. this.customHandles = this.createCustomHandles();
  273. this.updateParentHighlight();
  274. this.redraw();
  275. };
  276. /**
  277. * Function: isParentHighlightVisible
  278. *
  279. * Returns true if the parent highlight should be visible. This implementation
  280. * always returns true.
  281. */
  282. mxEdgeHandler.prototype.isParentHighlightVisible = mxVertexHandler.prototype.isParentHighlightVisible;
  283. /**
  284. * Function: updateParentHighlight
  285. *
  286. * Updates the highlight of the parent if <parentHighlightEnabled> is true.
  287. */
  288. mxEdgeHandler.prototype.updateParentHighlight = mxVertexHandler.prototype.updateParentHighlight;
  289. /**
  290. * Function: createCustomHandles
  291. *
  292. * Returns an array of custom handles. This implementation returns null.
  293. */
  294. mxEdgeHandler.prototype.createCustomHandles = function()
  295. {
  296. return null;
  297. };
  298. /**
  299. * Function: isVirtualBendsEnabled
  300. *
  301. * Returns true if virtual bends should be added. This returns true if
  302. * <virtualBendsEnabled> is true and the current style allows and
  303. * renders custom waypoints.
  304. */
  305. mxEdgeHandler.prototype.isVirtualBendsEnabled = function(evt)
  306. {
  307. return this.virtualBendsEnabled && (this.state.style[mxConstants.STYLE_EDGE] == null ||
  308. this.state.style[mxConstants.STYLE_EDGE] == mxConstants.NONE ||
  309. this.state.style[mxConstants.STYLE_NOEDGESTYLE] == 1) &&
  310. mxUtils.getValue(this.state.style, mxConstants.STYLE_SHAPE, null) != 'arrow';
  311. };
  312. /**
  313. * Function: isCellEnabled
  314. *
  315. * Returns true if the given cell allows new connections to be created. This implementation
  316. * always returns true.
  317. */
  318. mxEdgeHandler.prototype.isCellEnabled = function(cell)
  319. {
  320. return true;
  321. };
  322. /**
  323. * Function: isAddPointEvent
  324. *
  325. * Returns true if the given event is a trigger to add a new point. This
  326. * implementation returns true if shift is pressed.
  327. */
  328. mxEdgeHandler.prototype.isAddPointEvent = function(evt)
  329. {
  330. return mxEvent.isShiftDown(evt);
  331. };
  332. /**
  333. * Function: isRemovePointEvent
  334. *
  335. * Returns true if the given event is a trigger to remove a point. This
  336. * implementation returns true if shift is pressed.
  337. */
  338. mxEdgeHandler.prototype.isRemovePointEvent = function(evt)
  339. {
  340. return mxEvent.isShiftDown(evt);
  341. };
  342. /**
  343. * Function: getSelectionPoints
  344. *
  345. * Returns the list of points that defines the selection stroke.
  346. */
  347. mxEdgeHandler.prototype.getSelectionPoints = function(state)
  348. {
  349. return state.absolutePoints;
  350. };
  351. /**
  352. * Function: createParentHighlightShape
  353. *
  354. * Creates the shape used to draw the selection border.
  355. */
  356. mxEdgeHandler.prototype.createParentHighlightShape = function(bounds)
  357. {
  358. var shape = new mxRectangleShape(mxRectangle.fromRectangle(bounds),
  359. null, this.getSelectionColor());
  360. shape.strokewidth = this.getSelectionStrokeWidth();
  361. shape.isDashed = this.isSelectionDashed();
  362. return shape;
  363. };
  364. /**
  365. * Function: createSelectionShape
  366. *
  367. * Creates the shape used to draw the selection border.
  368. */
  369. mxEdgeHandler.prototype.createSelectionShape = function(points)
  370. {
  371. var shape = new this.state.shape.constructor();
  372. shape.outline = true;
  373. shape.apply(this.state);
  374. shape.isDashed = this.isSelectionDashed();
  375. shape.stroke = this.getSelectionColor();
  376. shape.isShadow = false;
  377. return shape;
  378. };
  379. /**
  380. * Function: getSelectionColor
  381. *
  382. * Returns <mxConstants.EDGE_SELECTION_COLOR>.
  383. */
  384. mxEdgeHandler.prototype.getSelectionColor = function()
  385. {
  386. return mxConstants.EDGE_SELECTION_COLOR;
  387. };
  388. /**
  389. * Function: getSelectionStrokeWidth
  390. *
  391. * Returns <mxConstants.EDGE_SELECTION_STROKEWIDTH>.
  392. */
  393. mxEdgeHandler.prototype.getSelectionStrokeWidth = function()
  394. {
  395. return mxConstants.EDGE_SELECTION_STROKEWIDTH;
  396. };
  397. /**
  398. * Function: isSelectionDashed
  399. *
  400. * Returns <mxConstants.EDGE_SELECTION_DASHED>.
  401. */
  402. mxEdgeHandler.prototype.isSelectionDashed = function()
  403. {
  404. return mxConstants.EDGE_SELECTION_DASHED;
  405. };
  406. /**
  407. * Function: isConnectableCell
  408. *
  409. * Returns true if the given cell is connectable. This is a hook to
  410. * disable floating connections. This implementation returns true.
  411. */
  412. mxEdgeHandler.prototype.isConnectableCell = function(cell)
  413. {
  414. return true;
  415. };
  416. /**
  417. * Function: getCellAt
  418. *
  419. * Creates and returns the <mxCellMarker> used in <marker>.
  420. */
  421. mxEdgeHandler.prototype.getCellAt = function(x, y)
  422. {
  423. return (!this.outlineConnect) ? this.graph.getCellAt(x, y) : null;
  424. };
  425. /**
  426. * Function: createMarker
  427. *
  428. * Creates and returns the <mxCellMarker> used in <marker>.
  429. */
  430. mxEdgeHandler.prototype.createMarker = function()
  431. {
  432. var marker = new mxCellMarker(this.graph);
  433. var self = this; // closure
  434. // Only returns edges if they are connectable and never returns
  435. // the edge that is currently being modified
  436. marker.getCell = function(me)
  437. {
  438. var cell = mxCellMarker.prototype.getCell.apply(this, arguments);
  439. // Checks for cell at preview point (with grid)
  440. if ((cell == self.state.cell || cell == null) && self.currentPoint != null)
  441. {
  442. cell = self.graph.getCellAt(self.currentPoint.x, self.currentPoint.y);
  443. }
  444. // Uses connectable parent vertex if one exists
  445. if (cell != null && !this.graph.isCellConnectable(cell))
  446. {
  447. var parent = this.graph.getModel().getParent(cell);
  448. if (this.graph.getModel().isVertex(parent) && this.graph.isCellConnectable(parent))
  449. {
  450. cell = parent;
  451. }
  452. }
  453. var model = self.graph.getModel();
  454. if ((this.graph.isSwimlane(cell) && self.currentPoint != null &&
  455. this.graph.hitsSwimlaneContent(cell, self.currentPoint.x, self.currentPoint.y)) ||
  456. (!self.isConnectableCell(cell)) || (cell == self.state.cell ||
  457. (cell != null && !self.graph.connectableEdges && model.isEdge(cell))) ||
  458. model.isAncestor(self.state.cell, cell))
  459. {
  460. cell = null;
  461. }
  462. if (!this.graph.isCellConnectable(cell))
  463. {
  464. cell = null;
  465. }
  466. return cell;
  467. };
  468. // Sets the highlight color according to validateConnection
  469. marker.isValidState = function(state)
  470. {
  471. var model = self.graph.getModel();
  472. var other = self.graph.view.getTerminalPort(state,
  473. self.graph.view.getState(model.getTerminal(self.state.cell,
  474. !self.isSource)), !self.isSource);
  475. var otherCell = (other != null) ? other.cell : null;
  476. var source = (self.isSource) ? state.cell : otherCell;
  477. var target = (self.isSource) ? otherCell : state.cell;
  478. // Updates the error message of the handler
  479. self.error = self.validateConnection(source, target);
  480. return self.error == null;
  481. };
  482. return marker;
  483. };
  484. /**
  485. * Function: validateConnection
  486. *
  487. * Returns the error message or an empty string if the connection for the
  488. * given source, target pair is not valid. Otherwise it returns null. This
  489. * implementation uses <mxGraph.getEdgeValidationError>.
  490. *
  491. * Parameters:
  492. *
  493. * source - <mxCell> that represents the source terminal.
  494. * target - <mxCell> that represents the target terminal.
  495. */
  496. mxEdgeHandler.prototype.validateConnection = function(source, target)
  497. {
  498. return this.graph.getEdgeValidationError(this.state.cell, source, target);
  499. };
  500. /**
  501. * Function: createBends
  502. *
  503. * Creates and returns the bends used for modifying the edge. This is
  504. * typically an array of <mxRectangleShapes>.
  505. */
  506. mxEdgeHandler.prototype.createBends = function()
  507. {
  508. var cell = this.state.cell;
  509. var bends = [];
  510. for (var i = 0; i < this.abspoints.length; i++)
  511. {
  512. if (this.isHandleVisible(i))
  513. {
  514. var source = i == 0;
  515. var target = i == this.abspoints.length - 1;
  516. var terminal = source || target;
  517. if (terminal || this.graph.isCellBendable(cell))
  518. {
  519. (mxUtils.bind(this, function(index)
  520. {
  521. var bend = this.createHandleShape(index);
  522. this.initBend(bend, mxUtils.bind(this, mxUtils.bind(this, function()
  523. {
  524. if (this.dblClickRemoveEnabled)
  525. {
  526. this.removePoint(this.state, index);
  527. }
  528. })));
  529. if (this.isHandleEnabled(i))
  530. {
  531. bend.setCursor((terminal) ? mxConstants.CURSOR_TERMINAL_HANDLE : mxConstants.CURSOR_BEND_HANDLE);
  532. }
  533. bends.push(bend);
  534. if (!terminal)
  535. {
  536. this.points.push(new mxPoint(0,0));
  537. bend.node.style.visibility = 'hidden';
  538. }
  539. }))(i);
  540. }
  541. }
  542. }
  543. return bends;
  544. };
  545. /**
  546. * Function: createVirtualBends
  547. *
  548. * Creates and returns the bends used for modifying the edge. This is
  549. * typically an array of <mxRectangleShapes>.
  550. */
  551. mxEdgeHandler.prototype.createVirtualBends = function()
  552. {
  553. var cell = this.state.cell;
  554. var last = this.abspoints[0];
  555. var bends = [];
  556. if (this.graph.isCellBendable(cell))
  557. {
  558. for (var i = 1; i < this.abspoints.length; i++)
  559. {
  560. (mxUtils.bind(this, function(bend)
  561. {
  562. this.initBend(bend);
  563. bend.setCursor(mxConstants.CURSOR_VIRTUAL_BEND_HANDLE);
  564. bends.push(bend);
  565. }))(this.createHandleShape());
  566. }
  567. }
  568. return bends;
  569. };
  570. /**
  571. * Function: isHandleEnabled
  572. *
  573. * Creates the shape used to display the given bend.
  574. */
  575. mxEdgeHandler.prototype.isHandleEnabled = function(index)
  576. {
  577. return true;
  578. };
  579. /**
  580. * Function: isHandleVisible
  581. *
  582. * Returns true if the handle at the given index is visible.
  583. */
  584. mxEdgeHandler.prototype.isHandleVisible = function(index)
  585. {
  586. var source = this.state.getVisibleTerminalState(true);
  587. var target = this.state.getVisibleTerminalState(false);
  588. var geo = this.graph.getCellGeometry(this.state.cell);
  589. var edgeStyle = (geo != null) ? this.graph.view.getEdgeStyle(this.state, geo.points, source, target) : null;
  590. return edgeStyle != mxEdgeStyle.EntityRelation || index == 0 || index == this.abspoints.length - 1;
  591. };
  592. /**
  593. * Function: createHandleShape
  594. *
  595. * Creates the shape used to display the given bend. Note that the index may be
  596. * null for special cases, such as when called from
  597. * <mxElbowEdgeHandler.createVirtualBend>. Only images and rectangles should be
  598. * returned if support for HTML labels with not foreign objects is required.
  599. * Index if null for virtual handles.
  600. */
  601. mxEdgeHandler.prototype.createHandleShape = function(index)
  602. {
  603. if (this.handleImage != null)
  604. {
  605. var shape = new mxImageShape(new mxRectangle(0, 0, this.handleImage.width, this.handleImage.height), this.handleImage.src);
  606. // Allows HTML rendering of the images
  607. shape.preserveImageAspect = false;
  608. return shape;
  609. }
  610. else
  611. {
  612. var s = mxConstants.HANDLE_SIZE;
  613. if (this.preferHtml)
  614. {
  615. s -= 1;
  616. }
  617. return new mxRectangleShape(new mxRectangle(0, 0, s, s), mxConstants.HANDLE_FILLCOLOR, mxConstants.HANDLE_STROKECOLOR);
  618. }
  619. };
  620. /**
  621. * Function: createLabelHandleShape
  622. *
  623. * Creates the shape used to display the the label handle.
  624. */
  625. mxEdgeHandler.prototype.createLabelHandleShape = function()
  626. {
  627. if (this.labelHandleImage != null)
  628. {
  629. var shape = new mxImageShape(new mxRectangle(0, 0, this.labelHandleImage.width, this.labelHandleImage.height), this.labelHandleImage.src);
  630. // Allows HTML rendering of the images
  631. shape.preserveImageAspect = false;
  632. return shape;
  633. }
  634. else
  635. {
  636. var s = mxConstants.LABEL_HANDLE_SIZE;
  637. return new mxRectangleShape(new mxRectangle(0, 0, s, s), mxConstants.LABEL_HANDLE_FILLCOLOR, mxConstants.HANDLE_STROKECOLOR);
  638. }
  639. };
  640. /**
  641. * Function: initBend
  642. *
  643. * Helper method to initialize the given bend.
  644. *
  645. * Parameters:
  646. *
  647. * bend - <mxShape> that represents the bend to be initialized.
  648. */
  649. mxEdgeHandler.prototype.initBend = function(bend, dblClick)
  650. {
  651. if (this.preferHtml)
  652. {
  653. bend.dialect = mxConstants.DIALECT_STRICTHTML;
  654. bend.init(this.graph.container);
  655. }
  656. else
  657. {
  658. bend.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
  659. mxConstants.DIALECT_MIXEDHTML : mxConstants.DIALECT_SVG;
  660. bend.init(this.graph.getView().getOverlayPane());
  661. }
  662. mxEvent.redirectMouseEvents(bend.node, this.graph, this.state,
  663. null, null, null, dblClick);
  664. // Fixes lost event tracking for images in quirks / IE8 standards
  665. if (mxClient.IS_QUIRKS || document.documentMode == 8)
  666. {
  667. mxEvent.addListener(bend.node, 'dragstart', function(evt)
  668. {
  669. mxEvent.consume(evt);
  670. return false;
  671. });
  672. }
  673. if (mxClient.IS_TOUCH)
  674. {
  675. bend.node.setAttribute('pointer-events', 'none');
  676. }
  677. };
  678. /**
  679. * Function: getHandleForEvent
  680. *
  681. * Returns the index of the handle for the given event.
  682. */
  683. mxEdgeHandler.prototype.getHandleForEvent = function(me)
  684. {
  685. var result = null;
  686. if (this.state != null)
  687. {
  688. // Connection highlight may consume events before they reach sizer handle
  689. var tol = (!mxEvent.isMouseEvent(me.getEvent())) ? this.tolerance : 1;
  690. var hit = (this.allowHandleBoundsCheck && (mxClient.IS_IE || tol > 0)) ?
  691. new mxRectangle(me.getGraphX() - tol, me.getGraphY() - tol, 2 * tol, 2 * tol) : null;
  692. var minDistSq = null;
  693. function checkShape(shape)
  694. {
  695. if (shape != null && shape.node != null && shape.node.style.display != 'none' &&
  696. shape.node.style.visibility != 'hidden' &&
  697. (me.isSource(shape) || (hit != null && mxUtils.intersects(shape.bounds, hit))))
  698. {
  699. var dx = me.getGraphX() - shape.bounds.getCenterX();
  700. var dy = me.getGraphY() - shape.bounds.getCenterY();
  701. var tmp = dx * dx + dy * dy;
  702. if (minDistSq == null || tmp <= minDistSq)
  703. {
  704. minDistSq = tmp;
  705. return true;
  706. }
  707. }
  708. return false;
  709. }
  710. if (this.customHandles != null && this.isCustomHandleEvent(me))
  711. {
  712. // Inverse loop order to match display order
  713. for (var i = this.customHandles.length - 1; i >= 0; i--)
  714. {
  715. if (checkShape(this.customHandles[i].shape))
  716. {
  717. // LATER: Return reference to active shape
  718. return mxEvent.CUSTOM_HANDLE - i;
  719. }
  720. }
  721. }
  722. if (me.isSource(this.state.text) || checkShape(this.labelShape))
  723. {
  724. result = mxEvent.LABEL_HANDLE;
  725. }
  726. if (this.bends != null)
  727. {
  728. for (var i = 0; i < this.bends.length; i++)
  729. {
  730. if (checkShape(this.bends[i]))
  731. {
  732. result = i;
  733. }
  734. }
  735. }
  736. if (this.virtualBends != null && this.isAddVirtualBendEvent(me))
  737. {
  738. for (var i = 0; i < this.virtualBends.length; i++)
  739. {
  740. if (checkShape(this.virtualBends[i]))
  741. {
  742. result = mxEvent.VIRTUAL_HANDLE - i;
  743. }
  744. }
  745. }
  746. }
  747. return result;
  748. };
  749. /**
  750. * Function: isAddVirtualBendEvent
  751. *
  752. * Returns true if the given event allows virtual bends to be added. This
  753. * implementation returns true.
  754. */
  755. mxEdgeHandler.prototype.isAddVirtualBendEvent = function(me)
  756. {
  757. return true;
  758. };
  759. /**
  760. * Function: isCustomHandleEvent
  761. *
  762. * Returns true if the given event allows custom handles to be changed. This
  763. * implementation returns true.
  764. */
  765. mxEdgeHandler.prototype.isCustomHandleEvent = function(me)
  766. {
  767. return true;
  768. };
  769. /**
  770. * Function: mouseDown
  771. *
  772. * Handles the event by checking if a special element of the handler
  773. * was clicked, in which case the index parameter is non-null. The
  774. * indices may be one of <LABEL_HANDLE> or the number of the respective
  775. * control point. The source and target points are used for reconnecting
  776. * the edge.
  777. */
  778. mxEdgeHandler.prototype.mouseDown = function(sender, me)
  779. {
  780. var handle = this.getHandleForEvent(me);
  781. if (this.bends != null && this.bends[handle] != null)
  782. {
  783. var b = this.bends[handle].bounds;
  784. this.snapPoint = new mxPoint(b.getCenterX(), b.getCenterY());
  785. }
  786. if (this.addEnabled && handle == null && this.isAddPointEvent(me.getEvent()))
  787. {
  788. this.addPoint(this.state, me.getEvent());
  789. me.consume();
  790. }
  791. else if (handle != null && !me.isConsumed() && this.graph.isEnabled())
  792. {
  793. if (this.removeEnabled && this.isRemovePointEvent(me.getEvent()))
  794. {
  795. this.removePoint(this.state, handle);
  796. }
  797. else if (handle != mxEvent.LABEL_HANDLE || this.graph.isLabelMovable(me.getCell()))
  798. {
  799. if (handle <= mxEvent.VIRTUAL_HANDLE)
  800. {
  801. mxUtils.setOpacity(this.virtualBends[mxEvent.VIRTUAL_HANDLE - handle].node, 100);
  802. }
  803. this.start(me.getX(), me.getY(), handle);
  804. }
  805. me.consume();
  806. }
  807. };
  808. /**
  809. * Function: start
  810. *
  811. * Starts the handling of the mouse gesture.
  812. */
  813. mxEdgeHandler.prototype.start = function(x, y, index)
  814. {
  815. this.startX = x;
  816. this.startY = y;
  817. this.isSource = (this.bends == null) ? false : index == 0;
  818. this.isTarget = (this.bends == null) ? false : index == this.bends.length - 1;
  819. this.isLabel = index == mxEvent.LABEL_HANDLE;
  820. if (this.isSource || this.isTarget)
  821. {
  822. var cell = this.state.cell;
  823. var terminal = this.graph.model.getTerminal(cell, this.isSource);
  824. if ((terminal == null && this.graph.isTerminalPointMovable(cell, this.isSource)) ||
  825. (terminal != null && this.graph.isCellDisconnectable(cell, terminal, this.isSource)))
  826. {
  827. this.index = index;
  828. }
  829. }
  830. else
  831. {
  832. this.index = index;
  833. }
  834. // Hides other custom handles
  835. if (this.index <= mxEvent.CUSTOM_HANDLE && this.index > mxEvent.VIRTUAL_HANDLE)
  836. {
  837. if (this.customHandles != null)
  838. {
  839. for (var i = 0; i < this.customHandles.length; i++)
  840. {
  841. if (i != mxEvent.CUSTOM_HANDLE - this.index)
  842. {
  843. this.customHandles[i].setVisible(false);
  844. }
  845. }
  846. }
  847. }
  848. };
  849. /**
  850. * Function: clonePreviewState
  851. *
  852. * Returns a clone of the current preview state for the given point and terminal.
  853. */
  854. mxEdgeHandler.prototype.clonePreviewState = function(point, terminal)
  855. {
  856. return this.state.clone();
  857. };
  858. /**
  859. * Function: getSnapToTerminalTolerance
  860. *
  861. * Returns the tolerance for the guides. Default value is
  862. * gridSize * scale / 2.
  863. */
  864. mxEdgeHandler.prototype.getSnapToTerminalTolerance = function()
  865. {
  866. return this.graph.gridSize * this.graph.view.scale / 2;
  867. };
  868. /**
  869. * Function: updateHint
  870. *
  871. * Hook for subclassers do show details while the handler is active.
  872. */
  873. mxEdgeHandler.prototype.updateHint = function(me, point) { };
  874. /**
  875. * Function: removeHint
  876. *
  877. * Hooks for subclassers to hide details when the handler gets inactive.
  878. */
  879. mxEdgeHandler.prototype.removeHint = function() { };
  880. /**
  881. * Function: roundLength
  882. *
  883. * Hook for rounding the unscaled width or height. This uses Math.round.
  884. */
  885. mxEdgeHandler.prototype.roundLength = function(length)
  886. {
  887. return Math.round(length);
  888. };
  889. /**
  890. * Function: isSnapToTerminalsEvent
  891. *
  892. * Returns true if <snapToTerminals> is true and if alt is not pressed.
  893. */
  894. mxEdgeHandler.prototype.isSnapToTerminalsEvent = function(me)
  895. {
  896. return this.snapToTerminals && !mxEvent.isAltDown(me.getEvent());
  897. };
  898. /**
  899. * Function: getPointForEvent
  900. *
  901. * Returns the point for the given event.
  902. */
  903. mxEdgeHandler.prototype.getPointForEvent = function(me)
  904. {
  905. var view = this.graph.getView();
  906. var scale = view.scale;
  907. var point = new mxPoint(this.roundLength(me.getGraphX() / scale) * scale,
  908. this.roundLength(me.getGraphY() / scale) * scale);
  909. var tt = this.getSnapToTerminalTolerance();
  910. var overrideX = false;
  911. var overrideY = false;
  912. if (tt > 0 && this.isSnapToTerminalsEvent(me))
  913. {
  914. function snapToPoint(pt)
  915. {
  916. if (pt != null)
  917. {
  918. var x = pt.x;
  919. if (Math.abs(point.x - x) < tt)
  920. {
  921. point.x = x;
  922. overrideX = true;
  923. }
  924. var y = pt.y;
  925. if (Math.abs(point.y - y) < tt)
  926. {
  927. point.y = y;
  928. overrideY = true;
  929. }
  930. }
  931. }
  932. // Temporary function
  933. function snapToTerminal(terminal)
  934. {
  935. if (terminal != null)
  936. {
  937. snapToPoint.call(this, new mxPoint(view.getRoutingCenterX(terminal),
  938. view.getRoutingCenterY(terminal)));
  939. }
  940. };
  941. snapToTerminal.call(this, this.state.getVisibleTerminalState(true));
  942. snapToTerminal.call(this, this.state.getVisibleTerminalState(false));
  943. if (this.state.absolutePoints != null)
  944. {
  945. for (var i = 0; i < this.state.absolutePoints.length; i++)
  946. {
  947. snapToPoint.call(this, this.state.absolutePoints[i]);
  948. }
  949. }
  950. }
  951. if (this.graph.isGridEnabledEvent(me.getEvent()))
  952. {
  953. var tr = view.translate;
  954. if (!overrideX)
  955. {
  956. point.x = (this.graph.snap(point.x / scale - tr.x) + tr.x) * scale;
  957. }
  958. if (!overrideY)
  959. {
  960. point.y = (this.graph.snap(point.y / scale - tr.y) + tr.y) * scale;
  961. }
  962. }
  963. return point;
  964. };
  965. /**
  966. * Function: getPreviewTerminalState
  967. *
  968. * Updates the given preview state taking into account the state of the constraint handler.
  969. */
  970. mxEdgeHandler.prototype.getPreviewTerminalState = function(me)
  971. {
  972. this.constraintHandler.update(me, this.isSource, true, me.isSource(this.marker.highlight.shape) ? null : this.currentPoint);
  973. if (this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null)
  974. {
  975. // Handles special case where grid is large and connection point is at actual point in which
  976. // case the outline is not followed as long as we're < gridSize / 2 away from that point
  977. if (this.marker.highlight != null && this.marker.highlight.state != null &&
  978. this.marker.highlight.state.cell == this.constraintHandler.currentFocus.cell)
  979. {
  980. // Direct repaint needed if cell already highlighted
  981. if (this.marker.highlight.shape.stroke != 'transparent')
  982. {
  983. this.marker.highlight.shape.stroke = 'transparent';
  984. this.marker.highlight.repaint();
  985. }
  986. }
  987. else
  988. {
  989. this.marker.markCell(this.constraintHandler.currentFocus.cell, 'transparent');
  990. }
  991. var model = this.graph.getModel();
  992. var other = this.graph.view.getTerminalPort(this.state,
  993. this.graph.view.getState(model.getTerminal(this.state.cell,
  994. !this.isSource)), !this.isSource);
  995. var otherCell = (other != null) ? other.cell : null;
  996. var source = (this.isSource) ? this.constraintHandler.currentFocus.cell : otherCell;
  997. var target = (this.isSource) ? otherCell : this.constraintHandler.currentFocus.cell;
  998. // Updates the error message of the handler
  999. this.error = this.validateConnection(source, target);
  1000. var result = null;
  1001. if (this.error == null)
  1002. {
  1003. result = this.constraintHandler.currentFocus;
  1004. }
  1005. if (this.error != null || (result != null &&
  1006. !this.isCellEnabled(result.cell)))
  1007. {
  1008. this.constraintHandler.reset();
  1009. }
  1010. return result;
  1011. }
  1012. else if (!this.graph.isIgnoreTerminalEvent(me.getEvent()))
  1013. {
  1014. this.marker.process(me);
  1015. var state = this.marker.getValidState();
  1016. if (state != null && !this.isCellEnabled(state.cell))
  1017. {
  1018. this.constraintHandler.reset();
  1019. this.marker.reset();
  1020. }
  1021. return this.marker.getValidState();
  1022. }
  1023. else
  1024. {
  1025. this.marker.reset();
  1026. return null;
  1027. }
  1028. };
  1029. /**
  1030. * Function: getPreviewPoints
  1031. *
  1032. * Updates the given preview state taking into account the state of the constraint handler.
  1033. *
  1034. * Parameters:
  1035. *
  1036. * pt - <mxPoint> that contains the current pointer position.
  1037. * me - Optional <mxMouseEvent> that contains the current event.
  1038. */
  1039. mxEdgeHandler.prototype.getPreviewPoints = function(pt, me)
  1040. {
  1041. var geometry = this.graph.getCellGeometry(this.state.cell);
  1042. var points = (geometry.points != null) ? geometry.points.slice() : null;
  1043. var point = new mxPoint(pt.x, pt.y);
  1044. var result = null;
  1045. if (!this.isSource && !this.isTarget)
  1046. {
  1047. this.convertPoint(point, false);
  1048. if (points == null)
  1049. {
  1050. points = [point];
  1051. }
  1052. else
  1053. {
  1054. // Adds point from virtual bend
  1055. if (this.index <= mxEvent.VIRTUAL_HANDLE)
  1056. {
  1057. points.splice(mxEvent.VIRTUAL_HANDLE - this.index, 0, point);
  1058. }
  1059. // Removes point if dragged on terminal point
  1060. if (!this.isSource && !this.isTarget)
  1061. {
  1062. for (var i = 0; i < this.bends.length; i++)
  1063. {
  1064. if (i != this.index)
  1065. {
  1066. var bend = this.bends[i];
  1067. if (bend != null && mxUtils.contains(bend.bounds, pt.x, pt.y))
  1068. {
  1069. if (this.index <= mxEvent.VIRTUAL_HANDLE)
  1070. {
  1071. points.splice(mxEvent.VIRTUAL_HANDLE - this.index, 1);
  1072. }
  1073. else
  1074. {
  1075. points.splice(this.index - 1, 1);
  1076. }
  1077. result = points;
  1078. }
  1079. }
  1080. }
  1081. // Removes point if user tries to straighten a segment
  1082. if (result == null && this.straightRemoveEnabled && (me == null || !mxEvent.isAltDown(me.getEvent())))
  1083. {
  1084. var tol = this.graph.tolerance * this.graph.tolerance;
  1085. var abs = this.state.absolutePoints.slice();
  1086. abs[this.index] = pt;
  1087. // Handes special case where removing waypoint affects tolerance (flickering)
  1088. var src = this.state.getVisibleTerminalState(true);
  1089. if (src != null)
  1090. {
  1091. var c = this.graph.getConnectionConstraint(this.state, src, true);
  1092. // Checks if point is not fixed
  1093. if (c == null || this.graph.getConnectionPoint(src, c) == null)
  1094. {
  1095. abs[0] = new mxPoint(src.view.getRoutingCenterX(src), src.view.getRoutingCenterY(src));
  1096. }
  1097. }
  1098. var trg = this.state.getVisibleTerminalState(false);
  1099. if (trg != null)
  1100. {
  1101. var c = this.graph.getConnectionConstraint(this.state, trg, false);
  1102. // Checks if point is not fixed
  1103. if (c == null || this.graph.getConnectionPoint(trg, c) == null)
  1104. {
  1105. abs[abs.length - 1] = new mxPoint(trg.view.getRoutingCenterX(trg), trg.view.getRoutingCenterY(trg));
  1106. }
  1107. }
  1108. function checkRemove(idx, tmp)
  1109. {
  1110. if (idx > 0 && idx < abs.length - 1 &&
  1111. mxUtils.ptSegDistSq(abs[idx - 1].x, abs[idx - 1].y,
  1112. abs[idx + 1].x, abs[idx + 1].y, tmp.x, tmp.y) < tol)
  1113. {
  1114. points.splice(idx - 1, 1);
  1115. result = points;
  1116. }
  1117. };
  1118. // LATER: Check if other points can be removed if a segment is made straight
  1119. checkRemove(this.index, pt);
  1120. }
  1121. }
  1122. // Updates existing point
  1123. if (result == null && this.index > mxEvent.VIRTUAL_HANDLE)
  1124. {
  1125. points[this.index - 1] = point;
  1126. }
  1127. }
  1128. }
  1129. else if (this.graph.resetEdgesOnConnect)
  1130. {
  1131. points = null;
  1132. }
  1133. return (result != null) ? result : points;
  1134. };
  1135. /**
  1136. * Function: isOutlineConnectEvent
  1137. *
  1138. * Returns true if <outlineConnect> is true and the source of the event is the outline shape
  1139. * or shift is pressed.
  1140. */
  1141. mxEdgeHandler.prototype.isOutlineConnectEvent = function(me)
  1142. {
  1143. var offset = mxUtils.getOffset(this.graph.container);
  1144. var evt = me.getEvent();
  1145. var clientX = mxEvent.getClientX(evt);
  1146. var clientY = mxEvent.getClientY(evt);
  1147. var doc = document.documentElement;
  1148. var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
  1149. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  1150. var gridX = this.currentPoint.x - this.graph.container.scrollLeft + offset.x - left;
  1151. var gridY = this.currentPoint.y - this.graph.container.scrollTop + offset.y - top;
  1152. return this.outlineConnect && !mxEvent.isShiftDown(me.getEvent()) &&
  1153. (me.isSource(this.marker.highlight.shape) ||
  1154. (mxEvent.isAltDown(me.getEvent()) && me.getState() != null) ||
  1155. this.marker.highlight.isHighlightAt(clientX, clientY) ||
  1156. ((gridX != clientX || gridY != clientY) && me.getState() == null &&
  1157. this.marker.highlight.isHighlightAt(gridX, gridY)));
  1158. };
  1159. /**
  1160. * Function: updatePreviewState
  1161. *
  1162. * Updates the given preview state taking into account the state of the constraint handler.
  1163. */
  1164. mxEdgeHandler.prototype.updatePreviewState = function(edge, point, terminalState, me, outline)
  1165. {
  1166. // Computes the points for the edge style and terminals
  1167. var sourceState = (this.isSource) ? terminalState : this.state.getVisibleTerminalState(true);
  1168. var targetState = (this.isTarget) ? terminalState : this.state.getVisibleTerminalState(false);
  1169. var sourceConstraint = this.graph.getConnectionConstraint(edge, sourceState, true);
  1170. var targetConstraint = this.graph.getConnectionConstraint(edge, targetState, false);
  1171. var constraint = this.constraintHandler.currentConstraint;
  1172. if (constraint == null && outline)
  1173. {
  1174. if (terminalState != null)
  1175. {
  1176. // Handles special case where mouse is on outline away from actual end point
  1177. // in which case the grid is ignored and mouse point is used instead
  1178. if (me.isSource(this.marker.highlight.shape))
  1179. {
  1180. point = new mxPoint(me.getGraphX(), me.getGraphY());
  1181. }
  1182. constraint = this.graph.getOutlineConstraint(point, terminalState, me);
  1183. this.constraintHandler.setFocus(me, terminalState, this.isSource);
  1184. this.constraintHandler.currentConstraint = constraint;
  1185. this.constraintHandler.currentPoint = point;
  1186. }
  1187. else
  1188. {
  1189. constraint = new mxConnectionConstraint();
  1190. }
  1191. }
  1192. if (this.outlineConnect && this.marker.highlight != null && this.marker.highlight.shape != null)
  1193. {
  1194. var s = this.graph.view.scale;
  1195. if (this.constraintHandler.currentConstraint != null &&
  1196. this.constraintHandler.currentFocus != null)
  1197. {
  1198. this.marker.highlight.shape.stroke = (outline) ? mxConstants.OUTLINE_HIGHLIGHT_COLOR : 'transparent';
  1199. this.marker.highlight.shape.strokewidth = mxConstants.OUTLINE_HIGHLIGHT_STROKEWIDTH / s / s;
  1200. this.marker.highlight.repaint();
  1201. }
  1202. else if (this.marker.hasValidState())
  1203. {
  1204. this.marker.highlight.shape.stroke = (this.graph.isCellConnectable(me.getCell()) &&
  1205. this.marker.getValidState() != me.getState()) ?
  1206. 'transparent' : mxConstants.DEFAULT_VALID_COLOR;
  1207. this.marker.highlight.shape.strokewidth = mxConstants.HIGHLIGHT_STROKEWIDTH / s / s;
  1208. this.marker.highlight.repaint();
  1209. }
  1210. }
  1211. if (this.isSource)
  1212. {
  1213. sourceConstraint = constraint;
  1214. }
  1215. else if (this.isTarget)
  1216. {
  1217. targetConstraint = constraint;
  1218. }
  1219. if (this.isSource || this.isTarget)
  1220. {
  1221. if (constraint != null && constraint.point != null)
  1222. {
  1223. edge.style[(this.isSource) ? mxConstants.STYLE_EXIT_X : mxConstants.STYLE_ENTRY_X] = constraint.point.x;
  1224. edge.style[(this.isSource) ? mxConstants.STYLE_EXIT_Y : mxConstants.STYLE_ENTRY_Y] = constraint.point.y;
  1225. }
  1226. else
  1227. {
  1228. delete edge.style[(this.isSource) ? mxConstants.STYLE_EXIT_X : mxConstants.STYLE_ENTRY_X];
  1229. delete edge.style[(this.isSource) ? mxConstants.STYLE_EXIT_Y : mxConstants.STYLE_ENTRY_Y];
  1230. }
  1231. }
  1232. edge.setVisibleTerminalState(sourceState, true);
  1233. edge.setVisibleTerminalState(targetState, false);
  1234. if (!this.isSource || sourceState != null)
  1235. {
  1236. edge.view.updateFixedTerminalPoint(edge, sourceState, true, sourceConstraint);
  1237. }
  1238. if (!this.isTarget || targetState != null)
  1239. {
  1240. edge.view.updateFixedTerminalPoint(edge, targetState, false, targetConstraint);
  1241. }
  1242. if ((this.isSource || this.isTarget) && terminalState == null)
  1243. {
  1244. edge.setAbsoluteTerminalPoint(point, this.isSource);
  1245. if (this.marker.getMarkedState() == null)
  1246. {
  1247. this.error = (this.graph.allowDanglingEdges) ? null : '';
  1248. }
  1249. }
  1250. edge.view.updatePoints(edge, this.points, sourceState, targetState);
  1251. edge.view.updateFloatingTerminalPoints(edge, sourceState, targetState);
  1252. };
  1253. /**
  1254. * Function: mouseMove
  1255. *
  1256. * Handles the event by updating the preview.
  1257. */
  1258. mxEdgeHandler.prototype.mouseMove = function(sender, me)
  1259. {
  1260. if (this.index != null && this.marker != null)
  1261. {
  1262. this.currentPoint = this.getPointForEvent(me);
  1263. this.error = null;
  1264. // Uses the current point from the constraint handler if available
  1265. if (!this.graph.isIgnoreTerminalEvent(me.getEvent()) && mxEvent.isShiftDown(me.getEvent()) && this.snapPoint != null)
  1266. {
  1267. if (Math.abs(this.snapPoint.x - this.currentPoint.x) < Math.abs(this.snapPoint.y - this.currentPoint.y))
  1268. {
  1269. this.currentPoint.x = this.snapPoint.x;
  1270. }
  1271. else
  1272. {
  1273. this.currentPoint.y = this.snapPoint.y;
  1274. }
  1275. }
  1276. if (this.index <= mxEvent.CUSTOM_HANDLE && this.index > mxEvent.VIRTUAL_HANDLE)
  1277. {
  1278. if (this.customHandles != null)
  1279. {
  1280. this.customHandles[mxEvent.CUSTOM_HANDLE - this.index].processEvent(me);
  1281. this.customHandles[mxEvent.CUSTOM_HANDLE - this.index].positionChanged();
  1282. if (this.shape != null && this.shape.node != null)
  1283. {
  1284. this.shape.node.style.display = 'none';
  1285. }
  1286. }
  1287. }
  1288. else if (this.isLabel)
  1289. {
  1290. this.label.x = this.currentPoint.x;
  1291. this.label.y = this.currentPoint.y;
  1292. }
  1293. else
  1294. {
  1295. this.points = this.getPreviewPoints(this.currentPoint, me);
  1296. var terminalState = (this.isSource || this.isTarget) ? this.getPreviewTerminalState(me) : null;
  1297. if (this.constraintHandler.currentConstraint != null &&
  1298. this.constraintHandler.currentFocus != null &&
  1299. this.constraintHandler.currentPoint != null)
  1300. {
  1301. this.currentPoint = this.constraintHandler.currentPoint.clone();
  1302. }
  1303. else if (this.outlineConnect)
  1304. {
  1305. // Need to check outline before cloning terminal state
  1306. var outline = (this.isSource || this.isTarget) ? this.isOutlineConnectEvent(me) : false
  1307. if (outline)
  1308. {
  1309. terminalState = this.marker.highlight.state;
  1310. }
  1311. else if (terminalState != null && terminalState != me.getState() &&
  1312. this.graph.isCellConnectable(me.getCell()) &&
  1313. this.marker.highlight.shape != null)
  1314. {
  1315. this.marker.highlight.shape.stroke = 'transparent';
  1316. this.marker.highlight.repaint();
  1317. terminalState = null;
  1318. }
  1319. }
  1320. if (terminalState != null && !this.isCellEnabled(terminalState.cell))
  1321. {
  1322. terminalState = null;
  1323. this.marker.reset();
  1324. }
  1325. var clone = this.clonePreviewState(this.currentPoint, (terminalState != null) ? terminalState.cell : null);
  1326. this.updatePreviewState(clone, this.currentPoint, terminalState, me, outline);
  1327. // Sets the color of the preview to valid or invalid, updates the
  1328. // points of the preview and redraws
  1329. var color = (this.error == null) ? this.marker.validColor : this.marker.invalidColor;
  1330. this.setPreviewColor(color);
  1331. this.abspoints = clone.absolutePoints;
  1332. this.active = true;
  1333. this.updateHint(me, this.currentPoint);
  1334. }
  1335. // This should go before calling isOutlineConnectEvent above. As a workaround
  1336. // we add an offset of gridSize to the hint to avoid problem with hit detection
  1337. // in highlight.isHighlightAt (which uses comonentFromPoint)
  1338. this.drawPreview();
  1339. mxEvent.consume(me.getEvent());
  1340. me.consume();
  1341. }
  1342. // Workaround for disabling the connect highlight when over handle
  1343. else if (mxClient.IS_IE && this.getHandleForEvent(me) != null)
  1344. {
  1345. me.consume(false);
  1346. }
  1347. };
  1348. /**
  1349. * Function: mouseUp
  1350. *
  1351. * Handles the event to applying the previewed changes on the edge by
  1352. * using <moveLabel>, <connect> or <changePoints>.
  1353. */
  1354. mxEdgeHandler.prototype.mouseUp = function(sender, me)
  1355. {
  1356. // Workaround for wrong event source in Webkit
  1357. if (this.index != null && this.marker != null)
  1358. {
  1359. if (this.shape != null && this.shape.node != null)
  1360. {
  1361. this.shape.node.style.display = '';
  1362. }
  1363. var edge = this.state.cell;
  1364. var index = this.index;
  1365. this.index = null;
  1366. // Ignores event if mouse has not been moved
  1367. if (me.getX() != this.startX || me.getY() != this.startY)
  1368. {
  1369. var clone = !this.graph.isIgnoreTerminalEvent(me.getEvent()) && this.graph.isCloneEvent(me.getEvent()) &&
  1370. this.cloneEnabled && this.graph.isCellsCloneable();
  1371. // Displays the reason for not carriying out the change
  1372. // if there is an error message with non-zero length
  1373. if (this.error != null)
  1374. {
  1375. if (this.error.length > 0)
  1376. {
  1377. this.graph.validationAlert(this.error);
  1378. }
  1379. }
  1380. else if (index <= mxEvent.CUSTOM_HANDLE && index > mxEvent.VIRTUAL_HANDLE)
  1381. {
  1382. if (this.customHandles != null)
  1383. {
  1384. var model = this.graph.getModel();
  1385. model.beginUpdate();
  1386. try
  1387. {
  1388. this.customHandles[mxEvent.CUSTOM_HANDLE - index].execute(me);
  1389. if (this.shape != null && this.shape.node != null)
  1390. {
  1391. this.shape.apply(this.state);
  1392. this.shape.redraw();
  1393. }
  1394. }
  1395. finally
  1396. {
  1397. model.endUpdate();
  1398. }
  1399. }
  1400. }
  1401. else if (this.isLabel)
  1402. {
  1403. this.moveLabel(this.state, this.label.x, this.label.y);
  1404. }
  1405. else if (this.isSource || this.isTarget)
  1406. {
  1407. var terminal = null;
  1408. if (this.constraintHandler.currentConstraint != null &&
  1409. this.constraintHandler.currentFocus != null)
  1410. {
  1411. terminal = this.constraintHandler.currentFocus.cell;
  1412. }
  1413. if (terminal == null && this.marker.hasValidState() && this.marker.highlight != null &&
  1414. this.marker.highlight.shape != null &&
  1415. this.marker.highlight.shape.stroke != 'transparent' &&
  1416. this.marker.highlight.shape.stroke != 'white')
  1417. {
  1418. terminal = this.marker.validState.cell;
  1419. }
  1420. if (terminal != null)
  1421. {
  1422. var model = this.graph.getModel();
  1423. var parent = model.getParent(edge);
  1424. model.beginUpdate();
  1425. try
  1426. {
  1427. // Clones and adds the cell
  1428. if (clone)
  1429. {
  1430. var geo = model.getGeometry(edge);
  1431. var clone = this.graph.cloneCell(edge);
  1432. model.add(parent, clone, model.getChildCount(parent));
  1433. if (geo != null)
  1434. {
  1435. geo = geo.clone();
  1436. model.setGeometry(clone, geo);
  1437. }
  1438. var other = model.getTerminal(edge, !this.isSource);
  1439. this.graph.connectCell(clone, other, !this.isSource);
  1440. edge = clone;
  1441. }
  1442. edge = this.connect(edge, terminal, this.isSource, clone, me);
  1443. }
  1444. finally
  1445. {
  1446. model.endUpdate();
  1447. }
  1448. }
  1449. else if (this.graph.isAllowDanglingEdges())
  1450. {
  1451. var pt = this.abspoints[(this.isSource) ? 0 : this.abspoints.length - 1];
  1452. pt.x = this.roundLength(pt.x / this.graph.view.scale - this.graph.view.translate.x);
  1453. pt.y = this.roundLength(pt.y / this.graph.view.scale - this.graph.view.translate.y);
  1454. var pstate = this.graph.getView().getState(
  1455. this.graph.getModel().getParent(edge));
  1456. if (pstate != null)
  1457. {
  1458. pt.x -= pstate.origin.x;
  1459. pt.y -= pstate.origin.y;
  1460. }
  1461. pt.x -= this.graph.panDx / this.graph.view.scale;
  1462. pt.y -= this.graph.panDy / this.graph.view.scale;
  1463. // Destroys and recreates this handler
  1464. edge = this.changeTerminalPoint(edge, pt, this.isSource, clone);
  1465. }
  1466. }
  1467. else if (this.active)
  1468. {
  1469. edge = this.changePoints(edge, this.points, clone);
  1470. }
  1471. else
  1472. {
  1473. this.graph.getView().invalidate(this.state.cell);
  1474. this.graph.getView().validate(this.state.cell);
  1475. }
  1476. }
  1477. else if (this.graph.isToggleEvent(me.getEvent()))
  1478. {
  1479. this.graph.selectCellForEvent(this.state.cell, me.getEvent());
  1480. }
  1481. // Resets the preview color the state of the handler if this
  1482. // handler has not been recreated
  1483. if (this.marker != null)
  1484. {
  1485. this.reset();
  1486. // Updates the selection if the edge has been cloned
  1487. if (edge != this.state.cell)
  1488. {
  1489. this.graph.setSelectionCell(edge);
  1490. }
  1491. }
  1492. me.consume();
  1493. }
  1494. };
  1495. /**
  1496. * Function: reset
  1497. *
  1498. * Resets the state of this handler.
  1499. */
  1500. mxEdgeHandler.prototype.reset = function()
  1501. {
  1502. if (this.active)
  1503. {
  1504. this.refresh();
  1505. }
  1506. this.error = null;
  1507. this.index = null;
  1508. this.label = null;
  1509. this.points = null;
  1510. this.snapPoint = null;
  1511. this.isLabel = false;
  1512. this.isSource = false;
  1513. this.isTarget = false;
  1514. this.active = false;
  1515. if (this.livePreview && this.sizers != null)
  1516. {
  1517. for (var i = 0; i < this.sizers.length; i++)
  1518. {
  1519. if (this.sizers[i] != null)
  1520. {
  1521. this.sizers[i].node.style.display = '';
  1522. }
  1523. }
  1524. }
  1525. if (this.marker != null)
  1526. {
  1527. this.marker.reset();
  1528. }
  1529. if (this.constraintHandler != null)
  1530. {
  1531. this.constraintHandler.reset();
  1532. }
  1533. if (this.customHandles != null)
  1534. {
  1535. for (var i = 0; i < this.customHandles.length; i++)
  1536. {
  1537. this.customHandles[i].reset();
  1538. }
  1539. }
  1540. this.setPreviewColor(mxConstants.EDGE_SELECTION_COLOR);
  1541. this.removeHint();
  1542. this.redraw();
  1543. };
  1544. /**
  1545. * Function: setPreviewColor
  1546. *
  1547. * Sets the color of the preview to the given value.
  1548. */
  1549. mxEdgeHandler.prototype.setPreviewColor = function(color)
  1550. {
  1551. if (this.shape != null)
  1552. {
  1553. this.shape.stroke = color;
  1554. }
  1555. };
  1556. /**
  1557. * Function: convertPoint
  1558. *
  1559. * Converts the given point in-place from screen to unscaled, untranslated
  1560. * graph coordinates and applies the grid. Returns the given, modified
  1561. * point instance.
  1562. *
  1563. * Parameters:
  1564. *
  1565. * point - <mxPoint> to be converted.
  1566. * gridEnabled - Boolean that specifies if the grid should be applied.
  1567. */
  1568. mxEdgeHandler.prototype.convertPoint = function(point, gridEnabled)
  1569. {
  1570. var scale = this.graph.getView().getScale();
  1571. var tr = this.graph.getView().getTranslate();
  1572. if (gridEnabled)
  1573. {
  1574. point.x = this.graph.snap(point.x);
  1575. point.y = this.graph.snap(point.y);
  1576. }
  1577. point.x = Math.round(point.x / scale - tr.x);
  1578. point.y = Math.round(point.y / scale - tr.y);
  1579. var pstate = this.graph.getView().getState(
  1580. this.graph.getModel().getParent(this.state.cell));
  1581. if (pstate != null)
  1582. {
  1583. point.x -= pstate.origin.x;
  1584. point.y -= pstate.origin.y;
  1585. }
  1586. return point;
  1587. };
  1588. /**
  1589. * Function: moveLabel
  1590. *
  1591. * Changes the coordinates for the label of the given edge.
  1592. *
  1593. * Parameters:
  1594. *
  1595. * edge - <mxCell> that represents the edge.
  1596. * x - Integer that specifies the x-coordinate of the new location.
  1597. * y - Integer that specifies the y-coordinate of the new location.
  1598. */
  1599. mxEdgeHandler.prototype.moveLabel = function(edgeState, x, y)
  1600. {
  1601. var model = this.graph.getModel();
  1602. var geometry = model.getGeometry(edgeState.cell);
  1603. if (geometry != null)
  1604. {
  1605. var scale = this.graph.getView().scale;
  1606. geometry = geometry.clone();
  1607. if (geometry.relative)
  1608. {
  1609. // Resets the relative location stored inside the geometry
  1610. var pt = this.graph.getView().getRelativePoint(edgeState, x, y);
  1611. geometry.x = Math.round(pt.x * 10000) / 10000;
  1612. geometry.y = Math.round(pt.y);
  1613. // Resets the offset inside the geometry to find the offset
  1614. // from the resulting point
  1615. geometry.offset = new mxPoint(0, 0);
  1616. var pt = this.graph.view.getPoint(edgeState, geometry);
  1617. geometry.offset = new mxPoint(Math.round((x - pt.x) / scale), Math.round((y - pt.y) / scale));
  1618. }
  1619. else
  1620. {
  1621. var points = edgeState.absolutePoints;
  1622. var p0 = points[0];
  1623. var pe = points[points.length - 1];
  1624. if (p0 != null && pe != null)
  1625. {
  1626. var cx = p0.x + (pe.x - p0.x) / 2;
  1627. var cy = p0.y + (pe.y - p0.y) / 2;
  1628. geometry.offset = new mxPoint(Math.round((x - cx) / scale), Math.round((y - cy) / scale));
  1629. geometry.x = 0;
  1630. geometry.y = 0;
  1631. }
  1632. }
  1633. model.setGeometry(edgeState.cell, geometry);
  1634. }
  1635. };
  1636. /**
  1637. * Function: connect
  1638. *
  1639. * Changes the terminal or terminal point of the given edge in the graph
  1640. * model.
  1641. *
  1642. * Parameters:
  1643. *
  1644. * edge - <mxCell> that represents the edge to be reconnected.
  1645. * terminal - <mxCell> that represents the new terminal.
  1646. * isSource - Boolean indicating if the new terminal is the source or
  1647. * target terminal.
  1648. * isClone - Boolean indicating if the new connection should be a clone of
  1649. * the old edge.
  1650. * me - <mxMouseEvent> that contains the mouse up event.
  1651. */
  1652. mxEdgeHandler.prototype.connect = function(edge, terminal, isSource, isClone, me)
  1653. {
  1654. var model = this.graph.getModel();
  1655. var parent = model.getParent(edge);
  1656. model.beginUpdate();
  1657. try
  1658. {
  1659. var constraint = this.constraintHandler.currentConstraint;
  1660. if (constraint == null)
  1661. {
  1662. constraint = new mxConnectionConstraint();
  1663. }
  1664. this.graph.connectCell(edge, terminal, isSource, constraint);
  1665. }
  1666. finally
  1667. {
  1668. model.endUpdate();
  1669. }
  1670. return edge;
  1671. };
  1672. /**
  1673. * Function: changeTerminalPoint
  1674. *
  1675. * Changes the terminal point of the given edge.
  1676. */
  1677. mxEdgeHandler.prototype.changeTerminalPoint = function(edge, point, isSource, clone)
  1678. {
  1679. var model = this.graph.getModel();
  1680. model.beginUpdate();
  1681. try
  1682. {
  1683. if (clone)
  1684. {
  1685. var parent = model.getParent(edge);
  1686. var terminal = model.getTerminal(edge, !isSource);
  1687. edge = this.graph.cloneCell(edge);
  1688. model.add(parent, edge, model.getChildCount(parent));
  1689. model.setTerminal(edge, terminal, !isSource);
  1690. }
  1691. var geo = model.getGeometry(edge);
  1692. if (geo != null)
  1693. {
  1694. geo = geo.clone();
  1695. geo.setTerminalPoint(point, isSource);
  1696. model.setGeometry(edge, geo);
  1697. this.graph.connectCell(edge, null, isSource, new mxConnectionConstraint());
  1698. }
  1699. }
  1700. finally
  1701. {
  1702. model.endUpdate();
  1703. }
  1704. return edge;
  1705. };
  1706. /**
  1707. * Function: changePoints
  1708. *
  1709. * Changes the control points of the given edge in the graph model.
  1710. */
  1711. mxEdgeHandler.prototype.changePoints = function(edge, points, clone)
  1712. {
  1713. var model = this.graph.getModel();
  1714. model.beginUpdate();
  1715. try
  1716. {
  1717. if (clone)
  1718. {
  1719. var parent = model.getParent(edge);
  1720. var source = model.getTerminal(edge, true);
  1721. var target = model.getTerminal(edge, false);
  1722. edge = this.graph.cloneCell(edge);
  1723. model.add(parent, edge, model.getChildCount(parent));
  1724. model.setTerminal(edge, source, true);
  1725. model.setTerminal(edge, target, false);
  1726. }
  1727. var geo = model.getGeometry(edge);
  1728. if (geo != null)
  1729. {
  1730. geo = geo.clone();
  1731. geo.points = points;
  1732. model.setGeometry(edge, geo);
  1733. }
  1734. }
  1735. finally
  1736. {
  1737. model.endUpdate();
  1738. }
  1739. return edge;
  1740. };
  1741. /**
  1742. * Function: addPoint
  1743. *
  1744. * Adds a control point for the given state and event.
  1745. */
  1746. mxEdgeHandler.prototype.addPoint = function(state, evt)
  1747. {
  1748. var pt = mxUtils.convertPoint(this.graph.container, mxEvent.getClientX(evt),
  1749. mxEvent.getClientY(evt));
  1750. var gridEnabled = this.graph.isGridEnabledEvent(evt);
  1751. this.convertPoint(pt, gridEnabled);
  1752. this.addPointAt(state, pt.x, pt.y);
  1753. mxEvent.consume(evt);
  1754. };
  1755. /**
  1756. * Function: addPointAt
  1757. *
  1758. * Adds a control point at the given point.
  1759. */
  1760. mxEdgeHandler.prototype.addPointAt = function(state, x, y)
  1761. {
  1762. var geo = this.graph.getCellGeometry(state.cell);
  1763. var pt = new mxPoint(x, y);
  1764. if (geo != null)
  1765. {
  1766. geo = geo.clone();
  1767. var t = this.graph.view.translate;
  1768. var s = this.graph.view.scale;
  1769. var offset = new mxPoint(t.x * s, t.y * s);
  1770. var parent = this.graph.model.getParent(this.state.cell);
  1771. if (this.graph.model.isVertex(parent))
  1772. {
  1773. var pState = this.graph.view.getState(parent);
  1774. offset = new mxPoint(pState.x, pState.y);
  1775. }
  1776. var index = mxUtils.findNearestSegment(state, pt.x * s + offset.x, pt.y * s + offset.y);
  1777. if (geo.points == null)
  1778. {
  1779. geo.points = [pt];
  1780. }
  1781. else
  1782. {
  1783. geo.points.splice(index, 0, pt);
  1784. }
  1785. this.graph.getModel().setGeometry(state.cell, geo);
  1786. this.refresh();
  1787. this.redraw();
  1788. }
  1789. };
  1790. /**
  1791. * Function: removePoint
  1792. *
  1793. * Removes the control point at the given index from the given state.
  1794. */
  1795. mxEdgeHandler.prototype.removePoint = function(state, index)
  1796. {
  1797. if (index > 0 && index < this.abspoints.length - 1)
  1798. {
  1799. var geo = this.graph.getCellGeometry(this.state.cell);
  1800. if (geo != null && geo.points != null)
  1801. {
  1802. geo = geo.clone();
  1803. geo.points.splice(index - 1, 1);
  1804. this.graph.getModel().setGeometry(state.cell, geo);
  1805. this.refresh();
  1806. this.redraw();
  1807. }
  1808. }
  1809. };
  1810. /**
  1811. * Function: getHandleFillColor
  1812. *
  1813. * Returns the fillcolor for the handle at the given index.
  1814. */
  1815. mxEdgeHandler.prototype.getHandleFillColor = function(index)
  1816. {
  1817. var isSource = index == 0;
  1818. var cell = this.state.cell;
  1819. var terminal = this.graph.getModel().getTerminal(cell, isSource);
  1820. var color = mxConstants.HANDLE_FILLCOLOR;
  1821. if ((terminal != null && !this.graph.isCellDisconnectable(cell, terminal, isSource)) ||
  1822. (terminal == null && !this.graph.isTerminalPointMovable(cell, isSource)))
  1823. {
  1824. color = mxConstants.LOCKED_HANDLE_FILLCOLOR;
  1825. }
  1826. else if (terminal != null && this.graph.isCellDisconnectable(cell, terminal, isSource))
  1827. {
  1828. color = mxConstants.CONNECT_HANDLE_FILLCOLOR;
  1829. }
  1830. return color;
  1831. };
  1832. /**
  1833. * Function: redraw
  1834. *
  1835. * Redraws the preview, and the bends- and label control points.
  1836. */
  1837. mxEdgeHandler.prototype.redraw = function(ignoreHandles)
  1838. {
  1839. if (this.state != null)
  1840. {
  1841. this.abspoints = this.state.absolutePoints.slice();
  1842. var g = this.graph.getModel().getGeometry(this.state.cell);
  1843. if (g != null)
  1844. {
  1845. var pts = g.points;
  1846. if (this.bends != null && this.bends.length > 0)
  1847. {
  1848. if (pts != null)
  1849. {
  1850. if (this.points == null)
  1851. {
  1852. this.points = [];
  1853. }
  1854. for (var i = 1; i < this.bends.length - 1; i++)
  1855. {
  1856. if (this.bends[i] != null && this.abspoints[i] != null)
  1857. {
  1858. this.points[i - 1] = pts[i - 1];
  1859. }
  1860. }
  1861. }
  1862. }
  1863. }
  1864. this.drawPreview();
  1865. if (!ignoreHandles)
  1866. {
  1867. this.redrawHandles();
  1868. }
  1869. }
  1870. };
  1871. /**
  1872. * Function: redrawHandles
  1873. *
  1874. * Redraws the handles.
  1875. */
  1876. mxEdgeHandler.prototype.redrawHandles = function()
  1877. {
  1878. var cell = this.state.cell;
  1879. // Updates the handle for the label position
  1880. var b = this.labelShape.bounds;
  1881. this.label = new mxPoint(this.state.absoluteOffset.x, this.state.absoluteOffset.y);
  1882. this.labelShape.bounds = new mxRectangle(Math.round(this.label.x - b.width / 2),
  1883. Math.round(this.label.y - b.height / 2), b.width, b.height);
  1884. // Shows or hides the label handle depending on the label
  1885. var lab = this.graph.getLabel(cell);
  1886. this.labelShape.visible = (lab != null && lab.length > 0 && this.graph.isLabelMovable(cell));
  1887. if (this.bends != null && this.bends.length > 0)
  1888. {
  1889. var n = this.abspoints.length - 1;
  1890. var p0 = this.abspoints[0];
  1891. var x0 = p0.x;
  1892. var y0 = p0.y;
  1893. b = this.bends[0].bounds;
  1894. this.bends[0].bounds = new mxRectangle(Math.floor(x0 - b.width / 2),
  1895. Math.floor(y0 - b.height / 2), b.width, b.height);
  1896. this.bends[0].fill = this.getHandleFillColor(0);
  1897. this.bends[0].redraw();
  1898. if (this.manageLabelHandle)
  1899. {
  1900. this.checkLabelHandle(this.bends[0].bounds);
  1901. }
  1902. var pe = this.abspoints[n];
  1903. var xn = pe.x;
  1904. var yn = pe.y;
  1905. var bn = this.bends.length - 1;
  1906. b = this.bends[bn].bounds;
  1907. this.bends[bn].bounds = new mxRectangle(Math.floor(xn - b.width / 2),
  1908. Math.floor(yn - b.height / 2), b.width, b.height);
  1909. this.bends[bn].fill = this.getHandleFillColor(bn);
  1910. this.bends[bn].redraw();
  1911. if (this.manageLabelHandle)
  1912. {
  1913. this.checkLabelHandle(this.bends[bn].bounds);
  1914. }
  1915. this.redrawInnerBends(p0, pe);
  1916. }
  1917. if (this.abspoints != null && this.virtualBends != null && this.virtualBends.length > 0)
  1918. {
  1919. var last = this.abspoints[0];
  1920. for (var i = 0; i < this.virtualBends.length; i++)
  1921. {
  1922. if (this.virtualBends[i] != null && this.abspoints[i + 1] != null)
  1923. {
  1924. var pt = this.abspoints[i + 1];
  1925. var b = this.virtualBends[i];
  1926. var x = last.x + (pt.x - last.x) / 2;
  1927. var y = last.y + (pt.y - last.y) / 2;
  1928. b.bounds = new mxRectangle(Math.floor(x - b.bounds.width / 2),
  1929. Math.floor(y - b.bounds.height / 2), b.bounds.width, b.bounds.height);
  1930. b.redraw();
  1931. mxUtils.setOpacity(b.node, this.virtualBendOpacity);
  1932. last = pt;
  1933. if (this.manageLabelHandle)
  1934. {
  1935. this.checkLabelHandle(b.bounds);
  1936. }
  1937. }
  1938. }
  1939. }
  1940. if (this.labelShape != null)
  1941. {
  1942. this.labelShape.redraw();
  1943. }
  1944. if (this.customHandles != null)
  1945. {
  1946. for (var i = 0; i < this.customHandles.length; i++)
  1947. {
  1948. var temp = this.customHandles[i].shape.node.style.display;
  1949. this.customHandles[i].redraw();
  1950. this.customHandles[i].shape.node.style.display = temp;
  1951. // Hides custom handles during text editing
  1952. this.customHandles[i].shape.node.style.visibility =
  1953. (this.isCustomHandleVisible(this.customHandles[i])) ?
  1954. '' : 'hidden';
  1955. }
  1956. }
  1957. };
  1958. /**
  1959. * Function: isCustomHandleVisible
  1960. *
  1961. * Returns true if the given custom handle is visible.
  1962. */
  1963. mxEdgeHandler.prototype.isCustomHandleVisible = function(handle)
  1964. {
  1965. return !this.graph.isEditing() && this.state.view.graph.getSelectionCount() == 1;
  1966. };
  1967. /**
  1968. * Function: hideHandles
  1969. *
  1970. * Shortcut to <hideSizers>.
  1971. */
  1972. mxEdgeHandler.prototype.setHandlesVisible = function(visible)
  1973. {
  1974. if (this.bends != null)
  1975. {
  1976. for (var i = 0; i < this.bends.length; i++)
  1977. {
  1978. this.bends[i].node.style.display = (visible) ? '' : 'none';
  1979. }
  1980. }
  1981. if (this.virtualBends != null)
  1982. {
  1983. for (var i = 0; i < this.virtualBends.length; i++)
  1984. {
  1985. this.virtualBends[i].node.style.display = (visible) ? '' : 'none';
  1986. }
  1987. }
  1988. if (this.labelShape != null)
  1989. {
  1990. this.labelShape.node.style.display = (visible) ? '' : 'none';
  1991. }
  1992. if (this.customHandles != null)
  1993. {
  1994. for (var i = 0; i < this.customHandles.length; i++)
  1995. {
  1996. this.customHandles[i].setVisible(visible);
  1997. }
  1998. }
  1999. };
  2000. /**
  2001. * Function: redrawInnerBends
  2002. *
  2003. * Updates and redraws the inner bends.
  2004. *
  2005. * Parameters:
  2006. *
  2007. * p0 - <mxPoint> that represents the location of the first point.
  2008. * pe - <mxPoint> that represents the location of the last point.
  2009. */
  2010. mxEdgeHandler.prototype.redrawInnerBends = function(p0, pe)
  2011. {
  2012. for (var i = 1; i < this.bends.length - 1; i++)
  2013. {
  2014. if (this.bends[i] != null)
  2015. {
  2016. if (this.abspoints[i] != null)
  2017. {
  2018. var x = this.abspoints[i].x;
  2019. var y = this.abspoints[i].y;
  2020. var b = this.bends[i].bounds;
  2021. this.bends[i].node.style.visibility = 'visible';
  2022. this.bends[i].bounds = new mxRectangle(Math.round(x - b.width / 2),
  2023. Math.round(y - b.height / 2), b.width, b.height);
  2024. if (this.manageLabelHandle)
  2025. {
  2026. this.checkLabelHandle(this.bends[i].bounds);
  2027. }
  2028. else if (this.handleImage == null && this.labelShape.visible && mxUtils.intersects(this.bends[i].bounds, this.labelShape.bounds))
  2029. {
  2030. w = mxConstants.HANDLE_SIZE + 3;
  2031. h = mxConstants.HANDLE_SIZE + 3;
  2032. this.bends[i].bounds = new mxRectangle(Math.round(x - w / 2), Math.round(y - h / 2), w, h);
  2033. }
  2034. this.bends[i].redraw();
  2035. }
  2036. else
  2037. {
  2038. this.bends[i].destroy();
  2039. this.bends[i] = null;
  2040. }
  2041. }
  2042. }
  2043. };
  2044. /**
  2045. * Function: checkLabelHandle
  2046. *
  2047. * Checks if the label handle intersects the given bounds and moves it if it
  2048. * intersects.
  2049. */
  2050. mxEdgeHandler.prototype.checkLabelHandle = function(b)
  2051. {
  2052. if (this.labelShape != null)
  2053. {
  2054. var b2 = this.labelShape.bounds;
  2055. if (mxUtils.intersects(b, b2))
  2056. {
  2057. if (b.getCenterY() < b2.getCenterY())
  2058. {
  2059. b2.y = b.y + b.height;
  2060. }
  2061. else
  2062. {
  2063. b2.y = b.y - b2.height;
  2064. }
  2065. }
  2066. }
  2067. };
  2068. /**
  2069. * Function: drawPreview
  2070. *
  2071. * Redraws the preview.
  2072. */
  2073. mxEdgeHandler.prototype.drawPreview = function()
  2074. {
  2075. try
  2076. {
  2077. if (this.isLabel)
  2078. {
  2079. var b = this.labelShape.bounds;
  2080. var bounds = new mxRectangle(Math.round(this.label.x - b.width / 2),
  2081. Math.round(this.label.y - b.height / 2), b.width, b.height);
  2082. if (!this.labelShape.bounds.equals(bounds))
  2083. {
  2084. this.labelShape.bounds = bounds;
  2085. this.labelShape.redraw();
  2086. }
  2087. }
  2088. if (this.shape != null && !mxUtils.equalPoints(this.shape.points, this.abspoints))
  2089. {
  2090. this.shape.apply(this.state);
  2091. this.shape.points = this.abspoints.slice();
  2092. this.shape.scale = this.state.view.scale;
  2093. this.shape.isDashed = this.isSelectionDashed();
  2094. this.shape.stroke = this.getSelectionColor();
  2095. this.shape.strokewidth = this.getSelectionStrokeWidth() / this.shape.scale / this.shape.scale;
  2096. this.shape.isShadow = false;
  2097. this.shape.redraw();
  2098. }
  2099. this.updateParentHighlight();
  2100. }
  2101. catch (e)
  2102. {
  2103. // ignore
  2104. }
  2105. };
  2106. /**
  2107. * Function: refresh
  2108. *
  2109. * Refreshes the bends of this handler.
  2110. */
  2111. mxEdgeHandler.prototype.refresh = function()
  2112. {
  2113. if (this.state != null)
  2114. {
  2115. this.abspoints = this.getSelectionPoints(this.state);
  2116. this.points = [];
  2117. if (this.bends != null)
  2118. {
  2119. this.destroyBends(this.bends);
  2120. this.bends = this.createBends();
  2121. }
  2122. if (this.virtualBends != null)
  2123. {
  2124. this.destroyBends(this.virtualBends);
  2125. this.virtualBends = this.createVirtualBends();
  2126. }
  2127. if (this.customHandles != null)
  2128. {
  2129. this.destroyBends(this.customHandles);
  2130. this.customHandles = this.createCustomHandles();
  2131. }
  2132. // Puts label node on top of bends
  2133. if (this.labelShape != null && this.labelShape.node != null && this.labelShape.node.parentNode != null)
  2134. {
  2135. this.labelShape.node.parentNode.appendChild(this.labelShape.node);
  2136. }
  2137. }
  2138. };
  2139. /**
  2140. * Function: isDestroyed
  2141. *
  2142. * Returns true if <destroy> was called.
  2143. */
  2144. mxEdgeHandler.prototype.isDestroyed = function()
  2145. {
  2146. return this.shape == null;
  2147. };
  2148. /**
  2149. * Function: destroyBends
  2150. *
  2151. * Destroys all elements in <bends>.
  2152. */
  2153. mxEdgeHandler.prototype.destroyBends = function(bends)
  2154. {
  2155. if (bends != null)
  2156. {
  2157. for (var i = 0; i < bends.length; i++)
  2158. {
  2159. if (bends[i] != null)
  2160. {
  2161. bends[i].destroy();
  2162. }
  2163. }
  2164. }
  2165. };
  2166. /**
  2167. * Function: destroy
  2168. *
  2169. * Destroys the handler and all its resources and DOM nodes. This does
  2170. * normally not need to be called as handlers are destroyed automatically
  2171. * when the corresponding cell is deselected.
  2172. */
  2173. mxEdgeHandler.prototype.destroy = function()
  2174. {
  2175. if (this.escapeHandler != null)
  2176. {
  2177. this.state.view.graph.removeListener(this.escapeHandler);
  2178. this.escapeHandler = null;
  2179. }
  2180. if (this.marker != null)
  2181. {
  2182. this.marker.destroy();
  2183. this.marker = null;
  2184. }
  2185. if (this.shape != null)
  2186. {
  2187. this.shape.destroy();
  2188. this.shape = null;
  2189. }
  2190. if (this.parentHighlight != null)
  2191. {
  2192. var parent = this.graph.model.getParent(this.state.cell);
  2193. var pstate = this.graph.view.getState(parent);
  2194. if (pstate != null && pstate.parentHighlight == this.parentHighlight)
  2195. {
  2196. pstate.parentHighlight = null;
  2197. }
  2198. this.parentHighlight.destroy();
  2199. this.parentHighlight = null;
  2200. }
  2201. if (this.labelShape != null)
  2202. {
  2203. this.labelShape.destroy();
  2204. this.labelShape = null;
  2205. }
  2206. if (this.constraintHandler != null)
  2207. {
  2208. this.constraintHandler.destroy();
  2209. this.constraintHandler = null;
  2210. }
  2211. this.destroyBends(this.virtualBends);
  2212. this.virtualBends = null;
  2213. this.destroyBends(this.customHandles);
  2214. this.customHandles = null;
  2215. this.destroyBends(this.bends);
  2216. this.bends = null;
  2217. this.removeHint();
  2218. };
  2219. __mxOutput.mxEdgeHandler = typeof mxEdgeHandler !== 'undefined' ? mxEdgeHandler : undefined;