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.

154 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 && 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. event.preventDefault();
  61. };
  62. /**
  63. * move the screen down
  64. * @private
  65. */
  66. exports._moveDown = function(event) {
  67. this.yIncrement = -this.constants.keyboard.speed.y;
  68. this.start(); // if there is no node movement, the calculation wont be done
  69. event.preventDefault();
  70. };
  71. /**
  72. * move the screen left
  73. * @private
  74. */
  75. exports._moveLeft = function(event) {
  76. this.xIncrement = this.constants.keyboard.speed.x;
  77. this.start(); // if there is no node movement, the calculation wont be done
  78. event.preventDefault();
  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. event.preventDefault();
  88. };
  89. /**
  90. * Zoom in, using the same method as the movement.
  91. * @private
  92. */
  93. exports._zoomIn = function(event) {
  94. this.zoomIncrement = this.constants.keyboard.speed.zoom;
  95. this.start(); // if there is no node movement, the calculation wont be done
  96. event.preventDefault();
  97. };
  98. /**
  99. * Zoom out
  100. * @private
  101. */
  102. exports._zoomOut = function(event) {
  103. this.zoomIncrement = -this.constants.keyboard.speed.zoom;
  104. this.start(); // if there is no node movement, the calculation wont be done
  105. event.preventDefault();
  106. };
  107. /**
  108. * Stop zooming and unhighlight the zoom controls
  109. * @private
  110. */
  111. exports._stopZoom = function(event) {
  112. this.zoomIncrement = 0;
  113. event && event.preventDefault();
  114. };
  115. /**
  116. * Stop moving in the Y direction and unHighlight the up and down
  117. * @private
  118. */
  119. exports._yStopMoving = function(event) {
  120. this.yIncrement = 0;
  121. event && event.preventDefault();
  122. };
  123. /**
  124. * Stop moving in the X direction and unHighlight left and right.
  125. * @private
  126. */
  127. exports._xStopMoving = function(event) {
  128. this.xIncrement = 0;
  129. event && event.preventDefault();
  130. };