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.

229 lines
9.1 KiB

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