RoamController.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import Eventful from 'zrender/lib/core/Eventful.js';
  42. import * as eventTool from 'zrender/lib/core/event.js';
  43. import * as interactionMutex from './interactionMutex.js';
  44. import { isString, bind, defaults, clone } from 'zrender/lib/core/util.js';
  45. ;
  46. var RoamController = /** @class */function (_super) {
  47. __extends(RoamController, _super);
  48. function RoamController(zr) {
  49. var _this = _super.call(this) || this;
  50. _this._zr = zr;
  51. // Avoid two roamController bind the same handler
  52. var mousedownHandler = bind(_this._mousedownHandler, _this);
  53. var mousemoveHandler = bind(_this._mousemoveHandler, _this);
  54. var mouseupHandler = bind(_this._mouseupHandler, _this);
  55. var mousewheelHandler = bind(_this._mousewheelHandler, _this);
  56. var pinchHandler = bind(_this._pinchHandler, _this);
  57. /**
  58. * Notice: only enable needed types. For example, if 'zoom'
  59. * is not needed, 'zoom' should not be enabled, otherwise
  60. * default mousewheel behaviour (scroll page) will be disabled.
  61. */
  62. _this.enable = function (controlType, opt) {
  63. // Disable previous first
  64. this.disable();
  65. this._opt = defaults(clone(opt) || {}, {
  66. zoomOnMouseWheel: true,
  67. moveOnMouseMove: true,
  68. // By default, wheel do not trigger move.
  69. moveOnMouseWheel: false,
  70. preventDefaultMouseMove: true
  71. });
  72. if (controlType == null) {
  73. controlType = true;
  74. }
  75. if (controlType === true || controlType === 'move' || controlType === 'pan') {
  76. zr.on('mousedown', mousedownHandler);
  77. zr.on('mousemove', mousemoveHandler);
  78. zr.on('mouseup', mouseupHandler);
  79. }
  80. if (controlType === true || controlType === 'scale' || controlType === 'zoom') {
  81. zr.on('mousewheel', mousewheelHandler);
  82. zr.on('pinch', pinchHandler);
  83. }
  84. };
  85. _this.disable = function () {
  86. zr.off('mousedown', mousedownHandler);
  87. zr.off('mousemove', mousemoveHandler);
  88. zr.off('mouseup', mouseupHandler);
  89. zr.off('mousewheel', mousewheelHandler);
  90. zr.off('pinch', pinchHandler);
  91. };
  92. return _this;
  93. }
  94. RoamController.prototype.isDragging = function () {
  95. return this._dragging;
  96. };
  97. RoamController.prototype.isPinching = function () {
  98. return this._pinching;
  99. };
  100. RoamController.prototype.setPointerChecker = function (pointerChecker) {
  101. this.pointerChecker = pointerChecker;
  102. };
  103. RoamController.prototype.dispose = function () {
  104. this.disable();
  105. };
  106. RoamController.prototype._mousedownHandler = function (e) {
  107. if (eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  108. return;
  109. }
  110. var el = e.target;
  111. while (el) {
  112. if (el.draggable) {
  113. return;
  114. }
  115. // check if host is draggable
  116. el = el.__hostTarget || el.parent;
  117. }
  118. var x = e.offsetX;
  119. var y = e.offsetY;
  120. // Only check on mosedown, but not mousemove.
  121. // Mouse can be out of target when mouse moving.
  122. if (this.pointerChecker && this.pointerChecker(e, x, y)) {
  123. this._x = x;
  124. this._y = y;
  125. this._dragging = true;
  126. }
  127. };
  128. RoamController.prototype._mousemoveHandler = function (e) {
  129. if (!this._dragging || !isAvailableBehavior('moveOnMouseMove', e, this._opt) || e.gestureEvent === 'pinch' || interactionMutex.isTaken(this._zr, 'globalPan')) {
  130. return;
  131. }
  132. var x = e.offsetX;
  133. var y = e.offsetY;
  134. var oldX = this._x;
  135. var oldY = this._y;
  136. var dx = x - oldX;
  137. var dy = y - oldY;
  138. this._x = x;
  139. this._y = y;
  140. this._opt.preventDefaultMouseMove && eventTool.stop(e.event);
  141. trigger(this, 'pan', 'moveOnMouseMove', e, {
  142. dx: dx,
  143. dy: dy,
  144. oldX: oldX,
  145. oldY: oldY,
  146. newX: x,
  147. newY: y,
  148. isAvailableBehavior: null
  149. });
  150. };
  151. RoamController.prototype._mouseupHandler = function (e) {
  152. if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  153. this._dragging = false;
  154. }
  155. };
  156. RoamController.prototype._mousewheelHandler = function (e) {
  157. var shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
  158. var shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
  159. var wheelDelta = e.wheelDelta;
  160. var absWheelDeltaDelta = Math.abs(wheelDelta);
  161. var originX = e.offsetX;
  162. var originY = e.offsetY;
  163. // wheelDelta maybe -0 in chrome mac.
  164. if (wheelDelta === 0 || !shouldZoom && !shouldMove) {
  165. return;
  166. }
  167. // If both `shouldZoom` and `shouldMove` is true, trigger
  168. // their event both, and the final behavior is determined
  169. // by event listener themselves.
  170. if (shouldZoom) {
  171. // Convenience:
  172. // Mac and VM Windows on Mac: scroll up: zoom out.
  173. // Windows: scroll up: zoom in.
  174. // FIXME: Should do more test in different environment.
  175. // wheelDelta is too complicated in difference nvironment
  176. // (https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel),
  177. // although it has been normallized by zrender.
  178. // wheelDelta of mouse wheel is bigger than touch pad.
  179. var factor = absWheelDeltaDelta > 3 ? 1.4 : absWheelDeltaDelta > 1 ? 1.2 : 1.1;
  180. var scale = wheelDelta > 0 ? factor : 1 / factor;
  181. checkPointerAndTrigger(this, 'zoom', 'zoomOnMouseWheel', e, {
  182. scale: scale,
  183. originX: originX,
  184. originY: originY,
  185. isAvailableBehavior: null
  186. });
  187. }
  188. if (shouldMove) {
  189. // FIXME: Should do more test in different environment.
  190. var absDelta = Math.abs(wheelDelta);
  191. // wheelDelta of mouse wheel is bigger than touch pad.
  192. var scrollDelta = (wheelDelta > 0 ? 1 : -1) * (absDelta > 3 ? 0.4 : absDelta > 1 ? 0.15 : 0.05);
  193. checkPointerAndTrigger(this, 'scrollMove', 'moveOnMouseWheel', e, {
  194. scrollDelta: scrollDelta,
  195. originX: originX,
  196. originY: originY,
  197. isAvailableBehavior: null
  198. });
  199. }
  200. };
  201. RoamController.prototype._pinchHandler = function (e) {
  202. if (interactionMutex.isTaken(this._zr, 'globalPan')) {
  203. return;
  204. }
  205. var scale = e.pinchScale > 1 ? 1.1 : 1 / 1.1;
  206. checkPointerAndTrigger(this, 'zoom', null, e, {
  207. scale: scale,
  208. originX: e.pinchX,
  209. originY: e.pinchY,
  210. isAvailableBehavior: null
  211. });
  212. };
  213. return RoamController;
  214. }(Eventful);
  215. function checkPointerAndTrigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  216. if (controller.pointerChecker && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)) {
  217. // When mouse is out of roamController rect,
  218. // default befavoius should not be be disabled, otherwise
  219. // page sliding is disabled, contrary to expectation.
  220. eventTool.stop(e.event);
  221. trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
  222. }
  223. }
  224. function trigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  225. // Also provide behavior checker for event listener, for some case that
  226. // multiple components share one listener.
  227. contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e);
  228. // TODO should not have type issue.
  229. controller.trigger(eventName, contollerEvent);
  230. }
  231. // settings: {
  232. // zoomOnMouseWheel
  233. // moveOnMouseMove
  234. // moveOnMouseWheel
  235. // }
  236. // The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
  237. function isAvailableBehavior(behaviorToCheck, e, settings) {
  238. var setting = settings[behaviorToCheck];
  239. return !behaviorToCheck || setting && (!isString(setting) || e.event[setting + 'Key']);
  240. }
  241. export default RoamController;