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.

173 lines
4.7 KiB

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