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.

146 lines
4.2 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 && wrapper.parentNode) {
  7. wrapper.parentNode.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.frame.appendChild(this.navigationDivs['wrapper']);
  27. var me = this;
  28. for (var i = 0; i < navigationDivs.length; i++) {
  29. this.navigationDivs[navigationDivs[i]] = document.createElement('div');
  30. this.navigationDivs[navigationDivs[i]].id = 'network-navigation_' + navigationDivs[i];
  31. this.navigationDivs[navigationDivs[i]].className = 'network-navigation ' + navigationDivs[i];
  32. this.navigationDivs['wrapper'].appendChild(this.navigationDivs[navigationDivs[i]]);
  33. var hammer = Hammer(this.navigationDivs[navigationDivs[i]], {prevent_default: true});
  34. hammer.on('touch', me[navigationDivActions[i]].bind(me));
  35. }
  36. var hammer = Hammer(document, {prevent_default: false});
  37. hammer.on('release', me._stopMovement.bind(me));
  38. };
  39. /**
  40. * this stops all movement induced by the navigation buttons
  41. *
  42. * @private
  43. */
  44. exports._stopMovement = function() {
  45. this._xStopMoving();
  46. this._yStopMoving();
  47. this._stopZoom();
  48. };
  49. /**
  50. * move the screen up
  51. * By using the increments, instead of adding a fixed number to the translation, we keep fluent and
  52. * instant movement. The onKeypress event triggers immediately, then pauses, then triggers frequently
  53. * To avoid this behaviour, we do the translation in the start loop.
  54. *
  55. * @private
  56. */
  57. exports._moveUp = function(event) {
  58. this.yIncrement = this.constants.keyboard.speed.y;
  59. this.start(); // if there is no node movement, the calculation wont be done
  60. };
  61. /**
  62. * move the screen down
  63. * @private
  64. */
  65. exports._moveDown = function(event) {
  66. this.yIncrement = -this.constants.keyboard.speed.y;
  67. this.start(); // if there is no node movement, the calculation wont be done
  68. };
  69. /**
  70. * move the screen left
  71. * @private
  72. */
  73. exports._moveLeft = function(event) {
  74. this.xIncrement = this.constants.keyboard.speed.x;
  75. this.start(); // if there is no node movement, the calculation wont be done
  76. };
  77. /**
  78. * move the screen right
  79. * @private
  80. */
  81. exports._moveRight = function(event) {
  82. this.xIncrement = -this.constants.keyboard.speed.y;
  83. this.start(); // if there is no node movement, the calculation wont be done
  84. };
  85. /**
  86. * Zoom in, using the same method as the movement.
  87. * @private
  88. */
  89. exports._zoomIn = function(event) {
  90. this.zoomIncrement = this.constants.keyboard.speed.zoom;
  91. this.start(); // if there is no node movement, the calculation wont be done
  92. };
  93. /**
  94. * Zoom out
  95. * @private
  96. */
  97. exports._zoomOut = function() {
  98. this.zoomIncrement = -this.constants.keyboard.speed.zoom;
  99. this.start(); // if there is no node movement, the calculation wont be done
  100. util.preventDefault(event);
  101. };
  102. /**
  103. * Stop zooming and unhighlight the zoom controls
  104. * @private
  105. */
  106. exports._stopZoom = function() {
  107. this.zoomIncrement = 0;
  108. };
  109. /**
  110. * Stop moving in the Y direction and unHighlight the up and down
  111. * @private
  112. */
  113. exports._yStopMoving = function() {
  114. this.yIncrement = 0;
  115. };
  116. /**
  117. * Stop moving in the X direction and unHighlight left and right.
  118. * @private
  119. */
  120. exports._xStopMoving = function() {
  121. this.xIncrement = 0;
  122. };