RoamController.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, extend, retrieve2 } from 'zrender/lib/core/util.js';
  45. import { makeInner } from '../../util/model.js';
  46. import { retrieveZInfo } from '../../util/graphic.js';
  47. import { onIrrelevantElement } from './cursorHelper.js';
  48. ;
  49. /**
  50. * An manager of zoom and pan(darg) hehavior.
  51. * But it is not responsible for updating the view, since view updates vary and can
  52. * not be handled in a uniform way.
  53. *
  54. * Note: regarding view updates:
  55. * - Transformabe views typically use `coord/View` (e.g., geo and series.graph roaming).
  56. * Some commonly used view update logic has been organized into `roamHelper.ts`.
  57. * - Non-transformable views handle updates themselves, possibly involving re-layout,
  58. * (e.g., treemap).
  59. * - Some scenarios do not require transformation (e.g., dataZoom roaming for cartesian,
  60. * brush component).
  61. */
  62. var RoamController = /** @class */function (_super) {
  63. __extends(RoamController, _super);
  64. function RoamController(zr) {
  65. var _this = _super.call(this) || this;
  66. _this._zr = zr;
  67. // Avoid two roamController bind the same handler
  68. var mousedownHandler = bind(_this._mousedownHandler, _this);
  69. var mousemoveHandler = bind(_this._mousemoveHandler, _this);
  70. var mouseupHandler = bind(_this._mouseupHandler, _this);
  71. var mousewheelHandler = bind(_this._mousewheelHandler, _this);
  72. var pinchHandler = bind(_this._pinchHandler, _this);
  73. /**
  74. * Notice:
  75. * - only enable needed types. For example, if 'zoom'
  76. * is not needed, 'zoom' should not be enabled, otherwise
  77. * default mousewheel behaviour (scroll page) will be disabled.
  78. * - This method is idempotent.
  79. */
  80. _this.enable = function (controlType, rawOpt) {
  81. var zInfo = rawOpt.zInfo;
  82. var _a = retrieveZInfo(zInfo.component),
  83. z = _a.z,
  84. zlevel = _a.zlevel;
  85. var zInfoParsed = {
  86. component: zInfo.component,
  87. z: z,
  88. zlevel: zlevel,
  89. // By default roam controller is the lowest z2 comparing to other elememts in a component.
  90. z2: retrieve2(zInfo.z2, -Infinity)
  91. };
  92. var triggerInfo = extend({}, rawOpt.triggerInfo);
  93. this._opt = defaults(extend({}, rawOpt), {
  94. zoomOnMouseWheel: true,
  95. moveOnMouseMove: true,
  96. // By default, wheel do not trigger move.
  97. moveOnMouseWheel: false,
  98. preventDefaultMouseMove: true,
  99. zInfoParsed: zInfoParsed,
  100. triggerInfo: triggerInfo
  101. });
  102. if (controlType == null) {
  103. controlType = true;
  104. }
  105. // A handy optimization for repeatedly calling `enable` during roaming.
  106. // Assert `disable` is only affected by `controlType`.
  107. if (!this._enabled || this._controlType !== controlType) {
  108. this._enabled = true;
  109. // Disable previous first
  110. this.disable();
  111. if (controlType === true || controlType === 'move' || controlType === 'pan') {
  112. addRoamZrListener(zr, 'mousedown', mousedownHandler, zInfoParsed);
  113. addRoamZrListener(zr, 'mousemove', mousemoveHandler, zInfoParsed);
  114. addRoamZrListener(zr, 'mouseup', mouseupHandler, zInfoParsed);
  115. }
  116. if (controlType === true || controlType === 'scale' || controlType === 'zoom') {
  117. addRoamZrListener(zr, 'mousewheel', mousewheelHandler, zInfoParsed);
  118. addRoamZrListener(zr, 'pinch', pinchHandler, zInfoParsed);
  119. }
  120. }
  121. };
  122. _this.disable = function () {
  123. this._enabled = false;
  124. removeRoamZrListener(zr, 'mousedown', mousedownHandler);
  125. removeRoamZrListener(zr, 'mousemove', mousemoveHandler);
  126. removeRoamZrListener(zr, 'mouseup', mouseupHandler);
  127. removeRoamZrListener(zr, 'mousewheel', mousewheelHandler);
  128. removeRoamZrListener(zr, 'pinch', pinchHandler);
  129. };
  130. return _this;
  131. }
  132. RoamController.prototype.isDragging = function () {
  133. return this._dragging;
  134. };
  135. RoamController.prototype.isPinching = function () {
  136. return this._pinching;
  137. };
  138. RoamController.prototype._checkPointer = function (e, x, y) {
  139. var opt = this._opt;
  140. var zInfoParsed = opt.zInfoParsed;
  141. if (onIrrelevantElement(e, opt.api, zInfoParsed.component)) {
  142. return false;
  143. }
  144. ;
  145. var triggerInfo = opt.triggerInfo;
  146. var roamTrigger = triggerInfo.roamTrigger;
  147. var inArea = false;
  148. if (roamTrigger === 'global') {
  149. inArea = true;
  150. }
  151. if (!inArea) {
  152. inArea = triggerInfo.isInSelf(e, x, y);
  153. }
  154. if (inArea && triggerInfo.isInClip && !triggerInfo.isInClip(e, x, y)) {
  155. inArea = false;
  156. }
  157. return inArea;
  158. };
  159. RoamController.prototype._decideCursorStyle = function (e, x, y, forReverse) {
  160. // If this cursor style decision is not strictly consistent with zrender,
  161. // it's fine - zr will set the cursor on the next mousemove.
  162. // This `grab` cursor style should take the lowest precedence. If the hovring element already
  163. // have a cursor, zrender will set it to be non-'default' before entering this handler.
  164. // (note, e.target is never silent, e.topTarget can be silent be irrelevant.)
  165. var target = e.target;
  166. if (!target && this._checkPointer(e, x, y)) {
  167. // To indicate users that this area is draggable, otherwise users probably cannot kwown
  168. // that when hovering out of the shape but still inside the bounding rect.
  169. return 'grab';
  170. }
  171. if (forReverse) {
  172. return target && target.cursor || 'default';
  173. }
  174. };
  175. RoamController.prototype.dispose = function () {
  176. this.disable();
  177. };
  178. RoamController.prototype._mousedownHandler = function (e) {
  179. if (eventTool.isMiddleOrRightButtonOnMouseUpDown(e) || eventConsumed(e)) {
  180. return;
  181. }
  182. var el = e.target;
  183. while (el) {
  184. if (el.draggable) {
  185. return;
  186. }
  187. // check if host is draggable
  188. el = el.__hostTarget || el.parent;
  189. }
  190. var x = e.offsetX;
  191. var y = e.offsetY;
  192. // To determine dragging start, only by checking on mosedown, but not mousemove.
  193. // Mouse can be out of target when mouse moving.
  194. if (this._checkPointer(e, x, y)) {
  195. this._x = x;
  196. this._y = y;
  197. this._dragging = true;
  198. }
  199. };
  200. RoamController.prototype._mousemoveHandler = function (e) {
  201. var zr = this._zr;
  202. if (e.gestureEvent === 'pinch' || interactionMutex.isTaken(zr, 'globalPan') || eventConsumed(e)) {
  203. return;
  204. }
  205. var x = e.offsetX;
  206. var y = e.offsetY;
  207. if (!this._dragging || !isAvailableBehavior('moveOnMouseMove', e, this._opt)) {
  208. var cursorStyle = this._decideCursorStyle(e, x, y, false);
  209. if (cursorStyle) {
  210. zr.setCursorStyle(cursorStyle);
  211. }
  212. return;
  213. }
  214. zr.setCursorStyle('grabbing');
  215. var oldX = this._x;
  216. var oldY = this._y;
  217. var dx = x - oldX;
  218. var dy = y - oldY;
  219. this._x = x;
  220. this._y = y;
  221. if (this._opt.preventDefaultMouseMove) {
  222. eventTool.stop(e.event);
  223. }
  224. e.__ecRoamConsumed = true;
  225. trigger(this, 'pan', 'moveOnMouseMove', e, {
  226. dx: dx,
  227. dy: dy,
  228. oldX: oldX,
  229. oldY: oldY,
  230. newX: x,
  231. newY: y,
  232. isAvailableBehavior: null
  233. });
  234. };
  235. RoamController.prototype._mouseupHandler = function (e) {
  236. if (eventConsumed(e)) {
  237. return;
  238. }
  239. var zr = this._zr;
  240. if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
  241. this._dragging = false;
  242. var cursorStyle = this._decideCursorStyle(e, e.offsetX, e.offsetY, true);
  243. if (cursorStyle) {
  244. zr.setCursorStyle(cursorStyle);
  245. }
  246. }
  247. };
  248. RoamController.prototype._mousewheelHandler = function (e) {
  249. if (eventConsumed(e)) {
  250. return;
  251. }
  252. var shouldZoom = isAvailableBehavior('zoomOnMouseWheel', e, this._opt);
  253. var shouldMove = isAvailableBehavior('moveOnMouseWheel', e, this._opt);
  254. var wheelDelta = e.wheelDelta;
  255. var absWheelDeltaDelta = Math.abs(wheelDelta);
  256. var originX = e.offsetX;
  257. var originY = e.offsetY;
  258. // wheelDelta maybe -0 in chrome mac.
  259. if (wheelDelta === 0 || !shouldZoom && !shouldMove) {
  260. return;
  261. }
  262. // If both `shouldZoom` and `shouldMove` is true, trigger
  263. // their event both, and the final behavior is determined
  264. // by event listener themselves.
  265. if (shouldZoom) {
  266. // Convenience:
  267. // Mac and VM Windows on Mac: scroll up: zoom out.
  268. // Windows: scroll up: zoom in.
  269. // FIXME: Should do more test in different environment.
  270. // wheelDelta is too complicated in difference nvironment
  271. // (https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel),
  272. // although it has been normallized by zrender.
  273. // wheelDelta of mouse wheel is bigger than touch pad.
  274. var factor = absWheelDeltaDelta > 3 ? 1.4 : absWheelDeltaDelta > 1 ? 1.2 : 1.1;
  275. var scale = wheelDelta > 0 ? factor : 1 / factor;
  276. this._checkTriggerMoveZoom(this, 'zoom', 'zoomOnMouseWheel', e, {
  277. scale: scale,
  278. originX: originX,
  279. originY: originY,
  280. isAvailableBehavior: null
  281. });
  282. }
  283. if (shouldMove) {
  284. // FIXME: Should do more test in different environment.
  285. var absDelta = Math.abs(wheelDelta);
  286. // wheelDelta of mouse wheel is bigger than touch pad.
  287. var scrollDelta = (wheelDelta > 0 ? 1 : -1) * (absDelta > 3 ? 0.4 : absDelta > 1 ? 0.15 : 0.05);
  288. this._checkTriggerMoveZoom(this, 'scrollMove', 'moveOnMouseWheel', e, {
  289. scrollDelta: scrollDelta,
  290. originX: originX,
  291. originY: originY,
  292. isAvailableBehavior: null
  293. });
  294. }
  295. };
  296. RoamController.prototype._pinchHandler = function (e) {
  297. if (interactionMutex.isTaken(this._zr, 'globalPan') || eventConsumed(e)) {
  298. return;
  299. }
  300. var scale = e.pinchScale > 1 ? 1.1 : 1 / 1.1;
  301. this._checkTriggerMoveZoom(this, 'zoom', null, e, {
  302. scale: scale,
  303. originX: e.pinchX,
  304. originY: e.pinchY,
  305. isAvailableBehavior: null
  306. });
  307. };
  308. RoamController.prototype._checkTriggerMoveZoom = function (controller, eventName, behaviorToCheck, e, contollerEvent) {
  309. if (controller._checkPointer(e, contollerEvent.originX, contollerEvent.originY)) {
  310. // When mouse is out of roamController rect,
  311. // default befavoius should not be be disabled, otherwise
  312. // page sliding is disabled, contrary to expectation.
  313. eventTool.stop(e.event);
  314. e.__ecRoamConsumed = true;
  315. trigger(controller, eventName, behaviorToCheck, e, contollerEvent);
  316. }
  317. };
  318. return RoamController;
  319. }(Eventful);
  320. function eventConsumed(e) {
  321. return e.__ecRoamConsumed;
  322. }
  323. var innerZrStore = makeInner();
  324. function ensureZrStore(zr) {
  325. var store = innerZrStore(zr);
  326. store.roam = store.roam || {};
  327. store.uniform = store.uniform || {};
  328. return store;
  329. }
  330. /**
  331. * Listeners are sorted by z2/z/zlevel in descending order.
  332. * This decides the precedence between different roam controllers if they are overlapped.
  333. *
  334. * [MEMO]: It's not easy to perfectly reconcile the conflicts caused by overlap.
  335. * - Consider cases:
  336. * - Multiple roam controllers overlapped.
  337. * - Usually only the topmost can trigger roam.
  338. * - Roam controllers overlap with other zr elements:
  339. * - zr elements are relevant or irrelevent to the host of the roam controller. e.g., axis split line
  340. * or series elements is relevant to a cartesian and should trigger roam.
  341. * - zr elements is above or below the roam controller host, which affects the precedence of interaction.
  342. * - zr elements may not silent only for triggering tooltip by hovering, which is available to roam;
  343. * or may not silent for click, where roam is not preferable.
  344. * - Approach - `addRoamZrListener+pointerChecker+onIrrelevantElement` (currently used):
  345. * - Resolve the precedence between different roam controllers
  346. * - But cannot prevent the handling on other zr elements that under the roam controller in z-order.
  347. * - Approach - "use an invisible zr elements to receive the zr events to trigger roam":
  348. * - More complicated in impl.
  349. * - May cause bad cases where zr event cannot be receive due to other non-silient zr elements covering it.
  350. */
  351. function addRoamZrListener(zr, eventType, listener, zInfoParsed) {
  352. var store = ensureZrStore(zr);
  353. var roam = store.roam;
  354. var listenerList = roam[eventType] = roam[eventType] || [];
  355. var idx = 0;
  356. for (; idx < listenerList.length; idx++) {
  357. var currZInfo = listenerList[idx].zInfoParsed;
  358. if ((currZInfo.zlevel - zInfoParsed.zlevel || currZInfo.z - zInfoParsed.z || currZInfo.z2 - zInfoParsed.z2
  359. // If all equals, the latter added one has a higher precedence.
  360. ) <= 0) {
  361. break;
  362. }
  363. }
  364. listenerList.splice(idx, 0, {
  365. listener: listener,
  366. zInfoParsed: zInfoParsed
  367. });
  368. ensureUniformListener(zr, eventType);
  369. }
  370. function removeRoamZrListener(zr, eventType, listener) {
  371. var store = ensureZrStore(zr);
  372. var listenerList = store.roam[eventType] || [];
  373. for (var idx = 0; idx < listenerList.length; idx++) {
  374. if (listenerList[idx].listener === listener) {
  375. listenerList.splice(idx, 1);
  376. if (!listenerList.length) {
  377. removeUniformListener(zr, eventType);
  378. }
  379. return;
  380. }
  381. }
  382. }
  383. function ensureUniformListener(zr, eventType) {
  384. var store = ensureZrStore(zr);
  385. if (!store.uniform[eventType]) {
  386. zr.on(eventType, store.uniform[eventType] = function (event) {
  387. var listenerList = store.roam[eventType];
  388. if (listenerList) {
  389. for (var i = 0; i < listenerList.length; i++) {
  390. listenerList[i].listener(event);
  391. }
  392. }
  393. });
  394. }
  395. }
  396. function removeUniformListener(zr, eventType) {
  397. var store = ensureZrStore(zr);
  398. var uniform = store.uniform;
  399. if (uniform[eventType]) {
  400. zr.off(eventType, uniform[eventType]);
  401. uniform[eventType] = null;
  402. }
  403. }
  404. function trigger(controller, eventName, behaviorToCheck, e, contollerEvent) {
  405. // Also provide behavior checker for event listener, for some case that
  406. // multiple components share one listener.
  407. contollerEvent.isAvailableBehavior = bind(isAvailableBehavior, null, behaviorToCheck, e);
  408. // TODO should not have type issue.
  409. controller.trigger(eventName, contollerEvent);
  410. }
  411. // settings: {
  412. // zoomOnMouseWheel
  413. // moveOnMouseMove
  414. // moveOnMouseWheel
  415. // }
  416. // The value can be: true / false / 'shift' / 'ctrl' / 'alt'.
  417. function isAvailableBehavior(behaviorToCheck, e, settings) {
  418. var setting = settings[behaviorToCheck];
  419. return !behaviorToCheck || setting && (!isString(setting) || e.event[setting + 'Key']);
  420. }
  421. export default RoamController;