roams.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. }; // Must not do anything depends on coordSysRecord outside the event handler here,
  94. // because coordSysRecord not completed yet.
  95. var controller = coordSysRecord.controller = new RoamController(api.getZr());
  96. each(['pan', 'zoom', 'scrollMove'], function (eventName) {
  97. controller.on(eventName, function (event) {
  98. var batch = [];
  99. coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
  100. // Check whether the behaviors (zoomOnMouseWheel, moveOnMouseMove,
  101. // moveOnMouseWheel, ...) enabled.
  102. if (!event.isAvailableBehavior(dzInfo.model.option)) {
  103. return;
  104. }
  105. var method = (dzInfo.getRange || {})[eventName];
  106. var range = method && method(dzInfo.dzReferCoordSysInfo, coordSysRecord.model.mainType, coordSysRecord.controller, event);
  107. !dzInfo.model.get('disabled', true) && range && batch.push({
  108. dataZoomId: dzInfo.model.id,
  109. start: range[0],
  110. end: range[1]
  111. });
  112. });
  113. batch.length && coordSysRecord.dispatchAction(batch);
  114. });
  115. });
  116. return coordSysRecord;
  117. }
  118. /**
  119. * This action will be throttled.
  120. */
  121. function dispatchAction(api, batch) {
  122. if (!api.isDisposed()) {
  123. api.dispatchAction({
  124. type: 'dataZoom',
  125. animation: {
  126. easing: 'cubicOut',
  127. duration: 100
  128. },
  129. batch: batch
  130. });
  131. }
  132. }
  133. function containsPoint(coordSysModel, e, x, y) {
  134. return coordSysModel.coordinateSystem.containPoint([x, y]);
  135. }
  136. /**
  137. * Merge roamController settings when multiple dataZooms share one roamController.
  138. */
  139. function mergeControllerParams(dataZoomInfoMap) {
  140. var controlType; // DO NOT use reserved word (true, false, undefined) as key literally. Even if encapsulated
  141. // as string, it is probably revert to reserved word by compress tool. See #7411.
  142. var prefix = 'type_';
  143. var typePriority = {
  144. 'type_true': 2,
  145. 'type_move': 1,
  146. 'type_false': 0,
  147. 'type_undefined': -1
  148. };
  149. var preventDefaultMouseMove = true;
  150. dataZoomInfoMap.each(function (dataZoomInfo) {
  151. var dataZoomModel = dataZoomInfo.model;
  152. var oneType = dataZoomModel.get('disabled', true) ? false : dataZoomModel.get('zoomLock', true) ? 'move' : true;
  153. if (typePriority[prefix + oneType] > typePriority[prefix + controlType]) {
  154. controlType = oneType;
  155. } // Prevent default move event by default. If one false, do not prevent. Otherwise
  156. // users may be confused why it does not work when multiple insideZooms exist.
  157. preventDefaultMouseMove = preventDefaultMouseMove && dataZoomModel.get('preventDefaultMouseMove', true);
  158. });
  159. return {
  160. controlType: controlType,
  161. opt: {
  162. // RoamController will enable all of these functionalities,
  163. // and the final behavior is determined by its event listener
  164. // provided by each inside zoom.
  165. zoomOnMouseWheel: true,
  166. moveOnMouseMove: true,
  167. moveOnMouseWheel: true,
  168. preventDefaultMouseMove: !!preventDefaultMouseMove
  169. }
  170. };
  171. }
  172. export function installDataZoomRoamProcessor(registers) {
  173. registers.registerProcessor(registers.PRIORITY.PROCESSOR.FILTER, function (ecModel, api) {
  174. var apiInner = inner(api);
  175. var coordSysRecordMap = apiInner.coordSysRecordMap || (apiInner.coordSysRecordMap = createHashMap());
  176. coordSysRecordMap.each(function (coordSysRecord) {
  177. // `coordSysRecordMap` always exists (becuase it hold the `roam controller`, which should
  178. // better not re-create each time), but clear `dataZoomInfoMap` each round of the workflow.
  179. coordSysRecord.dataZoomInfoMap = null;
  180. });
  181. ecModel.eachComponent({
  182. mainType: 'dataZoom',
  183. subType: 'inside'
  184. }, function (dataZoomModel) {
  185. var dzReferCoordSysWrap = collectReferCoordSysModelInfo(dataZoomModel);
  186. each(dzReferCoordSysWrap.infoList, function (dzCoordSysInfo) {
  187. var coordSysUid = dzCoordSysInfo.model.uid;
  188. var coordSysRecord = coordSysRecordMap.get(coordSysUid) || coordSysRecordMap.set(coordSysUid, createCoordSysRecord(api, dzCoordSysInfo.model));
  189. var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap || (coordSysRecord.dataZoomInfoMap = createHashMap()); // Notice these props might be changed each time for a single dataZoomModel.
  190. dataZoomInfoMap.set(dataZoomModel.uid, {
  191. dzReferCoordSysInfo: dzCoordSysInfo,
  192. model: dataZoomModel,
  193. getRange: null
  194. });
  195. });
  196. }); // (1) Merge dataZoom settings for each coord sys and set to the roam controller.
  197. // (2) Clear coord sys if not refered by any dataZoom.
  198. coordSysRecordMap.each(function (coordSysRecord) {
  199. var controller = coordSysRecord.controller;
  200. var firstDzInfo;
  201. var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap;
  202. if (dataZoomInfoMap) {
  203. var firstDzKey = dataZoomInfoMap.keys()[0];
  204. if (firstDzKey != null) {
  205. firstDzInfo = dataZoomInfoMap.get(firstDzKey);
  206. }
  207. }
  208. if (!firstDzInfo) {
  209. disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
  210. return;
  211. }
  212. var controllerParams = mergeControllerParams(dataZoomInfoMap);
  213. controller.enable(controllerParams.controlType, controllerParams.opt);
  214. controller.setPointerChecker(coordSysRecord.containsPoint);
  215. throttleUtil.createOrUpdate(coordSysRecord, 'dispatchAction', firstDzInfo.model.get('throttle', true), 'fixRate');
  216. });
  217. });
  218. }