panning.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.PanningManager = void 0;
  10. const x6_common_1 = require("@antv/x6-common");
  11. const base_1 = require("./base");
  12. class PanningManager extends base_1.Base {
  13. get widgetOptions() {
  14. return this.options.panning;
  15. }
  16. get pannable() {
  17. return this.widgetOptions && this.widgetOptions.enabled === true;
  18. }
  19. init() {
  20. this.onRightMouseDown = this.onRightMouseDown.bind(this);
  21. this.onSpaceKeyDown = this.onSpaceKeyDown.bind(this);
  22. this.onSpaceKeyUp = this.onSpaceKeyUp.bind(this);
  23. this.startListening();
  24. this.updateClassName();
  25. }
  26. startListening() {
  27. this.graph.on('blank:mousedown', this.onMouseDown, this);
  28. this.graph.on('node:unhandled:mousedown', this.onMouseDown, this);
  29. this.graph.on('edge:unhandled:mousedown', this.onMouseDown, this);
  30. x6_common_1.Dom.Event.on(this.graph.container, 'mousedown', this.onRightMouseDown);
  31. x6_common_1.Dom.Event.on(document.body, {
  32. keydown: this.onSpaceKeyDown,
  33. keyup: this.onSpaceKeyUp,
  34. });
  35. this.mousewheelHandle = new x6_common_1.Dom.MouseWheelHandle(this.graph.container, this.onMouseWheel.bind(this), this.allowMouseWheel.bind(this));
  36. this.mousewheelHandle.enable();
  37. }
  38. stopListening() {
  39. this.graph.off('blank:mousedown', this.onMouseDown, this);
  40. this.graph.off('node:unhandled:mousedown', this.onMouseDown, this);
  41. this.graph.off('edge:unhandled:mousedown', this.onMouseDown, this);
  42. x6_common_1.Dom.Event.off(this.graph.container, 'mousedown', this.onRightMouseDown);
  43. x6_common_1.Dom.Event.off(document.body, {
  44. keydown: this.onSpaceKeyDown,
  45. keyup: this.onSpaceKeyUp,
  46. });
  47. if (this.mousewheelHandle) {
  48. this.mousewheelHandle.disable();
  49. }
  50. }
  51. allowPanning(e, strict) {
  52. ;
  53. e.spaceKey = this.isSpaceKeyPressed;
  54. return (this.pannable &&
  55. x6_common_1.ModifierKey.isMatch(e, this.widgetOptions.modifiers, strict));
  56. }
  57. startPanning(evt) {
  58. const e = this.view.normalizeEvent(evt);
  59. this.clientX = e.clientX;
  60. this.clientY = e.clientY;
  61. this.panning = true;
  62. this.updateClassName();
  63. x6_common_1.Dom.Event.on(document.body, {
  64. 'mousemove.panning touchmove.panning': this.pan.bind(this),
  65. 'mouseup.panning touchend.panning': this.stopPanning.bind(this),
  66. 'mouseleave.panning': this.stopPanning.bind(this),
  67. });
  68. x6_common_1.Dom.Event.on(window, 'mouseup.panning', this.stopPanning.bind(this));
  69. }
  70. pan(evt) {
  71. const e = this.view.normalizeEvent(evt);
  72. const dx = e.clientX - this.clientX;
  73. const dy = e.clientY - this.clientY;
  74. this.clientX = e.clientX;
  75. this.clientY = e.clientY;
  76. this.graph.translateBy(dx, dy);
  77. }
  78. // eslint-disable-next-line
  79. stopPanning(e) {
  80. this.panning = false;
  81. this.updateClassName();
  82. x6_common_1.Dom.Event.off(document.body, '.panning');
  83. x6_common_1.Dom.Event.off(window, '.panning');
  84. }
  85. updateClassName() {
  86. const container = this.view.container;
  87. const panning = this.view.prefixClassName('graph-panning');
  88. const pannable = this.view.prefixClassName('graph-pannable');
  89. if (this.pannable) {
  90. if (this.panning) {
  91. x6_common_1.Dom.addClass(container, panning);
  92. x6_common_1.Dom.removeClass(container, pannable);
  93. }
  94. else {
  95. x6_common_1.Dom.removeClass(container, panning);
  96. x6_common_1.Dom.addClass(container, pannable);
  97. }
  98. }
  99. else {
  100. x6_common_1.Dom.removeClass(container, panning);
  101. x6_common_1.Dom.removeClass(container, pannable);
  102. }
  103. }
  104. onMouseDown({ e }) {
  105. if (!this.allowBlankMouseDown(e)) {
  106. return;
  107. }
  108. const selection = this.graph.getPlugin('selection');
  109. const allowRubberband = selection && selection.allowRubberband(e, true);
  110. if (this.allowPanning(e, true) ||
  111. (this.allowPanning(e) && !allowRubberband)) {
  112. this.startPanning(e);
  113. }
  114. }
  115. onRightMouseDown(e) {
  116. const eventTypes = this.widgetOptions.eventTypes;
  117. if (!((eventTypes === null || eventTypes === void 0 ? void 0 : eventTypes.includes('rightMouseDown')) && e.button === 2)) {
  118. return;
  119. }
  120. if (this.allowPanning(e, true)) {
  121. this.startPanning(e);
  122. }
  123. }
  124. onMouseWheel(e, deltaX, deltaY) {
  125. this.graph.translateBy(-deltaX, -deltaY);
  126. }
  127. onSpaceKeyDown(e) {
  128. if (e.which === 32) {
  129. this.isSpaceKeyPressed = true;
  130. }
  131. }
  132. onSpaceKeyUp(e) {
  133. if (e.which === 32) {
  134. this.isSpaceKeyPressed = false;
  135. }
  136. }
  137. allowBlankMouseDown(e) {
  138. const eventTypes = this.widgetOptions.eventTypes;
  139. return (((eventTypes === null || eventTypes === void 0 ? void 0 : eventTypes.includes('leftMouseDown')) && e.button === 0) ||
  140. ((eventTypes === null || eventTypes === void 0 ? void 0 : eventTypes.includes('mouseWheelDown')) && e.button === 1));
  141. }
  142. allowMouseWheel(e) {
  143. var _a;
  144. return (this.pannable &&
  145. !e.ctrlKey &&
  146. ((_a = this.widgetOptions.eventTypes) === null || _a === void 0 ? void 0 : _a.includes('mouseWheel')));
  147. }
  148. autoPanning(x, y) {
  149. const buffer = 10;
  150. const graphArea = this.graph.getGraphArea();
  151. let dx = 0;
  152. let dy = 0;
  153. if (x <= graphArea.left + buffer) {
  154. dx = -buffer;
  155. }
  156. if (y <= graphArea.top + buffer) {
  157. dy = -buffer;
  158. }
  159. if (x >= graphArea.right - buffer) {
  160. dx = buffer;
  161. }
  162. if (y >= graphArea.bottom - buffer) {
  163. dy = buffer;
  164. }
  165. if (dx !== 0 || dy !== 0) {
  166. this.graph.translateBy(-dx, -dy);
  167. }
  168. }
  169. enablePanning() {
  170. if (!this.pannable) {
  171. this.widgetOptions.enabled = true;
  172. this.updateClassName();
  173. }
  174. }
  175. disablePanning() {
  176. if (this.pannable) {
  177. this.widgetOptions.enabled = false;
  178. this.updateClassName();
  179. }
  180. }
  181. dispose() {
  182. this.stopListening();
  183. }
  184. }
  185. __decorate([
  186. base_1.Base.dispose()
  187. ], PanningManager.prototype, "dispose", null);
  188. exports.PanningManager = PanningManager;
  189. //# sourceMappingURL=panning.js.map