vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.5 KiB

  1. var util = require('../../util');
  2. var Hammer = require('../../module/hammer');
  3. exports._cleanNavigation = function() {
  4. // clean up previous navigation items
  5. var wrapper = document.getElementById('network-navigation_wrapper');
  6. if (wrapper != null) {
  7. this.containerElement.removeChild(wrapper);
  8. }
  9. document.onmouseup = null;
  10. };
  11. /**
  12. * Creation of the navigation controls nodes. They are drawn over the rest of the nodes and are not affected by scale and translation
  13. * they have a triggerFunction which is called on click. If the position of the navigation controls is dependent
  14. * on this.frame.canvas.clientWidth or this.frame.canvas.clientHeight, we flag horizontalAlignLeft and verticalAlignTop false.
  15. * This means that the location will be corrected by the _relocateNavigation function on a size change of the canvas.
  16. *
  17. * @private
  18. */
  19. exports._loadNavigationElements = function() {
  20. this._cleanNavigation();
  21. this.navigationDivs = {};
  22. var navigationDivs = ['up','down','left','right','zoomIn','zoomOut','zoomExtends'];
  23. var navigationDivActions = ['_moveUp','_moveDown','_moveLeft','_moveRight','_zoomIn','_zoomOut','zoomExtent'];
  24. this.navigationDivs['wrapper'] = document.createElement('div');
  25. this.navigationDivs['wrapper'].id = "network-navigation_wrapper";
  26. this.navigationDivs['wrapper'].style.position = "absolute";
  27. this.navigationDivs['wrapper'].style.width = this.frame.canvas.clientWidth + "px";
  28. this.navigationDivs['wrapper'].style.height = this.frame.canvas.clientHeight + "px";
  29. this.containerElement.insertBefore(this.navigationDivs['wrapper'],this.frame);
  30. var me = this;
  31. for (var i = 0; i < navigationDivs.length; i++) {
  32. this.navigationDivs[navigationDivs[i]] = document.createElement('div');
  33. this.navigationDivs[navigationDivs[i]].id = "network-navigation_" + navigationDivs[i];
  34. this.navigationDivs[navigationDivs[i]].className = "network-navigation " + navigationDivs[i];
  35. this.navigationDivs['wrapper'].appendChild(this.navigationDivs[navigationDivs[i]]);
  36. var hammer = Hammer(this.navigationDivs[navigationDivs[i]], {prevent_default: true});
  37. hammer.on("touch", me[navigationDivActions[i]].bind(me));
  38. }
  39. var hammer = Hammer(document, {prevent_default: false});
  40. hammer.on("release", me._stopMovement.bind(me));
  41. };
  42. /**
  43. * this stops all movement induced by the navigation buttons
  44. *
  45. * @private
  46. */
  47. exports._stopMovement = function() {
  48. this._xStopMoving();
  49. this._yStopMoving();
  50. this._stopZoom();
  51. };
  52. /**
  53. * move the screen up
  54. * By using the increments, instead of adding a fixed number to the translation, we keep fluent and
  55. * instant movement. The onKeypress event triggers immediately, then pauses, then triggers frequently
  56. * To avoid this behaviour, we do the translation in the start loop.
  57. *
  58. * @private
  59. */
  60. exports._moveUp = function(event) {
  61. this.yIncrement = this.constants.keyboard.speed.y;
  62. this.start(); // if there is no node movement, the calculation wont be done
  63. };
  64. /**
  65. * move the screen down
  66. * @private
  67. */
  68. exports._moveDown = function(event) {
  69. this.yIncrement = -this.constants.keyboard.speed.y;
  70. this.start(); // if there is no node movement, the calculation wont be done
  71. };
  72. /**
  73. * move the screen left
  74. * @private
  75. */
  76. exports._moveLeft = function(event) {
  77. this.xIncrement = this.constants.keyboard.speed.x;
  78. this.start(); // if there is no node movement, the calculation wont be done
  79. };
  80. /**
  81. * move the screen right
  82. * @private
  83. */
  84. exports._moveRight = function(event) {
  85. this.xIncrement = -this.constants.keyboard.speed.y;
  86. this.start(); // if there is no node movement, the calculation wont be done
  87. };
  88. /**
  89. * Zoom in, using the same method as the movement.
  90. * @private
  91. */
  92. exports._zoomIn = function(event) {
  93. this.zoomIncrement = this.constants.keyboard.speed.zoom;
  94. this.start(); // if there is no node movement, the calculation wont be done
  95. };
  96. /**
  97. * Zoom out
  98. * @private
  99. */
  100. exports._zoomOut = function() {
  101. this.zoomIncrement = -this.constants.keyboard.speed.zoom;
  102. this.start(); // if there is no node movement, the calculation wont be done
  103. util.preventDefault(event);
  104. };
  105. /**
  106. * Stop zooming and unhighlight the zoom controls
  107. * @private
  108. */
  109. exports._stopZoom = function() {
  110. this.zoomIncrement = 0;
  111. };
  112. /**
  113. * Stop moving in the Y direction and unHighlight the up and down
  114. * @private
  115. */
  116. exports._yStopMoving = function() {
  117. this.yIncrement = 0;
  118. };
  119. /**
  120. * Stop moving in the X direction and unHighlight left and right.
  121. * @private
  122. */
  123. exports._xStopMoving = function() {
  124. this.xIncrement = 0;
  125. };