Component.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 * as zrUtil from 'zrender/lib/core/util.js';
  42. import Model from './Model.js';
  43. import * as componentUtil from '../util/component.js';
  44. import { enableClassManagement, parseClassType, isExtendedClass, mountExtend } from '../util/clazz.js';
  45. import { makeInner, queryReferringComponents } from '../util/model.js';
  46. import * as layout from '../util/layout.js';
  47. var inner = makeInner();
  48. var ComponentModel = /** @class */function (_super) {
  49. __extends(ComponentModel, _super);
  50. function ComponentModel(option, parentModel, ecModel) {
  51. var _this = _super.call(this, option, parentModel, ecModel) || this;
  52. _this.uid = componentUtil.getUID('ec_cpt_model');
  53. return _this;
  54. }
  55. ComponentModel.prototype.init = function (option, parentModel, ecModel) {
  56. this.mergeDefaultAndTheme(option, ecModel);
  57. };
  58. ComponentModel.prototype.mergeDefaultAndTheme = function (option, ecModel) {
  59. var layoutMode = layout.fetchLayoutMode(this);
  60. var inputPositionParams = layoutMode ? layout.getLayoutParams(option) : {};
  61. var themeModel = ecModel.getTheme();
  62. zrUtil.merge(option, themeModel.get(this.mainType));
  63. zrUtil.merge(option, this.getDefaultOption());
  64. if (layoutMode) {
  65. layout.mergeLayoutParam(option, inputPositionParams, layoutMode);
  66. }
  67. };
  68. ComponentModel.prototype.mergeOption = function (option, ecModel) {
  69. zrUtil.merge(this.option, option, true);
  70. var layoutMode = layout.fetchLayoutMode(this);
  71. if (layoutMode) {
  72. layout.mergeLayoutParam(this.option, option, layoutMode);
  73. }
  74. };
  75. /**
  76. * Called immediately after `init` or `mergeOption` of this instance called.
  77. */
  78. ComponentModel.prototype.optionUpdated = function (newCptOption, isInit) {
  79. // MUST NOT do anything here.
  80. };
  81. /**
  82. * [How to declare defaultOption]:
  83. *
  84. * (A) If using class declaration in typescript (since echarts 5):
  85. * ```ts
  86. * import {ComponentOption} from '../model/option.js';
  87. * export interface XxxOption extends ComponentOption {
  88. * aaa: number
  89. * }
  90. * export class XxxModel extends Component {
  91. * static type = 'xxx';
  92. * static defaultOption: XxxOption = {
  93. * aaa: 123
  94. * }
  95. * }
  96. * Component.registerClass(XxxModel);
  97. * ```
  98. * ```ts
  99. * import {inheritDefaultOption} from '../util/component.js';
  100. * import {XxxModel, XxxOption} from './XxxModel.js';
  101. * export interface XxxSubOption extends XxxOption {
  102. * bbb: number
  103. * }
  104. * class XxxSubModel extends XxxModel {
  105. * static defaultOption: XxxSubOption = inheritDefaultOption(XxxModel.defaultOption, {
  106. * bbb: 456
  107. * })
  108. * fn() {
  109. * let opt = this.getDefaultOption();
  110. * // opt is {aaa: 123, bbb: 456}
  111. * }
  112. * }
  113. * ```
  114. *
  115. * (B) If using class extend (previous approach in echarts 3 & 4):
  116. * ```js
  117. * let XxxComponent = Component.extend({
  118. * defaultOption: {
  119. * xx: 123
  120. * }
  121. * })
  122. * ```
  123. * ```js
  124. * let XxxSubComponent = XxxComponent.extend({
  125. * defaultOption: {
  126. * yy: 456
  127. * },
  128. * fn: function () {
  129. * let opt = this.getDefaultOption();
  130. * // opt is {xx: 123, yy: 456}
  131. * }
  132. * })
  133. * ```
  134. */
  135. ComponentModel.prototype.getDefaultOption = function () {
  136. var ctor = this.constructor;
  137. if (!isExtendedClass(ctor)) {
  138. // When using ES class declaration, defaultOption must be declared as static.
  139. // And manually inherit the defaultOption from its parent class if needed, such as,
  140. // ```ts
  141. // static defaultOption = inheritDefaultOption(ParentModel.defaultOption, {...});
  142. // ```
  143. return ctor.defaultOption;
  144. }
  145. // FIXME: remove this approach?
  146. // Legacy: auto merge defaultOption from ancestor classes if using ParentClass.extend(subProto)
  147. var fields = inner(this);
  148. if (!fields.defaultOption) {
  149. var optList = [];
  150. var clz = ctor;
  151. while (clz) {
  152. var opt = clz.prototype.defaultOption;
  153. opt && optList.push(opt);
  154. clz = clz.superClass;
  155. }
  156. var defaultOption = {};
  157. for (var i = optList.length - 1; i >= 0; i--) {
  158. defaultOption = zrUtil.merge(defaultOption, optList[i], true);
  159. }
  160. fields.defaultOption = defaultOption;
  161. }
  162. return fields.defaultOption;
  163. };
  164. /**
  165. * Notice: always force to input param `useDefault` in case that forget to consider it.
  166. * The same behavior as `modelUtil.parseFinder`.
  167. *
  168. * @param useDefault In many cases like series refer axis and axis refer grid,
  169. * If axis index / axis id not specified, use the first target as default.
  170. * In other cases like dataZoom refer axis, if not specified, measn no refer.
  171. */
  172. ComponentModel.prototype.getReferringComponents = function (mainType, opt) {
  173. var indexKey = mainType + 'Index';
  174. var idKey = mainType + 'Id';
  175. return queryReferringComponents(this.ecModel, mainType, {
  176. index: this.get(indexKey, true),
  177. id: this.get(idKey, true)
  178. }, opt);
  179. };
  180. ComponentModel.prototype.getBoxLayoutParams = function () {
  181. // Consider itself having box layout configs.
  182. // For backward compatibility, by default do not `ignoreParent`.
  183. return layout.getBoxLayoutParams(this, false);
  184. };
  185. /**
  186. * If developers don't configure zlevel. We will assign zlevel to series based on the key, if provided.
  187. * For example, lines with trail effect is expected to be in an individual zlevel.
  188. *
  189. * @tutorial [GET_ZLEVEL_KEY_FOR_PROGRESSIVE]
  190. * Regarding "progressive rendering", zrender can automatically assign a dedicated "incremental layer"
  191. * for `el.incremental` per zlevel. But there is a trade-off:
  192. * - If we do not provide different zlevelKey for different series here, all incremental elements from
  193. * different series will be assigned to one incremental layer, which causes them to cover each other
  194. * in an order depending on progressive steps.
  195. * i.e., seriesA_el1 -covered_by-> seriesB_el1 -covered_by-> seriesC_el1 -> seriesA_el2 -> seriesB_el2 ...
  196. * This order may causes an unexpected visual result: series with small data are likely to be completely
  197. * covered by series with large data. (like in test/scatter-weibo.html)
  198. * - If we assign a different zlevelKey to each series, the "covering issue" above can be resolved,
  199. * but having one HTML Canvas per series may be excessively memory-consuming.
  200. * Therefore, we only automatically assign zlevelKey on `ScatterSeries` and `LinesSeries` for backward
  201. * compatibility, and not to other series. Users can explicitly assign zlevel if they encouter above
  202. * "covering issue".
  203. */
  204. ComponentModel.prototype.getZLevelKey = function () {
  205. return '';
  206. };
  207. ComponentModel.prototype.setZLevel = function (zlevel) {
  208. this.option.zlevel = zlevel;
  209. };
  210. ComponentModel.protoInitialize = function () {
  211. var proto = ComponentModel.prototype;
  212. proto.type = 'component';
  213. proto.id = '';
  214. proto.name = '';
  215. proto.mainType = '';
  216. proto.subType = '';
  217. proto.componentIndex = 0;
  218. }();
  219. return ComponentModel;
  220. }(Model);
  221. mountExtend(ComponentModel, Model);
  222. enableClassManagement(ComponentModel);
  223. componentUtil.enableSubTypeDefaulter(ComponentModel);
  224. componentUtil.enableTopologicalTravel(ComponentModel, getDependencies);
  225. function getDependencies(componentType) {
  226. var deps = [];
  227. zrUtil.each(ComponentModel.getClassesByMainType(componentType), function (clz) {
  228. deps = deps.concat(clz.dependencies || clz.prototype.dependencies || []);
  229. });
  230. // Ensure main type.
  231. deps = zrUtil.map(deps, function (type) {
  232. return parseClassType(type).main;
  233. });
  234. // Hack dataset for convenience.
  235. if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
  236. deps.unshift('dataset');
  237. }
  238. return deps;
  239. }
  240. export default ComponentModel;