Component.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 using class declaration, it is different to travel super class
  136. // in legacy env and auto merge defaultOption. So if using class
  137. // declaration, defaultOption should be merged manually.
  138. if (!isExtendedClass(ctor)) {
  139. // When using ts class, defaultOption must be declared as static.
  140. return ctor.defaultOption;
  141. }
  142. // FIXME: remove this approach?
  143. var fields = inner(this);
  144. if (!fields.defaultOption) {
  145. var optList = [];
  146. var clz = ctor;
  147. while (clz) {
  148. var opt = clz.prototype.defaultOption;
  149. opt && optList.push(opt);
  150. clz = clz.superClass;
  151. }
  152. var defaultOption = {};
  153. for (var i = optList.length - 1; i >= 0; i--) {
  154. defaultOption = zrUtil.merge(defaultOption, optList[i], true);
  155. }
  156. fields.defaultOption = defaultOption;
  157. }
  158. return fields.defaultOption;
  159. };
  160. /**
  161. * Notice: always force to input param `useDefault` in case that forget to consider it.
  162. * The same behavior as `modelUtil.parseFinder`.
  163. *
  164. * @param useDefault In many cases like series refer axis and axis refer grid,
  165. * If axis index / axis id not specified, use the first target as default.
  166. * In other cases like dataZoom refer axis, if not specified, measn no refer.
  167. */
  168. ComponentModel.prototype.getReferringComponents = function (mainType, opt) {
  169. var indexKey = mainType + 'Index';
  170. var idKey = mainType + 'Id';
  171. return queryReferringComponents(this.ecModel, mainType, {
  172. index: this.get(indexKey, true),
  173. id: this.get(idKey, true)
  174. }, opt);
  175. };
  176. ComponentModel.prototype.getBoxLayoutParams = function () {
  177. // Consider itself having box layout configs.
  178. var boxLayoutModel = this;
  179. return {
  180. left: boxLayoutModel.get('left'),
  181. top: boxLayoutModel.get('top'),
  182. right: boxLayoutModel.get('right'),
  183. bottom: boxLayoutModel.get('bottom'),
  184. width: boxLayoutModel.get('width'),
  185. height: boxLayoutModel.get('height')
  186. };
  187. };
  188. /**
  189. * Get key for zlevel.
  190. * If developers don't configure zlevel. We will assign zlevel to series based on the key.
  191. * For example, lines with trail effect and progressive series will in an individual zlevel.
  192. */
  193. ComponentModel.prototype.getZLevelKey = function () {
  194. return '';
  195. };
  196. ComponentModel.prototype.setZLevel = function (zlevel) {
  197. this.option.zlevel = zlevel;
  198. };
  199. ComponentModel.protoInitialize = function () {
  200. var proto = ComponentModel.prototype;
  201. proto.type = 'component';
  202. proto.id = '';
  203. proto.name = '';
  204. proto.mainType = '';
  205. proto.subType = '';
  206. proto.componentIndex = 0;
  207. }();
  208. return ComponentModel;
  209. }(Model);
  210. mountExtend(ComponentModel, Model);
  211. enableClassManagement(ComponentModel);
  212. componentUtil.enableSubTypeDefaulter(ComponentModel);
  213. componentUtil.enableTopologicalTravel(ComponentModel, getDependencies);
  214. function getDependencies(componentType) {
  215. var deps = [];
  216. zrUtil.each(ComponentModel.getClassesByMainType(componentType), function (clz) {
  217. deps = deps.concat(clz.dependencies || clz.prototype.dependencies || []);
  218. });
  219. // Ensure main type.
  220. deps = zrUtil.map(deps, function (type) {
  221. return parseClassType(type).main;
  222. });
  223. // Hack dataset for convenience.
  224. if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
  225. deps.unshift('dataset');
  226. }
  227. return deps;
  228. }
  229. export default ComponentModel;