DraggableNodes.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* *
  2. *
  3. * Networkgraph series
  4. *
  5. * (c) 2010-2020 Paweł Fus
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import Chart from '../../Core/Chart/Chart.js';
  13. import H from '../../Core/Globals.js';
  14. import U from '../../Core/Utilities.js';
  15. var addEvent = U.addEvent;
  16. /* eslint-disable no-invalid-this, valid-jsdoc */
  17. H.dragNodesMixin = {
  18. /**
  19. * Mouse down action, initializing drag&drop mode.
  20. *
  21. * @private
  22. * @param {Highcharts.Point} point The point that event occured.
  23. * @param {Highcharts.PointerEventObject} event Browser event, before normalization.
  24. * @return {void}
  25. */
  26. onMouseDown: function (point, event) {
  27. var normalizedEvent = this.chart.pointer.normalize(event);
  28. point.fixedPosition = {
  29. chartX: normalizedEvent.chartX,
  30. chartY: normalizedEvent.chartY,
  31. plotX: point.plotX,
  32. plotY: point.plotY
  33. };
  34. point.inDragMode = true;
  35. },
  36. /**
  37. * Mouse move action during drag&drop.
  38. *
  39. * @private
  40. *
  41. * @param {global.Event} event Browser event, before normalization.
  42. * @param {Highcharts.Point} point The point that event occured.
  43. *
  44. * @return {void}
  45. */
  46. onMouseMove: function (point, event) {
  47. if (point.fixedPosition && point.inDragMode) {
  48. var series = this, chart = series.chart, normalizedEvent = chart.pointer.normalize(event), diffX = point.fixedPosition.chartX - normalizedEvent.chartX, diffY = point.fixedPosition.chartY - normalizedEvent.chartY, newPlotX, newPlotY, graphLayoutsLookup = chart.graphLayoutsLookup;
  49. // At least 5px to apply change (avoids simple click):
  50. if (Math.abs(diffX) > 5 || Math.abs(diffY) > 5) {
  51. newPlotX = point.fixedPosition.plotX - diffX;
  52. newPlotY = point.fixedPosition.plotY - diffY;
  53. if (chart.isInsidePlot(newPlotX, newPlotY)) {
  54. point.plotX = newPlotX;
  55. point.plotY = newPlotY;
  56. point.hasDragged = true;
  57. this.redrawHalo(point);
  58. graphLayoutsLookup.forEach(function (layout) {
  59. layout.restartSimulation();
  60. });
  61. }
  62. }
  63. }
  64. },
  65. /**
  66. * Mouse up action, finalizing drag&drop.
  67. *
  68. * @private
  69. * @param {Highcharts.Point} point The point that event occured.
  70. * @return {void}
  71. */
  72. onMouseUp: function (point, event) {
  73. if (point.fixedPosition && point.hasDragged) {
  74. if (this.layout.enableSimulation) {
  75. this.layout.start();
  76. }
  77. else {
  78. this.chart.redraw();
  79. }
  80. point.inDragMode = point.hasDragged = false;
  81. if (!this.options.fixedDraggable) {
  82. delete point.fixedPosition;
  83. }
  84. }
  85. },
  86. // Draggable mode:
  87. /**
  88. * Redraw halo on mousemove during the drag&drop action.
  89. *
  90. * @private
  91. * @param {Highcharts.Point} point The point that should show halo.
  92. * @return {void}
  93. */
  94. redrawHalo: function (point) {
  95. if (point && this.halo) {
  96. this.halo.attr({
  97. d: point.haloPath(this.options.states.hover.halo.size)
  98. });
  99. }
  100. }
  101. };
  102. /*
  103. * Draggable mode:
  104. */
  105. addEvent(Chart, 'load', function () {
  106. var chart = this, mousedownUnbinder, mousemoveUnbinder, mouseupUnbinder;
  107. if (chart.container) {
  108. mousedownUnbinder = addEvent(chart.container, 'mousedown', function (event) {
  109. var point = chart.hoverPoint;
  110. if (point &&
  111. point.series &&
  112. point.series.hasDraggableNodes &&
  113. point.series.options.draggable) {
  114. point.series.onMouseDown(point, event);
  115. mousemoveUnbinder = addEvent(chart.container, 'mousemove', function (e) {
  116. return point &&
  117. point.series &&
  118. point.series.onMouseMove(point, e);
  119. });
  120. mouseupUnbinder = addEvent(chart.container.ownerDocument, 'mouseup', function (e) {
  121. mousemoveUnbinder();
  122. mouseupUnbinder();
  123. return point &&
  124. point.series &&
  125. point.series.onMouseUp(point, e);
  126. });
  127. }
  128. });
  129. }
  130. addEvent(chart, 'destroy', function () {
  131. mousedownUnbinder();
  132. });
  133. });