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.

176 lines
4.9 KiB

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