roams.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // Only create one roam controller for each coordinate system.
  41. // one roam controller might be refered by two inside data zoom
  42. // components (for example, one for x and one for y). When user
  43. // pan or zoom, only dispatch one action for those data zoom
  44. // components.
  45. import RoamController from '../../component/helper/RoamController.js';
  46. import * as throttleUtil from '../../util/throttle.js';
  47. import { makeInner } from '../../util/model.js';
  48. import { each, curry, createHashMap } from 'zrender/lib/core/util.js';
  49. import { collectReferCoordSysModelInfo } from './helper.js';
  50. var inner = makeInner();
  51. export function setViewInfoToCoordSysRecord(api, dataZoomModel, getRange) {
  52. inner(api).coordSysRecordMap.each(function (coordSysRecord) {
  53. var dzInfo = coordSysRecord.dataZoomInfoMap.get(dataZoomModel.uid);
  54. if (dzInfo) {
  55. dzInfo.getRange = getRange;
  56. }
  57. });
  58. }
  59. export function disposeCoordSysRecordIfNeeded(api, dataZoomModel) {
  60. var coordSysRecordMap = inner(api).coordSysRecordMap;
  61. var coordSysKeyArr = coordSysRecordMap.keys();
  62. for (var i = 0; i < coordSysKeyArr.length; i++) {
  63. var coordSysKey = coordSysKeyArr[i];
  64. var coordSysRecord = coordSysRecordMap.get(coordSysKey);
  65. var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap;
  66. if (dataZoomInfoMap) {
  67. var dzUid = dataZoomModel.uid;
  68. var dzInfo = dataZoomInfoMap.get(dzUid);
  69. if (dzInfo) {
  70. dataZoomInfoMap.removeKey(dzUid);
  71. if (!dataZoomInfoMap.keys().length) {
  72. disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. function disposeCoordSysRecord(coordSysRecordMap, coordSysRecord) {
  79. if (coordSysRecord) {
  80. coordSysRecordMap.removeKey(coordSysRecord.model.uid);
  81. var controller = coordSysRecord.controller;
  82. controller && controller.dispose();
  83. }
  84. }
  85. function createCoordSysRecord(api, coordSysModel) {
  86. // These init props will never change after record created.
  87. var coordSysRecord = {
  88. model: coordSysModel,
  89. containsPoint: curry(containsPoint, coordSysModel),
  90. dispatchAction: curry(dispatchAction, api),
  91. dataZoomInfoMap: null,
  92. controller: null
  93. };
  94. // Must not do anything depends on coordSysRecord outside the event handler here,
  95. // because coordSysRecord not completed yet.
  96. var controller = coordSysRecord.controller = new RoamController(api.getZr());
  97. each(['pan', 'zoom', 'scrollMove'], function (eventName) {
  98. controller.on(eventName, function (event) {
  99. var batch = [];
  100. coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
  101. // Check whether the behaviors (zoomOnMouseWheel, moveOnMouseMove,
  102. // moveOnMouseWheel, ...) enabled.
  103. if (!event.isAvailableBehavior(dzInfo.model.option)) {
  104. return;
  105. }
  106. var method = (dzInfo.getRange || {})[eventName];
  107. var range = method && method(dzInfo.dzReferCoordSysInfo, coordSysRecord.model.mainType, coordSysRecord.controller, event);
  108. !dzInfo.model.get('disabled', true) && range && batch.push({
  109. dataZoomId: dzInfo.model.id,
  110. start: range[0],
  111. end: range[1]
  112. });
  113. });
  114. batch.length && coordSysRecord.dispatchAction(batch);
  115. });
  116. });
  117. return coordSysRecord;
  118. }
  119. /**
  120. * This action will be throttled.
  121. */
  122. function dispatchAction(api, batch) {
  123. if (!api.isDisposed()) {
  124. api.dispatchAction({
  125. type: 'dataZoom',
  126. animation: {
  127. easing: 'cubicOut',
  128. duration: 100
  129. },
  130. batch: batch
  131. });
  132. }
  133. }
  134. function containsPoint(coordSysModel, e, x, y) {
  135. return coordSysModel.coordinateSystem.containPoint([x, y]);
  136. }
  137. /**
  138. * Merge roamController settings when multiple dataZooms share one roamController.
  139. */
  140. function mergeControllerParams(dataZoomInfoMap) {
  141. var controlType;
  142. // DO NOT use reserved word (true, false, undefined) as key literally. Even if encapsulated
  143. // as string, it is probably revert to reserved word by compress tool. See #7411.
  144. var prefix = 'type_';
  145. var typePriority = {
  146. 'type_true': 2,
  147. 'type_move': 1,
  148. 'type_false': 0,
  149. 'type_undefined': -1
  150. };
  151. var preventDefaultMouseMove = true;
  152. dataZoomInfoMap.each(function (dataZoomInfo) {
  153. var dataZoomModel = dataZoomInfo.model;
  154. var oneType = dataZoomModel.get('disabled', true) ? false : dataZoomModel.get('zoomLock', true) ? 'move' : true;
  155. if (typePriority[prefix + oneType] > typePriority[prefix + controlType]) {
  156. controlType = oneType;
  157. }
  158. // Prevent default move event by default. If one false, do not prevent. Otherwise
  159. // users may be confused why it does not work when multiple insideZooms exist.
  160. preventDefaultMouseMove = preventDefaultMouseMove && dataZoomModel.get('preventDefaultMouseMove', true);
  161. });
  162. return {
  163. controlType: controlType,
  164. opt: {
  165. // RoamController will enable all of these functionalities,
  166. // and the final behavior is determined by its event listener
  167. // provided by each inside zoom.
  168. zoomOnMouseWheel: true,
  169. moveOnMouseMove: true,
  170. moveOnMouseWheel: true,
  171. preventDefaultMouseMove: !!preventDefaultMouseMove
  172. }
  173. };
  174. }
  175. export function installDataZoomRoamProcessor(registers) {
  176. registers.registerProcessor(registers.PRIORITY.PROCESSOR.FILTER, function (ecModel, api) {
  177. var apiInner = inner(api);
  178. var coordSysRecordMap = apiInner.coordSysRecordMap || (apiInner.coordSysRecordMap = createHashMap());
  179. coordSysRecordMap.each(function (coordSysRecord) {
  180. // `coordSysRecordMap` always exists (because it holds the `roam controller`, which should
  181. // better not re-create each time), but clear `dataZoomInfoMap` each round of the workflow.
  182. coordSysRecord.dataZoomInfoMap = null;
  183. });
  184. ecModel.eachComponent({
  185. mainType: 'dataZoom',
  186. subType: 'inside'
  187. }, function (dataZoomModel) {
  188. var dzReferCoordSysWrap = collectReferCoordSysModelInfo(dataZoomModel);
  189. each(dzReferCoordSysWrap.infoList, function (dzCoordSysInfo) {
  190. var coordSysUid = dzCoordSysInfo.model.uid;
  191. var coordSysRecord = coordSysRecordMap.get(coordSysUid) || coordSysRecordMap.set(coordSysUid, createCoordSysRecord(api, dzCoordSysInfo.model));
  192. var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap || (coordSysRecord.dataZoomInfoMap = createHashMap());
  193. // Notice these props might be changed each time for a single dataZoomModel.
  194. dataZoomInfoMap.set(dataZoomModel.uid, {
  195. dzReferCoordSysInfo: dzCoordSysInfo,
  196. model: dataZoomModel,
  197. getRange: null
  198. });
  199. });
  200. });
  201. // (1) Merge dataZoom settings for each coord sys and set to the roam controller.
  202. // (2) Clear coord sys if not refered by any dataZoom.
  203. coordSysRecordMap.each(function (coordSysRecord) {
  204. var controller = coordSysRecord.controller;
  205. var firstDzInfo;
  206. var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap;
  207. if (dataZoomInfoMap) {
  208. var firstDzKey = dataZoomInfoMap.keys()[0];
  209. if (firstDzKey != null) {
  210. firstDzInfo = dataZoomInfoMap.get(firstDzKey);
  211. }
  212. }
  213. if (!firstDzInfo) {
  214. disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
  215. return;
  216. }
  217. var controllerParams = mergeControllerParams(dataZoomInfoMap);
  218. controller.enable(controllerParams.controlType, controllerParams.opt);
  219. controller.setPointerChecker(coordSysRecord.containsPoint);
  220. throttleUtil.createOrUpdate(coordSysRecord, 'dispatchAction', firstDzInfo.model.get('throttle', true), 'fixRate');
  221. });
  222. });
  223. }