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.

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