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.7 KiB

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