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.

278 lines
9.7 KiB

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