Component.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  80. * [How to declare defaultOption]:
  81. *
  82. * (A) If using class declaration in typescript (since echarts 5):
  83. * ```ts
  84. * import {ComponentOption} from '../model/option.js';
  85. * export interface XxxOption extends ComponentOption {
  86. * aaa: number
  87. * }
  88. * export class XxxModel extends Component {
  89. * static type = 'xxx';
  90. * static defaultOption: XxxOption = {
  91. * aaa: 123
  92. * }
  93. * }
  94. * Component.registerClass(XxxModel);
  95. * ```
  96. * ```ts
  97. * import {inheritDefaultOption} from '../util/component.js';
  98. * import {XxxModel, XxxOption} from './XxxModel.js';
  99. * export interface XxxSubOption extends XxxOption {
  100. * bbb: number
  101. * }
  102. * class XxxSubModel extends XxxModel {
  103. * static defaultOption: XxxSubOption = inheritDefaultOption(XxxModel.defaultOption, {
  104. * bbb: 456
  105. * })
  106. * fn() {
  107. * let opt = this.getDefaultOption();
  108. * // opt is {aaa: 123, bbb: 456}
  109. * }
  110. * }
  111. * ```
  112. *
  113. * (B) If using class extend (previous approach in echarts 3 & 4):
  114. * ```js
  115. * let XxxComponent = Component.extend({
  116. * defaultOption: {
  117. * xx: 123
  118. * }
  119. * })
  120. * ```
  121. * ```js
  122. * let XxxSubComponent = XxxComponent.extend({
  123. * defaultOption: {
  124. * yy: 456
  125. * },
  126. * fn: function () {
  127. * let opt = this.getDefaultOption();
  128. * // opt is {xx: 123, yy: 456}
  129. * }
  130. * })
  131. * ```
  132. */
  133. ComponentModel.prototype.getDefaultOption = function () {
  134. var ctor = this.constructor;
  135. if (!isExtendedClass(ctor)) {
  136. // When using ES class declaration, defaultOption must be declared as static.
  137. // And manually inherit the defaultOption from its parent class if needed, such as,
  138. // ```ts
  139. // static defaultOption = inheritDefaultOption(ParentModel.defaultOption, {...});
  140. // ```
  141. return ctor.defaultOption;
  142. }
  143. // FIXME: remove this approach?
  144. // Legacy: auto merge defaultOption from ancestor classes if using ParentClass.extend(subProto)
  145. var fields = inner(this);
  146. if (!fields.defaultOption) {
  147. var optList = [];
  148. var clz = ctor;
  149. while (clz) {
  150. var opt = clz.prototype.defaultOption;
  151. opt && optList.push(opt);
  152. clz = clz.superClass;
  153. }
  154. var defaultOption = {};
  155. for (var i = optList.length - 1; i >= 0; i--) {
  156. defaultOption = zrUtil.merge(defaultOption, optList[i], true);
  157. }
  158. fields.defaultOption = defaultOption;
  159. }
  160. return fields.defaultOption;
  161. };
  162. /**
  163. * Notice: always force to input param `useDefault` in case that forget to consider it.
  164. * The same behavior as `modelUtil.parseFinder`.
  165. *
  166. * @param useDefault In many cases like series refer axis and axis refer grid,
  167. * If axis index / axis id not specified, use the first target as default.
  168. * In other cases like dataZoom refer axis, if not specified, measn no refer.
  169. */
  170. ComponentModel.prototype.getReferringComponents = function (mainType, opt) {
  171. var indexKey = mainType + 'Index';
  172. var idKey = mainType + 'Id';
  173. return queryReferringComponents(this.ecModel, mainType, {
  174. index: this.get(indexKey, true),
  175. id: this.get(idKey, true)
  176. }, opt);
  177. };
  178. ComponentModel.prototype.getBoxLayoutParams = function () {
  179. // Consider itself having box layout configs.
  180. // For backward compatibility, by default do not `ignoreParent`.
  181. return layout.getBoxLayoutParams(this, false);
  182. };
  183. /**
  184. * Get key for zlevel.
  185. * If developers don't configure zlevel. We will assign zlevel to series based on the key.
  186. * For example, lines with trail effect and progressive series will in an individual zlevel.
  187. */
  188. ComponentModel.prototype.getZLevelKey = function () {
  189. return '';
  190. };
  191. ComponentModel.prototype.setZLevel = function (zlevel) {
  192. this.option.zlevel = zlevel;
  193. };
  194. ComponentModel.protoInitialize = function () {
  195. var proto = ComponentModel.prototype;
  196. proto.type = 'component';
  197. proto.id = '';
  198. proto.name = '';
  199. proto.mainType = '';
  200. proto.subType = '';
  201. proto.componentIndex = 0;
  202. }();
  203. return ComponentModel;
  204. }(Model);
  205. mountExtend(ComponentModel, Model);
  206. enableClassManagement(ComponentModel);
  207. componentUtil.enableSubTypeDefaulter(ComponentModel);
  208. componentUtil.enableTopologicalTravel(ComponentModel, getDependencies);
  209. function getDependencies(componentType) {
  210. var deps = [];
  211. zrUtil.each(ComponentModel.getClassesByMainType(componentType), function (clz) {
  212. deps = deps.concat(clz.dependencies || clz.prototype.dependencies || []);
  213. });
  214. // Ensure main type.
  215. deps = zrUtil.map(deps, function (type) {
  216. return parseClassType(type).main;
  217. });
  218. // Hack dataset for convenience.
  219. if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
  220. deps.unshift('dataset');
  221. }
  222. return deps;
  223. }
  224. export default ComponentModel;