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.

196 lines
5.9 KiB

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