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.

202 lines
7.7 KiB

  1. var util = require('../../../util');
  2. var Hammer = require('../../../module/hammer');
  3. var hammerUtil = require('../../../hammerUtil');
  4. var keycharm = require('keycharm');
  5. class NavigationHandler {
  6. constructor(body, canvas) {
  7. this.body = body;
  8. this.canvas = canvas;
  9. this.iconsCreated = false;
  10. this.navigationHammers = [];
  11. this.boundFunctions = {};
  12. this.touchTime = 0;
  13. this.activated = false;
  14. this.body.emitter.on("release", this._stopMovement.bind(this));
  15. this.body.emitter.on("activate", () => {this.activated = true; this.configureKeyboardBindings();});
  16. this.body.emitter.on("deactivate", () => {this.activated = false; this.configureKeyboardBindings();});
  17. this.options = {}
  18. }
  19. setOptions(options) {
  20. if (options !== undefined) {
  21. this.options = options;
  22. this.create();
  23. }
  24. }
  25. create() {
  26. if (this.options.showNavigationIcons === true) {
  27. if (this.iconsCreated === false) {
  28. this.loadNavigationElements();
  29. }
  30. }
  31. else if (this.iconsCreated === true) {
  32. this.cleanNavigation();
  33. }
  34. this.configureKeyboardBindings();
  35. }
  36. cleanNavigation() {
  37. // clean hammer bindings
  38. if (this.navigationHammers.length != 0) {
  39. for (var i = 0; i < this.navigationHammers.length; i++) {
  40. this.navigationHammers[i].destroy();
  41. }
  42. this.navigationHammers = [];
  43. }
  44. this._navigationReleaseOverload = function() {};
  45. // clean up previous navigation items
  46. if (this.navigationDOM && this.navigationDOM['wrapper'] && this.navigationDOM['wrapper'].parentNode) {
  47. this.navigationDOM['wrapper'].parentNode.removeChild(this.navigationDOM['wrapper']);
  48. }
  49. this.iconsCreated = false;
  50. }
  51. /**
  52. * Creation of the navigation controls nodes. They are drawn over the rest of the nodes and are not affected by scale and translation
  53. * they have a triggerFunction which is called on click. If the position of the navigation controls is dependent
  54. * on this.frame.canvas.clientWidth or this.frame.canvas.clientHeight, we flag horizontalAlignLeft and verticalAlignTop false.
  55. * This means that the location will be corrected by the _relocateNavigation function on a size change of the canvas.
  56. *
  57. * @private
  58. */
  59. loadNavigationElements() {
  60. this.cleanNavigation();
  61. this.navigationDOM = {};
  62. var navigationDivs = ['up','down','left','right','zoomIn','zoomOut','zoomExtends'];
  63. var navigationDivActions = ['_moveUp','_moveDown','_moveLeft','_moveRight','_zoomIn','_zoomOut','_zoomExtent'];
  64. this.navigationDOM['wrapper'] = document.createElement('div');
  65. this.canvas.frame.appendChild(this.navigationDOM['wrapper']);
  66. for (var i = 0; i < navigationDivs.length; i++) {
  67. this.navigationDOM[navigationDivs[i]] = document.createElement('div');
  68. this.navigationDOM[navigationDivs[i]].className = 'network-navigation ' + navigationDivs[i];
  69. this.navigationDOM['wrapper'].appendChild(this.navigationDOM[navigationDivs[i]]);
  70. var hammer = new Hammer(this.navigationDOM[navigationDivs[i]]);
  71. if (navigationDivActions[i] == "_zoomExtent") {
  72. hammerUtil.onTouch(hammer, this._zoomExtent.bind(this));
  73. }
  74. else {
  75. hammerUtil.onTouch(hammer, this.bindToRedraw.bind(this,navigationDivActions[i]));
  76. }
  77. this.navigationHammers.push(hammer);
  78. }
  79. this.iconsCreated = true;
  80. }
  81. bindToRedraw(action) {
  82. if (this.boundFunctions[action] === undefined) {
  83. this.boundFunctions[action] = this[action].bind(this);
  84. this.body.emitter.on("initRedraw", this.boundFunctions[action]);
  85. this.body.emitter.emit("_startRendering");
  86. }
  87. }
  88. unbindFromRedraw(action) {
  89. if (this.boundFunctions[action] !== undefined) {
  90. this.body.emitter.off("initRedraw", this.boundFunctions[action]);
  91. this.body.emitter.emit("_stopRendering");
  92. delete this.boundFunctions[action];
  93. }
  94. }
  95. /**
  96. * this stops all movement induced by the navigation buttons
  97. *
  98. * @private
  99. */
  100. _zoomExtent() {
  101. if (new Date().valueOf() - this.touchTime > 700) { // TODO: fix ugly hack to avoid hammer's double fireing of event (because we use release?)
  102. this.body.emitter.emit("zoomExtent", {duration: 700});
  103. this.touchTime = new Date().valueOf();
  104. }
  105. }
  106. /**
  107. * this stops all movement induced by the navigation buttons
  108. *
  109. * @private
  110. */
  111. _stopMovement() {
  112. for (let boundAction in this.boundFunctions) {
  113. if (this.boundFunctions.hasOwnProperty(boundAction)) {
  114. this.body.emitter.off("initRedraw", this.boundFunctions[boundAction]);
  115. this.body.emitter.emit("_stopRendering");
  116. }
  117. }
  118. this.boundFunctions = {};
  119. }
  120. _moveUp() {this.body.view.translation.y -= this.options.keyboard.speed.y;}
  121. _moveDown() {this.body.view.translation.y += this.options.keyboard.speed.y;}
  122. _moveLeft() {this.body.view.translation.x -= this.options.keyboard.speed.x;}
  123. _moveRight(){this.body.view.translation.x += this.options.keyboard.speed.x;}
  124. _zoomIn() {this.body.view.scale += this.options.keyboard.speed.zoom;}
  125. _zoomOut() {this.body.view.scale -= this.options.keyboard.speed.zoom;}
  126. /**
  127. * bind all keys using keycharm.
  128. */
  129. configureKeyboardBindings() {
  130. if (this.keycharm !== undefined) {
  131. this.keycharm.destroy();
  132. }
  133. if (this.options.keyboard.enabled === true) {
  134. if (this.options.keyboard.bindToWindow === true) {
  135. this.keycharm = keycharm({container: window, preventDefault: false});
  136. }
  137. else {
  138. this.keycharm = keycharm({container: this.canvas.frame, preventDefault: false});
  139. }
  140. this.keycharm.reset();
  141. if (this.activated === true) {
  142. this.keycharm.bind("up", this.bindToRedraw.bind(this, "_moveUp"), "keydown");
  143. this.keycharm.bind("down", this.bindToRedraw.bind(this, "_moveDown"), "keydown");
  144. this.keycharm.bind("left", this.bindToRedraw.bind(this, "_moveLeft"), "keydown");
  145. this.keycharm.bind("right", this.bindToRedraw.bind(this, "_moveRight"), "keydown");
  146. this.keycharm.bind("=", this.bindToRedraw.bind(this, "_zoomIn"), "keydown");
  147. this.keycharm.bind("num+", this.bindToRedraw.bind(this, "_zoomIn"), "keydown");
  148. this.keycharm.bind("num-", this.bindToRedraw.bind(this, "_zoomOut"), "keydown");
  149. this.keycharm.bind("-", this.bindToRedraw.bind(this, "_zoomOut"), "keydown");
  150. this.keycharm.bind("[", this.bindToRedraw.bind(this, "_zoomOut"), "keydown");
  151. this.keycharm.bind("]", this.bindToRedraw.bind(this, "_zoomIn"), "keydown");
  152. this.keycharm.bind("pageup", this.bindToRedraw.bind(this, "_zoomIn"), "keydown");
  153. this.keycharm.bind("pagedown", this.bindToRedraw.bind(this, "_zoomOut"), "keydown");
  154. this.keycharm.bind("up", this.unbindFromRedraw.bind(this, "_moveUp"), "keyup");
  155. this.keycharm.bind("down", this.unbindFromRedraw.bind(this, "_moveDown"), "keyup");
  156. this.keycharm.bind("left", this.unbindFromRedraw.bind(this, "_moveLeft"), "keyup");
  157. this.keycharm.bind("right", this.unbindFromRedraw.bind(this, "_moveRight"), "keyup");
  158. this.keycharm.bind("=", this.unbindFromRedraw.bind(this, "_zoomIn"), "keyup");
  159. this.keycharm.bind("num+", this.unbindFromRedraw.bind(this, "_zoomIn"), "keyup");
  160. this.keycharm.bind("num-", this.unbindFromRedraw.bind(this, "_zoomOut"), "keyup");
  161. this.keycharm.bind("-", this.unbindFromRedraw.bind(this, "_zoomOut"), "keyup");
  162. this.keycharm.bind("[", this.unbindFromRedraw.bind(this, "_zoomOut"), "keyup");
  163. this.keycharm.bind("]", this.unbindFromRedraw.bind(this, "_zoomIn"), "keyup");
  164. this.keycharm.bind("pageup", this.unbindFromRedraw.bind(this, "_zoomIn"), "keyup");
  165. this.keycharm.bind("pagedown", this.unbindFromRedraw.bind(this, "_zoomOut"), "keyup");
  166. }
  167. }
  168. }
  169. }
  170. export {NavigationHandler};