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.

181 lines
5.3 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. // clean up previous navigation items
  12. var wrapper = document.getElementById('network-navigation_wrapper');
  13. if (wrapper && wrapper.parentNode) {
  14. wrapper.parentNode.removeChild(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.navigationDivs = {};
  28. var navigationDivs = ['up','down','left','right','zoomIn','zoomOut','zoomExtends'];
  29. var navigationDivActions = ['_moveUp','_moveDown','_moveLeft','_moveRight','_zoomIn','_zoomOut','_zoomExtent'];
  30. this.navigationDivs['wrapper'] = document.createElement('div');
  31. this.navigationDivs['wrapper'].id = 'network-navigation_wrapper';
  32. this.frame.appendChild(this.navigationDivs['wrapper']);
  33. for (var i = 0; i < navigationDivs.length; i++) {
  34. this.navigationDivs[navigationDivs[i]] = document.createElement('div');
  35. this.navigationDivs[navigationDivs[i]].id = 'network-navigation_' + navigationDivs[i];
  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. var hammerDoc = Hammer(document, {prevent_default: false});
  43. hammerDoc.on('release', this._stopMovement.bind(this));
  44. this.navigationHammers.new.push(hammerDoc);
  45. this.navigationHammers.existing = this.navigationHammers.new;
  46. };
  47. /**
  48. * this stops all movement induced by the navigation buttons
  49. *
  50. * @private
  51. */
  52. exports._zoomExtent = function(event) {
  53. // FIXME: this is a workaround because the binding of Hammer on Document makes this fire twice
  54. if (this._zoomExtentLastTime === undefined || new Date() - this._zoomExtentLastTime > 200) {
  55. this._zoomExtentLastTime = new Date();
  56. this.zoomExtent({duration:800});
  57. event.stopPropagation();
  58. }
  59. };
  60. /**
  61. * this stops all movement induced by the navigation buttons
  62. *
  63. * @private
  64. */
  65. exports._stopMovement = function() {
  66. this._xStopMoving();
  67. this._yStopMoving();
  68. this._stopZoom();
  69. };
  70. /**
  71. * move the screen up
  72. * By using the increments, instead of adding a fixed number to the translation, we keep fluent and
  73. * instant movement. The onKeypress event triggers immediately, then pauses, then triggers frequently
  74. * To avoid this behaviour, we do the translation in the start loop.
  75. *
  76. * @private
  77. */
  78. exports._moveUp = 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 down
  85. * @private
  86. */
  87. exports._moveDown = function(event) {
  88. this.yIncrement = -this.constants.keyboard.speed.y;
  89. this.start(); // if there is no node movement, the calculation wont be done
  90. event.preventDefault();
  91. };
  92. /**
  93. * move the screen left
  94. * @private
  95. */
  96. exports._moveLeft = function(event) {
  97. this.xIncrement = this.constants.keyboard.speed.x;
  98. this.start(); // if there is no node movement, the calculation wont be done
  99. event.preventDefault();
  100. };
  101. /**
  102. * move the screen right
  103. * @private
  104. */
  105. exports._moveRight = function(event) {
  106. this.xIncrement = -this.constants.keyboard.speed.y;
  107. this.start(); // if there is no node movement, the calculation wont be done
  108. event.preventDefault();
  109. };
  110. /**
  111. * Zoom in, using the same method as the movement.
  112. * @private
  113. */
  114. exports._zoomIn = 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. * Zoom out
  121. * @private
  122. */
  123. exports._zoomOut = function(event) {
  124. this.zoomIncrement = -this.constants.keyboard.speed.zoom;
  125. this.start(); // if there is no node movement, the calculation wont be done
  126. event.preventDefault();
  127. };
  128. /**
  129. * Stop zooming and unhighlight the zoom controls
  130. * @private
  131. */
  132. exports._stopZoom = function(event) {
  133. this.zoomIncrement = 0;
  134. event && event.preventDefault();
  135. };
  136. /**
  137. * Stop moving in the Y direction and unHighlight the up and down
  138. * @private
  139. */
  140. exports._yStopMoving = function(event) {
  141. this.yIncrement = 0;
  142. event && event.preventDefault();
  143. };
  144. /**
  145. * Stop moving in the X direction and unHighlight left and right.
  146. * @private
  147. */
  148. exports._xStopMoving = function(event) {
  149. this.xIncrement = 0;
  150. event && event.preventDefault();
  151. };