Component.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 =
  49. /** @class */
  50. function (_super) {
  51. __extends(ComponentModel, _super);
  52. function ComponentModel(option, parentModel, ecModel) {
  53. var _this = _super.call(this, option, parentModel, ecModel) || this;
  54. _this.uid = componentUtil.getUID('ec_cpt_model');
  55. return _this;
  56. }
  57. ComponentModel.prototype.init = function (option, parentModel, ecModel) {
  58. this.mergeDefaultAndTheme(option, ecModel);
  59. };
  60. ComponentModel.prototype.mergeDefaultAndTheme = function (option, ecModel) {
  61. var layoutMode = layout.fetchLayoutMode(this);
  62. var inputPositionParams = layoutMode ? layout.getLayoutParams(option) : {};
  63. var themeModel = ecModel.getTheme();
  64. zrUtil.merge(option, themeModel.get(this.mainType));
  65. zrUtil.merge(option, this.getDefaultOption());
  66. if (layoutMode) {
  67. layout.mergeLayoutParam(option, inputPositionParams, layoutMode);
  68. }
  69. };
  70. ComponentModel.prototype.mergeOption = function (option, ecModel) {
  71. zrUtil.merge(this.option, option, true);
  72. var layoutMode = layout.fetchLayoutMode(this);
  73. if (layoutMode) {
  74. layout.mergeLayoutParam(this.option, option, layoutMode);
  75. }
  76. };
  77. /**
  78. * Called immediately after `init` or `mergeOption` of this instance called.
  79. */
  80. ComponentModel.prototype.optionUpdated = function (newCptOption, isInit) {};
  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; // If using class declaration, it is different to travel super class
  137. // in legacy env and auto merge defaultOption. So if using class
  138. // declaration, defaultOption should be merged manually.
  139. if (!isExtendedClass(ctor)) {
  140. // When using ts class, defaultOption must be declared as static.
  141. return ctor.defaultOption;
  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. }); // Ensure main type.
  219. deps = zrUtil.map(deps, function (type) {
  220. return parseClassType(type).main;
  221. }); // Hack dataset for convenience.
  222. if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
  223. deps.unshift('dataset');
  224. }
  225. return deps;
  226. }
  227. export default ComponentModel;