Component.js 6.8 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. var zrUtil = require("zrender/lib/core/util");
  20. var Model = require("./Model");
  21. var componentUtil = require("../util/component");
  22. var _clazz = require("../util/clazz");
  23. var enableClassManagement = _clazz.enableClassManagement;
  24. var parseClassType = _clazz.parseClassType;
  25. var _model = require("../util/model");
  26. var makeInner = _model.makeInner;
  27. var layout = require("../util/layout");
  28. var boxLayoutMixin = require("./mixin/boxLayout");
  29. /*
  30. * Licensed to the Apache Software Foundation (ASF) under one
  31. * or more contributor license agreements. See the NOTICE file
  32. * distributed with this work for additional information
  33. * regarding copyright ownership. The ASF licenses this file
  34. * to you under the Apache License, Version 2.0 (the
  35. * "License"); you may not use this file except in compliance
  36. * with the License. You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing,
  41. * software distributed under the License is distributed on an
  42. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  43. * KIND, either express or implied. See the License for the
  44. * specific language governing permissions and limitations
  45. * under the License.
  46. */
  47. /**
  48. * Component model
  49. *
  50. * @module echarts/model/Component
  51. */
  52. var inner = makeInner();
  53. /**
  54. * @alias module:echarts/model/Component
  55. * @constructor
  56. * @param {Object} option
  57. * @param {module:echarts/model/Model} parentModel
  58. * @param {module:echarts/model/Model} ecModel
  59. */
  60. var ComponentModel = Model.extend({
  61. type: 'component',
  62. /**
  63. * @readOnly
  64. * @type {string}
  65. */
  66. id: '',
  67. /**
  68. * Because simplified concept is probably better, series.name (or component.name)
  69. * has been having too many resposibilities:
  70. * (1) Generating id (which requires name in option should not be modified).
  71. * (2) As an index to mapping series when merging option or calling API (a name
  72. * can refer to more then one components, which is convinient is some case).
  73. * (3) Display.
  74. * @readOnly
  75. */
  76. name: '',
  77. /**
  78. * @readOnly
  79. * @type {string}
  80. */
  81. mainType: '',
  82. /**
  83. * @readOnly
  84. * @type {string}
  85. */
  86. subType: '',
  87. /**
  88. * @readOnly
  89. * @type {number}
  90. */
  91. componentIndex: 0,
  92. /**
  93. * @type {Object}
  94. * @protected
  95. */
  96. defaultOption: null,
  97. /**
  98. * @type {module:echarts/model/Global}
  99. * @readOnly
  100. */
  101. ecModel: null,
  102. /**
  103. * key: componentType
  104. * value: Component model list, can not be null.
  105. * @type {Object.<string, Array.<module:echarts/model/Model>>}
  106. * @readOnly
  107. */
  108. dependentModels: [],
  109. /**
  110. * @type {string}
  111. * @readOnly
  112. */
  113. uid: null,
  114. /**
  115. * Support merge layout params.
  116. * Only support 'box' now (left/right/top/bottom/width/height).
  117. * @type {string|Object} Object can be {ignoreSize: true}
  118. * @readOnly
  119. */
  120. layoutMode: null,
  121. $constructor: function (option, parentModel, ecModel, extraOpt) {
  122. Model.call(this, option, parentModel, ecModel, extraOpt);
  123. this.uid = componentUtil.getUID('ec_cpt_model');
  124. },
  125. init: function (option, parentModel, ecModel, extraOpt) {
  126. this.mergeDefaultAndTheme(option, ecModel);
  127. },
  128. mergeDefaultAndTheme: function (option, ecModel) {
  129. var layoutMode = this.layoutMode;
  130. var inputPositionParams = layoutMode ? layout.getLayoutParams(option) : {};
  131. var themeModel = ecModel.getTheme();
  132. zrUtil.merge(option, themeModel.get(this.mainType));
  133. zrUtil.merge(option, this.getDefaultOption());
  134. if (layoutMode) {
  135. layout.mergeLayoutParam(option, inputPositionParams, layoutMode);
  136. }
  137. },
  138. mergeOption: function (option, extraOpt) {
  139. zrUtil.merge(this.option, option, true);
  140. var layoutMode = this.layoutMode;
  141. if (layoutMode) {
  142. layout.mergeLayoutParam(this.option, option, layoutMode);
  143. }
  144. },
  145. // Hooker after init or mergeOption
  146. optionUpdated: function (newCptOption, isInit) {},
  147. getDefaultOption: function () {
  148. var fields = inner(this);
  149. if (!fields.defaultOption) {
  150. var optList = [];
  151. var Class = this.constructor;
  152. while (Class) {
  153. var opt = Class.prototype.defaultOption;
  154. opt && optList.push(opt);
  155. Class = Class.superClass;
  156. }
  157. var defaultOption = {};
  158. for (var i = optList.length - 1; i >= 0; i--) {
  159. defaultOption = zrUtil.merge(defaultOption, optList[i], true);
  160. }
  161. fields.defaultOption = defaultOption;
  162. }
  163. return fields.defaultOption;
  164. },
  165. getReferringComponents: function (mainType) {
  166. return this.ecModel.queryComponents({
  167. mainType: mainType,
  168. index: this.get(mainType + 'Index', true),
  169. id: this.get(mainType + 'Id', true)
  170. });
  171. }
  172. }); // Reset ComponentModel.extend, add preConstruct.
  173. // clazzUtil.enableClassExtend(
  174. // ComponentModel,
  175. // function (option, parentModel, ecModel, extraOpt) {
  176. // // Set dependentModels, componentIndex, name, id, mainType, subType.
  177. // zrUtil.extend(this, extraOpt);
  178. // this.uid = componentUtil.getUID('componentModel');
  179. // // this.setReadOnly([
  180. // // 'type', 'id', 'uid', 'name', 'mainType', 'subType',
  181. // // 'dependentModels', 'componentIndex'
  182. // // ]);
  183. // }
  184. // );
  185. // Add capability of registerClass, getClass, hasClass, registerSubTypeDefaulter and so on.
  186. enableClassManagement(ComponentModel, {
  187. registerWhenExtend: true
  188. });
  189. componentUtil.enableSubTypeDefaulter(ComponentModel); // Add capability of ComponentModel.topologicalTravel.
  190. componentUtil.enableTopologicalTravel(ComponentModel, getDependencies);
  191. function getDependencies(componentType) {
  192. var deps = [];
  193. zrUtil.each(ComponentModel.getClassesByMainType(componentType), function (Clazz) {
  194. deps = deps.concat(Clazz.prototype.dependencies || []);
  195. }); // Ensure main type.
  196. deps = zrUtil.map(deps, function (type) {
  197. return parseClassType(type).main;
  198. }); // Hack dataset for convenience.
  199. if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
  200. deps.unshift('dataset');
  201. }
  202. return deps;
  203. }
  204. zrUtil.mixin(ComponentModel, boxLayoutMixin);
  205. var _default = ComponentModel;
  206. module.exports = _default;