mxDragSource.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxDragSource
  7. *
  8. * Wrapper to create a drag source from a DOM element so that the element can
  9. * be dragged over a graph and dropped into the graph as a new cell.
  10. *
  11. * Problem is that in the dropHandler the current preview location is not
  12. * available, so the preview and the dropHandler must match.
  13. *
  14. * Constructor: mxDragSource
  15. *
  16. * Constructs a new drag source for the given element.
  17. */
  18. function mxDragSource(element, dropHandler)
  19. {
  20. this.element = element;
  21. this.dropHandler = dropHandler;
  22. // Handles a drag gesture on the element
  23. mxEvent.addGestureListeners(element, mxUtils.bind(this, function(evt)
  24. {
  25. this.mouseDown(evt);
  26. }));
  27. // Prevents native drag and drop
  28. mxEvent.addListener(element, 'dragstart', function(evt)
  29. {
  30. mxEvent.consume(evt);
  31. });
  32. this.eventConsumer = function(sender, evt)
  33. {
  34. var evtName = evt.getProperty('eventName');
  35. var me = evt.getProperty('event');
  36. if (evtName != mxEvent.MOUSE_DOWN)
  37. {
  38. me.consume();
  39. }
  40. };
  41. };
  42. /**
  43. * Variable: element
  44. *
  45. * Reference to the DOM node which was made draggable.
  46. */
  47. mxDragSource.prototype.element = null;
  48. /**
  49. * Variable: dropHandler
  50. *
  51. * Holds the DOM node that is used to represent the drag preview. If this is
  52. * null then the source element will be cloned and used for the drag preview.
  53. */
  54. mxDragSource.prototype.dropHandler = null;
  55. /**
  56. * Variable: dragOffset
  57. *
  58. * <mxPoint> that specifies the offset of the <dragElement>. Default is null.
  59. */
  60. mxDragSource.prototype.dragOffset = null;
  61. /**
  62. * Variable: dragElement
  63. *
  64. * Holds the DOM node that is used to represent the drag preview. If this is
  65. * null then the source element will be cloned and used for the drag preview.
  66. */
  67. mxDragSource.prototype.dragElement = null;
  68. /**
  69. * Variable: previewElement
  70. *
  71. * Optional <mxRectangle> that specifies the unscaled size of the preview.
  72. */
  73. mxDragSource.prototype.previewElement = null;
  74. /**
  75. * Variable: previewOffset
  76. *
  77. * Optional <mxPoint> that specifies the offset of the preview in pixels.
  78. */
  79. mxDragSource.prototype.previewOffset = null;
  80. /**
  81. * Variable: enabled
  82. *
  83. * Specifies if this drag source is enabled. Default is true.
  84. */
  85. mxDragSource.prototype.enabled = true;
  86. /**
  87. * Variable: currentGraph
  88. *
  89. * Reference to the <mxGraph> that is the current drop target.
  90. */
  91. mxDragSource.prototype.currentGraph = null;
  92. /**
  93. * Variable: currentDropTarget
  94. *
  95. * Holds the current drop target under the mouse.
  96. */
  97. mxDragSource.prototype.currentDropTarget = null;
  98. /**
  99. * Variable: currentPoint
  100. *
  101. * Holds the current drop location.
  102. */
  103. mxDragSource.prototype.currentPoint = null;
  104. /**
  105. * Variable: currentGuide
  106. *
  107. * Holds an <mxGuide> for the <currentGraph> if <dragPreview> is not null.
  108. */
  109. mxDragSource.prototype.currentGuide = null;
  110. /**
  111. * Variable: currentGuide
  112. *
  113. * Holds an <mxGuide> for the <currentGraph> if <dragPreview> is not null.
  114. */
  115. mxDragSource.prototype.currentHighlight = null;
  116. /**
  117. * Variable: autoscroll
  118. *
  119. * Specifies if the graph should scroll automatically. Default is true.
  120. */
  121. mxDragSource.prototype.autoscroll = true;
  122. /**
  123. * Variable: guidesEnabled
  124. *
  125. * Specifies if <mxGuide> should be enabled. Default is true.
  126. */
  127. mxDragSource.prototype.guidesEnabled = true;
  128. /**
  129. * Variable: gridEnabled
  130. *
  131. * Specifies if the grid should be allowed. Default is true.
  132. */
  133. mxDragSource.prototype.gridEnabled = true;
  134. /**
  135. * Variable: highlightDropTargets
  136. *
  137. * Specifies if drop targets should be highlighted. Default is true.
  138. */
  139. mxDragSource.prototype.highlightDropTargets = true;
  140. /**
  141. * Variable: dragElementZIndex
  142. *
  143. * ZIndex for the drag element. Default is 100.
  144. */
  145. mxDragSource.prototype.dragElementZIndex = 100;
  146. /**
  147. * Variable: dragElementOpacity
  148. *
  149. * Opacity of the drag element in %. Default is 70.
  150. */
  151. mxDragSource.prototype.dragElementOpacity = 70;
  152. /**
  153. * Variable: checkEventSource
  154. *
  155. * Whether the event source should be checked in <graphContainerEvent>. Default
  156. * is true.
  157. */
  158. mxDragSource.prototype.checkEventSource = true;
  159. /**
  160. * Function: isEnabled
  161. *
  162. * Returns <enabled>.
  163. */
  164. mxDragSource.prototype.isEnabled = function()
  165. {
  166. return this.enabled;
  167. };
  168. /**
  169. * Function: setEnabled
  170. *
  171. * Sets <enabled>.
  172. */
  173. mxDragSource.prototype.setEnabled = function(value)
  174. {
  175. this.enabled = value;
  176. };
  177. /**
  178. * Function: isGuidesEnabled
  179. *
  180. * Returns <guidesEnabled>.
  181. */
  182. mxDragSource.prototype.isGuidesEnabled = function()
  183. {
  184. return this.guidesEnabled;
  185. };
  186. /**
  187. * Function: setGuidesEnabled
  188. *
  189. * Sets <guidesEnabled>.
  190. */
  191. mxDragSource.prototype.setGuidesEnabled = function(value)
  192. {
  193. this.guidesEnabled = value;
  194. };
  195. /**
  196. * Function: isGridEnabled
  197. *
  198. * Returns <gridEnabled>.
  199. */
  200. mxDragSource.prototype.isGridEnabled = function()
  201. {
  202. return this.gridEnabled;
  203. };
  204. /**
  205. * Function: setGridEnabled
  206. *
  207. * Sets <gridEnabled>.
  208. */
  209. mxDragSource.prototype.setGridEnabled = function(value)
  210. {
  211. this.gridEnabled = value;
  212. };
  213. /**
  214. * Function: getGraphForEvent
  215. *
  216. * Returns the graph for the given mouse event. This implementation returns
  217. * null.
  218. */
  219. mxDragSource.prototype.getGraphForEvent = function(evt)
  220. {
  221. return null;
  222. };
  223. /**
  224. * Function: getDropTarget
  225. *
  226. * Returns the drop target for the given graph and coordinates. This
  227. * implementation uses <mxGraph.getCellAt>.
  228. */
  229. mxDragSource.prototype.getDropTarget = function(graph, x, y, evt)
  230. {
  231. return graph.getCellAt(x, y);
  232. };
  233. /**
  234. * Function: createDragElement
  235. *
  236. * Creates and returns a clone of the <dragElementPrototype> or the <element>
  237. * if the former is not defined.
  238. */
  239. mxDragSource.prototype.createDragElement = function(evt)
  240. {
  241. return this.element.cloneNode(true);
  242. };
  243. /**
  244. * Function: createPreviewElement
  245. *
  246. * Creates and returns an element which can be used as a preview in the given
  247. * graph.
  248. */
  249. mxDragSource.prototype.createPreviewElement = function(graph)
  250. {
  251. return null;
  252. };
  253. /**
  254. * Function: isActive
  255. *
  256. * Returns true if this drag source is active.
  257. */
  258. mxDragSource.prototype.isActive = function()
  259. {
  260. return this.mouseMoveHandler != null;
  261. };
  262. /**
  263. * Function: reset
  264. *
  265. * Stops and removes everything and restores the state of the object.
  266. */
  267. mxDragSource.prototype.reset = function()
  268. {
  269. if (this.currentGraph != null)
  270. {
  271. this.dragExit(this.currentGraph);
  272. this.currentGraph = null;
  273. }
  274. this.removeDragElement();
  275. this.removeListeners();
  276. this.stopDrag();
  277. };
  278. /**
  279. * Function: mouseDown
  280. *
  281. * Returns the drop target for the given graph and coordinates. This
  282. * implementation uses <mxGraph.getCellAt>.
  283. *
  284. * To ignore popup menu events for a drag source, this function can be
  285. * overridden as follows.
  286. *
  287. * (code)
  288. * var mouseDown = dragSource.mouseDown;
  289. *
  290. * dragSource.mouseDown = function(evt)
  291. * {
  292. * if (!mxEvent.isPopupTrigger(evt))
  293. * {
  294. * mouseDown.apply(this, arguments);
  295. * }
  296. * };
  297. * (end)
  298. */
  299. mxDragSource.prototype.mouseDown = function(evt)
  300. {
  301. if (this.enabled && !mxEvent.isConsumed(evt) && this.mouseMoveHandler == null)
  302. {
  303. this.startDrag(evt);
  304. this.mouseMoveHandler = mxUtils.bind(this, this.mouseMove);
  305. this.mouseUpHandler = mxUtils.bind(this, this.mouseUp);
  306. mxEvent.addGestureListeners(document, null, this.mouseMoveHandler, this.mouseUpHandler);
  307. if (mxClient.IS_TOUCH && !mxEvent.isMouseEvent(evt))
  308. {
  309. this.eventSource = mxEvent.getSource(evt);
  310. mxEvent.addGestureListeners(this.eventSource, null, this.mouseMoveHandler, this.mouseUpHandler);
  311. }
  312. }
  313. };
  314. /**
  315. * Function: startDrag
  316. *
  317. * Creates the <dragElement> using <createDragElement>.
  318. */
  319. mxDragSource.prototype.startDrag = function(evt)
  320. {
  321. this.dragElement = this.createDragElement(evt);
  322. this.dragElement.style.position = 'absolute';
  323. this.dragElement.style.zIndex = this.dragElementZIndex;
  324. mxUtils.setOpacity(this.dragElement, this.dragElementOpacity);
  325. if (this.checkEventSource && mxClient.IS_SVG)
  326. {
  327. this.dragElement.style.pointerEvents = 'none';
  328. }
  329. };
  330. /**
  331. * Function: stopDrag
  332. *
  333. * Invokes <removeDragElement>.
  334. */
  335. mxDragSource.prototype.stopDrag = function()
  336. {
  337. // LATER: This used to have a mouse event. If that is still needed we need to add another
  338. // final call to the DnD protocol to add a cleanup step in the case of escape press, which
  339. // is not associated with a mouse event and which currently calles this method.
  340. this.removeDragElement();
  341. };
  342. /**
  343. * Function: removeDragElement
  344. *
  345. * Removes and destroys the <dragElement>.
  346. */
  347. mxDragSource.prototype.removeDragElement = function()
  348. {
  349. if (this.dragElement != null)
  350. {
  351. if (this.dragElement.parentNode != null)
  352. {
  353. this.dragElement.parentNode.removeChild(this.dragElement);
  354. }
  355. this.dragElement = null;
  356. }
  357. };
  358. /**
  359. * Function: getElementForEvent
  360. *
  361. * Returns the topmost element under the given event.
  362. */
  363. mxDragSource.prototype.getElementForEvent = function(evt)
  364. {
  365. return ((mxEvent.isTouchEvent(evt) || mxEvent.isPenEvent(evt)) ?
  366. document.elementFromPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt)) :
  367. mxEvent.getSource(evt));
  368. };
  369. /**
  370. * Function: graphContainsEvent
  371. *
  372. * Returns true if the given graph contains the given event.
  373. */
  374. mxDragSource.prototype.graphContainsEvent = function(graph, evt)
  375. {
  376. var x = mxEvent.getClientX(evt);
  377. var y = mxEvent.getClientY(evt);
  378. var offset = mxUtils.getOffset(graph.container);
  379. var origin = mxUtils.getScrollOrigin();
  380. var elt = this.getElementForEvent(evt);
  381. if (this.checkEventSource)
  382. {
  383. while (elt != null && elt != graph.container)
  384. {
  385. elt = elt.parentNode;
  386. }
  387. }
  388. // Checks if event is inside the bounds of the graph container
  389. return elt != null && x >= offset.x - origin.x && y >= offset.y - origin.y &&
  390. x <= offset.x - origin.x + graph.container.offsetWidth &&
  391. y <= offset.y - origin.y + graph.container.offsetHeight;
  392. };
  393. /**
  394. * Function: mouseMove
  395. *
  396. * Gets the graph for the given event using <getGraphForEvent>, updates the
  397. * <currentGraph>, calling <dragEnter> and <dragExit> on the new and old graph,
  398. * respectively, and invokes <dragOver> if <currentGraph> is not null.
  399. */
  400. mxDragSource.prototype.mouseMove = function(evt)
  401. {
  402. var graph = this.getGraphForEvent(evt);
  403. // Checks if event is inside the bounds of the graph container
  404. if (graph != null && !this.graphContainsEvent(graph, evt))
  405. {
  406. graph = null;
  407. }
  408. if (graph != this.currentGraph)
  409. {
  410. if (this.currentGraph != null)
  411. {
  412. this.dragExit(this.currentGraph, evt);
  413. }
  414. this.currentGraph = graph;
  415. if (this.currentGraph != null)
  416. {
  417. this.dragEnter(this.currentGraph, evt);
  418. }
  419. }
  420. if (this.currentGraph != null)
  421. {
  422. this.dragOver(this.currentGraph, evt);
  423. }
  424. if (this.dragElement != null && (this.previewElement == null || this.previewElement.style.visibility != 'visible'))
  425. {
  426. var x = mxEvent.getClientX(evt);
  427. var y = mxEvent.getClientY(evt);
  428. if (this.dragElement.parentNode == null)
  429. {
  430. document.body.appendChild(this.dragElement);
  431. }
  432. this.dragElement.style.visibility = 'visible';
  433. if (this.dragOffset != null)
  434. {
  435. x += this.dragOffset.x;
  436. y += this.dragOffset.y;
  437. }
  438. var offset = mxUtils.getDocumentScrollOrigin(document);
  439. this.dragElement.style.left = (x + offset.x) + 'px';
  440. this.dragElement.style.top = (y + offset.y) + 'px';
  441. }
  442. else if (this.dragElement != null)
  443. {
  444. this.dragElement.style.visibility = 'hidden';
  445. }
  446. mxEvent.consume(evt);
  447. };
  448. /**
  449. * Function: mouseUp
  450. *
  451. * Processes the mouse up event and invokes <drop>, <dragExit> and <stopDrag>
  452. * as required.
  453. */
  454. mxDragSource.prototype.mouseUp = function(evt)
  455. {
  456. if (this.currentGraph != null)
  457. {
  458. if (this.currentPoint != null && (this.previewElement == null ||
  459. this.previewElement.style.visibility != 'hidden'))
  460. {
  461. var scale = this.currentGraph.view.scale;
  462. var tr = this.currentGraph.view.translate;
  463. var x = this.currentPoint.x / scale - tr.x;
  464. var y = this.currentPoint.y / scale - tr.y;
  465. this.drop(this.currentGraph, evt, this.currentDropTarget, x, y);
  466. }
  467. this.dragExit(this.currentGraph);
  468. this.currentGraph = null;
  469. }
  470. this.stopDrag();
  471. this.removeListeners();
  472. mxEvent.consume(evt);
  473. };
  474. /**
  475. * Function: removeListeners
  476. *
  477. * Actives the given graph as a drop target.
  478. */
  479. mxDragSource.prototype.removeListeners = function()
  480. {
  481. if (this.eventSource != null)
  482. {
  483. mxEvent.removeGestureListeners(this.eventSource, null, this.mouseMoveHandler, this.mouseUpHandler);
  484. this.eventSource = null;
  485. }
  486. mxEvent.removeGestureListeners(document, null, this.mouseMoveHandler, this.mouseUpHandler);
  487. this.mouseMoveHandler = null;
  488. this.mouseUpHandler = null;
  489. };
  490. /**
  491. * Function: dragEnter
  492. *
  493. * Actives the given graph as a drop target.
  494. */
  495. mxDragSource.prototype.dragEnter = function(graph, evt)
  496. {
  497. graph.isMouseDown = true;
  498. graph.isMouseTrigger = mxEvent.isMouseEvent(evt);
  499. this.previewElement = this.createPreviewElement(graph);
  500. if (this.previewElement != null && this.checkEventSource && mxClient.IS_SVG)
  501. {
  502. this.previewElement.style.pointerEvents = 'none';
  503. }
  504. // Guide is only needed if preview element is used
  505. if (this.isGuidesEnabled() && this.previewElement != null)
  506. {
  507. this.currentGuide = new mxGuide(graph, graph.graphHandler.getGuideStates());
  508. }
  509. if (this.highlightDropTargets)
  510. {
  511. this.currentHighlight = new mxCellHighlight(graph, mxConstants.DROP_TARGET_COLOR);
  512. }
  513. // Consumes all events in the current graph before they are fired
  514. graph.addListener(mxEvent.FIRE_MOUSE_EVENT, this.eventConsumer);
  515. };
  516. /**
  517. * Function: dragExit
  518. *
  519. * Deactivates the given graph as a drop target.
  520. */
  521. mxDragSource.prototype.dragExit = function(graph, evt)
  522. {
  523. this.currentDropTarget = null;
  524. this.currentPoint = null;
  525. graph.isMouseDown = false;
  526. // Consumes all events in the current graph before they are fired
  527. graph.removeListener(this.eventConsumer);
  528. if (this.previewElement != null)
  529. {
  530. if (this.previewElement.parentNode != null)
  531. {
  532. this.previewElement.parentNode.removeChild(this.previewElement);
  533. }
  534. this.previewElement = null;
  535. }
  536. if (this.currentGuide != null)
  537. {
  538. this.currentGuide.destroy();
  539. this.currentGuide = null;
  540. }
  541. if (this.currentHighlight != null)
  542. {
  543. this.currentHighlight.destroy();
  544. this.currentHighlight = null;
  545. }
  546. };
  547. /**
  548. * Function: dragOver
  549. *
  550. * Implements autoscroll, updates the <currentPoint>, highlights any drop
  551. * targets and updates the preview.
  552. */
  553. mxDragSource.prototype.dragOver = function(graph, evt)
  554. {
  555. var offset = mxUtils.getOffset(graph.container);
  556. var origin = mxUtils.getScrollOrigin(graph.container);
  557. var x = mxEvent.getClientX(evt) - offset.x + origin.x - graph.panDx;
  558. var y = mxEvent.getClientY(evt) - offset.y + origin.y - graph.panDy;
  559. if (graph.autoScroll && (this.autoscroll == null || this.autoscroll))
  560. {
  561. graph.scrollPointToVisible(x, y, graph.autoExtend);
  562. }
  563. // Highlights the drop target under the mouse
  564. if (this.currentHighlight != null && graph.isDropEnabled())
  565. {
  566. this.currentDropTarget = this.getDropTarget(graph, x, y, evt);
  567. var state = graph.getView().getState(this.currentDropTarget);
  568. this.currentHighlight.highlight(state);
  569. }
  570. // Updates the location of the preview
  571. if (this.previewElement != null)
  572. {
  573. if (this.previewElement.parentNode == null)
  574. {
  575. graph.container.appendChild(this.previewElement);
  576. this.previewElement.style.zIndex = '3';
  577. this.previewElement.style.position = 'absolute';
  578. }
  579. var gridEnabled = this.isGridEnabled() && graph.isGridEnabledEvent(evt);
  580. var hideGuide = true;
  581. // Grid and guides
  582. if (this.currentGuide != null && this.currentGuide.isEnabledForEvent(evt))
  583. {
  584. // LATER: HTML preview appears smaller than SVG preview
  585. var w = parseInt(this.previewElement.style.width);
  586. var h = parseInt(this.previewElement.style.height);
  587. var bounds = new mxRectangle(0, 0, w, h);
  588. var delta = new mxPoint(x, y);
  589. delta = this.currentGuide.move(bounds, delta, gridEnabled, true);
  590. hideGuide = false;
  591. x = delta.x;
  592. y = delta.y;
  593. }
  594. else if (gridEnabled)
  595. {
  596. var scale = graph.view.scale;
  597. var tr = graph.view.translate;
  598. var off = graph.gridSize / 2;
  599. x = (graph.snap(x / scale - tr.x - off) + tr.x) * scale;
  600. y = (graph.snap(y / scale - tr.y - off) + tr.y) * scale;
  601. }
  602. if (this.currentGuide != null && hideGuide)
  603. {
  604. this.currentGuide.hide();
  605. }
  606. if (this.previewOffset != null)
  607. {
  608. x += this.previewOffset.x;
  609. y += this.previewOffset.y;
  610. }
  611. this.previewElement.style.left = Math.round(x) + 'px';
  612. this.previewElement.style.top = Math.round(y) + 'px';
  613. this.previewElement.style.visibility = 'visible';
  614. }
  615. this.currentPoint = new mxPoint(x, y);
  616. };
  617. /**
  618. * Function: drop
  619. *
  620. * Returns the drop target for the given graph and coordinates. This
  621. * implementation uses <mxGraph.getCellAt>.
  622. */
  623. mxDragSource.prototype.drop = function(graph, evt, dropTarget, x, y)
  624. {
  625. this.dropHandler.apply(this, arguments);
  626. // Had to move this to after the insert because it will
  627. // affect the scrollbars of the window in IE to try and
  628. // make the complete container visible.
  629. // LATER: Should be made optional.
  630. if (graph.container.style.visibility != 'hidden')
  631. {
  632. graph.container.focus();
  633. }
  634. };
  635. __mxOutput.mxDragSource = typeof mxDragSource !== 'undefined' ? mxDragSource : undefined;