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.

177 lines
5.0 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]].id = 'network-navigation_' + navigationDivs[i];
  37. this.navigationDivs[navigationDivs[i]].className = 'network-navigation ' + navigationDivs[i];
  38. this.navigationDivs['wrapper'].appendChild(this.navigationDivs[navigationDivs[i]]);
  39. var hammer = Hammer(this.navigationDivs[navigationDivs[i]], {prevent_default: true});
  40. hammer.on('touch', this[navigationDivActions[i]].bind(this));
  41. this.navigationHammers.new.push(hammer);
  42. }
  43. this._navigationReleaseOverload = this._stopMovement;
  44. this.navigationHammers.existing = this.navigationHammers.new;
  45. };
  46. /**
  47. * this stops all movement induced by the navigation buttons
  48. *
  49. * @private
  50. */
  51. exports._zoomExtent = function(event) {
  52. this.zoomExtent({duration:800});
  53. event.stopPropagation();
  54. };
  55. /**
  56. * this stops all movement induced by the navigation buttons
  57. *
  58. * @private
  59. */
  60. exports._stopMovement = function() {
  61. this._xStopMoving();
  62. this._yStopMoving();
  63. this._stopZoom();
  64. };
  65. /**
  66. * move the screen up
  67. * By using the increments, instead of adding a fixed number to the translation, we keep fluent and
  68. * instant movement. The onKeypress event triggers immediately, then pauses, then triggers frequently
  69. * To avoid this behaviour, we do the translation in the start loop.
  70. *
  71. * @private
  72. */
  73. exports._moveUp = function(event) {
  74. this.yIncrement = this.constants.keyboard.speed.y;
  75. this.start(); // if there is no node movement, the calculation wont be done
  76. event.preventDefault();
  77. };
  78. /**
  79. * move the screen down
  80. * @private
  81. */
  82. exports._moveDown = function(event) {
  83. this.yIncrement = -this.constants.keyboard.speed.y;
  84. this.start(); // if there is no node movement, the calculation wont be done
  85. event.preventDefault();
  86. };
  87. /**
  88. * move the screen left
  89. * @private
  90. */
  91. exports._moveLeft = function(event) {
  92. this.xIncrement = this.constants.keyboard.speed.x;
  93. this.start(); // if there is no node movement, the calculation wont be done
  94. event.preventDefault();
  95. };
  96. /**
  97. * move the screen right
  98. * @private
  99. */
  100. exports._moveRight = function(event) {
  101. this.xIncrement = -this.constants.keyboard.speed.y;
  102. this.start(); // if there is no node movement, the calculation wont be done
  103. event.preventDefault();
  104. };
  105. /**
  106. * Zoom in, using the same method as the movement.
  107. * @private
  108. */
  109. exports._zoomIn = function(event) {
  110. this.zoomIncrement = this.constants.keyboard.speed.zoom;
  111. this.start(); // if there is no node movement, the calculation wont be done
  112. event.preventDefault();
  113. };
  114. /**
  115. * Zoom out
  116. * @private
  117. */
  118. exports._zoomOut = function(event) {
  119. this.zoomIncrement = -this.constants.keyboard.speed.zoom;
  120. this.start(); // if there is no node movement, the calculation wont be done
  121. event.preventDefault();
  122. };
  123. /**
  124. * Stop zooming and unhighlight the zoom controls
  125. * @private
  126. */
  127. exports._stopZoom = function(event) {
  128. this.zoomIncrement = 0;
  129. event && event.preventDefault();
  130. };
  131. /**
  132. * Stop moving in the Y direction and unHighlight the up and down
  133. * @private
  134. */
  135. exports._yStopMoving = function(event) {
  136. this.yIncrement = 0;
  137. event && event.preventDefault();
  138. };
  139. /**
  140. * Stop moving in the X direction and unHighlight left and right.
  141. * @private
  142. */
  143. exports._xStopMoving = function(event) {
  144. this.xIncrement = 0;
  145. event && event.preventDefault();
  146. };