DraggableNodes.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) {
  74. if (point.hasDragged) {
  75. if (this.layout.enableSimulation) {
  76. this.layout.start();
  77. }
  78. else {
  79. this.chart.redraw();
  80. }
  81. }
  82. point.inDragMode = point.hasDragged = false;
  83. if (!this.options.fixedDraggable) {
  84. delete point.fixedPosition;
  85. }
  86. }
  87. },
  88. // Draggable mode:
  89. /**
  90. * Redraw halo on mousemove during the drag&drop action.
  91. *
  92. * @private
  93. * @param {Highcharts.Point} point The point that should show halo.
  94. * @return {void}
  95. */
  96. redrawHalo: function (point) {
  97. if (point && this.halo) {
  98. this.halo.attr({
  99. d: point.haloPath(this.options.states.hover.halo.size)
  100. });
  101. }
  102. }
  103. };
  104. /*
  105. * Draggable mode:
  106. */
  107. addEvent(Chart, 'load', function () {
  108. var chart = this, mousedownUnbinder, mousemoveUnbinder, mouseupUnbinder;
  109. if (chart.container) {
  110. mousedownUnbinder = addEvent(chart.container, 'mousedown', function (event) {
  111. var point = chart.hoverPoint;
  112. if (point &&
  113. point.series &&
  114. point.series.hasDraggableNodes &&
  115. point.series.options.draggable) {
  116. point.series.onMouseDown(point, event);
  117. mousemoveUnbinder = addEvent(chart.container, 'mousemove', function (e) {
  118. return point &&
  119. point.series &&
  120. point.series.onMouseMove(point, e);
  121. });
  122. mouseupUnbinder = addEvent(chart.container.ownerDocument, 'mouseup', function (e) {
  123. mousemoveUnbinder();
  124. mouseupUnbinder();
  125. return point &&
  126. point.series &&
  127. point.series.onMouseUp(point, e);
  128. });
  129. }
  130. });
  131. }
  132. addEvent(chart, 'destroy', function () {
  133. mousedownUnbinder();
  134. });
  135. });