mxConnectionHandler.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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 <mxClient.IS_VML>.
  331. */
  332. mxConnectionHandler.prototype.movePreviewAway = mxClient.IS_VML;
  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. };
  403. /**
  404. * Function: isCreateTarget
  405. *
  406. * Returns <createTarget>.
  407. *
  408. * Parameters:
  409. *
  410. * evt - Current active native pointer event.
  411. */
  412. mxConnectionHandler.prototype.isCreateTarget = function(evt)
  413. {
  414. return this.createTarget;
  415. };
  416. /**
  417. * Function: setCreateTarget
  418. *
  419. * Sets <createTarget>.
  420. */
  421. mxConnectionHandler.prototype.setCreateTarget = function(value)
  422. {
  423. this.createTarget = value;
  424. };
  425. /**
  426. * Function: createShape
  427. *
  428. * Creates the preview shape for new connections.
  429. */
  430. mxConnectionHandler.prototype.createShape = function()
  431. {
  432. // Creates the edge preview
  433. var shape = (this.livePreview && this.edgeState != null) ?
  434. this.graph.cellRenderer.createShape(this.edgeState) :
  435. new mxPolyline([], mxConstants.INVALID_COLOR);
  436. shape.dialect = (this.graph.dialect != mxConstants.DIALECT_SVG) ?
  437. mxConstants.DIALECT_VML : mxConstants.DIALECT_SVG;
  438. shape.scale = this.graph.view.scale;
  439. shape.pointerEvents = false;
  440. shape.isDashed = true;
  441. shape.init(this.graph.getView().getOverlayPane());
  442. mxEvent.redirectMouseEvents(shape.node, this.graph, null);
  443. return shape;
  444. };
  445. /**
  446. * Function: init
  447. *
  448. * Initializes the shapes required for this connection handler. This should
  449. * be invoked if <mxGraph.container> is assigned after the connection
  450. * handler has been created.
  451. */
  452. mxConnectionHandler.prototype.init = function()
  453. {
  454. this.graph.addMouseListener(this);
  455. this.marker = this.createMarker();
  456. this.constraintHandler = new mxConstraintHandler(this.graph);
  457. // Redraws the icons if the graph changes
  458. this.changeHandler = mxUtils.bind(this, function(sender)
  459. {
  460. if (this.iconState != null)
  461. {
  462. this.iconState = this.graph.getView().getState(this.iconState.cell);
  463. }
  464. if (this.iconState != null)
  465. {
  466. this.redrawIcons(this.icons, this.iconState);
  467. this.constraintHandler.reset();
  468. }
  469. else if (this.previous != null && this.graph.view.getState(this.previous.cell) == null)
  470. {
  471. this.reset();
  472. }
  473. });
  474. this.graph.getModel().addListener(mxEvent.CHANGE, this.changeHandler);
  475. this.graph.getView().addListener(mxEvent.SCALE, this.changeHandler);
  476. this.graph.getView().addListener(mxEvent.TRANSLATE, this.changeHandler);
  477. this.graph.getView().addListener(mxEvent.SCALE_AND_TRANSLATE, this.changeHandler);
  478. // Removes the icon if we step into/up or start editing
  479. this.drillHandler = mxUtils.bind(this, function(sender)
  480. {
  481. this.reset();
  482. });
  483. this.graph.addListener(mxEvent.START_EDITING, this.drillHandler);
  484. this.graph.getView().addListener(mxEvent.DOWN, this.drillHandler);
  485. this.graph.getView().addListener(mxEvent.UP, this.drillHandler);
  486. };
  487. /**
  488. * Function: isConnectableCell
  489. *
  490. * Returns true if the given cell is connectable. This is a hook to
  491. * disable floating connections. This implementation returns true.
  492. */
  493. mxConnectionHandler.prototype.isConnectableCell = function(cell)
  494. {
  495. return true;
  496. };
  497. /**
  498. * Function: createMarker
  499. *
  500. * Creates and returns the <mxCellMarker> used in <marker>.
  501. */
  502. mxConnectionHandler.prototype.createMarker = function()
  503. {
  504. var marker = new mxCellMarker(this.graph);
  505. marker.hotspotEnabled = true;
  506. // Overrides to return cell at location only if valid (so that
  507. // there is no highlight for invalid cells)
  508. marker.getCell = mxUtils.bind(this, function(me)
  509. {
  510. var cell = mxCellMarker.prototype.getCell.apply(marker, arguments);
  511. this.error = null;
  512. // Checks for cell at preview point (with grid)
  513. if (cell == null && this.currentPoint != null)
  514. {
  515. cell = this.graph.getCellAt(this.currentPoint.x, this.currentPoint.y);
  516. }
  517. // Uses connectable parent vertex if one exists
  518. if (cell != null && !this.graph.isCellConnectable(cell))
  519. {
  520. var parent = this.graph.getModel().getParent(cell);
  521. if (this.graph.getModel().isVertex(parent) && this.graph.isCellConnectable(parent))
  522. {
  523. cell = parent;
  524. }
  525. }
  526. if ((this.graph.isSwimlane(cell) && this.currentPoint != null &&
  527. this.graph.hitsSwimlaneContent(cell, this.currentPoint.x, this.currentPoint.y)) ||
  528. !this.isConnectableCell(cell))
  529. {
  530. cell = null;
  531. }
  532. if (cell != null)
  533. {
  534. if (this.isConnecting())
  535. {
  536. if (this.previous != null)
  537. {
  538. this.error = this.validateConnection(this.previous.cell, cell);
  539. if (this.error != null && this.error.length == 0)
  540. {
  541. cell = null;
  542. // Enables create target inside groups
  543. if (this.isCreateTarget(me.getEvent()))
  544. {
  545. this.error = null;
  546. }
  547. }
  548. }
  549. }
  550. else if (!this.isValidSource(cell, me))
  551. {
  552. cell = null;
  553. }
  554. }
  555. else if (this.isConnecting() && !this.isCreateTarget(me.getEvent()) &&
  556. !this.graph.allowDanglingEdges)
  557. {
  558. this.error = '';
  559. }
  560. return cell;
  561. });
  562. // Sets the highlight color according to validateConnection
  563. marker.isValidState = mxUtils.bind(this, function(state)
  564. {
  565. if (this.isConnecting())
  566. {
  567. return this.error == null;
  568. }
  569. else
  570. {
  571. return mxCellMarker.prototype.isValidState.apply(marker, arguments);
  572. }
  573. });
  574. // Overrides to use marker color only in highlight mode or for
  575. // target selection
  576. marker.getMarkerColor = mxUtils.bind(this, function(evt, state, isValid)
  577. {
  578. return (this.connectImage == null || this.isConnecting()) ?
  579. mxCellMarker.prototype.getMarkerColor.apply(marker, arguments) :
  580. null;
  581. });
  582. // Overrides to use hotspot only for source selection otherwise
  583. // intersects always returns true when over a cell
  584. marker.intersects = mxUtils.bind(this, function(state, evt)
  585. {
  586. if (this.connectImage != null || this.isConnecting())
  587. {
  588. return true;
  589. }
  590. return mxCellMarker.prototype.intersects.apply(marker, arguments);
  591. });
  592. return marker;
  593. };
  594. /**
  595. * Function: start
  596. *
  597. * Starts a new connection for the given state and coordinates.
  598. */
  599. mxConnectionHandler.prototype.start = function(state, x, y, edgeState)
  600. {
  601. this.previous = state;
  602. this.first = new mxPoint(x, y);
  603. this.edgeState = (edgeState != null) ? edgeState : this.createEdgeState(null);
  604. // Marks the source state
  605. this.marker.currentColor = this.marker.validColor;
  606. this.marker.markedState = state;
  607. this.marker.mark();
  608. this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
  609. };
  610. /**
  611. * Function: isConnecting
  612. *
  613. * Returns true if the source terminal has been clicked and a new
  614. * connection is currently being previewed.
  615. */
  616. mxConnectionHandler.prototype.isConnecting = function()
  617. {
  618. return this.first != null && this.shape != null;
  619. };
  620. /**
  621. * Function: isValidSource
  622. *
  623. * Returns <mxGraph.isValidSource> for the given source terminal.
  624. *
  625. * Parameters:
  626. *
  627. * cell - <mxCell> that represents the source terminal.
  628. * me - <mxMouseEvent> that is associated with this call.
  629. */
  630. mxConnectionHandler.prototype.isValidSource = function(cell, me)
  631. {
  632. return this.graph.isValidSource(cell);
  633. };
  634. /**
  635. * Function: isValidTarget
  636. *
  637. * Returns true. The call to <mxGraph.isValidTarget> is implicit by calling
  638. * <mxGraph.getEdgeValidationError> in <validateConnection>. This is an
  639. * additional hook for disabling certain targets in this specific handler.
  640. *
  641. * Parameters:
  642. *
  643. * cell - <mxCell> that represents the target terminal.
  644. */
  645. mxConnectionHandler.prototype.isValidTarget = function(cell)
  646. {
  647. return true;
  648. };
  649. /**
  650. * Function: validateConnection
  651. *
  652. * Returns the error message or an empty string if the connection for the
  653. * given source target pair is not valid. Otherwise it returns null. This
  654. * implementation uses <mxGraph.getEdgeValidationError>.
  655. *
  656. * Parameters:
  657. *
  658. * source - <mxCell> that represents the source terminal.
  659. * target - <mxCell> that represents the target terminal.
  660. */
  661. mxConnectionHandler.prototype.validateConnection = function(source, target)
  662. {
  663. if (!this.isValidTarget(target))
  664. {
  665. return '';
  666. }
  667. return this.graph.getEdgeValidationError(null, source, target);
  668. };
  669. /**
  670. * Function: getConnectImage
  671. *
  672. * Hook to return the <mxImage> used for the connection icon of the given
  673. * <mxCellState>. This implementation returns <connectImage>.
  674. *
  675. * Parameters:
  676. *
  677. * state - <mxCellState> whose connect image should be returned.
  678. */
  679. mxConnectionHandler.prototype.getConnectImage = function(state)
  680. {
  681. return this.connectImage;
  682. };
  683. /**
  684. * Function: isMoveIconToFrontForState
  685. *
  686. * Returns true if the state has a HTML label in the graph's container, otherwise
  687. * it returns <moveIconFront>.
  688. *
  689. * Parameters:
  690. *
  691. * state - <mxCellState> whose connect icons should be returned.
  692. */
  693. mxConnectionHandler.prototype.isMoveIconToFrontForState = function(state)
  694. {
  695. if (state.text != null && state.text.node.parentNode == this.graph.container)
  696. {
  697. return true;
  698. }
  699. return this.moveIconFront;
  700. };
  701. /**
  702. * Function: createIcons
  703. *
  704. * Creates the array <mxImageShapes> that represent the connect icons for
  705. * the given <mxCellState>.
  706. *
  707. * Parameters:
  708. *
  709. * state - <mxCellState> whose connect icons should be returned.
  710. */
  711. mxConnectionHandler.prototype.createIcons = function(state)
  712. {
  713. var image = this.getConnectImage(state);
  714. if (image != null && state != null)
  715. {
  716. this.iconState = state;
  717. var icons = [];
  718. // Cannot use HTML for the connect icons because the icon receives all
  719. // mouse move events in IE, must use VML and SVG instead even if the
  720. // connect-icon appears behind the selection border and the selection
  721. // border consumes the events before the icon gets a chance
  722. var bounds = new mxRectangle(0, 0, image.width, image.height);
  723. var icon = new mxImageShape(bounds, image.src, null, null, 0);
  724. icon.preserveImageAspect = false;
  725. if (this.isMoveIconToFrontForState(state))
  726. {
  727. icon.dialect = mxConstants.DIALECT_STRICTHTML;
  728. icon.init(this.graph.container);
  729. }
  730. else
  731. {
  732. icon.dialect = (this.graph.dialect == mxConstants.DIALECT_SVG) ?
  733. mxConstants.DIALECT_SVG : mxConstants.DIALECT_VML;
  734. icon.init(this.graph.getView().getOverlayPane());
  735. // Move the icon back in the overlay pane
  736. if (this.moveIconBack && icon.node.previousSibling != null)
  737. {
  738. icon.node.parentNode.insertBefore(icon.node, icon.node.parentNode.firstChild);
  739. }
  740. }
  741. icon.node.style.cursor = mxConstants.CURSOR_CONNECT;
  742. // Events transparency
  743. var getState = mxUtils.bind(this, function()
  744. {
  745. return (this.currentState != null) ? this.currentState : state;
  746. });
  747. // Updates the local icon before firing the mouse down event.
  748. var mouseDown = mxUtils.bind(this, function(evt)
  749. {
  750. if (!mxEvent.isConsumed(evt))
  751. {
  752. this.icon = icon;
  753. this.graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
  754. new mxMouseEvent(evt, getState()));
  755. }
  756. });
  757. mxEvent.redirectMouseEvents(icon.node, this.graph, getState, mouseDown);
  758. icons.push(icon);
  759. this.redrawIcons(icons, this.iconState);
  760. return icons;
  761. }
  762. return null;
  763. };
  764. /**
  765. * Function: redrawIcons
  766. *
  767. * Redraws the given array of <mxImageShapes>.
  768. *
  769. * Parameters:
  770. *
  771. * icons - Optional array of <mxImageShapes> to be redrawn.
  772. */
  773. mxConnectionHandler.prototype.redrawIcons = function(icons, state)
  774. {
  775. if (icons != null && icons[0] != null && state != null)
  776. {
  777. var pos = this.getIconPosition(icons[0], state);
  778. icons[0].bounds.x = pos.x;
  779. icons[0].bounds.y = pos.y;
  780. icons[0].redraw();
  781. }
  782. };
  783. /**
  784. * Function: getIconPosition
  785. *
  786. * Returns the center position of the given icon.
  787. *
  788. * Parameters:
  789. *
  790. * icon - The connect icon of <mxImageShape> with the mouse.
  791. * state - <mxCellState> under the mouse.
  792. */
  793. mxConnectionHandler.prototype.getIconPosition = function(icon, state)
  794. {
  795. var scale = this.graph.getView().scale;
  796. var cx = state.getCenterX();
  797. var cy = state.getCenterY();
  798. if (this.graph.isSwimlane(state.cell))
  799. {
  800. var size = this.graph.getStartSize(state.cell);
  801. cx = (size.width != 0) ? state.x + size.width * scale / 2 : cx;
  802. cy = (size.height != 0) ? state.y + size.height * scale / 2 : cy;
  803. var alpha = mxUtils.toRadians(mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION) || 0);
  804. if (alpha != 0)
  805. {
  806. var cos = Math.cos(alpha);
  807. var sin = Math.sin(alpha);
  808. var ct = new mxPoint(state.getCenterX(), state.getCenterY());
  809. var pt = mxUtils.getRotatedPoint(new mxPoint(cx, cy), cos, sin, ct);
  810. cx = pt.x;
  811. cy = pt.y;
  812. }
  813. }
  814. return new mxPoint(cx - icon.bounds.width / 2,
  815. cy - icon.bounds.height / 2);
  816. };
  817. /**
  818. * Function: destroyIcons
  819. *
  820. * Destroys the connect icons and resets the respective state.
  821. */
  822. mxConnectionHandler.prototype.destroyIcons = function()
  823. {
  824. if (this.icons != null)
  825. {
  826. for (var i = 0; i < this.icons.length; i++)
  827. {
  828. this.icons[i].destroy();
  829. }
  830. this.icons = null;
  831. this.icon = null;
  832. this.selectedIcon = null;
  833. this.iconState = null;
  834. }
  835. };
  836. /**
  837. * Function: isStartEvent
  838. *
  839. * Returns true if the given mouse down event should start this handler. The
  840. * This implementation returns true if the event does not force marquee
  841. * selection, and the currentConstraint and currentFocus of the
  842. * <constraintHandler> are not null, or <previous> and <error> are not null and
  843. * <icons> is null or <icons> and <icon> are not null.
  844. */
  845. mxConnectionHandler.prototype.isStartEvent = function(me)
  846. {
  847. return ((this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null) ||
  848. (this.previous != null && this.error == null && (this.icons == null || (this.icons != null &&
  849. this.icon != null))));
  850. };
  851. /**
  852. * Function: mouseDown
  853. *
  854. * Handles the event by initiating a new connection.
  855. */
  856. mxConnectionHandler.prototype.mouseDown = function(sender, me)
  857. {
  858. this.mouseDownCounter++;
  859. if (this.isEnabled() && this.graph.isEnabled() && !me.isConsumed() &&
  860. !this.isConnecting() && this.isStartEvent(me))
  861. {
  862. if (this.constraintHandler.currentConstraint != null &&
  863. this.constraintHandler.currentFocus != null &&
  864. this.constraintHandler.currentPoint != null)
  865. {
  866. this.sourceConstraint = this.constraintHandler.currentConstraint;
  867. this.previous = this.constraintHandler.currentFocus;
  868. this.first = this.constraintHandler.currentPoint.clone();
  869. }
  870. else
  871. {
  872. // Stores the location of the initial mousedown
  873. this.first = new mxPoint(me.getGraphX(), me.getGraphY());
  874. }
  875. this.edgeState = this.createEdgeState(me);
  876. this.mouseDownCounter = 1;
  877. if (this.waypointsEnabled && this.shape == null)
  878. {
  879. this.waypoints = null;
  880. this.shape = this.createShape();
  881. if (this.edgeState != null)
  882. {
  883. this.shape.apply(this.edgeState);
  884. }
  885. }
  886. // Stores the starting point in the geometry of the preview
  887. if (this.previous == null && this.edgeState != null)
  888. {
  889. var pt = this.graph.getPointForEvent(me.getEvent());
  890. this.edgeState.cell.geometry.setTerminalPoint(pt, true);
  891. }
  892. this.fireEvent(new mxEventObject(mxEvent.START, 'state', this.previous));
  893. me.consume();
  894. }
  895. this.selectedIcon = this.icon;
  896. this.icon = null;
  897. };
  898. /**
  899. * Function: isImmediateConnectSource
  900. *
  901. * Returns true if a tap on the given source state should immediately start
  902. * connecting. This implementation returns true if the state is not movable
  903. * in the graph.
  904. */
  905. mxConnectionHandler.prototype.isImmediateConnectSource = function(state)
  906. {
  907. return !this.graph.isCellMovable(state.cell);
  908. };
  909. /**
  910. * Function: createEdgeState
  911. *
  912. * Hook to return an <mxCellState> which may be used during the preview.
  913. * This implementation returns null.
  914. *
  915. * Use the following code to create a preview for an existing edge style:
  916. *
  917. * (code)
  918. * graph.connectionHandler.createEdgeState = function(me)
  919. * {
  920. * var edge = graph.createEdge(null, null, null, null, null, 'edgeStyle=elbowEdgeStyle');
  921. *
  922. * return new mxCellState(this.graph.view, edge, this.graph.getCellStyle(edge));
  923. * };
  924. * (end)
  925. */
  926. mxConnectionHandler.prototype.createEdgeState = function(me)
  927. {
  928. return null;
  929. };
  930. /**
  931. * Function: isOutlineConnectEvent
  932. *
  933. * Returns true if <outlineConnect> is true and the source of the event is the outline shape
  934. * or shift is pressed.
  935. */
  936. mxConnectionHandler.prototype.isOutlineConnectEvent = function(me)
  937. {
  938. var offset = mxUtils.getOffset(this.graph.container);
  939. var evt = me.getEvent();
  940. var clientX = mxEvent.getClientX(evt);
  941. var clientY = mxEvent.getClientY(evt);
  942. var doc = document.documentElement;
  943. var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
  944. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  945. var gridX = this.currentPoint.x - this.graph.container.scrollLeft + offset.x - left;
  946. var gridY = this.currentPoint.y - this.graph.container.scrollTop + offset.y - top;
  947. return this.outlineConnect && !mxEvent.isShiftDown(me.getEvent()) &&
  948. (me.isSource(this.marker.highlight.shape) ||
  949. (mxEvent.isAltDown(me.getEvent()) && me.getState() != null) ||
  950. this.marker.highlight.isHighlightAt(clientX, clientY) ||
  951. ((gridX != clientX || gridY != clientY) && me.getState() == null &&
  952. this.marker.highlight.isHighlightAt(gridX, gridY)));
  953. };
  954. /**
  955. * Function: updateCurrentState
  956. *
  957. * Updates the current state for a given mouse move event by using
  958. * the <marker>.
  959. */
  960. mxConnectionHandler.prototype.updateCurrentState = function(me, point)
  961. {
  962. this.constraintHandler.update(me, this.first == null, false, (this.first == null ||
  963. me.isSource(this.marker.highlight.shape)) ? null : point);
  964. if (this.constraintHandler.currentFocus != null && this.constraintHandler.currentConstraint != null)
  965. {
  966. // Handles special case where grid is large and connection point is at actual point in which
  967. // case the outline is not followed as long as we're < gridSize / 2 away from that point
  968. if (this.marker.highlight != null && this.marker.highlight.state != null &&
  969. this.marker.highlight.state.cell == this.constraintHandler.currentFocus.cell)
  970. {
  971. // Direct repaint needed if cell already highlighted
  972. if (this.marker.highlight.shape.stroke != 'transparent')
  973. {
  974. this.marker.highlight.shape.stroke = 'transparent';
  975. this.marker.highlight.repaint();
  976. }
  977. }
  978. else
  979. {
  980. this.marker.markCell(this.constraintHandler.currentFocus.cell, 'transparent');
  981. }
  982. // Updates validation state
  983. if (this.previous != null)
  984. {
  985. this.error = this.validateConnection(this.previous.cell, this.constraintHandler.currentFocus.cell);
  986. if (this.error == null)
  987. {
  988. this.currentState = this.constraintHandler.currentFocus;
  989. }
  990. if (this.error != null || (this.currentState != null &&
  991. !this.isCellEnabled(this.currentState.cell)))
  992. {
  993. this.constraintHandler.reset();
  994. }
  995. }
  996. }
  997. else
  998. {
  999. if (this.graph.isIgnoreTerminalEvent(me.getEvent()))
  1000. {
  1001. this.marker.reset();
  1002. this.currentState = null;
  1003. }
  1004. else
  1005. {
  1006. this.marker.process(me);
  1007. this.currentState = this.marker.getValidState();
  1008. }
  1009. if (this.currentState != null && !this.isCellEnabled(this.currentState.cell))
  1010. {
  1011. this.constraintHandler.reset();
  1012. this.marker.reset();
  1013. this.currentState = null;
  1014. }
  1015. var outline = this.isOutlineConnectEvent(me);
  1016. if (this.currentState != null && outline)
  1017. {
  1018. // Handles special case where mouse is on outline away from actual end point
  1019. // in which case the grid is ignored and mouse point is used instead
  1020. if (me.isSource(this.marker.highlight.shape))
  1021. {
  1022. point = new mxPoint(me.getGraphX(), me.getGraphY());
  1023. }
  1024. var constraint = this.graph.getOutlineConstraint(point, this.currentState, me);
  1025. this.constraintHandler.setFocus(me, this.currentState, false);
  1026. this.constraintHandler.currentConstraint = constraint;
  1027. this.constraintHandler.currentPoint = point;
  1028. }
  1029. if (this.outlineConnect)
  1030. {
  1031. if (this.marker.highlight != null && this.marker.highlight.shape != null)
  1032. {
  1033. var s = this.graph.view.scale;
  1034. if (this.constraintHandler.currentConstraint != null &&
  1035. this.constraintHandler.currentFocus != null)
  1036. {
  1037. this.marker.highlight.shape.stroke = mxConstants.OUTLINE_HIGHLIGHT_COLOR;
  1038. this.marker.highlight.shape.strokewidth = mxConstants.OUTLINE_HIGHLIGHT_STROKEWIDTH / s / s;
  1039. this.marker.highlight.repaint();
  1040. }
  1041. else if (this.marker.hasValidState())
  1042. {
  1043. // Handles special case where actual end point of edge and current mouse point
  1044. // are not equal (due to grid snapping) and there is no hit on shape or highlight
  1045. // but ignores cases where parent is used for non-connectable child cells
  1046. if (this.graph.isCellConnectable(me.getCell()) &&
  1047. this.marker.getValidState() != me.getState())
  1048. {
  1049. this.marker.highlight.shape.stroke = 'transparent';
  1050. this.currentState = null;
  1051. }
  1052. else
  1053. {
  1054. this.marker.highlight.shape.stroke = mxConstants.DEFAULT_VALID_COLOR;
  1055. }
  1056. this.marker.highlight.shape.strokewidth = mxConstants.HIGHLIGHT_STROKEWIDTH / s / s;
  1057. this.marker.highlight.repaint();
  1058. }
  1059. }
  1060. }
  1061. }
  1062. };
  1063. /**
  1064. * Function: isCellEnabled
  1065. *
  1066. * Returns true if the given cell allows new connections to be created. This implementation
  1067. * always returns true.
  1068. */
  1069. mxConnectionHandler.prototype.isCellEnabled = function(cell)
  1070. {
  1071. return true;
  1072. };
  1073. /**
  1074. * Function: convertWaypoint
  1075. *
  1076. * Converts the given point from screen coordinates to model coordinates.
  1077. */
  1078. mxConnectionHandler.prototype.convertWaypoint = function(point)
  1079. {
  1080. var scale = this.graph.getView().getScale();
  1081. var tr = this.graph.getView().getTranslate();
  1082. point.x = point.x / scale - tr.x;
  1083. point.y = point.y / scale - tr.y;
  1084. };
  1085. /**
  1086. * Function: snapToPreview
  1087. *
  1088. * Called to snap the given point to the current preview. This snaps to the
  1089. * first point of the preview if alt is not pressed.
  1090. */
  1091. mxConnectionHandler.prototype.snapToPreview = function(me, point)
  1092. {
  1093. if (!mxEvent.isAltDown(me.getEvent()) && this.previous != null)
  1094. {
  1095. var tol = this.graph.gridSize * this.graph.view.scale / 2;
  1096. var tmp = (this.sourceConstraint != null) ? this.first :
  1097. new mxPoint(this.previous.getCenterX(), this.previous.getCenterY());
  1098. if (Math.abs(tmp.x - me.getGraphX()) < tol)
  1099. {
  1100. point.x = tmp.x;
  1101. }
  1102. if (Math.abs(tmp.y - me.getGraphY()) < tol)
  1103. {
  1104. point.y = tmp.y;
  1105. }
  1106. }
  1107. };
  1108. /**
  1109. * Function: mouseMove
  1110. *
  1111. * Handles the event by updating the preview edge or by highlighting
  1112. * a possible source or target terminal.
  1113. */
  1114. mxConnectionHandler.prototype.mouseMove = function(sender, me)
  1115. {
  1116. if (!me.isConsumed() && (this.ignoreMouseDown || this.first != null || !this.graph.isMouseDown))
  1117. {
  1118. // Handles special case when handler is disabled during highlight
  1119. if (!this.isEnabled() && this.currentState != null)
  1120. {
  1121. this.destroyIcons();
  1122. this.currentState = null;
  1123. }
  1124. var view = this.graph.getView();
  1125. var scale = view.scale;
  1126. var tr = view.translate;
  1127. var point = new mxPoint(me.getGraphX(), me.getGraphY());
  1128. this.error = null;
  1129. if (this.graph.isGridEnabledEvent(me.getEvent()))
  1130. {
  1131. point = new mxPoint((this.graph.snap(point.x / scale - tr.x) + tr.x) * scale,
  1132. (this.graph.snap(point.y / scale - tr.y) + tr.y) * scale);
  1133. }
  1134. this.snapToPreview(me, point);
  1135. this.currentPoint = point;
  1136. if ((this.first != null || (this.isEnabled() && this.graph.isEnabled())) &&
  1137. (this.shape != null || this.first == null ||
  1138. Math.abs(me.getGraphX() - this.first.x) > this.graph.tolerance ||
  1139. Math.abs(me.getGraphY() - this.first.y) > this.graph.tolerance))
  1140. {
  1141. this.updateCurrentState(me, point);
  1142. }
  1143. if (this.first != null)
  1144. {
  1145. var constraint = null;
  1146. var current = point;
  1147. // Uses the current point from the constraint handler if available
  1148. if (this.constraintHandler.currentConstraint != null &&
  1149. this.constraintHandler.currentFocus != null &&
  1150. this.constraintHandler.currentPoint != null)
  1151. {
  1152. constraint = this.constraintHandler.currentConstraint;
  1153. current = this.constraintHandler.currentPoint.clone();
  1154. }
  1155. else if (this.previous != null && !this.graph.isIgnoreTerminalEvent(me.getEvent()) &&
  1156. mxEvent.isShiftDown(me.getEvent()))
  1157. {
  1158. if (Math.abs(this.previous.getCenterX() - point.x) <
  1159. Math.abs(this.previous.getCenterY() - point.y))
  1160. {
  1161. point.x = this.previous.getCenterX();
  1162. }
  1163. else
  1164. {
  1165. point.y = this.previous.getCenterY();
  1166. }
  1167. }
  1168. var pt2 = this.first;
  1169. // Moves the connect icon with the mouse
  1170. if (this.selectedIcon != null)
  1171. {
  1172. var w = this.selectedIcon.bounds.width;
  1173. var h = this.selectedIcon.bounds.height;
  1174. if (this.currentState != null && this.targetConnectImage)
  1175. {
  1176. var pos = this.getIconPosition(this.selectedIcon, this.currentState);
  1177. this.selectedIcon.bounds.x = pos.x;
  1178. this.selectedIcon.bounds.y = pos.y;
  1179. }
  1180. else
  1181. {
  1182. var bounds = new mxRectangle(me.getGraphX() + this.connectIconOffset.x,
  1183. me.getGraphY() + this.connectIconOffset.y, w, h);
  1184. this.selectedIcon.bounds = bounds;
  1185. }
  1186. this.selectedIcon.redraw();
  1187. }
  1188. // Uses edge state to compute the terminal points
  1189. if (this.edgeState != null)
  1190. {
  1191. this.updateEdgeState(current, constraint);
  1192. current = this.edgeState.absolutePoints[this.edgeState.absolutePoints.length - 1];
  1193. pt2 = this.edgeState.absolutePoints[0];
  1194. }
  1195. else
  1196. {
  1197. if (this.currentState != null)
  1198. {
  1199. if (this.constraintHandler.currentConstraint == null)
  1200. {
  1201. var tmp = this.getTargetPerimeterPoint(this.currentState, me);
  1202. if (tmp != null)
  1203. {
  1204. current = tmp;
  1205. }
  1206. }
  1207. }
  1208. // Computes the source perimeter point
  1209. if (this.sourceConstraint == null && this.previous != null)
  1210. {
  1211. var next = (this.waypoints != null && this.waypoints.length > 0) ?
  1212. this.waypoints[0] : current;
  1213. var tmp = this.getSourcePerimeterPoint(this.previous, next, me);
  1214. if (tmp != null)
  1215. {
  1216. pt2 = tmp;
  1217. }
  1218. }
  1219. }
  1220. // Makes sure the cell under the mousepointer can be detected
  1221. // by moving the preview shape away from the mouse. This
  1222. // makes sure the preview shape does not prevent the detection
  1223. // of the cell under the mousepointer even for slow gestures.
  1224. if (this.currentState == null && this.movePreviewAway)
  1225. {
  1226. var tmp = pt2;
  1227. if (this.edgeState != null && this.edgeState.absolutePoints.length >= 2)
  1228. {
  1229. var tmp2 = this.edgeState.absolutePoints[this.edgeState.absolutePoints.length - 2];
  1230. if (tmp2 != null)
  1231. {
  1232. tmp = tmp2;
  1233. }
  1234. }
  1235. var dx = current.x - tmp.x;
  1236. var dy = current.y - tmp.y;
  1237. var len = Math.sqrt(dx * dx + dy * dy);
  1238. if (len == 0)
  1239. {
  1240. return;
  1241. }
  1242. // Stores old point to reuse when creating edge
  1243. this.originalPoint = current.clone();
  1244. current.x -= dx * 4 / len;
  1245. current.y -= dy * 4 / len;
  1246. }
  1247. else
  1248. {
  1249. this.originalPoint = null;
  1250. }
  1251. // Creates the preview shape (lazy)
  1252. if (this.shape == null)
  1253. {
  1254. var dx = Math.abs(me.getGraphX() - this.first.x);
  1255. var dy = Math.abs(me.getGraphY() - this.first.y);
  1256. if (dx > this.graph.tolerance || dy > this.graph.tolerance)
  1257. {
  1258. this.shape = this.createShape();
  1259. if (this.edgeState != null)
  1260. {
  1261. this.shape.apply(this.edgeState);
  1262. }
  1263. // Revalidates current connection
  1264. this.updateCurrentState(me, point);
  1265. }
  1266. }
  1267. // Updates the points in the preview edge
  1268. if (this.shape != null)
  1269. {
  1270. if (this.edgeState != null)
  1271. {
  1272. this.shape.points = this.edgeState.absolutePoints;
  1273. }
  1274. else
  1275. {
  1276. var pts = [pt2];
  1277. if (this.waypoints != null)
  1278. {
  1279. pts = pts.concat(this.waypoints);
  1280. }
  1281. pts.push(current);
  1282. this.shape.points = pts;
  1283. }
  1284. this.drawPreview();
  1285. }
  1286. // Makes sure endpoint of edge is visible during connect
  1287. if (this.cursor != null)
  1288. {
  1289. this.graph.container.style.cursor = this.cursor;
  1290. }
  1291. mxEvent.consume(me.getEvent());
  1292. me.consume();
  1293. }
  1294. else if (!this.isEnabled() || !this.graph.isEnabled())
  1295. {
  1296. this.constraintHandler.reset();
  1297. }
  1298. else if (this.previous != this.currentState && this.edgeState == null)
  1299. {
  1300. this.destroyIcons();
  1301. // Sets the cursor on the current shape
  1302. if (this.currentState != null && this.error == null && this.constraintHandler.currentConstraint == null)
  1303. {
  1304. this.icons = this.createIcons(this.currentState);
  1305. if (this.icons == null)
  1306. {
  1307. this.currentState.setCursor(mxConstants.CURSOR_CONNECT);
  1308. me.consume();
  1309. }
  1310. }
  1311. this.previous = this.currentState;
  1312. }
  1313. else if (this.previous == this.currentState && this.currentState != null && this.icons == null &&
  1314. !this.graph.isMouseDown)
  1315. {
  1316. // Makes sure that no cursors are changed
  1317. me.consume();
  1318. }
  1319. if (!this.graph.isMouseDown && this.currentState != null && this.icons != null)
  1320. {
  1321. var hitsIcon = false;
  1322. var target = me.getSource();
  1323. for (var i = 0; i < this.icons.length && !hitsIcon; i++)
  1324. {
  1325. hitsIcon = target == this.icons[i].node || target.parentNode == this.icons[i].node;
  1326. }
  1327. if (!hitsIcon)
  1328. {
  1329. this.updateIcons(this.currentState, this.icons, me);
  1330. }
  1331. }
  1332. }
  1333. else
  1334. {
  1335. this.constraintHandler.reset();
  1336. }
  1337. };
  1338. /**
  1339. * Function: updateEdgeState
  1340. *
  1341. * Updates <edgeState>.
  1342. */
  1343. mxConnectionHandler.prototype.updateEdgeState = function(current, constraint)
  1344. {
  1345. // TODO: Use generic method for writing constraint to style
  1346. if (this.sourceConstraint != null && this.sourceConstraint.point != null)
  1347. {
  1348. this.edgeState.style[mxConstants.STYLE_EXIT_X] = this.sourceConstraint.point.x;
  1349. this.edgeState.style[mxConstants.STYLE_EXIT_Y] = this.sourceConstraint.point.y;
  1350. }
  1351. if (constraint != null && constraint.point != null)
  1352. {
  1353. this.edgeState.style[mxConstants.STYLE_ENTRY_X] = constraint.point.x;
  1354. this.edgeState.style[mxConstants.STYLE_ENTRY_Y] = constraint.point.y;
  1355. }
  1356. else
  1357. {
  1358. delete this.edgeState.style[mxConstants.STYLE_ENTRY_X];
  1359. delete this.edgeState.style[mxConstants.STYLE_ENTRY_Y];
  1360. }
  1361. this.edgeState.absolutePoints = [null, (this.currentState != null) ? null : current];
  1362. this.graph.view.updateFixedTerminalPoint(this.edgeState, this.previous, true, this.sourceConstraint);
  1363. if (this.currentState != null)
  1364. {
  1365. if (constraint == null)
  1366. {
  1367. constraint = this.graph.getConnectionConstraint(this.edgeState, this.previous, false);
  1368. }
  1369. this.edgeState.setAbsoluteTerminalPoint(null, false);
  1370. this.graph.view.updateFixedTerminalPoint(this.edgeState, this.currentState, false, constraint);
  1371. }
  1372. // Scales and translates the waypoints to the model
  1373. var realPoints = null;
  1374. if (this.waypoints != null)
  1375. {
  1376. realPoints = [];
  1377. for (var i = 0; i < this.waypoints.length; i++)
  1378. {
  1379. var pt = this.waypoints[i].clone();
  1380. this.convertWaypoint(pt);
  1381. realPoints[i] = pt;
  1382. }
  1383. }
  1384. this.graph.view.updatePoints(this.edgeState, realPoints, this.previous, this.currentState);
  1385. this.graph.view.updateFloatingTerminalPoints(this.edgeState, this.previous, this.currentState);
  1386. };
  1387. /**
  1388. * Function: getTargetPerimeterPoint
  1389. *
  1390. * Returns the perimeter point for the given target state.
  1391. *
  1392. * Parameters:
  1393. *
  1394. * state - <mxCellState> that represents the target cell state.
  1395. * me - <mxMouseEvent> that represents the mouse move.
  1396. */
  1397. mxConnectionHandler.prototype.getTargetPerimeterPoint = function(state, me)
  1398. {
  1399. var result = null;
  1400. var view = state.view;
  1401. var targetPerimeter = view.getPerimeterFunction(state);
  1402. if (targetPerimeter != null)
  1403. {
  1404. var next = (this.waypoints != null && this.waypoints.length > 0) ?
  1405. this.waypoints[this.waypoints.length - 1] :
  1406. new mxPoint(this.previous.getCenterX(), this.previous.getCenterY());
  1407. var tmp = targetPerimeter(view.getPerimeterBounds(state),
  1408. this.edgeState, next, false);
  1409. if (tmp != null)
  1410. {
  1411. result = tmp;
  1412. }
  1413. }
  1414. else
  1415. {
  1416. result = new mxPoint(state.getCenterX(), state.getCenterY());
  1417. }
  1418. return result;
  1419. };
  1420. /**
  1421. * Function: getSourcePerimeterPoint
  1422. *
  1423. * Hook to update the icon position(s) based on a mouseOver event. This is
  1424. * an empty implementation.
  1425. *
  1426. * Parameters:
  1427. *
  1428. * state - <mxCellState> that represents the target cell state.
  1429. * next - <mxPoint> that represents the next point along the previewed edge.
  1430. * me - <mxMouseEvent> that represents the mouse move.
  1431. */
  1432. mxConnectionHandler.prototype.getSourcePerimeterPoint = function(state, next, me)
  1433. {
  1434. var result = null;
  1435. var view = state.view;
  1436. var sourcePerimeter = view.getPerimeterFunction(state);
  1437. var c = new mxPoint(state.getCenterX(), state.getCenterY());
  1438. if (sourcePerimeter != null)
  1439. {
  1440. var theta = mxUtils.getValue(state.style, mxConstants.STYLE_ROTATION, 0);
  1441. var rad = -theta * (Math.PI / 180);
  1442. if (theta != 0)
  1443. {
  1444. next = mxUtils.getRotatedPoint(new mxPoint(next.x, next.y), Math.cos(rad), Math.sin(rad), c);
  1445. }
  1446. var tmp = sourcePerimeter(view.getPerimeterBounds(state), state, next, false);
  1447. if (tmp != null)
  1448. {
  1449. if (theta != 0)
  1450. {
  1451. tmp = mxUtils.getRotatedPoint(new mxPoint(tmp.x, tmp.y), Math.cos(-rad), Math.sin(-rad), c);
  1452. }
  1453. result = tmp;
  1454. }
  1455. }
  1456. else
  1457. {
  1458. result = c;
  1459. }
  1460. return result;
  1461. };
  1462. /**
  1463. * Function: updateIcons
  1464. *
  1465. * Hook to update the icon position(s) based on a mouseOver event. This is
  1466. * an empty implementation.
  1467. *
  1468. * Parameters:
  1469. *
  1470. * state - <mxCellState> under the mouse.
  1471. * icons - Array of currently displayed icons.
  1472. * me - <mxMouseEvent> that contains the mouse event.
  1473. */
  1474. mxConnectionHandler.prototype.updateIcons = function(state, icons, me)
  1475. {
  1476. // empty
  1477. };
  1478. /**
  1479. * Function: isStopEvent
  1480. *
  1481. * Returns true if the given mouse up event should stop this handler. The
  1482. * connection will be created if <error> is null. Note that this is only
  1483. * called if <waypointsEnabled> is true. This implemtation returns true
  1484. * if there is a cell state in the given event.
  1485. */
  1486. mxConnectionHandler.prototype.isStopEvent = function(me)
  1487. {
  1488. return me.getState() != null;
  1489. };
  1490. /**
  1491. * Function: addWaypoint
  1492. *
  1493. * Adds the waypoint for the given event to <waypoints>.
  1494. */
  1495. mxConnectionHandler.prototype.addWaypointForEvent = function(me)
  1496. {
  1497. var point = mxUtils.convertPoint(this.graph.container, me.getX(), me.getY());
  1498. var dx = Math.abs(point.x - this.first.x);
  1499. var dy = Math.abs(point.y - this.first.y);
  1500. var addPoint = this.waypoints != null || (this.mouseDownCounter > 1 &&
  1501. (dx > this.graph.tolerance || dy > this.graph.tolerance));
  1502. if (addPoint)
  1503. {
  1504. if (this.waypoints == null)
  1505. {
  1506. this.waypoints = [];
  1507. }
  1508. var scale = this.graph.view.scale;
  1509. var point = new mxPoint(this.graph.snap(me.getGraphX() / scale) * scale,
  1510. this.graph.snap(me.getGraphY() / scale) * scale);
  1511. this.waypoints.push(point);
  1512. }
  1513. };
  1514. /**
  1515. * Function: checkConstraints
  1516. *
  1517. * Returns true if the connection for the given constraints is valid. This
  1518. * implementation returns true if the constraints are not pointing to the
  1519. * same fixed connection point.
  1520. */
  1521. mxConnectionHandler.prototype.checkConstraints = function(c1, c2)
  1522. {
  1523. return (c1 == null || c2 == null || c1.point == null || c2.point == null ||
  1524. !c1.point.equals(c2.point) || c1.dx != c2.dx || c1.dy != c2.dy ||
  1525. c1.perimeter != c2.perimeter);
  1526. };
  1527. /**
  1528. * Function: mouseUp
  1529. *
  1530. * Handles the event by inserting the new connection.
  1531. */
  1532. mxConnectionHandler.prototype.mouseUp = function(sender, me)
  1533. {
  1534. if (!me.isConsumed() && this.isConnecting())
  1535. {
  1536. if (this.waypointsEnabled && !this.isStopEvent(me))
  1537. {
  1538. this.addWaypointForEvent(me);
  1539. me.consume();
  1540. return;
  1541. }
  1542. var c1 = this.sourceConstraint;
  1543. var c2 = this.constraintHandler.currentConstraint;
  1544. var source = (this.previous != null) ? this.previous.cell : null;
  1545. var target = null;
  1546. if (this.constraintHandler.currentConstraint != null &&
  1547. this.constraintHandler.currentFocus != null)
  1548. {
  1549. target = this.constraintHandler.currentFocus.cell;
  1550. }
  1551. if (target == null && this.currentState != null)
  1552. {
  1553. target = this.currentState.cell;
  1554. }
  1555. // Inserts the edge if no validation error exists and if constraints differ
  1556. if (this.error == null && (source == null || target == null ||
  1557. source != target || this.checkConstraints(c1, c2)))
  1558. {
  1559. this.connect(source, target, me.getEvent(), me.getCell());
  1560. }
  1561. else
  1562. {
  1563. // Selects the source terminal for self-references
  1564. if (this.previous != null && this.marker.validState != null &&
  1565. this.previous.cell == this.marker.validState.cell)
  1566. {
  1567. this.graph.selectCellForEvent(this.marker.source, me.getEvent());
  1568. }
  1569. // Displays the error message if it is not an empty string,
  1570. // for empty error messages, the event is silently dropped
  1571. if (this.error != null && this.error.length > 0)
  1572. {
  1573. this.graph.validationAlert(this.error);
  1574. }
  1575. }
  1576. // Redraws the connect icons and resets the handler state
  1577. this.destroyIcons();
  1578. me.consume();
  1579. }
  1580. if (this.first != null)
  1581. {
  1582. this.reset();
  1583. }
  1584. };
  1585. /**
  1586. * Function: reset
  1587. *
  1588. * Resets the state of this handler.
  1589. */
  1590. mxConnectionHandler.prototype.reset = function()
  1591. {
  1592. if (this.shape != null)
  1593. {
  1594. this.shape.destroy();
  1595. this.shape = null;
  1596. }
  1597. // Resets the cursor on the container
  1598. if (this.cursor != null && this.graph.container != null)
  1599. {
  1600. this.graph.container.style.cursor = '';
  1601. }
  1602. this.destroyIcons();
  1603. this.marker.reset();
  1604. this.constraintHandler.reset();
  1605. this.originalPoint = null;
  1606. this.currentPoint = null;
  1607. this.edgeState = null;
  1608. this.previous = null;
  1609. this.error = null;
  1610. this.sourceConstraint = null;
  1611. this.mouseDownCounter = 0;
  1612. this.first = null;
  1613. this.fireEvent(new mxEventObject(mxEvent.RESET));
  1614. };
  1615. /**
  1616. * Function: drawPreview
  1617. *
  1618. * Redraws the preview edge using the color and width returned by
  1619. * <getEdgeColor> and <getEdgeWidth>.
  1620. */
  1621. mxConnectionHandler.prototype.drawPreview = function()
  1622. {
  1623. this.updatePreview(this.error == null);
  1624. this.shape.redraw();
  1625. };
  1626. /**
  1627. * Function: getEdgeColor
  1628. *
  1629. * Returns the color used to draw the preview edge. This returns green if
  1630. * there is no edge validation error and red otherwise.
  1631. *
  1632. * Parameters:
  1633. *
  1634. * valid - Boolean indicating if the color for a valid edge should be
  1635. * returned.
  1636. */
  1637. mxConnectionHandler.prototype.updatePreview = function(valid)
  1638. {
  1639. this.shape.strokewidth = this.getEdgeWidth(valid);
  1640. this.shape.stroke = this.getEdgeColor(valid);
  1641. };
  1642. /**
  1643. * Function: getEdgeColor
  1644. *
  1645. * Returns the color used to draw the preview edge. This returns green if
  1646. * there is no edge validation error and red otherwise.
  1647. *
  1648. * Parameters:
  1649. *
  1650. * valid - Boolean indicating if the color for a valid edge should be
  1651. * returned.
  1652. */
  1653. mxConnectionHandler.prototype.getEdgeColor = function(valid)
  1654. {
  1655. return (valid) ? mxConstants.VALID_COLOR : mxConstants.INVALID_COLOR;
  1656. };
  1657. /**
  1658. * Function: getEdgeWidth
  1659. *
  1660. * Returns the width used to draw the preview edge. This returns 3 if
  1661. * there is no edge validation error and 1 otherwise.
  1662. *
  1663. * Parameters:
  1664. *
  1665. * valid - Boolean indicating if the width for a valid edge should be
  1666. * returned.
  1667. */
  1668. mxConnectionHandler.prototype.getEdgeWidth = function(valid)
  1669. {
  1670. return (valid) ? 3 : 1;
  1671. };
  1672. /**
  1673. * Function: connect
  1674. *
  1675. * Connects the given source and target using a new edge. This
  1676. * implementation uses <createEdge> to create the edge.
  1677. *
  1678. * Parameters:
  1679. *
  1680. * source - <mxCell> that represents the source terminal.
  1681. * target - <mxCell> that represents the target terminal.
  1682. * evt - Mousedown event of the connect gesture.
  1683. * dropTarget - <mxCell> that represents the cell under the mouse when it was
  1684. * released.
  1685. */
  1686. mxConnectionHandler.prototype.connect = function(source, target, evt, dropTarget)
  1687. {
  1688. if (target != null || this.isCreateTarget(evt) || this.graph.allowDanglingEdges)
  1689. {
  1690. // Uses the common parent of source and target or
  1691. // the default parent to insert the edge
  1692. var model = this.graph.getModel();
  1693. var terminalInserted = false;
  1694. var edge = null;
  1695. model.beginUpdate();
  1696. try
  1697. {
  1698. if (source != null && target == null && !this.graph.isIgnoreTerminalEvent(evt) && this.isCreateTarget(evt))
  1699. {
  1700. target = this.createTargetVertex(evt, source);
  1701. if (target != null)
  1702. {
  1703. dropTarget = this.graph.getDropTarget([target], evt, dropTarget);
  1704. terminalInserted = true;
  1705. // Disables edges as drop targets if the target cell was created
  1706. // FIXME: Should not shift if vertex was aligned (same in Java)
  1707. if (dropTarget == null || !this.graph.getModel().isEdge(dropTarget))
  1708. {
  1709. var pstate = this.graph.getView().getState(dropTarget);
  1710. if (pstate != null)
  1711. {
  1712. var tmp = model.getGeometry(target);
  1713. tmp.x -= pstate.origin.x;
  1714. tmp.y -= pstate.origin.y;
  1715. }
  1716. }
  1717. else
  1718. {
  1719. dropTarget = this.graph.getDefaultParent();
  1720. }
  1721. this.graph.addCell(target, dropTarget);
  1722. }
  1723. }
  1724. var parent = this.graph.getDefaultParent();
  1725. if (source != null && target != null &&
  1726. model.getParent(source) == model.getParent(target) &&
  1727. model.getParent(model.getParent(source)) != model.getRoot())
  1728. {
  1729. parent = model.getParent(source);
  1730. if ((source.geometry != null && source.geometry.relative) &&
  1731. (target.geometry != null && target.geometry.relative))
  1732. {
  1733. parent = model.getParent(parent);
  1734. }
  1735. }
  1736. // Uses the value of the preview edge state for inserting
  1737. // the new edge into the graph
  1738. var value = null;
  1739. var style = null;
  1740. if (this.edgeState != null)
  1741. {
  1742. value = this.edgeState.cell.value;
  1743. style = this.edgeState.cell.style;
  1744. }
  1745. edge = this.insertEdge(parent, null, value, source, target, style);
  1746. if (edge != null)
  1747. {
  1748. // Updates the connection constraints
  1749. this.graph.setConnectionConstraint(edge, source, true, this.sourceConstraint);
  1750. this.graph.setConnectionConstraint(edge, target, false, this.constraintHandler.currentConstraint);
  1751. // Uses geometry of the preview edge state
  1752. if (this.edgeState != null)
  1753. {
  1754. model.setGeometry(edge, this.edgeState.cell.geometry);
  1755. }
  1756. var parent = model.getParent(source);
  1757. // Inserts edge before source
  1758. if (this.isInsertBefore(edge, source, target, evt, dropTarget))
  1759. {
  1760. var index = null;
  1761. var tmp = source;
  1762. while (tmp.parent != null && tmp.geometry != null &&
  1763. tmp.geometry.relative && tmp.parent != edge.parent)
  1764. {
  1765. tmp = this.graph.model.getParent(tmp);
  1766. }
  1767. if (tmp != null && tmp.parent != null && tmp.parent == edge.parent)
  1768. {
  1769. model.add(parent, edge, tmp.parent.getIndex(tmp));
  1770. }
  1771. }
  1772. // Makes sure the edge has a non-null, relative geometry
  1773. var geo = model.getGeometry(edge);
  1774. if (geo == null)
  1775. {
  1776. geo = new mxGeometry();
  1777. geo.relative = true;
  1778. model.setGeometry(edge, geo);
  1779. }
  1780. // Uses scaled waypoints in geometry
  1781. if (this.waypoints != null && this.waypoints.length > 0)
  1782. {
  1783. var s = this.graph.view.scale;
  1784. var tr = this.graph.view.translate;
  1785. geo.points = [];
  1786. for (var i = 0; i < this.waypoints.length; i++)
  1787. {
  1788. var pt = this.waypoints[i];
  1789. geo.points.push(new mxPoint(pt.x / s - tr.x, pt.y / s - tr.y));
  1790. }
  1791. }
  1792. if (target == null)
  1793. {
  1794. var t = this.graph.view.translate;
  1795. var s = this.graph.view.scale;
  1796. var pt = (this.originalPoint != null) ?
  1797. new mxPoint(this.originalPoint.x / s - t.x, this.originalPoint.y / s - t.y) :
  1798. new mxPoint(this.currentPoint.x / s - t.x, this.currentPoint.y / s - t.y);
  1799. pt.x -= this.graph.panDx / this.graph.view.scale;
  1800. pt.y -= this.graph.panDy / this.graph.view.scale;
  1801. geo.setTerminalPoint(pt, false);
  1802. }
  1803. this.fireEvent(new mxEventObject(mxEvent.CONNECT, 'cell', edge, 'terminal', target,
  1804. 'event', evt, 'target', dropTarget, 'terminalInserted', terminalInserted));
  1805. }
  1806. }
  1807. catch (e)
  1808. {
  1809. mxLog.show();
  1810. mxLog.debug(e.message);
  1811. }
  1812. finally
  1813. {
  1814. model.endUpdate();
  1815. }
  1816. if (this.select)
  1817. {
  1818. this.selectCells(edge, (terminalInserted) ? target : null);
  1819. }
  1820. }
  1821. };
  1822. /**
  1823. * Function: selectCells
  1824. *
  1825. * Selects the given edge after adding a new connection. The target argument
  1826. * contains the target vertex if one has been inserted.
  1827. */
  1828. mxConnectionHandler.prototype.selectCells = function(edge, target)
  1829. {
  1830. this.graph.setSelectionCell(edge);
  1831. };
  1832. /**
  1833. * Function: insertEdge
  1834. *
  1835. * Creates, inserts and returns the new edge for the given parameters. This
  1836. * implementation does only use <createEdge> if <factoryMethod> is defined,
  1837. * otherwise <mxGraph.insertEdge> will be used.
  1838. */
  1839. mxConnectionHandler.prototype.insertEdge = function(parent, id, value, source, target, style)
  1840. {
  1841. if (this.factoryMethod == null)
  1842. {
  1843. return this.graph.insertEdge(parent, id, value, source, target, style);
  1844. }
  1845. else
  1846. {
  1847. var edge = this.createEdge(value, source, target, style);
  1848. edge = this.graph.addEdge(edge, parent, source, target);
  1849. return edge;
  1850. }
  1851. };
  1852. /**
  1853. * Function: createTargetVertex
  1854. *
  1855. * Hook method for creating new vertices on the fly if no target was
  1856. * under the mouse. This is only called if <createTarget> is true and
  1857. * returns null.
  1858. *
  1859. * Parameters:
  1860. *
  1861. * evt - Mousedown event of the connect gesture.
  1862. * source - <mxCell> that represents the source terminal.
  1863. */
  1864. mxConnectionHandler.prototype.createTargetVertex = function(evt, source)
  1865. {
  1866. // Uses the first non-relative source
  1867. var geo = this.graph.getCellGeometry(source);
  1868. while (geo != null && geo.relative)
  1869. {
  1870. source = this.graph.getModel().getParent(source);
  1871. geo = this.graph.getCellGeometry(source);
  1872. }
  1873. var clone = this.graph.cloneCell(source);
  1874. var geo = this.graph.getModel().getGeometry(clone);
  1875. if (geo != null)
  1876. {
  1877. var t = this.graph.view.translate;
  1878. var s = this.graph.view.scale;
  1879. var point = new mxPoint(this.currentPoint.x / s - t.x, this.currentPoint.y / s - t.y);
  1880. geo.x = Math.round(point.x - geo.width / 2 - this.graph.panDx / s);
  1881. geo.y = Math.round(point.y - geo.height / 2 - this.graph.panDy / s);
  1882. // Aligns with source if within certain tolerance
  1883. var tol = this.getAlignmentTolerance();
  1884. if (tol > 0)
  1885. {
  1886. var sourceState = this.graph.view.getState(source);
  1887. if (sourceState != null)
  1888. {
  1889. var x = sourceState.x / s - t.x;
  1890. var y = sourceState.y / s - t.y;
  1891. if (Math.abs(x - geo.x) <= tol)
  1892. {
  1893. geo.x = Math.round(x);
  1894. }
  1895. if (Math.abs(y - geo.y) <= tol)
  1896. {
  1897. geo.y = Math.round(y);
  1898. }
  1899. }
  1900. }
  1901. }
  1902. return clone;
  1903. };
  1904. /**
  1905. * Function: getAlignmentTolerance
  1906. *
  1907. * Returns the tolerance for aligning new targets to sources. This returns the grid size / 2.
  1908. */
  1909. mxConnectionHandler.prototype.getAlignmentTolerance = function(evt)
  1910. {
  1911. return (this.graph.isGridEnabled()) ? this.graph.gridSize / 2 : this.graph.tolerance;
  1912. };
  1913. /**
  1914. * Function: createEdge
  1915. *
  1916. * Creates and returns a new edge using <factoryMethod> if one exists. If
  1917. * no factory method is defined, then a new default edge is returned. The
  1918. * source and target arguments are informal, the actual connection is
  1919. * setup later by the caller of this function.
  1920. *
  1921. * Parameters:
  1922. *
  1923. * value - Value to be used for creating the edge.
  1924. * source - <mxCell> that represents the source terminal.
  1925. * target - <mxCell> that represents the target terminal.
  1926. * style - Optional style from the preview edge.
  1927. */
  1928. mxConnectionHandler.prototype.createEdge = function(value, source, target, style)
  1929. {
  1930. var edge = null;
  1931. // Creates a new edge using the factoryMethod
  1932. if (this.factoryMethod != null)
  1933. {
  1934. edge = this.factoryMethod(source, target, style);
  1935. }
  1936. if (edge == null)
  1937. {
  1938. edge = new mxCell(value || '');
  1939. edge.setEdge(true);
  1940. edge.setStyle(style);
  1941. var geo = new mxGeometry();
  1942. geo.relative = true;
  1943. edge.setGeometry(geo);
  1944. }
  1945. return edge;
  1946. };
  1947. /**
  1948. * Function: destroy
  1949. *
  1950. * Destroys the handler and all its resources and DOM nodes. This should be
  1951. * called on all instances. It is called automatically for the built-in
  1952. * instance created for each <mxGraph>.
  1953. */
  1954. mxConnectionHandler.prototype.destroy = function()
  1955. {
  1956. this.graph.removeMouseListener(this);
  1957. if (this.shape != null)
  1958. {
  1959. this.shape.destroy();
  1960. this.shape = null;
  1961. }
  1962. if (this.marker != null)
  1963. {
  1964. this.marker.destroy();
  1965. this.marker = null;
  1966. }
  1967. if (this.constraintHandler != null)
  1968. {
  1969. this.constraintHandler.destroy();
  1970. this.constraintHandler = null;
  1971. }
  1972. if (this.changeHandler != null)
  1973. {
  1974. this.graph.getModel().removeListener(this.changeHandler);
  1975. this.graph.getView().removeListener(this.changeHandler);
  1976. this.changeHandler = null;
  1977. }
  1978. if (this.drillHandler != null)
  1979. {
  1980. this.graph.removeListener(this.drillHandler);
  1981. this.graph.getView().removeListener(this.drillHandler);
  1982. this.drillHandler = null;
  1983. }
  1984. if (this.escapeHandler != null)
  1985. {
  1986. this.graph.removeListener(this.escapeHandler);
  1987. this.escapeHandler = null;
  1988. }
  1989. };
  1990. __mxOutput.mxConnectionHandler = typeof mxConnectionHandler !== 'undefined' ? mxConnectionHandler : undefined;