RoamController.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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) || e.target && e.target.draggable) {
  109. return;
  110. }
  111. var x = e.offsetX;
  112. var y = e.offsetY; // Only check on mosedown, but not mousemove.
  113. // Mouse can be out of target when mouse moving.
  114. if (this.pointerChecker && this.pointerChecker(e, x, y)) {
  115. this._x = x;
  116. this._y = y;
  117. this._dragging = true;
  118. }
  119. };
  120. RoamController.prototype._mousemoveHandler = function (e) {
  121. if (!this._dragging || !isAvailableBehavior('moveOnMouseMove', e, this._opt) || e.gestureEvent === 'pinch' || interactionMutex.isTaken(this._zr, 'globalPan')) {
  122. return;
  123. }
  124. var x = e.offsetX;
  125. var y = e.offsetY;
  126. var oldX = this._x;
  127. var oldY = this._y;
  128. var dx = x - oldX;
  129. var dy = y - oldY;
  130. this._x = x;
  131. this._y = y;
  132. this._opt.preventDefaultMouseMove && eventTool.stop(e.event);
  133. trigger(this, 'pan', 'moveOnMouseMove', e, {
  134. dx: dx,
  135. dy: dy,
  136. oldX: oldX,
  137. oldY: oldY,
  138. newX: x,
  139. newY: y,
  140. isAvailableBehavior: null
  141. });
  142. };
  143. RoamController.prototype._mouseupHandler = function (e) {
  144. if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  145. this._dragging = false;
  146. }
  147. };
  148. RoamController.prototype._mousewheelHandler = function (e) {
  149. var shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
  150. var shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
  151. var wheelDelta = e.wheelDelta;
  152. var absWheelDeltaDelta = Math.abs(wheelDelta);
  153. var originX = e.offsetX;
  154. var originY = e.offsetY; // wheelDelta maybe -0 in chrome mac.
  155. if (wheelDelta === 0 || !shouldZoom && !shouldMove) {
  156. return;
  157. } // If both `shouldZoom` and `shouldMove` is true, trigger
  158. // their event both, and the final behavior is determined
  159. // by event listener themselves.
  160. if (shouldZoom) {
  161. // Convenience:
  162. // Mac and VM Windows on Mac: scroll up: zoom out.
  163. // Windows: scroll up: zoom in.
  164. // FIXME: Should do more test in different environment.
  165. // wheelDelta is too complicated in difference nvironment
  166. // (https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel),
  167. // although it has been normallized by zrender.
  168. // wheelDelta of mouse wheel is bigger than touch pad.
  169. var factor = absWheelDeltaDelta > 3 ? 1.4 : absWheelDeltaDelta > 1 ? 1.2 : 1.1;
  170. var scale = wheelDelta > 0 ? factor : 1 / factor;
  171. checkPointerAndTrigger(this, 'zoom', 'zoomOnMouseWheel', e, {
  172. scale: scale,
  173. originX: originX,
  174. originY: originY,
  175. isAvailableBehavior: null
  176. });
  177. }
  178. if (shouldMove) {
  179. // FIXME: Should do more test in different environment.
  180. var absDelta = Math.abs(wheelDelta); // wheelDelta of mouse wheel is bigger than touch pad.
  181. var scrollDelta = (wheelDelta > 0 ? 1 : -1) * (absDelta > 3 ? 0.4 : absDelta > 1 ? 0.15 : 0.05);
  182. checkPointerAndTrigger(this, 'scrollMove', 'moveOnMouseWheel', e, {
  183. scrollDelta: scrollDelta,
  184. originX: originX,
  185. originY: originY,
  186. isAvailableBehavior: null
  187. });
  188. }
  189. };
  190. RoamController.prototype._pinchHandler = function (e) {
  191. if (interactionMutex.isTaken(this._zr, 'globalPan')) {
  192. return;
  193. }
  194. var scale = e.pinchScale > 1 ? 1.1 : 1 / 1.1;
  195. checkPointerAndTrigger(this, 'zoom', null, e, {
  196. scale: scale,
  197. originX: e.pinchX,
  198. originY: e.pinchY,
  199. isAvailableBehavior: null
  200. });
  201. };
  202. return RoamController;
  203. }(Eventful);
  204. function checkPointerAndTrigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  205. if (controller.pointerChecker && controller.pointerChecker(e, contollerEvent.originX, contollerEvent.originY)) {
  206. // When mouse is out of roamController rect,
  207. // default befavoius should not be be disabled, otherwise
  208. // page sliding is disabled, contrary to expectation.
  209. eventTool.stop(e.event);
  210. trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
  211. }
  212. }
  213. function trigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  214. // Also provide behavior checker for event listener, for some case that
  215. // multiple components share one listener.
  216. contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e); // TODO should not have type issue.
  217. controller.trigger(eventName, contollerEvent);
  218. } // settings: {
  219. // zoomOnMouseWheel
  220. // moveOnMouseMove
  221. // moveOnMouseWheel
  222. // }
  223. // The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
  224. function isAvailableBehavior(behaviorToCheck, e, settings) {
  225. var setting = settings[behaviorToCheck];
  226. return !behaviorToCheck || setting && (!isString(setting) || e.event[setting + 'Key']);
  227. }
  228. export default RoamController;