Graph database Analysis of the Steam Network
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.

349 lines
9.3 KiB

  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Initialize packages:
  6. sigma.utils.pkg('sigma.captors');
  7. /**
  8. * The user inputs default captor. It deals with mouse events, keyboards
  9. * events and touch events.
  10. *
  11. * @param {DOMElement} target The DOM element where the listeners will be
  12. * bound.
  13. * @param {camera} camera The camera related to the target.
  14. * @param {configurable} settings The settings function.
  15. * @return {sigma.captor} The fresh new captor instance.
  16. */
  17. sigma.captors.mouse = function(target, camera, settings) {
  18. var _self = this,
  19. _target = target,
  20. _camera = camera,
  21. _settings = settings,
  22. // CAMERA MANAGEMENT:
  23. // ******************
  24. // The camera position when the user starts dragging:
  25. _startCameraX,
  26. _startCameraY,
  27. _startCameraAngle,
  28. // The latest stage position:
  29. _lastCameraX,
  30. _lastCameraY,
  31. _lastCameraAngle,
  32. _lastCameraRatio,
  33. // MOUSE MANAGEMENT:
  34. // *****************
  35. // The mouse position when the user starts dragging:
  36. _startMouseX,
  37. _startMouseY,
  38. _isMouseDown,
  39. _isMoving,
  40. _hasDragged,
  41. _downStartTime,
  42. _movingTimeoutId;
  43. sigma.classes.dispatcher.extend(this);
  44. sigma.utils.doubleClick(_target, 'click', _doubleClickHandler);
  45. _target.addEventListener('DOMMouseScroll', _wheelHandler, false);
  46. _target.addEventListener('mousewheel', _wheelHandler, false);
  47. _target.addEventListener('mousemove', _moveHandler, false);
  48. _target.addEventListener('mousedown', _downHandler, false);
  49. _target.addEventListener('click', _clickHandler, false);
  50. _target.addEventListener('mouseout', _outHandler, false);
  51. document.addEventListener('mouseup', _upHandler, false);
  52. /**
  53. * This method unbinds every handlers that makes the captor work.
  54. */
  55. this.kill = function() {
  56. sigma.utils.unbindDoubleClick(_target, 'click');
  57. _target.removeEventListener('DOMMouseScroll', _wheelHandler);
  58. _target.removeEventListener('mousewheel', _wheelHandler);
  59. _target.removeEventListener('mousemove', _moveHandler);
  60. _target.removeEventListener('mousedown', _downHandler);
  61. _target.removeEventListener('click', _clickHandler);
  62. _target.removeEventListener('mouseout', _outHandler);
  63. document.removeEventListener('mouseup', _upHandler);
  64. };
  65. // MOUSE EVENTS:
  66. // *************
  67. /**
  68. * The handler listening to the 'move' mouse event. It will effectively
  69. * drag the graph.
  70. *
  71. * @param {event} e A mouse event.
  72. */
  73. function _moveHandler(e) {
  74. var x,
  75. y,
  76. pos;
  77. // Dispatch event:
  78. if (_settings('mouseEnabled')) {
  79. _self.dispatchEvent('mousemove',
  80. sigma.utils.mouseCoords(e));
  81. if (_isMouseDown) {
  82. _isMoving = true;
  83. _hasDragged = true;
  84. if (_movingTimeoutId)
  85. clearTimeout(_movingTimeoutId);
  86. _movingTimeoutId = setTimeout(function() {
  87. _isMoving = false;
  88. }, _settings('dragTimeout'));
  89. sigma.misc.animation.killAll(_camera);
  90. _camera.isMoving = true;
  91. pos = _camera.cameraPosition(
  92. sigma.utils.getX(e) - _startMouseX,
  93. sigma.utils.getY(e) - _startMouseY,
  94. true
  95. );
  96. x = _startCameraX - pos.x;
  97. y = _startCameraY - pos.y;
  98. if (x !== _camera.x || y !== _camera.y) {
  99. _lastCameraX = _camera.x;
  100. _lastCameraY = _camera.y;
  101. _camera.goTo({
  102. x: x,
  103. y: y
  104. });
  105. }
  106. if (e.preventDefault)
  107. e.preventDefault();
  108. else
  109. e.returnValue = false;
  110. e.stopPropagation();
  111. return false;
  112. }
  113. }
  114. }
  115. /**
  116. * The handler listening to the 'up' mouse event. It will stop dragging the
  117. * graph.
  118. *
  119. * @param {event} e A mouse event.
  120. */
  121. function _upHandler(e) {
  122. if (_settings('mouseEnabled') && _isMouseDown) {
  123. _isMouseDown = false;
  124. if (_movingTimeoutId)
  125. clearTimeout(_movingTimeoutId);
  126. _camera.isMoving = false;
  127. var x = sigma.utils.getX(e),
  128. y = sigma.utils.getY(e);
  129. if (_isMoving) {
  130. sigma.misc.animation.killAll(_camera);
  131. sigma.misc.animation.camera(
  132. _camera,
  133. {
  134. x: _camera.x +
  135. _settings('mouseInertiaRatio') * (_camera.x - _lastCameraX),
  136. y: _camera.y +
  137. _settings('mouseInertiaRatio') * (_camera.y - _lastCameraY)
  138. },
  139. {
  140. easing: 'quadraticOut',
  141. duration: _settings('mouseInertiaDuration')
  142. }
  143. );
  144. } else if (
  145. _startMouseX !== x ||
  146. _startMouseY !== y
  147. )
  148. _camera.goTo({
  149. x: _camera.x,
  150. y: _camera.y
  151. });
  152. _self.dispatchEvent('mouseup',
  153. sigma.utils.mouseCoords(e));
  154. // Update _isMoving flag:
  155. _isMoving = false;
  156. }
  157. }
  158. /**
  159. * The handler listening to the 'down' mouse event. It will start observing
  160. * the mouse position for dragging the graph.
  161. *
  162. * @param {event} e A mouse event.
  163. */
  164. function _downHandler(e) {
  165. if (_settings('mouseEnabled')) {
  166. _startCameraX = _camera.x;
  167. _startCameraY = _camera.y;
  168. _lastCameraX = _camera.x;
  169. _lastCameraY = _camera.y;
  170. _startMouseX = sigma.utils.getX(e);
  171. _startMouseY = sigma.utils.getY(e);
  172. _hasDragged = false;
  173. _downStartTime = (new Date()).getTime();
  174. switch (e.which) {
  175. case 2:
  176. // Middle mouse button pressed
  177. // Do nothing.
  178. break;
  179. case 3:
  180. // Right mouse button pressed
  181. _self.dispatchEvent('rightclick',
  182. sigma.utils.mouseCoords(e, _startMouseX, _startMouseY));
  183. break;
  184. // case 1:
  185. default:
  186. // Left mouse button pressed
  187. _isMouseDown = true;
  188. _self.dispatchEvent('mousedown',
  189. sigma.utils.mouseCoords(e, _startMouseX, _startMouseY));
  190. }
  191. }
  192. }
  193. /**
  194. * The handler listening to the 'out' mouse event. It will just redispatch
  195. * the event.
  196. *
  197. * @param {event} e A mouse event.
  198. */
  199. function _outHandler(e) {
  200. if (_settings('mouseEnabled'))
  201. _self.dispatchEvent('mouseout');
  202. }
  203. /**
  204. * The handler listening to the 'click' mouse event. It will redispatch the
  205. * click event, but with normalized X and Y coordinates.
  206. *
  207. * @param {event} e A mouse event.
  208. */
  209. function _clickHandler(e) {
  210. if (_settings('mouseEnabled')) {
  211. var event = sigma.utils.mouseCoords(e);
  212. event.isDragging =
  213. (((new Date()).getTime() - _downStartTime) > 100) && _hasDragged;
  214. _self.dispatchEvent('click', event);
  215. }
  216. if (e.preventDefault)
  217. e.preventDefault();
  218. else
  219. e.returnValue = false;
  220. e.stopPropagation();
  221. return false;
  222. }
  223. /**
  224. * The handler listening to the double click custom event. It will
  225. * basically zoom into the graph.
  226. *
  227. * @param {event} e A mouse event.
  228. */
  229. function _doubleClickHandler(e) {
  230. var pos,
  231. ratio,
  232. animation;
  233. if (_settings('mouseEnabled')) {
  234. ratio = 1 / _settings('doubleClickZoomingRatio');
  235. _self.dispatchEvent('doubleclick',
  236. sigma.utils.mouseCoords(e, _startMouseX, _startMouseY));
  237. if (_settings('doubleClickEnabled')) {
  238. pos = _camera.cameraPosition(
  239. sigma.utils.getX(e) - sigma.utils.getCenter(e).x,
  240. sigma.utils.getY(e) - sigma.utils.getCenter(e).y,
  241. true
  242. );
  243. animation = {
  244. duration: _settings('doubleClickZoomDuration')
  245. };
  246. sigma.utils.zoomTo(_camera, pos.x, pos.y, ratio, animation);
  247. }
  248. if (e.preventDefault)
  249. e.preventDefault();
  250. else
  251. e.returnValue = false;
  252. e.stopPropagation();
  253. return false;
  254. }
  255. }
  256. /**
  257. * The handler listening to the 'wheel' mouse event. It will basically zoom
  258. * in or not into the graph.
  259. *
  260. * @param {event} e A mouse event.
  261. */
  262. function _wheelHandler(e) {
  263. var pos,
  264. ratio,
  265. animation,
  266. wheelDelta = sigma.utils.getDelta(e);
  267. if (_settings('mouseEnabled') && _settings('mouseWheelEnabled') && wheelDelta !== 0) {
  268. ratio = wheelDelta > 0 ?
  269. 1 / _settings('zoomingRatio') :
  270. _settings('zoomingRatio');
  271. pos = _camera.cameraPosition(
  272. sigma.utils.getX(e) - sigma.utils.getCenter(e).x,
  273. sigma.utils.getY(e) - sigma.utils.getCenter(e).y,
  274. true
  275. );
  276. animation = {
  277. duration: _settings('mouseZoomDuration')
  278. };
  279. sigma.utils.zoomTo(_camera, pos.x, pos.y, ratio, animation);
  280. if (e.preventDefault)
  281. e.preventDefault();
  282. else
  283. e.returnValue = false;
  284. e.stopPropagation();
  285. return false;
  286. }
  287. }
  288. };
  289. }).call(this);