mxConnectionHandler.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. * Copyright (c) 2006-2016, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxConnectionHandler
  7. *
  8. * Graph event handler that creates new connections. Uses <mxTerminalMarker>
  9. * for finding and highlighting the source and target vertices and
  10. * <factoryMethod> to create the edge instance. This handler is built-into
  11. * <mxGraph.connectionHandler> and enabled using <mxGraph.setConnectable>.
  12. *
  13. * Example:
  14. *
  15. * (code)
  16. * new mxConnectionHandler(graph, function(source, target, style)
  17. * {
  18. * edge = new mxCell('', new mxGeometry());
  19. * edge.setEdge(true);
  20. * edge.setStyle(style);
  21. * edge.geometry.relative = true;
  22. * return edge;
  23. * });
  24. * (end)
  25. *
  26. * Here is an alternative solution that just sets a specific user object for
  27. * new edges by overriding <insertEdge>.
  28. *
  29. * (code)
  30. * mxConnectionHandlerInsertEdge = mxConnectionHandler.prototype.insertEdge;
  31. * mxConnectionHandler.prototype.insertEdge = function(parent, id, value, source, target, style)
  32. * {
  33. * value = 'Test';
  34. *
  35. * return mxConnectionHandlerInsertEdge.apply(this, arguments);
  36. * };
  37. * (end)
  38. *
  39. * Using images to trigger connections:
  40. *
  41. * This handler uses mxTerminalMarker to find the source and target cell for
  42. * the new connection and creates a new edge using <connect>. The new edge is
  43. * created using <createEdge> which in turn uses <factoryMethod> or creates a
  44. * new default edge.
  45. *
  46. * The handler uses a "highlight-paradigm" for indicating if a cell is being
  47. * used as a source or target terminal, as seen in other diagramming products.
  48. * In order to allow both, moving and connecting cells at the same time,
  49. * <mxConstants.DEFAULT_HOTSPOT> is used in the handler to determine the hotspot
  50. * of a cell, that is, the region of the cell which is used to trigger a new
  51. * connection. The constant is a value between 0 and 1 that specifies the
  52. * amount of the width and height around the center to be used for the hotspot
  53. * of a cell and its default value is 0.5. In addition,
  54. * <mxConstants.MIN_HOTSPOT_SIZE> defines the minimum number of pixels for the
  55. * width and height of the hotspot.
  56. *
  57. * This solution, while standards compliant, may be somewhat confusing because
  58. * there is no visual indicator for the hotspot and the highlight is seen to
  59. * switch on and off while the mouse is being moved in and out. Furthermore,
  60. * this paradigm does not allow to create different connections depending on
  61. * the highlighted hotspot as there is only one hotspot per cell and it
  62. * normally does not allow cells to be moved and connected at the same time as
  63. * there is no clear indication of the connectable area of the cell.
  64. *
  65. * To come across these issues, the handle has an additional <createIcons> hook
  66. * with a default implementation that allows to create one icon to be used to
  67. * trigger new connections. If this icon is specified, then new connections can
  68. * only be created if the image is clicked while the cell is being highlighted.
  69. * The <createIcons> hook may be overridden to create more than one
  70. * <mxImageShape> for creating new connections, but the default implementation
  71. * supports one image and is used as follows:
  72. *
  73. * In order to display the "connect image" whenever the mouse is over the cell,
  74. * an DEFAULT_HOTSPOT of 1 should be used:
  75. *
  76. * (code)
  77. * mxConstants.DEFAULT_HOTSPOT = 1;
  78. * (end)
  79. *
  80. * In order to avoid confusion with the highlighting, the highlight color
  81. * should not be used with a connect image:
  82. *
  83. * (code)
  84. * mxConstants.HIGHLIGHT_COLOR = null;
  85. * (end)
  86. *
  87. * To install the image, the connectImage field of the mxConnectionHandler must
  88. * be assigned a new <mxImage> instance:
  89. *
  90. * (code)
  91. * mxConnectionHandler.prototype.connectImage = new mxImage('images/green-dot.gif', 14, 14);
  92. * (end)
  93. *
  94. * This will use the green-dot.gif with a width and height of 14 pixels as the
  95. * image to trigger new connections. In createIcons the icon field of the
  96. * handler will be set in order to remember the icon that has been clicked for
  97. * creating the new connection. This field will be available under selectedIcon
  98. * in the connect method, which may be overridden to take the icon that
  99. * triggered the new connection into account. This is useful if more than one
  100. * icon may be used to create a connection.
  101. *
  102. * Group: Events
  103. *
  104. * Event: mxEvent.START
  105. *
  106. * Fires when a new connection is being created by the user. The <code>state</code>
  107. * property contains the state of the source cell.
  108. *
  109. * Event: mxEvent.CONNECT
  110. *
  111. * Fires between begin- and endUpdate in <connect>. The <code>cell</code>
  112. * property contains the inserted edge, the <code>event</code> and <code>target</code>
  113. * properties contain the respective arguments that were passed to <connect> (where
  114. * target corresponds to the dropTarget argument). Finally, the <code>terminal</code>
  115. * property corresponds to the target argument in <connect> or the clone of the source
  116. * terminal if <createTarget> is enabled.
  117. *
  118. * Note that the target is the cell under the mouse where the mouse button was released.
  119. * Depending on the logic in the handler, this doesn't necessarily have to be the target
  120. * of the inserted edge. To print the source, target or any optional ports IDs that the
  121. * edge is connected to, the following code can be used. To get more details about the
  122. * actual connection point, <mxGraph.getConnectionConstraint> can be used. To resolve
  123. * the port IDs, use <mxGraphModel.getCell>.
  124. *
  125. * (code)
  126. * graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
  127. * {
  128. * var edge = evt.getProperty('cell');
  129. * var source = graph.getModel().getTerminal(edge, true);
  130. * var target = graph.getModel().getTerminal(edge, false);
  131. *
  132. * var style = graph.getCellStyle(edge);
  133. * var sourcePortId = style[mxConstants.STYLE_SOURCE_PORT];
  134. * var targetPortId = style[mxConstants.STYLE_TARGET_PORT];
  135. *
  136. * mxLog.show();
  137. * mxLog.debug('connect', edge, source.id, target.id, sourcePortId, targetPortId);
  138. * });
  139. * (end)
  140. *
  141. * Event: mxEvent.RESET
  142. *
  143. * Fires when the <reset> method is invoked.
  144. *
  145. * Constructor: mxConnectionHandler
  146. *
  147. * Constructs an event handler that connects vertices using the specified
  148. * factory method to create the new edges. Modify
  149. * <mxConstants.ACTIVE_REGION> to setup the region on a cell which triggers
  150. * the creation of a new connection or use connect icons as explained
  151. * above.
  152. *
  153. * Parameters:
  154. *
  155. * graph - Reference to the enclosing <mxGraph>.
  156. * factoryMethod - Optional function to create the edge. The function takes
  157. * the source and target <mxCell> as the first and second argument and an
  158. * optional cell style from the preview as the third argument. It returns
  159. * the <mxCell> that represents the new edge.
  160. */
  161. function mxConnectionHandler(graph, factoryMethod)
  162. {
  163. mxEventSource.call(this);
  164. if (graph != null)
  165. {
  166. this.graph = graph;
  167. this.factoryMethod = factoryMethod;
  168. this.init();
  169. // Handles escape keystrokes
  170. this.escapeHandler = mxUtils.bind(this, function(sender, evt)
  171. {
  172. this.reset();
  173. });
  174. this.graph.addListener(mxEvent.ESCAPE, this.escapeHandler);
  175. }
  176. };
  177. /**
  178. * Extends mxEventSource.
  179. */
  180. mxUtils.extend(mxConnectionHandler, mxEventSource);
  181. /**
  182. * Variable: graph
  183. *
  184. * Reference to the enclosing <mxGraph>.
  185. */
  186. mxConnectionHandler.prototype.graph = null;
  187. /**
  188. * Variable: factoryMethod
  189. *
  190. * Function that is used for creating new edges. The function takes the
  191. * source and target <mxCell> as the first and second argument and returns
  192. * a new <mxCell> that represents the edge. This is used in <createEdge>.
  193. */
  194. mxConnectionHandler.prototype.factoryMethod = true;
  195. /**
  196. * Variable: moveIconFront
  197. *
  198. * Specifies if icons should be displayed inside the graph container instead
  199. * of the overlay pane. This is used for HTML labels on vertices which hide
  200. * the connect icon. This has precendence over <moveIconBack> when set
  201. * to true. Default is false.
  202. */
  203. mxConnectionHandler.prototype.moveIconFront = false;
  204. /**
  205. * Variable: moveIconBack
  206. *
  207. * Specifies if icons should be moved to the back of the overlay pane. This can
  208. * be set to true if the icons of the connection handler conflict with other
  209. * handles, such as the vertex label move handle. Default is false.
  210. */
  211. mxConnectionHandler.prototype.moveIconBack = false;
  212. /**
  213. * Variable: connectImage
  214. *
  215. * <mxImage> that is used to trigger the creation of a new connection. This
  216. * is used in <createIcons>. Default is null.
  217. */
  218. mxConnectionHandler.prototype.connectImage = null;
  219. /**
  220. * Variable: targetConnectImage
  221. *
  222. * Specifies if the connect icon should be centered on the target state
  223. * while connections are being previewed. Default is false.
  224. */
  225. mxConnectionHandler.prototype.targetConnectImage = false;
  226. /**
  227. * Variable: enabled
  228. *
  229. * Specifies if events are handled. Default is true.
  230. */
  231. mxConnectionHandler.prototype.enabled = true;
  232. /**
  233. * Variable: select
  234. *
  235. * Specifies if new edges should be selected. Default is true.
  236. */
  237. mxConnectionHandler.prototype.select = true;
  238. /**
  239. * Variable: createTarget
  240. *
  241. * Specifies if <createTargetVertex> should be called if no target was under the
  242. * mouse for the new connection. Setting this to true means the connection
  243. * will be drawn as valid if no target is under the mouse, and
  244. * <createTargetVertex> will be called before the connection is created between
  245. * the source cell and the newly created vertex in <createTargetVertex>, which
  246. * can be overridden to create a new target. Default is false.
  247. */
  248. mxConnectionHandler.prototype.createTarget = false;
  249. /**
  250. * Variable: marker
  251. *
  252. * Holds the <mxTerminalMarker> used for finding source and target cells.
  253. */
  254. mxConnectionHandler.prototype.marker = null;
  255. /**
  256. * Variable: constraintHandler
  257. *
  258. * Holds the <mxConstraintHandler> used for drawing and highlighting
  259. * constraints.
  260. */
  261. mxConnectionHandler.prototype.constraintHandler = null;
  262. /**
  263. * Variable: error
  264. *
  265. * Holds the current validation error while connections are being created.
  266. */
  267. mxConnectionHandler.prototype.error = null;
  268. /**
  269. * Variable: waypointsEnabled
  270. *
  271. * Specifies if single clicks should add waypoints on the new edge. Default is
  272. * false.
  273. */
  274. mxConnectionHandler.prototype.waypointsEnabled = false;
  275. /**
  276. * Variable: ignoreMouseDown
  277. *
  278. * Specifies if the connection handler should ignore the state of the mouse
  279. * button when highlighting the source. Default is false, that is, the
  280. * handler only highlights the source if no button is being pressed.
  281. */
  282. mxConnectionHandler.prototype.ignoreMouseDown = false;
  283. /**
  284. * Variable: first
  285. *
  286. * Holds the <mxPoint> where the mouseDown took place while the handler is
  287. * active.
  288. */
  289. mxConnectionHandler.prototype.first = null;
  290. /**
  291. * Variable: connectIconOffset
  292. *
  293. * Holds the offset for connect icons during connection preview.
  294. * Default is mxPoint(0, <mxConstants.TOOLTIP_VERTICAL_OFFSET>).
  295. * Note that placing the icon under the mouse pointer with an
  296. * offset of (0,0) will affect hit detection.
  297. */
  298. mxConnectionHandler.prototype.connectIconOffset = new mxPoint(0, mxConstants.TOOLTIP_VERTICAL_OFFSET);
  299. /**
  300. * Variable: edgeState
  301. *
  302. * Optional <mxCellState> that represents the preview edge while the
  303. * handler is active. This is created in <createEdgeState>.
  304. */
  305. mxConnectionHandler.prototype.edgeState = null;
  306. /**
  307. * Variable: changeHandler
  308. *
  309. * Holds the change event listener for later removal.
  310. */
  311. mxConnectionHandler.prototype.changeHandler = null;
  312. /**
  313. * Variable: drillHandler
  314. *
  315. * Holds the drill event listener for later removal.
  316. */
  317. mxConnectionHandler.prototype.drillHandler = null;
  318. /**
  319. * Variable: mouseDownCounter
  320. *
  321. * Counts the number of mouseDown events since the start. The initial mouse
  322. * down event counts as 1.
  323. */
  324. mxConnectionHandler.prototype.mouseDownCounter = 0;
  325. /**
  326. * Variable: movePreviewAway
  327. *
  328. * Switch to enable moving the preview away from the mousepointer. This is required in browsers
  329. * where the preview cannot be made transparent to events and if the built-in hit detection on
  330. * the HTML elements in the page should be used. Default is the value of false.
  331. */
  332. mxConnectionHandler.prototype.movePreviewAway = false;
  333. /**
  334. * Variable: outlineConnect
  335. *
  336. * Specifies if connections to the outline of a highlighted target should be
  337. * enabled. This will allow to place the connection point along the outline of
  338. * the highlighted target. Default is false.
  339. */
  340. mxConnectionHandler.prototype.outlineConnect = false;
  341. /**
  342. * Variable: livePreview
  343. *
  344. * Specifies if the actual shape of the edge state should be used for the preview.
  345. * Default is false. (Ignored if no edge state is created in <createEdgeState>.)
  346. */
  347. mxConnectionHandler.prototype.livePreview = false;
  348. /**
  349. * Variable: cursor
  350. *
  351. * Specifies the cursor to be used while the handler is active. Default is null.
  352. */
  353. mxConnectionHandler.prototype.cursor = null;
  354. /**
  355. * Variable: insertBeforeSource
  356. *
  357. * Specifies if new edges should be inserted before the source vertex in the
  358. * cell hierarchy. Default is false for backwards compatibility.
  359. */
  360. mxConnectionHandler.prototype.insertBeforeSource = false;
  361. /**
  362. * Function: isEnabled
  363. *
  364. * Returns true if events are handled. This implementation
  365. * returns <enabled>.
  366. */
  367. mxConnectionHandler.prototype.isEnabled = function()
  368. {
  369. return this.enabled;
  370. };
  371. /**
  372. * Function: setEnabled
  373. *
  374. * Enables or disables event handling. This implementation
  375. * updates <enabled>.
  376. *
  377. * Parameters:
  378. *
  379. * enabled - Boolean that specifies the new enabled state.
  380. */
  381. mxConnectionHandler.prototype.setEnabled = function(enabled)
  382. {
  383. this.enabled = enabled;
  384. };
  385. /**
  386. * Function: isInsertBefore
  387. *
  388. * Returns <insertBeforeSource> for non-loops and false for loops.
  389. *
  390. * Parameters:
  391. *
  392. * edge - <mxCell> that represents the edge to be inserted.
  393. * source - <mxCell> that represents the source terminal.
  394. * target - <mxCell> that represents the target terminal.
  395. * evt - Mousedown event of the connect gesture.
  396. * dropTarget - <mxCell> that represents the cell under the mouse when it was
  397. * released.
  398. */
  399. mxConnectionHandler.prototype.isInsertBefore = function(edge, source, target, evt, dropTarget)
  400. {
  401. return this.insertBeforeSource && source != target &&
  402. this.graph.model.getParent(edge) ==
  403. this.graph.model.getParent(source);
  404. };
  405. /**
  406. * Function: isCreateTarget
  407. *
  408. * Returns <createTarget>.
  409. *
  410. * Parameters:
  411. *
  412. * evt - Current active native pointer event.
  413. */
  414. mxConnectionHandler.prototype.isCreateTarget = function(evt)
  415. {
  416. return this.createTarget;
  417. };
  418. /**
  419. * Function: setCreateTarget
  420. *
  421. * Sets <createTarget>.
  422. */
  423. mxConnectionHandler.prototype.setCreateTarget = function(value)
  424. {
  425. this.createTarget = value;
  426. };
  427. /**
  428. * Function: createShape
  429. *
  430. * Creates the preview shape for new connections.
  431. */
  432. mxConnectionHandler.prototype.createShape = function()
  433. {
  434. // Creates the edge preview
  435. var shape = (this.livePreview && this.edgeState != null) ?
  436. this.graph.cellRenderer.createShape(this.edgeState) :
  437. new mxPolyline([], mxConstants.INVALID_COLOR);
  438. shape.dialect = mxConstants.DIALECT_SVG;
  439. shape.scale = this.graph.view.scale;
  440. shape.svgStrokeTolerance = 0;
  441. shape.pointerEvents = false;
  442. shape.isDashed = true;
  443. shape.init(this.graph.getView().getOverlayPane());
  444. mxEvent.redirectMouseEvents(shape.node, this.graph, null);
  445. return shape;
  446. };
  447. /**
  448. * Function: init
  449. *
  450. * Initializes the shapes required for this connection handler. This should
  451. * be invoked if <mxGraph.container> is assigned after the connection
  452. * handler has been created.
  453. */
  454. mxConnectionHandler.prototype.init = function()
  455. {
  456. this.graph.addMouseListener(this);
  457. this.marker = this.createMarker();
  458. this.constraintHandler = new mxConstraintHandler(this.graph);
  459. // Redraws the icons if the graph changes
  460. this.changeHandler = mxUtils.bind(this, function(sender)
  461. {
  462. if (this.iconState != null)
  463. {
  464. this.iconState = this.graph.getView().getState(this.iconState.cell);
  465. }
  466. if (this.iconState != null)
  467. {
  468. this.redrawIcons(this.icons, this.iconState);
  469. this.constraintHandler.reset();
  470. }
  471. else if (this.previous != null && this.graph.view.getState(this.previous.cell) == null)
  472. {
  473. this.reset();
  474. }
  475. });
  476. this.graph.getModel().addListener(mxEvent.CHANGE, this.changeHandler);
  477. this.graph.getView().addListener(mxEvent.SCALE, this.changeHandler);
  478. this.graph.getView().addListener(mxEvent.TRANSLATE, this.changeHandler);
  479. this.graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, this.changeHandler);
  480. // Removes the icon if we step into/up or start editing
  481. this.drillHandler = mxUtils.bind(this, function(sender)
  482. {
  483. this.reset();
  484. });
  485. this.graph.addListener(mxEvent.START_EDITING, this.drillHandler);
  486. this.graph.getView().addListener(mxEvent.DOWN, this.drillHandler);
  487. this.graph.getView().addListener(mxEvent.UP, this.drillHandler);
  488. };
  489. /**
  490. * Function: isConnectableCell
  491. *
  492. * Returns true if the given cell is connectable. This is a hook to
  493. * disable floating connections. This implementation returns true.
  494. */
  495. mxConnectionHandler.prototype.isConnectableCell = function(cell)
  496. {
  497. return true;
  498. };
  499. /**
  500. * Function: createMarker
  501. *
  502. * Creates and returns the <mxCellMarker> used in <marker>.
  503. */
  504. mxConnectionHandler.prototype.createMarker = function()
  505. {
  506. var marker = new mxCellMarker(this.graph);
  507. marker.hotspotEnabled = true;
  508. // Overrides to return cell at location only if valid (so that
  509. // there is no highlight for invalid cells)
  510. marker.getCell = mxUtils.bind(this, function(me)
  511. {
  512. var cell = mxCellMarker.prototype.getCell.apply(marker, arguments);
  513. this.error = null;
  514. // Checks for cell at preview point (with grid)
  515. if (cell == null && this.currentPoint != null)
  516. {
  517. cell = this.graph.getCellAt(this.currentPoint.x, this.currentPoint.y);
  518. }
  519. // Uses connectable parent vertex if one exists
  520. if (cell != null && !this.graph.isCellConnectable(cell))
  521. {
  522. var parent = this.graph.getModel().getParent(cell);
  523. if (this.graph.getModel().isVertex(parent) && this.graph.isCellConnectable(parent))
  524. {
  525. cell = parent;
  526. }
  527. }
  528. if ((this.graph.isSwimlane(cell) && this.currentPoint != null &&
  529. this.graph.hitsSwimlaneContent(cell, this.currentPoint.x, this.currentPoint.y)) ||
  530. !this.isConnectableCell(cell))
  531. {
  532. cell = null;
  533. }
  534. if (cell != null)
  535. {
  536. if (this.isConnecting())
  537. {
  538. if (this.previous != null)
  539. {
  540. this.error = this.validateConnection(this.previous.cell, cell);
  541. if (this.error != null && this.error.length == 0)
  542. {
  543. cell = null;
  544. // Enables create target inside groups
  545. if (this.isCreateTarget(me.getEvent()))
  546. {
  547. this.error = null;
  548. }
  549. }
  550. }
  551. }
  552. else if (!this.isValidSource(cell, me))
  553. {
  554. cell = null;
  555. }
  556. }
  557. else if (this.isConnecting() && !this.isCreateTarget(me.getEvent()) &&
  558. !this.graph.allowDanglingEdges)
  559. {
  560. this.error = '';
  561. }
  562. return cell;
  563. });
  564. // Sets the highlight color according to validateConnection
  565. marker.isValidState = mxUtils.bind(this, function(state)
  566. {
  567. if (this.isConnecting())
  568. {
  569. return this.error == null;
  570. }
  571. else
  572. {
  573. return mxCellMarker.prototype.isValidState.apply(marker, arguments);
  574. }
  575. });
  576. // Overrides to use marker color only in highlight mode or for
  577. // target selection
  578. marker.getMarkerColor = mxUtils.bind(this, function(evt, state, isValid)
  579. {
  580. return (this.connectImage == null || this.isConnecting()) ?
  581. mxCellMarker.prototype.getMarkerColor.apply(marker, arguments) :
  582. null;
  583. });
  584. // Overrides to use hotspot only for source selection otherwise
  585. // intersects always returns true when over a cell
  586. marker.intersects = mxUtils.bind(this, function(state, evt)
  587. {
  588. if (this.connectImage != null || this.isConnecting())
  589. {
  590. return true;
  591. }
  592. return mxCellMarker.prototype.intersects.apply(marker, arguments);
  593. });
  594. return marker;
  595. };
  596. /**
  597. * Function: start
  598. *
  599. * Starts a new connection for the given state and coordinates.
  600. */
  601. mxConnectionHandler.prototype.start = function(state, x, y, edgeState)
  602. {
  603. this.previous = state;
  604. this.first = new mxPoint(x, y);
  605. this.edgeState = (edgeState != null) ? edgeState : this.createEdgeState(null);
  606. // Marks the source state
  607. this.marker.currentColor = this.marker.validColor;
  608. this.marker.markedState = state;
  609. this.marker.mark();
  610. this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
  611. };
  612. /**
  613. * Function: isConnecting
  614. *
  615. * Returns true if the source terminal has been clicked and a new
  616. * connection is currently being previewed.
  617. */
  618. mxConnectionHandler.prototype.isConnecting = function()
  619. {
  620. return this.first != null && this.shape != null;
  621. };
  622. /**
  623. * Function: isValidSource
  624. *
  625. * Returns <mxGraph.isValidSource> for the given source terminal.
  626. *
  627. * Parameters:
  628. *
  629. * cell - <mxCell> that represents the source terminal.
  630. * me - <mxMouseEvent> that is associated with this call.
  631. */
  632. mxConnectionHandler.prototype.isValidSource = function(cell, me)
  633. {
  634. return this.graph.isValidSource(cell);
  635. };
  636. /**
  637. * Function: isValidTarget
  638. *
  639. * Returns true. The call to <mxGraph.isValidTarget> is implicit by calling
  640. * <mxGraph.getEdgeValidationError> in <validateConnection>. This is an
  641. * additional hook for disabling certain targets in this specific handler.
  642. *
  643. * Parameters:
  644. *
  645. * cell - <mxCell> that represents the target terminal.
  646. */
  647. mxConnectionHandler.prototype.isValidTarget = function(cell)
  648. {
  649. return true;
  650. };
  651. /**
  652. * Function: validateConnection
  653. *
  654. * Returns the error message or an empty string if the connection for the
  655. * given source target pair is not valid. Otherwise it returns null. This
  656. * implementation uses <mxGraph.getEdgeValidationError>.
  657. *
  658. * Parameters:
  659. *
  660. * source - <mxCell> that represents the source terminal.
  661. * target - <mxCell> that represents the target terminal.
  662. */
  663. mxConnectionHandler.prototype.validateConnection = function(source, target)
  664. {
  665. if (!this.isValidTarget(target))
  666. {
  667. return '';
  668. }
  669. return this.graph.getEdgeValidationError(null, source, target);
  670. };
  671. /**
  672. * Function: getConnectImage
  673. *
  674. * Hook to return the <mxImage> used for the connection icon of the given
  675. * <mxCellState>. This implementation returns <connectImage>.
  676. *
  677. * Parameters:
  678. *
  679. * state - <mxCellState> whose connect image should be returned.
  680. */
  681. mxConnectionHandler.prototype.getConnectImage = function(state)
  682. {
  683. return this.connectImage;
  684. };
  685. /**
  686. * Function: isMoveIconToFrontForState
  687. *
  688. * Returns true if the state has a HTML label in the graph's container, otherwise
  689. * it returns <moveIconFront>.
  690. *
  691. * Parameters:
  692. *
  693. * state - <mxCellState> whose connect icons should be returned.
  694. */
  695. mxConnectionHandler.prototype.isMoveIconToFrontForState = function(state)
  696. {
  697. if (state.text != null && state.text.node.parentNode == this.graph.container)
  698. {
  699. return true;
  700. }
  701. return this.moveIconFront;
  702. };
  703. /**
  704. * Function: createIcons
  705. *
  706. * Creates the array <mxImageShapes> that represent the connect icons for
  707. * the given <mxCellState>.
  708. *
  709. * Parameters:
  710. *
  711. * state - <mxCellState> whose connect icons should be returned.
  712. */
  713. mxConnectionHandler.prototype.createIcons = function(state)
  714. {
  715. var image = this.getConnectImage(state);
  716. if (image != null && state != null)
  717. {
  718. this.iconState = state;
  719. var icons = [];
  720. // Cannot use HTML for the connect icons because the icon receives all
  721. // mouse move events in IE, must use SVG instead even if the
  722. // connect-icon appears behind the selection border and the selection
  723. // border consumes the events before the icon gets a chance
  724. var bounds = new mxRectangle(0, 0, image.width, image.height);
  725. var icon = new mxImageShape(bounds, image.src, null, null, 0);
  726. icon.preserveImageAspect = false;
  727. if (this.isMoveIconToFrontForState(state))
  728. {
  729. icon.dialect = mxConstants.DIALECT_STRICTHTML;
  730. icon.init(this.graph.container);
  731. }
  732. else
  733. {
  734. icon.dialect = mxConstants.DIALECT_SVG;
  735. icon.init(this.graph.getView().getOverlayPane());
  736. // Move the icon back in the overlay pane
  737. if (this.moveIconBack && icon.node.previousSibling != null)
  738. {
  739. icon.node.parentNode.insertBefore(icon.node, icon.node.parentNode.firstChild);
  740. }
  741. }
  742. icon.node.style.cursor = mxConstants.CURSOR_CONNECT;
  743. // Events transparency
  744. var getState = mxUtils.bind(this, function()
  745. {
  746. return (this.currentState != null) ? this.currentState : state;
  747. });
  748. // Updates the local icon before firing the mouse down event.
  749. var mouseDown = mxUtils.bind(this, function(evt)
  750. {
  751. if (!mxEvent.isConsumed(evt))
  752. {
  753. this.icon = icon;
  754. this.graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
  755. new mxMouseEvent(evt, getState()));
  756. }
  757. });
  758. mxEvent.redirectMouseEvents(icon.node, this.graph, getState, mouseDown);
  759. icons.push(icon);
  760. this.redrawIcons(icons, this.iconState);
  761. return icons;
  762. }
  763. return null;
  764. };
  765. /**
  766. * Function: redrawIcons
  767. *
  768. * Redraws the given array of <mxImageShapes>.
  769. *
  770. * Parameters:
  771. *
  772. * icons - Optional array of <mxImageShapes> to be redrawn.
  773. */
  774. mxConnectionHandler.prototype.redrawIcons = function(icons, state)
  775. {
  776. if (icons != null && icons[0] != null && state != null)
  777. {
  778. var pos = this.getIconPosition(icons[0], state);
  779. icons[0].bounds.x = pos.x;
  780. icons[0].bounds.y = pos.y;
  781. icons[0].redraw();
  782. }
  783. };
  784. /**
  785. * Function: getIconPosition
  786. *
  787. * Returns the center position of the given icon.
  788. *
  789. * Parameters:
  790. *
  791. * icon - The connect icon of <mxImageShape> with the mouse.
  792. * state - <mxCellState> under the mouse.
  793. */
  794. mxConnectionHandler.prototype.getIconPosition = function(icon, state)
  795. {
  796. var scale = this.graph.getView().scale;
  797. var cx = state.getCenterX();
  798. var cy = state.getCenterY();
  799. if (this.graph.isSwimlane(state.cell))
  800. {
  801. var size = this.graph.getStartSize(state.cell);
  802. cx = (size.width != 0) ? state.x + size.width * scale / 2 : cx;
  803. cy = (size.height != 0) ? state.y + size.height * scale / 2 : cy;
  804. var alpha = mxUtils.toRadians(mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION) || 0);
  805. if (alpha != 0)
  806. {
  807. var cos = Math.cos(alpha);
  808. var sin = Math.sin(alpha);
  809. var ct = new mxPoint(state.getCenterX(), state.getCenterY());
  810. var pt = mxUtils.getRotatedPoint(new mxPoint(cx, cy), cos, sin, ct);
  811. cx = pt.x;
  812. cy = pt.y;
  813. }
  814. }
  815. return new mxPoint(cx - icon.bounds.width / 2,
  816. cy - icon.bounds.height / 2);
  817. };
  818. /**
  819. * Function: destroyIcons
  820. *
  821. * Destroys the connect icons and resets the respective state.
  822. */
  823. mxConnectionHandler.prototype.destroyIcons = function()
  824. {
  825. if (this.icons != null)
  826. {
  827. for (var i = 0; i < this.icons.length; i++)
  828. {
  829. this.icons[i].destroy();
  830. }
  831. this.icons = null;
  832. this.icon = null;
  833. this.selectedIcon = null;
  834. this.iconState = null;
  835. }
  836. };
  837. /**
  838. * Function: isStartEvent
  839. *
  840. * Returns true if the given mouse down event should start this handler. The
  841. * This implementation returns true if the event does not force marquee
  842. * selection, and the currentConstraint and currentFocus of the
  843. * <constraintHandler> are not null, or <previous> and <error> are not null and
  844. * <icons> is null or <icons> and <icon> are not null.
  845. */
  846. mxConnectionHandler.prototype.isStartEvent = function(me)
  847. {
  848. return ((this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null) ||
  849. (this.previous != null && this.error == null && (this.icons == null || (this.icons != null &&
  850. this.icon != null))));
  851. };
  852. /**
  853. * Function: mouseDown
  854. *
  855. * Handles the event by initiating a new connection.
  856. */
  857. mxConnectionHandler.prototype.mouseDown = function(sender, me)
  858. {
  859. this.mouseDownCounter++;
  860. if (this.isEnabled() && this.graph.isEnabled() && !me.isConsumed() &&
  861. !this.isConnecting() && this.isStartEvent(me))
  862. {
  863. if (this.constraintHandler.currentConstraint != null &&
  864. this.constraintHandler.currentFocus != null &&
  865. this.constraintHandler.currentPoint != null)
  866. {
  867. this.sourceConstraint = this.constraintHandler.currentConstraint;
  868. this.previous = this.constraintHandler.currentFocus;
  869. this.first = this.constraintHandler.currentPoint.clone();
  870. }
  871. else
  872. {
  873. // Stores the location of the initial mousedown
  874. this.first = new mxPoint(me.getGraphX(), me.getGraphY());
  875. }
  876. this.edgeState = this.createEdgeState(me);
  877. this.mouseDownCounter = 1;
  878. if (this.waypointsEnabled && this.shape == null)
  879. {
  880. this.waypoints = null;
  881. this.shape = this.createShape();
  882. if (this.edgeState != null)
  883. {
  884. this.shape.apply(this.edgeState);
  885. }
  886. }
  887. // Stores the starting point in the geometry of the preview
  888. if (this.previous == null && this.edgeState != null)
  889. {
  890. var pt = this.graph.getPointForEvent(me.getEvent());
  891. this.edgeState.cell.geometry.setTerminalPoint(pt, true);
  892. }
  893. this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
  894. me.consume();
  895. }
  896. this.selectedIcon = this.icon;
  897. this.icon = null;
  898. };
  899. /**
  900. * Function: isImmediateConnectSource
  901. *
  902. * Returns true if a tap on the given source state should immediately start
  903. * connecting. This implementation returns true if the state is not movable
  904. * in the graph.
  905. */
  906. mxConnectionHandler.prototype.isImmediateConnectSource = function(state)
  907. {
  908. return !this.graph.isCellMovable(state.cell);
  909. };
  910. /**
  911. * Function: createEdgeState
  912. *
  913. * Hook to return an <mxCellState> which may be used during the preview.
  914. * This implementation returns null.
  915. *
  916. * Use the following code to create a preview for an existing edge style:
  917. *
  918. * (code)
  919. * graph.connectionHandler.createEdgeState = function(me)
  920. * {
  921. * var edge = graph.createEdge(null, null, null, null, null, 'edgeStyle=elbowEdgeStyle');
  922. *
  923. * return new mxCellState(this.graph.view, edge, this.graph.getCellStyle(edge));
  924. * };
  925. * (end)
  926. */
  927. mxConnectionHandler.prototype.createEdgeState = function(me)
  928. {
  929. return null;
  930. };
  931. /**
  932. * Function: isOutlineConnectEvent
  933. *
  934. * Returns true if <outlineConnect> is true and the source of the event is the
  935. * outline shape or shift is pressed.
  936. */
  937. mxConnectionHandler.prototype.isOutlineConnectEvent = function(me)
  938. {
  939. if (mxEvent.isShiftDown(me.getEvent()) && mxEvent.isAltDown(me.getEvent()))
  940. {
  941. return false;
  942. }
  943. else
  944. {
  945. var offset = mxUtils.getOffset(this.graph.container);
  946. var evt = me.getEvent();
  947. var clientX = mxEvent.getClientX(evt);
  948. var clientY = mxEvent.getClientY(evt);
  949. var doc = document.documentElement;
  950. var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
  951. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  952. var gridX = this.currentPoint.x - this.graph.container.scrollLeft + offset.x - left;
  953. var gridY = this.currentPoint.y - this.graph.container.scrollTop + offset.y - top;
  954. return this.outlineConnect && ((mxEvent.isShiftDown(me.getEvent()) &&
  955. !mxEvent.isAltDown(me.getEvent())) || (me.isSource(this.marker.highlight.shape) ||
  956. (!mxEvent.isShiftDown(me.getEvent()) && mxEvent.isAltDown(me.getEvent()) &&
  957. me.getState() != null) || this.marker.highlight.isHighlightAt(clientX, clientY) ||
  958. ((gridX != clientX || gridY != clientY) && me.getState() == null &&
  959. this.marker.highlight.isHighlightAt(gridX, gridY))));
  960. }
  961. };
  962. /**
  963. * Function: updateCurrentState
  964. *
  965. * Updates the current state for a given mouse move event by using
  966. * the <marker>.
  967. */
  968. mxConnectionHandler.prototype.updateCurrentState = function(me, point)
  969. {
  970. this.constraintHandler.update(me, this.first == null, false, (this.first == null ||
  971. me.isSource(this.marker.highlight.shape)) ? null : point);
  972. if (this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null)
  973. {
  974. // Handles special case where grid is large and connection point is at actual point in which
  975. // case the outline is not followed as long as we're < gridSize / 2 away from that point
  976. if (this.marker.highlight != null && this.marker.highlight.state != null &&
  977. this.marker.highlight.state.cell == this.constraintHandler.currentFocus.cell)
  978. {
  979. // Direct repaint needed if cell already highlighted
  980. if (this.marker.highlight.shape.stroke != 'transparent')
  981. {
  982. this.marker.highlight.shape.stroke = 'transparent';
  983. this.marker.highlight.repaint();
  984. }
  985. }
  986. else
  987. {
  988. this.marker.markCell(this.constraintHandler.currentFocus.cell, 'transparent');
  989. }
  990. // Updates validation state
  991. if (this.previous != null)
  992. {
  993. this.error = this.validateConnection(this.previous.cell, this.constraintHandler.currentFocus.cell);
  994. if (this.error == null)
  995. {
  996. this.currentState = this.constraintHandler.currentFocus;
  997. }
  998. if (this.error != null || (this.currentState != null &&
  999. !this.isCellEnabled(this.currentState.cell)))
  1000. {
  1001. this.constraintHandler.reset();
  1002. }
  1003. }
  1004. }
  1005. else
  1006. {
  1007. if (this.graph.isIgnoreTerminalEvent(me.getEvent()))
  1008. {
  1009. this.marker.reset();
  1010. this.currentState = null;
  1011. }
  1012. else
  1013. {
  1014. this.marker.process(me);
  1015. this.currentState = this.marker.getValidState();
  1016. }
  1017. if (this.currentState != null && !this.isCellEnabled(this.currentState.cell))
  1018. {
  1019. this.constraintHandler.reset();
  1020. this.marker.reset();
  1021. this.currentState = null;
  1022. }
  1023. var outline = this.isOutlineConnectEvent(me);
  1024. if (this.currentState != null && outline)
  1025. {
  1026. // Handles special case where mouse is on outline away from actual end point
  1027. // in which case the grid is ignored and mouse point is used instead
  1028. if (me.isSource(this.marker.highlight.shape))
  1029. {
  1030. point = new mxPoint(me.getGraphX(), me.getGraphY());
  1031. }
  1032. var constraint = this.graph.getOutlineConstraint(point, this.currentState, me);
  1033. this.constraintHandler.setFocus(me, this.currentState, false);
  1034. this.constraintHandler.currentConstraint = constraint;
  1035. this.constraintHandler.currentPoint = point;
  1036. }
  1037. if (this.outlineConnect)
  1038. {
  1039. if (this.marker.highlight != null && this.marker.highlight.shape != null)
  1040. {
  1041. var s = this.graph.view.scale;
  1042. if (this.constraintHandler.currentConstraint != null &&
  1043. this.constraintHandler.currentFocus != null)
  1044. {
  1045. this.marker.highlight.shape.stroke = mxConstants.OUTLINE_HIGHLIGHT_COLOR;
  1046. this.marker.highlight.shape.strokewidth = mxConstants.OUTLINE_HIGHLIGHT_STROKEWIDTH / s / s;
  1047. this.marker.highlight.repaint();
  1048. }
  1049. else if (this.marker.hasValidState())
  1050. {
  1051. // Handles special case where actual end point of edge and current mouse point
  1052. // are not equal (due to grid snapping) and there is no hit on shape or highlight
  1053. // but ignores cases where parent is used for non-connectable child cells
  1054. if (this.graph.isCellConnectable(me.getCell()) &&
  1055. this.marker.getValidState() != me.getState())
  1056. {
  1057. this.marker.highlight.shape.stroke = 'transparent';
  1058. this.currentState = null;
  1059. }
  1060. else
  1061. {
  1062. this.marker.highlight.shape.stroke = mxConstants.DEFAULT_VALID_COLOR;
  1063. }
  1064. this.marker.highlight.shape.strokewidth = mxConstants.HIGHLIGHT_STROKEWIDTH / s / s;
  1065. this.marker.highlight.repaint();
  1066. }
  1067. }
  1068. }
  1069. }
  1070. };
  1071. /**
  1072. * Function: isCellEnabled
  1073. *
  1074. * Returns true if the given cell allows new connections to be created. This implementation
  1075. * always returns true.
  1076. */
  1077. mxConnectionHandler.prototype.isCellEnabled = function(cell)
  1078. {
  1079. return true;
  1080. };
  1081. /**
  1082. * Function: convertWaypoint
  1083. *
  1084. * Converts the given point from screen coordinates to model coordinates.
  1085. */
  1086. mxConnectionHandler.prototype.convertWaypoint = function(point)
  1087. {
  1088. var scale = this.graph.getView().getScale();
  1089. var tr = this.graph.getView().getTranslate();
  1090. point.x = point.x / scale - tr.x;
  1091. point.y = point.y / scale - tr.y;
  1092. };
  1093. /**
  1094. * Function: snapToPreview
  1095. *
  1096. * Called to snap the given point to the current preview. This snaps to the
  1097. * first point of the preview if alt is not pressed.
  1098. */
  1099. mxConnectionHandler.prototype.snapToPreview = function(me, point)
  1100. {
  1101. if (!mxEvent.isAltDown(me.getEvent()) && this.previous != null)
  1102. {
  1103. var tol = this.graph.gridSize * this.graph.view.scale / 2;
  1104. var tmp = (this.sourceConstraint != null) ? this.first :
  1105. new mxPoint(this.previous.getCenterX(), this.previous.getCenterY());
  1106. if (Math.abs(tmp.x - me.getGraphX()) < tol)
  1107. {
  1108. point.x = tmp.x;
  1109. }
  1110. if (Math.abs(tmp.y - me.getGraphY()) < tol)
  1111. {
  1112. point.y = tmp.y;
  1113. }
  1114. }
  1115. };
  1116. /**
  1117. * Function: mouseMove
  1118. *
  1119. * Handles the event by updating the preview edge or by highlighting
  1120. * a possible source or target terminal.
  1121. */
  1122. mxConnectionHandler.prototype.mouseMove = function(sender, me)
  1123. {
  1124. if (!me.isConsumed() && (this.ignoreMouseDown || this.first != null || !this.graph.isMouseDown))
  1125. {
  1126. // Handles special case when handler is disabled during highlight
  1127. if (!this.isEnabled() && this.currentState != null)
  1128. {
  1129. this.destroyIcons();
  1130. this.currentState = null;
  1131. }
  1132. var view = this.graph.getView();
  1133. var scale = view.scale;
  1134. var tr = view.translate;
  1135. var point = new mxPoint(me.getGraphX(), me.getGraphY());
  1136. this.error = null;
  1137. if (this.graph.isGridEnabledEvent(me.getEvent()))
  1138. {
  1139. point = new mxPoint((this.graph.snap(point.x / scale - tr.x) + tr.x) * scale,
  1140. (this.graph.snap(point.y / scale - tr.y) + tr.y) * scale);
  1141. }
  1142. this.snapToPreview(me, point);
  1143. this.currentPoint = point;
  1144. if ((this.first != null || (this.isEnabled() && this.graph.isEnabled())) &&
  1145. (this.shape != null || this.first == null ||
  1146. Math.abs(me.getGraphX() - this.first.x) > this.graph.tolerance ||
  1147. Math.abs(me.getGraphY() - this.first.y) > this.graph.tolerance))
  1148. {
  1149. this.updateCurrentState(me, point);
  1150. }
  1151. if (this.first != null)
  1152. {
  1153. var constraint = null;
  1154. var current = point;
  1155. // Uses the current point from the constraint handler if available
  1156. if (this.constraintHandler.currentConstraint != null &&
  1157. this.constraintHandler.currentFocus != null &&
  1158. this.constraintHandler.currentPoint != null)
  1159. {
  1160. constraint = this.constraintHandler.currentConstraint;
  1161. current = this.constraintHandler.currentPoint.clone();
  1162. }
  1163. else if (this.previous != null && mxEvent.isShiftDown(me.getEvent()) &&
  1164. !this.graph.isIgnoreTerminalEvent(me.getEvent()))
  1165. {
  1166. var pt = new mxPoint(this.previous.getCenterX(), this.previous.getCenterY());
  1167. if (this.sourceConstraint != null)
  1168. {
  1169. pt = this.first;
  1170. }
  1171. if (Math.abs(pt.x - point.x) <
  1172. Math.abs(pt.y - point.y))
  1173. {
  1174. point.x = pt.x;
  1175. }
  1176. else
  1177. {
  1178. point.y = pt.y;
  1179. }
  1180. }
  1181. var pt2 = this.first;
  1182. // Moves the connect icon with the mouse
  1183. if (this.selectedIcon != null)
  1184. {
  1185. var w = this.selectedIcon.bounds.width;
  1186. var h = this.selectedIcon.bounds.height;
  1187. if (this.currentState != null && this.targetConnectImage)
  1188. {
  1189. var pos = this.getIconPosition(this.selectedIcon, this.currentState);
  1190. this.selectedIcon.bounds.x = pos.x;
  1191. this.selectedIcon.bounds.y = pos.y;
  1192. }
  1193. else
  1194. {
  1195. var bounds = new mxRectangle(me.getGraphX() + this.connectIconOffset.x,
  1196. me.getGraphY() + this.connectIconOffset.y, w, h);
  1197. this.selectedIcon.bounds = bounds;
  1198. }
  1199. this.selectedIcon.redraw();
  1200. }
  1201. // Uses edge state to compute the terminal points
  1202. if (this.edgeState != null)
  1203. {
  1204. this.updateEdgeState(current, constraint);
  1205. current = this.edgeState.absolutePoints[this.edgeState.absolutePoints.length - 1];
  1206. pt2 = this.edgeState.absolutePoints[0];
  1207. }
  1208. else
  1209. {
  1210. if (this.currentState != null)
  1211. {
  1212. if (this.constraintHandler.currentConstraint == null)
  1213. {
  1214. var tmp = this.getTargetPerimeterPoint(this.currentState, me);
  1215. if (tmp != null)
  1216. {
  1217. current = tmp;
  1218. }
  1219. }
  1220. }
  1221. // Computes the source perimeter point
  1222. if (this.sourceConstraint == null && this.previous != null)
  1223. {
  1224. var next = (this.waypoints != null && this.waypoints.length > 0) ?
  1225. this.waypoints[0] : current;
  1226. var tmp = this.getSourcePerimeterPoint(this.previous, next, me);
  1227. if (tmp != null)
  1228. {
  1229. pt2 = tmp;
  1230. }
  1231. }
  1232. }
  1233. // Makes sure the cell under the mousepointer can be detected
  1234. // by moving the preview shape away from the mouse. This
  1235. // makes sure the preview shape does not prevent the detection
  1236. // of the cell under the mousepointer even for slow gestures.
  1237. if (this.currentState == null && this.movePreviewAway)
  1238. {
  1239. var tmp = pt2;
  1240. if (this.edgeState != null && this.edgeState.absolutePoints.length >= 2)
  1241. {
  1242. var tmp2 = this.edgeState.absolutePoints[this.edgeState.absolutePoints.length - 2];
  1243. if (tmp2 != null)
  1244. {
  1245. tmp = tmp2;
  1246. }
  1247. }
  1248. var dx = current.x - tmp.x;
  1249. var dy = current.y - tmp.y;
  1250. var len = Math.sqrt(dx * dx + dy * dy);
  1251. if (len == 0)
  1252. {
  1253. return;
  1254. }
  1255. // Stores old point to reuse when creating edge
  1256. this.originalPoint = current.clone();
  1257. current.x -= dx * 4 / len;
  1258. current.y -= dy * 4 / len;
  1259. }
  1260. else
  1261. {
  1262. this.originalPoint = null;
  1263. }
  1264. // Creates the preview shape (lazy)
  1265. if (this.shape == null)
  1266. {
  1267. var dx = Math.abs(me.getGraphX() - this.first.x);
  1268. var dy = Math.abs(me.getGraphY() - this.first.y);
  1269. if (dx > this.graph.tolerance || dy > this.graph.tolerance)
  1270. {
  1271. this.shape = this.createShape();
  1272. if (this.edgeState != null)
  1273. {
  1274. this.shape.apply(this.edgeState);
  1275. }
  1276. // Revalidates current connection
  1277. this.updateCurrentState(me, point);
  1278. }
  1279. }
  1280. // Updates the points in the preview edge
  1281. if (this.shape != null)
  1282. {
  1283. if (this.edgeState != null)
  1284. {
  1285. this.shape.points = this.edgeState.absolutePoints;
  1286. }
  1287. else
  1288. {
  1289. var pts = [pt2];
  1290. if (this.waypoints != null)
  1291. {
  1292. pts = pts.concat(this.waypoints);
  1293. }
  1294. pts.push(current);
  1295. this.shape.points = pts;
  1296. }
  1297. this.drawPreview();
  1298. }
  1299. // Makes sure endpoint of edge is visible during connect
  1300. if (this.cursor != null)
  1301. {
  1302. this.graph.container.style.cursor = this.cursor;
  1303. }
  1304. mxEvent.consume(me.getEvent());
  1305. me.consume();
  1306. }
  1307. else if (!this.isEnabled() || !this.graph.isEnabled())
  1308. {
  1309. this.constraintHandler.reset();
  1310. }
  1311. else if (this.previous != this.currentState && this.edgeState == null)
  1312. {
  1313. this.destroyIcons();
  1314. // Sets the cursor on the current shape
  1315. if (this.currentState != null && this.error == null && this.constraintHandler.currentConstraint == null)
  1316. {
  1317. this.icons = this.createIcons(this.currentState);
  1318. if (this.icons == null)
  1319. {
  1320. this.currentState.setCursor(mxConstants.CURSOR_CONNECT);
  1321. me.consume();
  1322. }
  1323. }
  1324. this.previous = this.currentState;
  1325. }
  1326. else if (this.previous == this.currentState && this.currentState != null && this.icons == null &&
  1327. !this.graph.isMouseDown)
  1328. {
  1329. // Makes sure that no cursors are changed
  1330. me.consume();
  1331. }
  1332. if (!this.graph.isMouseDown && this.currentState != null && this.icons != null)
  1333. {
  1334. var hitsIcon = false;
  1335. var target = me.getSource();
  1336. for (var i = 0; i < this.icons.length && !hitsIcon; i++)
  1337. {
  1338. hitsIcon = target == this.icons[i].node || target.parentNode == this.icons[i].node;
  1339. }
  1340. if (!hitsIcon)
  1341. {
  1342. this.updateIcons(this.currentState, this.icons, me);
  1343. }
  1344. }
  1345. }
  1346. else
  1347. {
  1348. this.constraintHandler.reset();
  1349. }
  1350. };
  1351. /**
  1352. * Function: updateEdgeState
  1353. *
  1354. * Updates <edgeState>.
  1355. */
  1356. mxConnectionHandler.prototype.updateEdgeState = function(current, constraint)
  1357. {
  1358. // TODO: Use generic method for writing constraint to style
  1359. if (this.sourceConstraint != null && this.sourceConstraint.point != null)
  1360. {
  1361. this.edgeState.style[mxConstants.STYLE_EXIT_X] = this.sourceConstraint.point.x;
  1362. this.edgeState.style[mxConstants.STYLE_EXIT_Y] = this.sourceConstraint.point.y;
  1363. }
  1364. if (constraint != null && constraint.point != null)
  1365. {
  1366. this.edgeState.style[mxConstants.STYLE_ENTRY_X] = constraint.point.x;
  1367. this.edgeState.style[mxConstants.STYLE_ENTRY_Y] = constraint.point.y;
  1368. }
  1369. else
  1370. {
  1371. delete this.edgeState.style[mxConstants.STYLE_ENTRY_X];
  1372. delete this.edgeState.style[mxConstants.STYLE_ENTRY_Y];
  1373. }
  1374. this.edgeState.absolutePoints = [null, (this.currentState != null) ? null : current];
  1375. this.graph.view.updateFixedTerminalPoint(this.edgeState, this.previous, true, this.sourceConstraint);
  1376. if (this.currentState != null)
  1377. {
  1378. if (constraint == null)
  1379. {
  1380. constraint = this.graph.getConnectionConstraint(this.edgeState, this.previous, false);
  1381. }
  1382. this.edgeState.setAbsoluteTerminalPoint(null, false);
  1383. this.graph.view.updateFixedTerminalPoint(this.edgeState, this.currentState, false, constraint);
  1384. }
  1385. // Scales and translates the waypoints to the model
  1386. var realPoints = null;
  1387. if (this.waypoints != null)
  1388. {
  1389. realPoints = [];
  1390. for (var i = 0; i < this.waypoints.length; i++)
  1391. {
  1392. var pt = this.waypoints[i].clone();
  1393. this.convertWaypoint(pt);
  1394. realPoints[i] = pt;
  1395. }
  1396. }
  1397. this.graph.view.updatePoints(this.edgeState, realPoints, this.previous, this.currentState);
  1398. this.graph.view.updateFloatingTerminalPoints(this.edgeState, this.previous, this.currentState);
  1399. };
  1400. /**
  1401. * Function: getTargetPerimeterPoint
  1402. *
  1403. * Returns the perimeter point for the given target state.
  1404. *
  1405. * Parameters:
  1406. *
  1407. * state - <mxCellState> that represents the target cell state.
  1408. * me - <mxMouseEvent> that represents the mouse move.
  1409. */
  1410. mxConnectionHandler.prototype.getTargetPerimeterPoint = function(state, me)
  1411. {
  1412. var result = null;
  1413. var view = state.view;
  1414. var targetPerimeter = view.getPerimeterFunction(state);
  1415. if (targetPerimeter != null)
  1416. {
  1417. var next = (this.waypoints != null && this.waypoints.length > 0) ?
  1418. this.waypoints[this.waypoints.length - 1] :
  1419. new mxPoint(this.previous.getCenterX(), this.previous.getCenterY());
  1420. var tmp = targetPerimeter(view.getPerimeterBounds(state),
  1421. this.edgeState, next, false);
  1422. if (tmp != null)
  1423. {
  1424. result = tmp;
  1425. }
  1426. }
  1427. else
  1428. {
  1429. result = new mxPoint(state.getCenterX(), state.getCenterY());
  1430. }
  1431. return result;
  1432. };
  1433. /**
  1434. * Function: getSourcePerimeterPoint
  1435. *
  1436. * Hook to update the icon position(s) based on a mouseOver event. This is
  1437. * an empty implementation.
  1438. *
  1439. * Parameters:
  1440. *
  1441. * state - <mxCellState> that represents the target cell state.
  1442. * next - <mxPoint> that represents the next point along the previewed edge.
  1443. * me - <mxMouseEvent> that represents the mouse move.
  1444. */
  1445. mxConnectionHandler.prototype.getSourcePerimeterPoint = function(state, next, me)
  1446. {
  1447. var result = null;
  1448. var view = state.view;
  1449. var sourcePerimeter = view.getPerimeterFunction(state);
  1450. var c = new mxPoint(state.getCenterX(), state.getCenterY());
  1451. if (sourcePerimeter != null)
  1452. {
  1453. var theta = mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION, 0);
  1454. var rad = -theta * (Math.PI / 180);
  1455. if (theta != 0)
  1456. {
  1457. next = mxUtils.getRotatedPoint(new mxPoint(next.x, next.y), Math.cos(rad), Math.sin(rad), c);
  1458. }
  1459. var tmp = sourcePerimeter(view.getPerimeterBounds(state), state, next, false);
  1460. if (tmp != null)
  1461. {
  1462. if (theta != 0)
  1463. {
  1464. tmp = mxUtils.getRotatedPoint(new mxPoint(tmp.x, tmp.y), Math.cos(-rad), Math.sin(-rad), c);
  1465. }
  1466. result = tmp;
  1467. }
  1468. }
  1469. else
  1470. {
  1471. result = c;
  1472. }
  1473. return result;
  1474. };
  1475. /**
  1476. * Function: updateIcons
  1477. *
  1478. * Hook to update the icon position(s) based on a mouseOver event. This is
  1479. * an empty implementation.
  1480. *
  1481. * Parameters:
  1482. *
  1483. * state - <mxCellState> under the mouse.
  1484. * icons - Array of currently displayed icons.
  1485. * me - <mxMouseEvent> that contains the mouse event.
  1486. */
  1487. mxConnectionHandler.prototype.updateIcons = function(state, icons, me)
  1488. {
  1489. // empty
  1490. };
  1491. /**
  1492. * Function: isStopEvent
  1493. *
  1494. * Returns true if the given mouse up event should stop this handler. The
  1495. * connection will be created if <error> is null. Note that this is only
  1496. * called if <waypointsEnabled> is true. This implemtation returns true
  1497. * if there is a cell state in the given event.
  1498. */
  1499. mxConnectionHandler.prototype.isStopEvent = function(me)
  1500. {
  1501. return me.getState() != null;
  1502. };
  1503. /**
  1504. * Function: addWaypoint
  1505. *
  1506. * Adds the waypoint for the given event to <waypoints>.
  1507. */
  1508. mxConnectionHandler.prototype.addWaypointForEvent = function(me)
  1509. {
  1510. var point = mxUtils.convertPoint(this.graph.container, me.getX(), me.getY());
  1511. var dx = Math.abs(point.x - this.first.x);
  1512. var dy = Math.abs(point.y - this.first.y);
  1513. var addPoint = this.waypoints != null || (this.mouseDownCounter > 1 &&
  1514. (dx > this.graph.tolerance || dy > this.graph.tolerance));
  1515. if (addPoint)
  1516. {
  1517. if (this.waypoints == null)
  1518. {
  1519. this.waypoints = [];
  1520. }
  1521. var scale = this.graph.view.scale;
  1522. var point = new mxPoint(this.graph.snap(me.getGraphX() / scale) * scale,
  1523. this.graph.snap(me.getGraphY() / scale) * scale);
  1524. this.waypoints.push(point);
  1525. }
  1526. };
  1527. /**
  1528. * Function: checkConstraints
  1529. *
  1530. * Returns true if the connection for the given constraints is valid. This
  1531. * implementation returns true if the constraints are not pointing to the
  1532. * same fixed connection point.
  1533. */
  1534. mxConnectionHandler.prototype.checkConstraints = function(c1, c2)
  1535. {
  1536. return (c1 == null || c2 == null || c1.point == null || c2.point == null ||
  1537. !c1.point.equals(c2.point) || c1.dx != c2.dx || c1.dy != c2.dy ||
  1538. c1.perimeter != c2.perimeter);
  1539. };
  1540. /**
  1541. * Function: mouseUp
  1542. *
  1543. * Handles the event by inserting the new connection.
  1544. */
  1545. mxConnectionHandler.prototype.mouseUp = function(sender, me)
  1546. {
  1547. if (!me.isConsumed() && this.isConnecting())
  1548. {
  1549. if (this.waypointsEnabled && !this.isStopEvent(me))
  1550. {
  1551. this.addWaypointForEvent(me);
  1552. me.consume();
  1553. return;
  1554. }
  1555. var c1 = this.sourceConstraint;
  1556. var c2 = this.constraintHandler.currentConstraint;
  1557. var source = (this.previous != null) ? this.previous.cell : null;
  1558. var target = null;
  1559. if (this.constraintHandler.currentConstraint != null &&
  1560. this.constraintHandler.currentFocus != null)
  1561. {
  1562. target = this.constraintHandler.currentFocus.cell;
  1563. }
  1564. if (target == null && this.currentState != null)
  1565. {
  1566. target = this.currentState.cell;
  1567. }
  1568. // Inserts the edge if no validation error exists and if constraints differ
  1569. if (this.error == null && (source == null || target == null ||
  1570. source != target || this.checkConstraints(c1, c2)))
  1571. {
  1572. this.connect(source, target, me.getEvent(), me.getCell());
  1573. }
  1574. else
  1575. {
  1576. // Selects the source terminal for self-references
  1577. if (this.previous != null && this.marker.validState != null &&
  1578. this.previous.cell == this.marker.validState.cell)
  1579. {
  1580. this.graph.selectCellForEvent(this.marker.source, me.getEvent());
  1581. }
  1582. // Displays the error message if it is not an empty string,
  1583. // for empty error messages, the event is silently dropped
  1584. if (this.error != null && this.error.length > 0)
  1585. {
  1586. this.graph.validationAlert(this.error);
  1587. }
  1588. }
  1589. // Redraws the connect icons and resets the handler state
  1590. this.destroyIcons();
  1591. me.consume();
  1592. }
  1593. if (this.first != null)
  1594. {
  1595. this.reset();
  1596. }
  1597. };
  1598. /**
  1599. * Function: reset
  1600. *
  1601. * Resets the state of this handler.
  1602. */
  1603. mxConnectionHandler.prototype.reset = function()
  1604. {
  1605. if (this.shape != null)
  1606. {
  1607. this.shape.destroy();
  1608. this.shape = null;
  1609. }
  1610. // Resets the cursor on the container
  1611. if (this.cursor != null && this.graph.container != null)
  1612. {
  1613. this.graph.container.style.cursor = '';
  1614. }
  1615. this.destroyIcons();
  1616. this.marker.reset();
  1617. this.constraintHandler.reset();
  1618. this.originalPoint = null;
  1619. this.currentPoint = null;
  1620. this.edgeState = null;
  1621. this.previous = null;
  1622. this.error = null;
  1623. this.sourceConstraint = null;
  1624. this.mouseDownCounter = 0;
  1625. this.first = null;
  1626. this.fireEvent(new mxEventObject(mxEvent.RESET));
  1627. };
  1628. /**
  1629. * Function: drawPreview
  1630. *
  1631. * Redraws the preview edge using the color and width returned by
  1632. * <getEdgeColor> and <getEdgeWidth>.
  1633. */
  1634. mxConnectionHandler.prototype.drawPreview = function()
  1635. {
  1636. this.updatePreview(this.error == null);
  1637. if (this.edgeState != null)
  1638. {
  1639. this.edgeState.shape = this.shape;
  1640. this.graph.cellRenderer.postConfigureShape(this.edgeState);
  1641. this.edgeState.shape = null;
  1642. }
  1643. this.shape.redraw();
  1644. };
  1645. /**
  1646. * Function: getEdgeColor
  1647. *
  1648. * Returns the color used to draw the preview edge. This returns green if
  1649. * there is no edge validation error and red otherwise.
  1650. *
  1651. * Parameters:
  1652. *
  1653. * valid - Boolean indicating if the color for a valid edge should be
  1654. * returned.
  1655. */
  1656. mxConnectionHandler.prototype.updatePreview = function(valid)
  1657. {
  1658. this.shape.strokewidth = this.getEdgeWidth(valid);
  1659. this.shape.stroke = this.getEdgeColor(valid);
  1660. };
  1661. /**
  1662. * Function: getEdgeColor
  1663. *
  1664. * Returns the color used to draw the preview edge. This returns green if
  1665. * there is no edge validation error and red otherwise.
  1666. *
  1667. * Parameters:
  1668. *
  1669. * valid - Boolean indicating if the color for a valid edge should be
  1670. * returned.
  1671. */
  1672. mxConnectionHandler.prototype.getEdgeColor = function(valid)
  1673. {
  1674. return (valid) ? mxConstants.VALID_COLOR : mxConstants.INVALID_COLOR;
  1675. };
  1676. /**
  1677. * Function: getEdgeWidth
  1678. *
  1679. * Returns the width used to draw the preview edge. This returns 3 if
  1680. * there is no edge validation error and 1 otherwise.
  1681. *
  1682. * Parameters:
  1683. *
  1684. * valid - Boolean indicating if the width for a valid edge should be
  1685. * returned.
  1686. */
  1687. mxConnectionHandler.prototype.getEdgeWidth = function(valid)
  1688. {
  1689. return (valid) ? 3 : 1;
  1690. };
  1691. /**
  1692. * Function: connect
  1693. *
  1694. * Connects the given source and target using a new edge. This
  1695. * implementation uses <createEdge> to create the edge.
  1696. *
  1697. * Parameters:
  1698. *
  1699. * source - <mxCell> that represents the source terminal.
  1700. * target - <mxCell> that represents the target terminal.
  1701. * evt - Mousedown event of the connect gesture.
  1702. * dropTarget - <mxCell> that represents the cell under the mouse when it was
  1703. * released.
  1704. */
  1705. mxConnectionHandler.prototype.connect = function(source, target, evt, dropTarget)
  1706. {
  1707. if (target != null || this.isCreateTarget(evt) || this.graph.allowDanglingEdges)
  1708. {
  1709. // Uses the common parent of source and target or
  1710. // the default parent to insert the edge
  1711. var model = this.graph.getModel();
  1712. var terminalInserted = false;
  1713. var edge = null;
  1714. model.beginUpdate();
  1715. try
  1716. {
  1717. if (source != null && target == null && !this.graph.isIgnoreTerminalEvent(evt) && this.isCreateTarget(evt))
  1718. {
  1719. target = this.createTargetVertex(evt, source);
  1720. if (target != null)
  1721. {
  1722. dropTarget = this.graph.getDropTarget([target], evt, dropTarget);
  1723. terminalInserted = true;
  1724. // Disables edges as drop targets if the target cell was created
  1725. // FIXME: Should not shift if vertex was aligned (same in Java)
  1726. if (dropTarget == null || !this.graph.getModel().isEdge(dropTarget))
  1727. {
  1728. var pstate = this.graph.getView().getState(dropTarget);
  1729. if (pstate != null)
  1730. {
  1731. var tmp = model.getGeometry(target);
  1732. tmp.x -= pstate.origin.x;
  1733. tmp.y -= pstate.origin.y;
  1734. }
  1735. }
  1736. else
  1737. {
  1738. dropTarget = this.graph.getDefaultParent();
  1739. }
  1740. this.graph.addCell(target, dropTarget);
  1741. }
  1742. }
  1743. var parent = this.graph.getDefaultParent();
  1744. var refSource = this.graph.getReferenceTerminal(source);
  1745. var refTarget = this.graph.getReferenceTerminal(target);
  1746. var refParent = parent;
  1747. if (refSource != null && refTarget != null)
  1748. {
  1749. refParent = model.getNearestCommonAncestor(refSource, refTarget);
  1750. }
  1751. else if (refSource != null)
  1752. {
  1753. refParent = model.getParent(refSource);
  1754. }
  1755. if (refParent != null && !model.isEdge(refParent) &&
  1756. refParent != model.getRoot())
  1757. {
  1758. parent = refParent;
  1759. }
  1760. // Uses the value of the preview edge state for inserting
  1761. // the new edge into the graph
  1762. var value = null;
  1763. var style = null;
  1764. if (this.edgeState != null)
  1765. {
  1766. value = this.edgeState.cell.value;
  1767. style = this.edgeState.cell.style;
  1768. }
  1769. edge = this.insertEdge(parent, null, value, source, target, style);
  1770. if (edge != null)
  1771. {
  1772. // Updates the connection constraints
  1773. this.graph.setConnectionConstraint(edge, source, true, this.sourceConstraint);
  1774. this.graph.setConnectionConstraint(edge, target, false, this.constraintHandler.currentConstraint);
  1775. // Uses geometry of the preview edge state
  1776. if (this.edgeState != null)
  1777. {
  1778. model.setGeometry(edge, this.edgeState.cell.geometry);
  1779. }
  1780. // Inserts non-overlapping edge before source
  1781. if (this.isInsertBefore(edge, source, target, evt, dropTarget) &&
  1782. (this.constraintHandler.currentConstraint == null ||
  1783. this.constraintHandler.currentConstraint.perimeter))
  1784. {
  1785. var tmp = source;
  1786. while (tmp.parent != null && tmp.geometry != null &&
  1787. tmp.geometry.relative && tmp.parent != edge.parent)
  1788. {
  1789. tmp = this.graph.model.getParent(tmp);
  1790. }
  1791. if (tmp != null && tmp.parent != null && tmp.parent == edge.parent)
  1792. {
  1793. model.add(parent, edge, tmp.parent.getIndex(tmp));
  1794. }
  1795. }
  1796. // Makes sure the edge has a non-null, relative geometry
  1797. var geo = model.getGeometry(edge);
  1798. if (geo == null)
  1799. {
  1800. geo = new mxGeometry();
  1801. geo.relative = true;
  1802. model.setGeometry(edge, geo);
  1803. }
  1804. // Uses scaled waypoints in geometry
  1805. if (this.waypoints != null && this.waypoints.length > 0)
  1806. {
  1807. var s = this.graph.view.scale;
  1808. var tr = this.graph.view.translate;
  1809. geo.points = [];
  1810. for (var i = 0; i < this.waypoints.length; i++)
  1811. {
  1812. var pt = this.waypoints[i];
  1813. geo.points.push(new mxPoint(pt.x / s - tr.x, pt.y / s - tr.y));
  1814. }
  1815. }
  1816. if (target == null)
  1817. {
  1818. var t = this.graph.view.translate;
  1819. var s = this.graph.view.scale;
  1820. var pt = (this.originalPoint != null) ?
  1821. new mxPoint(this.originalPoint.x / s - t.x, this.originalPoint.y / s - t.y) :
  1822. new mxPoint(this.currentPoint.x / s - t.x, this.currentPoint.y / s - t.y);
  1823. pt.x -= this.graph.panDx / this.graph.view.scale;
  1824. pt.y -= this.graph.panDy / this.graph.view.scale;
  1825. var pstate = this.graph.getView().getState(model.getParent(edge))
  1826. if (pstate != null)
  1827. {
  1828. pt.x -= pstate.origin.x;
  1829. pt.y -= pstate.origin.y;
  1830. }
  1831. geo.setTerminalPoint(pt, false);
  1832. }
  1833. this.fireEvent(new mxEventObject(mxEvent.CONNECT, 'cell', edge, 'terminal', target,
  1834. 'event', evt, 'target', dropTarget, 'terminalInserted', terminalInserted));
  1835. }
  1836. }
  1837. catch (e)
  1838. {
  1839. mxLog.show();
  1840. mxLog.debug(e.message);
  1841. }
  1842. finally
  1843. {
  1844. model.endUpdate();
  1845. }
  1846. if (this.select)
  1847. {
  1848. this.selectCells(edge, (terminalInserted) ? target : null);
  1849. }
  1850. }
  1851. };
  1852. /**
  1853. * Function: selectCells
  1854. *
  1855. * Selects the given edge after adding a new connection. The target argument
  1856. * contains the target vertex if one has been inserted.
  1857. */
  1858. mxConnectionHandler.prototype.selectCells = function(edge, target)
  1859. {
  1860. this.graph.setSelectionCell(edge);
  1861. };
  1862. /**
  1863. * Function: insertEdge
  1864. *
  1865. * Creates, inserts and returns the new edge for the given parameters. This
  1866. * implementation does only use <createEdge> if <factoryMethod> is defined,
  1867. * otherwise <mxGraph.insertEdge> will be used.
  1868. */
  1869. mxConnectionHandler.prototype.insertEdge = function(parent, id, value, source, target, style)
  1870. {
  1871. if (this.factoryMethod == null)
  1872. {
  1873. return this.graph.insertEdge(parent, id, value, source, target, style);
  1874. }
  1875. else
  1876. {
  1877. var edge = this.createEdge(value, source, target, style);
  1878. edge = this.graph.addEdge(edge, parent, source, target);
  1879. return edge;
  1880. }
  1881. };
  1882. /**
  1883. * Function: createTargetVertex
  1884. *
  1885. * Hook method for creating new vertices on the fly if no target was
  1886. * under the mouse. This is only called if <createTarget> is true and
  1887. * returns null.
  1888. *
  1889. * Parameters:
  1890. *
  1891. * evt - Mousedown event of the connect gesture.
  1892. * source - <mxCell> that represents the source terminal.
  1893. */
  1894. mxConnectionHandler.prototype.createTargetVertex = function(evt, source)
  1895. {
  1896. // Uses the first non-relative source
  1897. var geo = this.graph.getCellGeometry(source);
  1898. while (geo != null && geo.relative)
  1899. {
  1900. source = this.graph.getModel().getParent(source);
  1901. geo = this.graph.getCellGeometry(source);
  1902. }
  1903. var clone = this.graph.cloneCell(source);
  1904. var geo = this.graph.getModel().getGeometry(clone);
  1905. if (geo != null)
  1906. {
  1907. var t = this.graph.view.translate;
  1908. var s = this.graph.view.scale;
  1909. var point = new mxPoint(this.currentPoint.x / s - t.x, this.currentPoint.y / s - t.y);
  1910. geo.x = Math.round(point.x - geo.width / 2 - this.graph.panDx / s);
  1911. geo.y = Math.round(point.y - geo.height / 2 - this.graph.panDy / s);
  1912. // Aligns with source if within certain tolerance
  1913. var tol = this.getAlignmentTolerance();
  1914. if (tol > 0)
  1915. {
  1916. var sourceState = this.graph.view.getState(source);
  1917. if (sourceState != null)
  1918. {
  1919. var x = sourceState.x / s - t.x;
  1920. var y = sourceState.y / s - t.y;
  1921. if (Math.abs(x - geo.x) <= tol)
  1922. {
  1923. geo.x = Math.round(x);
  1924. }
  1925. if (Math.abs(y - geo.y) <= tol)
  1926. {
  1927. geo.y = Math.round(y);
  1928. }
  1929. }
  1930. }
  1931. }
  1932. return clone;
  1933. };
  1934. /**
  1935. * Function: getAlignmentTolerance
  1936. *
  1937. * Returns the tolerance for aligning new targets to sources. This returns the grid size / 2.
  1938. */
  1939. mxConnectionHandler.prototype.getAlignmentTolerance = function(evt)
  1940. {
  1941. return (this.graph.isGridEnabled()) ? this.graph.gridSize / 2 : this.graph.tolerance;
  1942. };
  1943. /**
  1944. * Function: createEdge
  1945. *
  1946. * Creates and returns a new edge using <factoryMethod> if one exists. If
  1947. * no factory method is defined, then a new default edge is returned. The
  1948. * source and target arguments are informal, the actual connection is
  1949. * setup later by the caller of this function.
  1950. *
  1951. * Parameters:
  1952. *
  1953. * value - Value to be used for creating the edge.
  1954. * source - <mxCell> that represents the source terminal.
  1955. * target - <mxCell> that represents the target terminal.
  1956. * style - Optional style from the preview edge.
  1957. */
  1958. mxConnectionHandler.prototype.createEdge = function(value, source, target, style)
  1959. {
  1960. var edge = null;
  1961. // Creates a new edge using the factoryMethod
  1962. if (this.factoryMethod != null)
  1963. {
  1964. edge = this.factoryMethod(source, target, style);
  1965. }
  1966. if (edge == null)
  1967. {
  1968. edge = new mxCell(value || '');
  1969. edge.setEdge(true);
  1970. edge.setStyle(style);
  1971. var geo = new mxGeometry();
  1972. geo.relative = true;
  1973. edge.setGeometry(geo);
  1974. }
  1975. return edge;
  1976. };
  1977. /**
  1978. * Function: destroy
  1979. *
  1980. * Destroys the handler and all its resources and DOM nodes. This should be
  1981. * called on all instances. It is called automatically for the built-in
  1982. * instance created for each <mxGraph>.
  1983. */
  1984. mxConnectionHandler.prototype.destroy = function()
  1985. {
  1986. this.graph.removeMouseListener(this);
  1987. if (this.shape != null)
  1988. {
  1989. this.shape.destroy();
  1990. this.shape = null;
  1991. }
  1992. if (this.marker != null)
  1993. {
  1994. this.marker.destroy();
  1995. this.marker = null;
  1996. }
  1997. if (this.constraintHandler != null)
  1998. {
  1999. this.constraintHandler.destroy();
  2000. this.constraintHandler = null;
  2001. }
  2002. if (this.changeHandler != null)
  2003. {
  2004. this.graph.getModel().removeListener(this.changeHandler);
  2005. this.graph.getView().removeListener(this.changeHandler);
  2006. this.changeHandler = null;
  2007. }
  2008. if (this.drillHandler != null)
  2009. {
  2010. this.graph.removeListener(this.drillHandler);
  2011. this.graph.getView().removeListener(this.drillHandler);
  2012. this.drillHandler = null;
  2013. }
  2014. if (this.escapeHandler != null)
  2015. {
  2016. this.graph.removeListener(this.escapeHandler);
  2017. this.escapeHandler = null;
  2018. }
  2019. };