RoamController.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 =
  47. /** @class */
  48. function (_super) {
  49. __extends(RoamController, _super);
  50. function RoamController(zr) {
  51. var _this = _super.call(this) || this;
  52. _this._zr = zr; // Avoid two roamController bind the same handler
  53. var mousedownHandler = bind(_this._mousedownHandler, _this);
  54. var mousemoveHandler = bind(_this._mousemoveHandler, _this);
  55. var mouseupHandler = bind(_this._mouseupHandler, _this);
  56. var mousewheelHandler = bind(_this._mousewheelHandler, _this);
  57. var pinchHandler = bind(_this._pinchHandler, _this);
  58. /**
  59. * Notice: only enable needed types. For example, if 'zoom'
  60. * is not needed, 'zoom' should not be enabled, otherwise
  61. * default mousewheel behaviour (scroll page) will be disabled.
  62. */
  63. _this.enable = function (controlType, opt) {
  64. // Disable previous first
  65. this.disable();
  66. this._opt = defaults(clone(opt) || {}, {
  67. zoomOnMouseWheel: true,
  68. moveOnMouseMove: true,
  69. // By default, wheel do not trigger move.
  70. moveOnMouseWheel: false,
  71. preventDefaultMouseMove: true
  72. });
  73. if (controlType == null) {
  74. controlType = true;
  75. }
  76. if (controlType === true || controlType === 'move' || controlType === 'pan') {
  77. zr.on('mousedown', mousedownHandler);
  78. zr.on('mousemove', mousemoveHandler);
  79. zr.on('mouseup', mouseupHandler);
  80. }
  81. if (controlType === true || controlType === 'scale' || controlType === 'zoom') {
  82. zr.on('mousewheel', mousewheelHandler);
  83. zr.on('pinch', pinchHandler);
  84. }
  85. };
  86. _this.disable = function () {
  87. zr.off('mousedown', mousedownHandler);
  88. zr.off('mousemove', mousemoveHandler);
  89. zr.off('mouseup', mouseupHandler);
  90. zr.off('mousewheel', mousewheelHandler);
  91. zr.off('pinch', pinchHandler);
  92. };
  93. return _this;
  94. }
  95. RoamController.prototype.isDragging = function () {
  96. return this._dragging;
  97. };
  98. RoamController.prototype.isPinching = function () {
  99. return this._pinching;
  100. };
  101. RoamController.prototype.setPointerChecker = function (pointerChecker) {
  102. this.pointerChecker = pointerChecker;
  103. };
  104. RoamController.prototype.dispose = function () {
  105. this.disable();
  106. };
  107. RoamController.prototype._mousedownHandler = function (e) {
  108. if (eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  109. return;
  110. }
  111. var el = e.target;
  112. while (el) {
  113. if (el.draggable) {
  114. return;
  115. } // check if host is draggable
  116. el = el.__hostTarget || el.parent;
  117. }
  118. var x = e.offsetX;
  119. var y = e.offsetY; // Only check on mosedown, but not mousemove.
  120. // Mouse can be out of target when mouse moving.
  121. if (this.pointerChecker && this.pointerChecker(e, x, y)) {
  122. this._x = x;
  123. this._y = y;
  124. this._dragging = true;
  125. }
  126. };
  127. RoamController.prototype._mousemoveHandler = function (e) {
  128. if (!this._dragging || !isAvailableBehavior('moveOnMouseMove', e, this._opt) || e.gestureEvent === 'pinch' || interactionMutex.isTaken(this._zr, 'globalPan')) {
  129. return;
  130. }
  131. var x = e.offsetX;
  132. var y = e.offsetY;
  133. var oldX = this._x;
  134. var oldY = this._y;
  135. var dx = x - oldX;
  136. var dy = y - oldY;
  137. this._x = x;
  138. this._y = y;
  139. this._opt.preventDefaultMouseMove && eventTool.stop(e.event);
  140. trigger(this, 'pan', 'moveOnMouseMove', e, {
  141. dx: dx,
  142. dy: dy,
  143. oldX: oldX,
  144. oldY: oldY,
  145. newX: x,
  146. newY: y,
  147. isAvailableBehavior: null
  148. });
  149. };
  150. RoamController.prototype._mouseupHandler = function (e) {
  151. if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  152. this._dragging = false;
  153. }
  154. };
  155. RoamController.prototype._mousewheelHandler = function (e) {
  156. var shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
  157. var shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
  158. var wheelDelta = e.wheelDelta;
  159. var absWheelDeltaDelta = Math.abs(wheelDelta);
  160. var originX = e.offsetX;
  161. var originY = e.offsetY; // wheelDelta maybe -0 in chrome mac.
  162. if (wheelDelta === 0 || !shouldZoom && !shouldMove) {
  163. return;
  164. } // If both `shouldZoom` and `shouldMove` is true, trigger
  165. // their event both, and the final behavior is determined
  166. // by event listener themselves.
  167. if (shouldZoom) {
  168. // Convenience:
  169. // Mac and VM Windows on Mac: scroll up: zoom out.
  170. // Windows: scroll up: zoom in.
  171. // FIXME: Should do more test in different environment.
  172. // wheelDelta is too complicated in difference nvironment
  173. // (https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel),
  174. // although it has been normallized by zrender.
  175. // wheelDelta of mouse wheel is bigger than touch pad.
  176. var factor = absWheelDeltaDelta > 3 ? 1.4 : absWheelDeltaDelta > 1 ? 1.2 : 1.1;
  177. var scale = wheelDelta > 0 ? factor : 1 / factor;
  178. checkPointerAndTrigger(this, 'zoom', 'zoomOnMouseWheel', e, {
  179. scale: scale,
  180. originX: originX,
  181. originY: originY,
  182. isAvailableBehavior: null
  183. });
  184. }
  185. if (shouldMove) {
  186. // FIXME: Should do more test in different environment.
  187. var absDelta = Math.abs(wheelDelta); // wheelDelta of mouse wheel is bigger than touch pad.
  188. var scrollDelta = (wheelDelta > 0 ? 1 : -1) * (absDelta > 3 ? 0.4 : absDelta > 1 ? 0.15 : 0.05);
  189. checkPointerAndTrigger(this, 'scrollMove', 'moveOnMouseWheel', e, {
  190. scrollDelta: scrollDelta,
  191. originX: originX,
  192. originY: originY,
  193. isAvailableBehavior: null
  194. });
  195. }
  196. };
  197. RoamController.prototype._pinchHandler = function (e) {
  198. if (interactionMutex.isTaken(this._zr, 'globalPan')) {
  199. return;
  200. }
  201. var scale = e.pinchScale > 1 ? 1.1 : 1 / 1.1;
  202. checkPointerAndTrigger(this, 'zoom', null, e, {
  203. scale: scale,
  204. originX: e.pinchX,
  205. originY: e.pinchY,
  206. isAvailableBehavior: null
  207. });
  208. };
  209. return RoamController;
  210. }(Eventful);
  211. function checkPointerAndTrigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  212. if (controller.pointerChecker && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)) {
  213. // When mouse is out of roamController rect,
  214. // default befavoius should not be be disabled, otherwise
  215. // page sliding is disabled, contrary to expectation.
  216. eventTool.stop(e.event);
  217. trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
  218. }
  219. }
  220. function trigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  221. // Also provide behavior checker for event listener, for some case that
  222. // multiple components share one listener.
  223. contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e); // TODO should not have type issue.
  224. controller.trigger(eventName, contollerEvent);
  225. } // settings: {
  226. // zoomOnMouseWheel
  227. // moveOnMouseMove
  228. // moveOnMouseWheel
  229. // }
  230. // The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
  231. function isAvailableBehavior(behaviorToCheck, e, settings) {
  232. var setting = settings[behaviorToCheck];
  233. return !behaviorToCheck || setting && (!isString(setting) || e.event[setting + 'Key']);
  234. }
  235. export default RoamController;