event-contoller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * @fileoverview 事件处理器
  3. * @author dxq613@gmail.com
  4. */
  5. import GraphEvent from './graph-event';
  6. import { each, isParent } from '../util/util';
  7. var CLICK_OFFSET = 40;
  8. var LEFT_BTN_CODE = 0;
  9. var DELEGATION_SPLIT = ':';
  10. var EVENTS = [
  11. 'mousedown',
  12. 'mouseup',
  13. 'dblclick',
  14. 'mouseout',
  15. 'mouseover',
  16. 'mousemove',
  17. 'mouseleave',
  18. 'mouseenter',
  19. 'touchstart',
  20. 'touchmove',
  21. 'touchend',
  22. 'dragenter',
  23. 'dragover',
  24. 'dragleave',
  25. 'drop',
  26. 'contextmenu',
  27. 'mousewheel',
  28. ];
  29. // 是否有委托事件监听
  30. function hasDelegation(events, type) {
  31. for (var key in events) {
  32. if (events.hasOwnProperty(key) && key.indexOf(DELEGATION_SPLIT + type) >= 0) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. // 触发目标事件,目标只能是 shape 或 canvas
  39. function emitTargetEvent(target, type, eventObj) {
  40. eventObj.name = type;
  41. eventObj.target = target;
  42. eventObj.currentTarget = target;
  43. eventObj.delegateTarget = target;
  44. target.emit(type, eventObj);
  45. }
  46. // 事件冒泡, enter 和 leave 需要对 fromShape 和 toShape 进行判同
  47. function bubbleEvent(container, type, eventObj) {
  48. if (eventObj.bubbles) {
  49. var relativeShape = void 0;
  50. var isOverEvent = false;
  51. if (type === 'mouseenter') {
  52. relativeShape = eventObj.fromShape;
  53. isOverEvent = true;
  54. }
  55. else if (type === 'mouseleave') {
  56. isOverEvent = true;
  57. relativeShape = eventObj.toShape;
  58. }
  59. // canvas 上的 mouseenter, mouseleave 事件,仅当进入或者移出 canvas 时触发
  60. if (container.isCanvas() && isOverEvent) {
  61. return;
  62. }
  63. // 如果相关图形同当前图形在同一个容器内,不触发事件
  64. if (relativeShape && isParent(container, relativeShape)) {
  65. // 阻止继续向上冒泡
  66. eventObj.bubbles = false;
  67. return;
  68. }
  69. // 事件名称可能在委托过程中被修改,因此事件冒泡时需要重新设置事件名称
  70. eventObj.name = type;
  71. eventObj.currentTarget = container;
  72. eventObj.delegateTarget = container;
  73. container.emit(type, eventObj);
  74. }
  75. }
  76. var EventController = /** @class */ (function () {
  77. function EventController(cfg) {
  78. var _this = this;
  79. // 正在被拖拽的图形
  80. this.draggingShape = null;
  81. this.dragging = false;
  82. // 当前鼠标/touch所在位置的图形
  83. this.currentShape = null;
  84. this.mousedownShape = null;
  85. this.mousedownPoint = null;
  86. // 统一处理所有的回调
  87. this._eventCallback = function (ev) {
  88. var type = ev.type;
  89. _this._triggerEvent(type, ev);
  90. };
  91. // 在 document 处理拖拽到画布外的事件,处理从图形上移除画布未被捕捉的问题
  92. this._onDocumentMove = function (ev) {
  93. var canvas = _this.canvas;
  94. var el = canvas.get('el');
  95. if (el !== ev.target) {
  96. // 不在 canvas 上移动
  97. if (_this.dragging || _this.currentShape) {
  98. var pointInfo = _this._getPointInfo(ev);
  99. // 还在拖拽过程中
  100. if (_this.dragging) {
  101. _this._emitEvent('drag', ev, pointInfo, _this.draggingShape);
  102. }
  103. // 说明从某个图形直接移动到了画布外面,
  104. // 修复了 mouseleave 的 bug 后不再出现这种情况
  105. // if (this.currentShape) {
  106. // this._emitEvent('mouseleave', ev, pointInfo, this.currentShape, this.currentShape, null);
  107. // this.currentShape = null;
  108. // }
  109. }
  110. }
  111. };
  112. // 在 document 上处理拖拽到外面,释放鼠标时触发 dragend
  113. this._onDocumentMouseUp = function (ev) {
  114. var canvas = _this.canvas;
  115. var el = canvas.get('el');
  116. if (el !== ev.target) {
  117. // 不在 canvas 上移动
  118. if (_this.dragging) {
  119. var pointInfo = _this._getPointInfo(ev);
  120. if (_this.draggingShape) {
  121. // 如果存在拖拽的图形,则也触发 drop 事件
  122. _this._emitEvent('drop', ev, pointInfo, null);
  123. }
  124. _this._emitEvent('dragend', ev, pointInfo, _this.draggingShape);
  125. _this._afterDrag(_this.draggingShape, pointInfo, ev);
  126. }
  127. }
  128. };
  129. this.canvas = cfg.canvas;
  130. }
  131. EventController.prototype.init = function () {
  132. this._bindEvents();
  133. };
  134. // 注册事件
  135. EventController.prototype._bindEvents = function () {
  136. var _this = this;
  137. var el = this.canvas.get('el');
  138. each(EVENTS, function (eventName) {
  139. el.addEventListener(eventName, _this._eventCallback);
  140. });
  141. if (document) {
  142. // 处理移动到外面没有触发 shape mouse leave 的事件
  143. // 处理拖拽到外部的问题
  144. document.addEventListener('mousemove', this._onDocumentMove);
  145. // 处理拖拽过程中在外部释放鼠标的问题
  146. document.addEventListener('mouseup', this._onDocumentMouseUp);
  147. }
  148. };
  149. // 清理事件
  150. EventController.prototype._clearEvents = function () {
  151. var _this = this;
  152. var el = this.canvas.get('el');
  153. each(EVENTS, function (eventName) {
  154. el.removeEventListener(eventName, _this._eventCallback);
  155. });
  156. if (document) {
  157. document.removeEventListener('mousemove', this._onDocumentMove);
  158. document.removeEventListener('mouseup', this._onDocumentMouseUp);
  159. }
  160. };
  161. EventController.prototype._getEventObj = function (type, event, point, target, fromShape, toShape) {
  162. var eventObj = new GraphEvent(type, event);
  163. eventObj.fromShape = fromShape;
  164. eventObj.toShape = toShape;
  165. eventObj.x = point.x;
  166. eventObj.y = point.y;
  167. eventObj.clientX = point.clientX;
  168. eventObj.clientY = point.clientY;
  169. eventObj.propagationPath.push(target);
  170. // 事件的x,y应该是基于画布左上角的,与canvas的matrix无关
  171. return eventObj;
  172. };
  173. // 根据点获取图形,提取成独立方法,便于后续优化
  174. EventController.prototype._getShape = function (point, ev) {
  175. return this.canvas.getShape(point.x, point.y, ev);
  176. };
  177. // 获取事件的当前点的信息
  178. EventController.prototype._getPointInfo = function (ev) {
  179. var canvas = this.canvas;
  180. var clientPoint = canvas.getClientByEvent(ev);
  181. var point = canvas.getPointByEvent(ev);
  182. return {
  183. x: point.x,
  184. y: point.y,
  185. clientX: clientPoint.x,
  186. clientY: clientPoint.y,
  187. };
  188. };
  189. // 触发事件
  190. EventController.prototype._triggerEvent = function (type, ev) {
  191. var pointInfo = this._getPointInfo(ev);
  192. // 每次都获取图形有一定成本,后期可以考虑进行缓存策略
  193. var shape = this._getShape(pointInfo, ev);
  194. var method = this["_on" + type];
  195. var leaveCanvas = false;
  196. if (method) {
  197. method.call(this, pointInfo, shape, ev);
  198. }
  199. else {
  200. var preShape = this.currentShape;
  201. // 如果进入、移出画布时存在图形,则要分别触发事件
  202. if (type === 'mouseenter' || type === 'dragenter' || type === 'mouseover') {
  203. this._emitEvent(type, ev, pointInfo, null, null, shape); // 先进入画布
  204. if (shape) {
  205. this._emitEvent(type, ev, pointInfo, shape, null, shape); // 再触发图形的事件
  206. }
  207. if (type === 'mouseenter' && this.draggingShape) {
  208. // 如果正在拖拽图形, 则触发 dragleave
  209. this._emitEvent('dragenter', ev, pointInfo, null);
  210. }
  211. }
  212. else if (type === 'mouseleave' || type === 'dragleave' || type === 'mouseout') {
  213. leaveCanvas = true;
  214. if (preShape) {
  215. this._emitEvent(type, ev, pointInfo, preShape, preShape, null); // 先触发图形的事件
  216. }
  217. this._emitEvent(type, ev, pointInfo, null, preShape, null); // 再触发离开画布事件
  218. if (type === 'mouseleave' && this.draggingShape) {
  219. this._emitEvent('dragleave', ev, pointInfo, null);
  220. }
  221. }
  222. else {
  223. this._emitEvent(type, ev, pointInfo, shape, null, null); // 一般事件中不需要考虑 from, to
  224. }
  225. }
  226. if (!leaveCanvas) {
  227. this.currentShape = shape;
  228. }
  229. // 当鼠标从画布移动到 shape 或者从 preShape 移动到 shape 时,应用 shape 上的鼠标样式
  230. if (shape && !shape.get('destroyed')) {
  231. var canvas = this.canvas;
  232. var el = canvas.get('el');
  233. el.style.cursor = shape.attr('cursor') || canvas.get('cursor');
  234. }
  235. };
  236. // 记录下点击的位置、图形,便于拖拽事件、click 事件的判定
  237. EventController.prototype._onmousedown = function (pointInfo, shape, event) {
  238. // 只有鼠标左键的 mousedown 事件才会设置 mousedownShape 等属性,避免鼠标右键的 mousedown 事件引起其他事件发生
  239. if (event.button === LEFT_BTN_CODE) {
  240. this.mousedownShape = shape;
  241. this.mousedownPoint = pointInfo;
  242. this.mousedownTimeStamp = event.timeStamp;
  243. }
  244. this._emitEvent('mousedown', event, pointInfo, shape, null, null); // mousedown 不考虑fromShape, toShape
  245. };
  246. // mouseleave 和 mouseenter 都是成对存在的
  247. // mouseenter 和 mouseover 同时触发
  248. EventController.prototype._emitMouseoverEvents = function (event, pointInfo, fromShape, toShape) {
  249. var el = this.canvas.get('el');
  250. if (fromShape !== toShape) {
  251. if (fromShape) {
  252. this._emitEvent('mouseout', event, pointInfo, fromShape, fromShape, toShape);
  253. this._emitEvent('mouseleave', event, pointInfo, fromShape, fromShape, toShape);
  254. // 当鼠标从 fromShape 移动到画布上时,重置鼠标样式
  255. if (!toShape || toShape.get('destroyed')) {
  256. el.style.cursor = this.canvas.get('cursor');
  257. }
  258. }
  259. if (toShape) {
  260. this._emitEvent('mouseover', event, pointInfo, toShape, fromShape, toShape);
  261. this._emitEvent('mouseenter', event, pointInfo, toShape, fromShape, toShape);
  262. }
  263. }
  264. };
  265. // dragover 不等同于 mouseover,而等同于 mousemove
  266. EventController.prototype._emitDragoverEvents = function (event, pointInfo, fromShape, toShape, isCanvasEmit) {
  267. if (toShape) {
  268. if (toShape !== fromShape) {
  269. if (fromShape) {
  270. this._emitEvent('dragleave', event, pointInfo, fromShape, fromShape, toShape);
  271. }
  272. this._emitEvent('dragenter', event, pointInfo, toShape, fromShape, toShape);
  273. }
  274. if (!isCanvasEmit) {
  275. this._emitEvent('dragover', event, pointInfo, toShape);
  276. }
  277. }
  278. else if (fromShape) {
  279. // TODO: 此处判断有问题,当 drag 图形时,也会触发一次 dragleave 事件,因为此时 toShape 为 null,这不是所期望的
  280. // 经过空白区域
  281. this._emitEvent('dragleave', event, pointInfo, fromShape, fromShape, toShape);
  282. }
  283. if (isCanvasEmit) {
  284. this._emitEvent('dragover', event, pointInfo, toShape);
  285. }
  286. };
  287. // drag 完成后,需要做一些清理工作
  288. EventController.prototype._afterDrag = function (draggingShape, pointInfo, event) {
  289. if (draggingShape) {
  290. draggingShape.set('capture', true); // 恢复可以拾取
  291. this.draggingShape = null;
  292. }
  293. this.dragging = false;
  294. // drag 完成后,有可能 draggingShape 已经移动到了当前位置,所以不能直接取当前图形
  295. var shape = this._getShape(pointInfo, event);
  296. // 拖拽完成后,进行 enter,leave 的判定
  297. if (shape !== draggingShape) {
  298. this._emitMouseoverEvents(event, pointInfo, draggingShape, shape);
  299. }
  300. this.currentShape = shape; // 更新当前 shape,如果不处理当前图形的 mouseleave 事件可能会出问题
  301. };
  302. // 按键抬起时,会终止拖拽、触发点击
  303. EventController.prototype._onmouseup = function (pointInfo, shape, event) {
  304. // eevent.button === 0 表示鼠标左键事件,此处加上判断主要是为了避免右键鼠标会触发 mouseup 和 click 事件
  305. // ref: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
  306. if (event.button === LEFT_BTN_CODE) {
  307. var draggingShape = this.draggingShape;
  308. if (this.dragging) {
  309. // 存在可以拖拽的图形,同时拖拽到其他图形上时触发 drag 事件
  310. if (draggingShape) {
  311. this._emitEvent('drop', event, pointInfo, shape);
  312. }
  313. this._emitEvent('dragend', event, pointInfo, draggingShape);
  314. this._afterDrag(draggingShape, pointInfo, event);
  315. }
  316. else {
  317. this._emitEvent('mouseup', event, pointInfo, shape); // 先触发 mouseup 再触发 click
  318. if (shape === this.mousedownShape) {
  319. this._emitEvent('click', event, pointInfo, shape);
  320. }
  321. this.mousedownShape = null;
  322. this.mousedownPoint = null;
  323. }
  324. }
  325. };
  326. // 当触发浏览器的 dragover 事件时,不会再触发 mousemove ,所以这时候的 dragenter, dragleave 事件需要重新处理
  327. EventController.prototype._ondragover = function (pointInfo, shape, event) {
  328. event.preventDefault(); // 如果不对 dragover 进行 preventDefault,则不会在 canvas 上触发 drop 事件
  329. var preShape = this.currentShape;
  330. this._emitDragoverEvents(event, pointInfo, preShape, shape, true);
  331. };
  332. // 大量的图形事件,都通过 mousemove 模拟
  333. EventController.prototype._onmousemove = function (pointInfo, shape, event) {
  334. var canvas = this.canvas;
  335. var preShape = this.currentShape;
  336. var draggingShape = this.draggingShape;
  337. // 正在拖拽时
  338. if (this.dragging) {
  339. // 正在拖拽中
  340. if (draggingShape) {
  341. // 如果拖拽了 shape 会触发 dragenter, dragleave, dragover 和 drag 事件
  342. this._emitDragoverEvents(event, pointInfo, preShape, shape, false);
  343. }
  344. // 如果存在 draggingShape 则会在 draggingShape 上触发 drag 事件,冒泡到 canvas 上
  345. // 否则在 canvas 上触发 drag 事件
  346. this._emitEvent('drag', event, pointInfo, draggingShape);
  347. }
  348. else {
  349. var mousedownPoint = this.mousedownPoint;
  350. if (mousedownPoint) {
  351. // 当鼠标点击下去,同时移动时,进行 drag 判定
  352. var mousedownShape = this.mousedownShape;
  353. var now = event.timeStamp;
  354. var timeWindow = now - this.mousedownTimeStamp;
  355. var dx = mousedownPoint.clientX - pointInfo.clientX;
  356. var dy = mousedownPoint.clientY - pointInfo.clientY;
  357. var dist = dx * dx + dy * dy;
  358. if (timeWindow > 120 || dist > CLICK_OFFSET) {
  359. if (mousedownShape && mousedownShape.get('draggable')) {
  360. // 设置了 draggable 的 shape 才能触发 drag 相关的事件
  361. draggingShape = this.mousedownShape; // 拖动鼠标点下时的 shape
  362. draggingShape.set('capture', false); // 禁止继续拾取,否则无法进行 dragover,dragenter,dragleave,drop的判定
  363. this.draggingShape = draggingShape;
  364. this.dragging = true;
  365. this._emitEvent('dragstart', event, pointInfo, draggingShape);
  366. // 清理按下鼠标时缓存的值
  367. this.mousedownShape = null;
  368. this.mousedownPoint = null;
  369. }
  370. else if (!mousedownShape && canvas.get('draggable')) {
  371. // 设置了 draggable 的 canvas 才能触发 drag 相关的事件
  372. this.dragging = true;
  373. this._emitEvent('dragstart', event, pointInfo, null);
  374. // 清理按下鼠标时缓存的值
  375. this.mousedownShape = null;
  376. this.mousedownPoint = null;
  377. }
  378. else {
  379. this._emitMouseoverEvents(event, pointInfo, preShape, shape);
  380. this._emitEvent('mousemove', event, pointInfo, shape);
  381. }
  382. }
  383. else {
  384. this._emitMouseoverEvents(event, pointInfo, preShape, shape);
  385. this._emitEvent('mousemove', event, pointInfo, shape);
  386. }
  387. }
  388. else {
  389. // 没有按键按下时,则直接触发 mouse over 相关的各种事件
  390. this._emitMouseoverEvents(event, pointInfo, preShape, shape);
  391. // 始终触发移动
  392. this._emitEvent('mousemove', event, pointInfo, shape);
  393. }
  394. }
  395. };
  396. // 触发事件
  397. EventController.prototype._emitEvent = function (type, event, pointInfo, shape, fromShape, toShape) {
  398. var eventObj = this._getEventObj(type, event, pointInfo, shape, fromShape, toShape);
  399. // 存在 shape 触发,则进行冒泡处理
  400. if (shape) {
  401. eventObj.shape = shape;
  402. // 触发 shape 上的事件
  403. emitTargetEvent(shape, type, eventObj);
  404. var parent_1 = shape.getParent();
  405. // 执行冒泡
  406. while (parent_1) {
  407. // 委托事件要先触发
  408. parent_1.emitDelegation(type, eventObj);
  409. // 事件冒泡停止,不能妨碍委托事件
  410. if (!eventObj.propagationStopped) {
  411. bubbleEvent(parent_1, type, eventObj);
  412. }
  413. eventObj.propagationPath.push(parent_1);
  414. parent_1 = parent_1.getParent();
  415. }
  416. }
  417. else {
  418. // 如果没有 shape 直接在 canvas 上触发
  419. var canvas = this.canvas;
  420. // 直接触发 canvas 上的事件
  421. emitTargetEvent(canvas, type, eventObj);
  422. }
  423. };
  424. EventController.prototype.destroy = function () {
  425. // 清理事件
  426. this._clearEvents();
  427. // 清理缓存的对象
  428. this.canvas = null;
  429. this.currentShape = null;
  430. this.draggingShape = null;
  431. this.mousedownPoint = null;
  432. this.mousedownShape = null;
  433. this.mousedownTimeStamp = null;
  434. };
  435. return EventController;
  436. }());
  437. export default EventController;
  438. //# sourceMappingURL=event-contoller.js.map