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.

206 lines
8.1 KiB

9 years ago
9 years ago
9 years ago
  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();});
  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.navigationButtons === 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. // 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','_fit'];
  64. this.navigationDOM['wrapper'] = document.createElement('div');
  65. this.navigationDOM['wrapper'].className = 'vis-navigation';
  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 = 'vis-button vis-' + navigationDivs[i];
  70. this.navigationDOM['wrapper'].appendChild(this.navigationDOM[navigationDivs[i]]);
  71. var hammer = new Hammer(this.navigationDOM[navigationDivs[i]]);
  72. if (navigationDivActions[i] === "_fit") {
  73. hammerUtil.onTouch(hammer, this._fit.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. _fit() {
  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("fit", {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() {
  126. this.body.view.scale *= 1+this.options.keyboard.speed.zoom;
  127. this.body.emitter.emit('zoom', {direction: '+', scale: this.body.view.scale});}
  128. _zoomOut() {
  129. this.body.view.scale /= 1+this.options.keyboard.speed.zoom;
  130. this.body.emitter.emit('zoom', {direction: '-', scale: this.body.view.scale});
  131. }
  132. /**
  133. * bind all keys using keycharm.
  134. */
  135. configureKeyboardBindings() {
  136. if (this.keycharm !== undefined) {
  137. this.keycharm.destroy();
  138. }
  139. if (this.options.keyboard.enabled === true) {
  140. if (this.options.keyboard.bindToWindow === true) {
  141. this.keycharm = keycharm({container: window, preventDefault: true});
  142. }
  143. else {
  144. this.keycharm = keycharm({container: this.canvas.frame, preventDefault: true});
  145. }
  146. this.keycharm.reset();
  147. if (this.activated === true) {
  148. this.keycharm.bind("up", () => {this.bindToRedraw("_moveUp") ;}, "keydown");
  149. this.keycharm.bind("down", () => {this.bindToRedraw("_moveDown") ;}, "keydown");
  150. this.keycharm.bind("left", () => {this.bindToRedraw("_moveLeft") ;}, "keydown");
  151. this.keycharm.bind("right", () => {this.bindToRedraw("_moveRight");}, "keydown");
  152. this.keycharm.bind("=", () => {this.bindToRedraw("_zoomIn") ;}, "keydown");
  153. this.keycharm.bind("num+", () => {this.bindToRedraw("_zoomIn") ;}, "keydown");
  154. this.keycharm.bind("num-", () => {this.bindToRedraw("_zoomOut") ;}, "keydown");
  155. this.keycharm.bind("-", () => {this.bindToRedraw("_zoomOut") ;}, "keydown");
  156. this.keycharm.bind("[", () => {this.bindToRedraw("_zoomOut") ;}, "keydown");
  157. this.keycharm.bind("]", () => {this.bindToRedraw("_zoomIn") ;}, "keydown");
  158. this.keycharm.bind("pageup", () => {this.bindToRedraw("_zoomIn") ;}, "keydown");
  159. this.keycharm.bind("pagedown", () => {this.bindToRedraw("_zoomOut") ;}, "keydown");
  160. this.keycharm.bind("up", () => {this.unbindFromRedraw("_moveUp") ;}, "keyup");
  161. this.keycharm.bind("down", () => {this.unbindFromRedraw("_moveDown") ;}, "keyup");
  162. this.keycharm.bind("left", () => {this.unbindFromRedraw("_moveLeft") ;}, "keyup");
  163. this.keycharm.bind("right", () => {this.unbindFromRedraw("_moveRight");}, "keyup");
  164. this.keycharm.bind("=", () => {this.unbindFromRedraw("_zoomIn") ;}, "keyup");
  165. this.keycharm.bind("num+", () => {this.unbindFromRedraw("_zoomIn") ;}, "keyup");
  166. this.keycharm.bind("num-", () => {this.unbindFromRedraw("_zoomOut") ;}, "keyup");
  167. this.keycharm.bind("-", () => {this.unbindFromRedraw("_zoomOut") ;}, "keyup");
  168. this.keycharm.bind("[", () => {this.unbindFromRedraw("_zoomOut") ;}, "keyup");
  169. this.keycharm.bind("]", () => {this.unbindFromRedraw("_zoomIn") ;}, "keyup");
  170. this.keycharm.bind("pageup", () => {this.unbindFromRedraw("_zoomIn") ;}, "keyup");
  171. this.keycharm.bind("pagedown", () => {this.unbindFromRedraw("_zoomOut") ;}, "keyup");
  172. }
  173. }
  174. }
  175. }
  176. export default NavigationHandler;