Global.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. /**
  42. * Caution: If the mechanism should be changed some day, these cases
  43. * should be considered:
  44. *
  45. * (1) In `merge option` mode, if using the same option to call `setOption`
  46. * many times, the result should be the same (try our best to ensure that).
  47. * (2) In `merge option` mode, if a component has no id/name specified, it
  48. * will be merged by index, and the result sequence of the components is
  49. * consistent to the original sequence.
  50. * (3) In `replaceMerge` mode, keep the result sequence of the components is
  51. * consistent to the original sequence, even though there might result in "hole".
  52. * (4) `reset` feature (in toolbox). Find detailed info in comments about
  53. * `mergeOption` in module:echarts/model/OptionManager.
  54. */
  55. import { each, filter, isArray, isObject, isString, createHashMap, assert, clone, merge, extend, mixin, isFunction } from 'zrender/lib/core/util.js';
  56. import * as modelUtil from '../util/model.js';
  57. import Model from './Model.js';
  58. import ComponentModel from './Component.js';
  59. import globalDefault from './globalDefault.js';
  60. import { resetSourceDefaulter } from '../data/helper/sourceHelper.js';
  61. import { concatInternalOptions } from './internalComponentCreator.js';
  62. import { PaletteMixin } from './mixin/palette.js';
  63. import { error, warn } from '../util/log.js'; // -----------------------
  64. // Internal method names:
  65. // -----------------------
  66. var reCreateSeriesIndices;
  67. var assertSeriesInitialized;
  68. var initBase;
  69. var OPTION_INNER_KEY = '\0_ec_inner';
  70. var OPTION_INNER_VALUE = 1;
  71. var BUITIN_COMPONENTS_MAP = {
  72. grid: 'GridComponent',
  73. polar: 'PolarComponent',
  74. geo: 'GeoComponent',
  75. singleAxis: 'SingleAxisComponent',
  76. parallel: 'ParallelComponent',
  77. calendar: 'CalendarComponent',
  78. graphic: 'GraphicComponent',
  79. toolbox: 'ToolboxComponent',
  80. tooltip: 'TooltipComponent',
  81. axisPointer: 'AxisPointerComponent',
  82. brush: 'BrushComponent',
  83. title: 'TitleComponent',
  84. timeline: 'TimelineComponent',
  85. markPoint: 'MarkPointComponent',
  86. markLine: 'MarkLineComponent',
  87. markArea: 'MarkAreaComponent',
  88. legend: 'LegendComponent',
  89. dataZoom: 'DataZoomComponent',
  90. visualMap: 'VisualMapComponent',
  91. // aria: 'AriaComponent',
  92. // dataset: 'DatasetComponent',
  93. // Dependencies
  94. xAxis: 'GridComponent',
  95. yAxis: 'GridComponent',
  96. angleAxis: 'PolarComponent',
  97. radiusAxis: 'PolarComponent'
  98. };
  99. var BUILTIN_CHARTS_MAP = {
  100. line: 'LineChart',
  101. bar: 'BarChart',
  102. pie: 'PieChart',
  103. scatter: 'ScatterChart',
  104. radar: 'RadarChart',
  105. map: 'MapChart',
  106. tree: 'TreeChart',
  107. treemap: 'TreemapChart',
  108. graph: 'GraphChart',
  109. gauge: 'GaugeChart',
  110. funnel: 'FunnelChart',
  111. parallel: 'ParallelChart',
  112. sankey: 'SankeyChart',
  113. boxplot: 'BoxplotChart',
  114. candlestick: 'CandlestickChart',
  115. effectScatter: 'EffectScatterChart',
  116. lines: 'LinesChart',
  117. heatmap: 'HeatmapChart',
  118. pictorialBar: 'PictorialBarChart',
  119. themeRiver: 'ThemeRiverChart',
  120. sunburst: 'SunburstChart',
  121. custom: 'CustomChart'
  122. };
  123. var componetsMissingLogPrinted = {};
  124. function checkMissingComponents(option) {
  125. each(option, function (componentOption, mainType) {
  126. if (!ComponentModel.hasClass(mainType)) {
  127. var componentImportName = BUITIN_COMPONENTS_MAP[mainType];
  128. if (componentImportName && !componetsMissingLogPrinted[componentImportName]) {
  129. error("Component " + mainType + " is used but not imported.\nimport { " + componentImportName + " } from 'echarts/components';\necharts.use([" + componentImportName + "]);");
  130. componetsMissingLogPrinted[componentImportName] = true;
  131. }
  132. }
  133. });
  134. }
  135. var GlobalModel =
  136. /** @class */
  137. function (_super) {
  138. __extends(GlobalModel, _super);
  139. function GlobalModel() {
  140. return _super !== null && _super.apply(this, arguments) || this;
  141. }
  142. GlobalModel.prototype.init = function (option, parentModel, ecModel, theme, locale, optionManager) {
  143. theme = theme || {};
  144. this.option = null; // Mark as not initialized.
  145. this._theme = new Model(theme);
  146. this._locale = new Model(locale);
  147. this._optionManager = optionManager;
  148. };
  149. GlobalModel.prototype.setOption = function (option, opts, optionPreprocessorFuncs) {
  150. if (process.env.NODE_ENV !== 'production') {
  151. assert(option != null, 'option is null/undefined');
  152. assert(option[OPTION_INNER_KEY] !== OPTION_INNER_VALUE, 'please use chart.getOption()');
  153. }
  154. var innerOpt = normalizeSetOptionInput(opts);
  155. this._optionManager.setOption(option, optionPreprocessorFuncs, innerOpt);
  156. this._resetOption(null, innerOpt);
  157. };
  158. /**
  159. * @param type null/undefined: reset all.
  160. * 'recreate': force recreate all.
  161. * 'timeline': only reset timeline option
  162. * 'media': only reset media query option
  163. * @return Whether option changed.
  164. */
  165. GlobalModel.prototype.resetOption = function (type, opt) {
  166. return this._resetOption(type, normalizeSetOptionInput(opt));
  167. };
  168. GlobalModel.prototype._resetOption = function (type, opt) {
  169. var optionChanged = false;
  170. var optionManager = this._optionManager;
  171. if (!type || type === 'recreate') {
  172. var baseOption = optionManager.mountOption(type === 'recreate');
  173. if (process.env.NODE_ENV !== 'production') {
  174. checkMissingComponents(baseOption);
  175. }
  176. if (!this.option || type === 'recreate') {
  177. initBase(this, baseOption);
  178. } else {
  179. this.restoreData();
  180. this._mergeOption(baseOption, opt);
  181. }
  182. optionChanged = true;
  183. }
  184. if (type === 'timeline' || type === 'media') {
  185. this.restoreData();
  186. } // By design, if `setOption(option2)` at the second time, and `option2` is a `ECUnitOption`,
  187. // it should better not have the same props with `MediaUnit['option']`.
  188. // Because either `option2` or `MediaUnit['option']` will be always merged to "current option"
  189. // rather than original "baseOption". If they both override a prop, the result might be
  190. // unexpected when media state changed after `setOption` called.
  191. // If we really need to modify a props in each `MediaUnit['option']`, use the full version
  192. // (`{baseOption, media}`) in `setOption`.
  193. // For `timeline`, the case is the same.
  194. if (!type || type === 'recreate' || type === 'timeline') {
  195. var timelineOption = optionManager.getTimelineOption(this);
  196. if (timelineOption) {
  197. optionChanged = true;
  198. this._mergeOption(timelineOption, opt);
  199. }
  200. }
  201. if (!type || type === 'recreate' || type === 'media') {
  202. var mediaOptions = optionManager.getMediaOption(this);
  203. if (mediaOptions.length) {
  204. each(mediaOptions, function (mediaOption) {
  205. optionChanged = true;
  206. this._mergeOption(mediaOption, opt);
  207. }, this);
  208. }
  209. }
  210. return optionChanged;
  211. };
  212. GlobalModel.prototype.mergeOption = function (option) {
  213. this._mergeOption(option, null);
  214. };
  215. GlobalModel.prototype._mergeOption = function (newOption, opt) {
  216. var option = this.option;
  217. var componentsMap = this._componentsMap;
  218. var componentsCount = this._componentsCount;
  219. var newCmptTypes = [];
  220. var newCmptTypeMap = createHashMap();
  221. var replaceMergeMainTypeMap = opt && opt.replaceMergeMainTypeMap;
  222. resetSourceDefaulter(this); // If no component class, merge directly.
  223. // For example: color, animaiton options, etc.
  224. each(newOption, function (componentOption, mainType) {
  225. if (componentOption == null) {
  226. return;
  227. }
  228. if (!ComponentModel.hasClass(mainType)) {
  229. // globalSettingTask.dirty();
  230. option[mainType] = option[mainType] == null ? clone(componentOption) : merge(option[mainType], componentOption, true);
  231. } else if (mainType) {
  232. newCmptTypes.push(mainType);
  233. newCmptTypeMap.set(mainType, true);
  234. }
  235. });
  236. if (replaceMergeMainTypeMap) {
  237. // If there is a mainType `xxx` in `replaceMerge` but not declared in option,
  238. // we trade it as it is declared in option as `{xxx: []}`. Because:
  239. // (1) for normal merge, `{xxx: null/undefined}` are the same meaning as `{xxx: []}`.
  240. // (2) some preprocessor may convert some of `{xxx: null/undefined}` to `{xxx: []}`.
  241. replaceMergeMainTypeMap.each(function (val, mainTypeInReplaceMerge) {
  242. if (ComponentModel.hasClass(mainTypeInReplaceMerge) && !newCmptTypeMap.get(mainTypeInReplaceMerge)) {
  243. newCmptTypes.push(mainTypeInReplaceMerge);
  244. newCmptTypeMap.set(mainTypeInReplaceMerge, true);
  245. }
  246. });
  247. }
  248. ComponentModel.topologicalTravel(newCmptTypes, ComponentModel.getAllClassMainTypes(), visitComponent, this);
  249. function visitComponent(mainType) {
  250. var newCmptOptionList = concatInternalOptions(this, mainType, modelUtil.normalizeToArray(newOption[mainType]));
  251. var oldCmptList = componentsMap.get(mainType);
  252. var mergeMode = // `!oldCmptList` means init. See the comment in `mappingToExists`
  253. !oldCmptList ? 'replaceAll' : replaceMergeMainTypeMap && replaceMergeMainTypeMap.get(mainType) ? 'replaceMerge' : 'normalMerge';
  254. var mappingResult = modelUtil.mappingToExists(oldCmptList, newCmptOptionList, mergeMode); // Set mainType and complete subType.
  255. modelUtil.setComponentTypeToKeyInfo(mappingResult, mainType, ComponentModel); // Empty it before the travel, in order to prevent `this._componentsMap`
  256. // from being used in the `init`/`mergeOption`/`optionUpdated` of some
  257. // components, which is probably incorrect logic.
  258. option[mainType] = null;
  259. componentsMap.set(mainType, null);
  260. componentsCount.set(mainType, 0);
  261. var optionsByMainType = [];
  262. var cmptsByMainType = [];
  263. var cmptsCountByMainType = 0;
  264. var tooltipExists;
  265. var tooltipWarningLogged;
  266. each(mappingResult, function (resultItem, index) {
  267. var componentModel = resultItem.existing;
  268. var newCmptOption = resultItem.newOption;
  269. if (!newCmptOption) {
  270. if (componentModel) {
  271. // Consider where is no new option and should be merged using {},
  272. // see removeEdgeAndAdd in topologicalTravel and
  273. // ComponentModel.getAllClassMainTypes.
  274. componentModel.mergeOption({}, this);
  275. componentModel.optionUpdated({}, false);
  276. } // If no both `resultItem.exist` and `resultItem.option`,
  277. // either it is in `replaceMerge` and not matched by any id,
  278. // or it has been removed in previous `replaceMerge` and left a "hole" in this component index.
  279. } else {
  280. var isSeriesType = mainType === 'series';
  281. var ComponentModelClass = ComponentModel.getClass(mainType, resultItem.keyInfo.subType, !isSeriesType // Give a more detailed warn later if series don't exists
  282. );
  283. if (!ComponentModelClass) {
  284. if (process.env.NODE_ENV !== 'production') {
  285. var subType = resultItem.keyInfo.subType;
  286. var seriesImportName = BUILTIN_CHARTS_MAP[subType];
  287. if (!componetsMissingLogPrinted[subType]) {
  288. componetsMissingLogPrinted[subType] = true;
  289. if (seriesImportName) {
  290. error("Series " + subType + " is used but not imported.\nimport { " + seriesImportName + " } from 'echarts/charts';\necharts.use([" + seriesImportName + "]);");
  291. } else {
  292. error("Unknown series " + subType);
  293. }
  294. }
  295. }
  296. return;
  297. } // TODO Before multiple tooltips get supported, we do this check to avoid unexpected exception.
  298. if (mainType === 'tooltip') {
  299. if (tooltipExists) {
  300. if (process.env.NODE_ENV !== 'production') {
  301. if (!tooltipWarningLogged) {
  302. warn('Currently only one tooltip component is allowed.');
  303. tooltipWarningLogged = true;
  304. }
  305. }
  306. return;
  307. }
  308. tooltipExists = true;
  309. }
  310. if (componentModel && componentModel.constructor === ComponentModelClass) {
  311. componentModel.name = resultItem.keyInfo.name; // componentModel.settingTask && componentModel.settingTask.dirty();
  312. componentModel.mergeOption(newCmptOption, this);
  313. componentModel.optionUpdated(newCmptOption, false);
  314. } else {
  315. // PENDING Global as parent ?
  316. var extraOpt = extend({
  317. componentIndex: index
  318. }, resultItem.keyInfo);
  319. componentModel = new ComponentModelClass(newCmptOption, this, this, extraOpt); // Assign `keyInfo`
  320. extend(componentModel, extraOpt);
  321. if (resultItem.brandNew) {
  322. componentModel.__requireNewView = true;
  323. }
  324. componentModel.init(newCmptOption, this, this); // Call optionUpdated after init.
  325. // newCmptOption has been used as componentModel.option
  326. // and may be merged with theme and default, so pass null
  327. // to avoid confusion.
  328. componentModel.optionUpdated(null, true);
  329. }
  330. }
  331. if (componentModel) {
  332. optionsByMainType.push(componentModel.option);
  333. cmptsByMainType.push(componentModel);
  334. cmptsCountByMainType++;
  335. } else {
  336. // Always do assign to avoid elided item in array.
  337. optionsByMainType.push(void 0);
  338. cmptsByMainType.push(void 0);
  339. }
  340. }, this);
  341. option[mainType] = optionsByMainType;
  342. componentsMap.set(mainType, cmptsByMainType);
  343. componentsCount.set(mainType, cmptsCountByMainType); // Backup series for filtering.
  344. if (mainType === 'series') {
  345. reCreateSeriesIndices(this);
  346. }
  347. } // If no series declared, ensure `_seriesIndices` initialized.
  348. if (!this._seriesIndices) {
  349. reCreateSeriesIndices(this);
  350. }
  351. };
  352. /**
  353. * Get option for output (cloned option and inner info removed)
  354. */
  355. GlobalModel.prototype.getOption = function () {
  356. var option = clone(this.option);
  357. each(option, function (optInMainType, mainType) {
  358. if (ComponentModel.hasClass(mainType)) {
  359. var opts = modelUtil.normalizeToArray(optInMainType); // Inner cmpts need to be removed.
  360. // Inner cmpts might not be at last since ec5.0, but still
  361. // compatible for users: if inner cmpt at last, splice the returned array.
  362. var realLen = opts.length;
  363. var metNonInner = false;
  364. for (var i = realLen - 1; i >= 0; i--) {
  365. // Remove options with inner id.
  366. if (opts[i] && !modelUtil.isComponentIdInternal(opts[i])) {
  367. metNonInner = true;
  368. } else {
  369. opts[i] = null;
  370. !metNonInner && realLen--;
  371. }
  372. }
  373. opts.length = realLen;
  374. option[mainType] = opts;
  375. }
  376. });
  377. delete option[OPTION_INNER_KEY];
  378. return option;
  379. };
  380. GlobalModel.prototype.getTheme = function () {
  381. return this._theme;
  382. };
  383. GlobalModel.prototype.getLocaleModel = function () {
  384. return this._locale;
  385. };
  386. GlobalModel.prototype.setUpdatePayload = function (payload) {
  387. this._payload = payload;
  388. };
  389. GlobalModel.prototype.getUpdatePayload = function () {
  390. return this._payload;
  391. };
  392. /**
  393. * @param idx If not specified, return the first one.
  394. */
  395. GlobalModel.prototype.getComponent = function (mainType, idx) {
  396. var list = this._componentsMap.get(mainType);
  397. if (list) {
  398. var cmpt = list[idx || 0];
  399. if (cmpt) {
  400. return cmpt;
  401. } else if (idx == null) {
  402. for (var i = 0; i < list.length; i++) {
  403. if (list[i]) {
  404. return list[i];
  405. }
  406. }
  407. }
  408. }
  409. };
  410. /**
  411. * @return Never be null/undefined.
  412. */
  413. GlobalModel.prototype.queryComponents = function (condition) {
  414. var mainType = condition.mainType;
  415. if (!mainType) {
  416. return [];
  417. }
  418. var index = condition.index;
  419. var id = condition.id;
  420. var name = condition.name;
  421. var cmpts = this._componentsMap.get(mainType);
  422. if (!cmpts || !cmpts.length) {
  423. return [];
  424. }
  425. var result;
  426. if (index != null) {
  427. result = [];
  428. each(modelUtil.normalizeToArray(index), function (idx) {
  429. cmpts[idx] && result.push(cmpts[idx]);
  430. });
  431. } else if (id != null) {
  432. result = queryByIdOrName('id', id, cmpts);
  433. } else if (name != null) {
  434. result = queryByIdOrName('name', name, cmpts);
  435. } else {
  436. // Return all non-empty components in that mainType
  437. result = filter(cmpts, function (cmpt) {
  438. return !!cmpt;
  439. });
  440. }
  441. return filterBySubType(result, condition);
  442. };
  443. /**
  444. * The interface is different from queryComponents,
  445. * which is convenient for inner usage.
  446. *
  447. * @usage
  448. * let result = findComponents(
  449. * {mainType: 'dataZoom', query: {dataZoomId: 'abc'}}
  450. * );
  451. * let result = findComponents(
  452. * {mainType: 'series', subType: 'pie', query: {seriesName: 'uio'}}
  453. * );
  454. * let result = findComponents(
  455. * {mainType: 'series',
  456. * filter: function (model, index) {...}}
  457. * );
  458. * // result like [component0, componnet1, ...]
  459. */
  460. GlobalModel.prototype.findComponents = function (condition) {
  461. var query = condition.query;
  462. var mainType = condition.mainType;
  463. var queryCond = getQueryCond(query);
  464. var result = queryCond ? this.queryComponents(queryCond) // Retrieve all non-empty components.
  465. : filter(this._componentsMap.get(mainType), function (cmpt) {
  466. return !!cmpt;
  467. });
  468. return doFilter(filterBySubType(result, condition));
  469. function getQueryCond(q) {
  470. var indexAttr = mainType + 'Index';
  471. var idAttr = mainType + 'Id';
  472. var nameAttr = mainType + 'Name';
  473. return q && (q[indexAttr] != null || q[idAttr] != null || q[nameAttr] != null) ? {
  474. mainType: mainType,
  475. // subType will be filtered finally.
  476. index: q[indexAttr],
  477. id: q[idAttr],
  478. name: q[nameAttr]
  479. } : null;
  480. }
  481. function doFilter(res) {
  482. return condition.filter ? filter(res, condition.filter) : res;
  483. }
  484. };
  485. GlobalModel.prototype.eachComponent = function (mainType, cb, context) {
  486. var componentsMap = this._componentsMap;
  487. if (isFunction(mainType)) {
  488. var ctxForAll_1 = cb;
  489. var cbForAll_1 = mainType;
  490. componentsMap.each(function (cmpts, componentType) {
  491. for (var i = 0; cmpts && i < cmpts.length; i++) {
  492. var cmpt = cmpts[i];
  493. cmpt && cbForAll_1.call(ctxForAll_1, componentType, cmpt, cmpt.componentIndex);
  494. }
  495. });
  496. } else {
  497. var cmpts = isString(mainType) ? componentsMap.get(mainType) : isObject(mainType) ? this.findComponents(mainType) : null;
  498. for (var i = 0; cmpts && i < cmpts.length; i++) {
  499. var cmpt = cmpts[i];
  500. cmpt && cb.call(context, cmpt, cmpt.componentIndex);
  501. }
  502. }
  503. };
  504. /**
  505. * Get series list before filtered by name.
  506. */
  507. GlobalModel.prototype.getSeriesByName = function (name) {
  508. var nameStr = modelUtil.convertOptionIdName(name, null);
  509. return filter(this._componentsMap.get('series'), function (oneSeries) {
  510. return !!oneSeries && nameStr != null && oneSeries.name === nameStr;
  511. });
  512. };
  513. /**
  514. * Get series list before filtered by index.
  515. */
  516. GlobalModel.prototype.getSeriesByIndex = function (seriesIndex) {
  517. return this._componentsMap.get('series')[seriesIndex];
  518. };
  519. /**
  520. * Get series list before filtered by type.
  521. * FIXME: rename to getRawSeriesByType?
  522. */
  523. GlobalModel.prototype.getSeriesByType = function (subType) {
  524. return filter(this._componentsMap.get('series'), function (oneSeries) {
  525. return !!oneSeries && oneSeries.subType === subType;
  526. });
  527. };
  528. /**
  529. * Get all series before filtered.
  530. */
  531. GlobalModel.prototype.getSeries = function () {
  532. return filter(this._componentsMap.get('series'), function (oneSeries) {
  533. return !!oneSeries;
  534. });
  535. };
  536. /**
  537. * Count series before filtered.
  538. */
  539. GlobalModel.prototype.getSeriesCount = function () {
  540. return this._componentsCount.get('series');
  541. };
  542. /**
  543. * After filtering, series may be different
  544. * from raw series.
  545. */
  546. GlobalModel.prototype.eachSeries = function (cb, context) {
  547. assertSeriesInitialized(this);
  548. each(this._seriesIndices, function (rawSeriesIndex) {
  549. var series = this._componentsMap.get('series')[rawSeriesIndex];
  550. cb.call(context, series, rawSeriesIndex);
  551. }, this);
  552. };
  553. /**
  554. * Iterate raw series before filtered.
  555. *
  556. * @param {Function} cb
  557. * @param {*} context
  558. */
  559. GlobalModel.prototype.eachRawSeries = function (cb, context) {
  560. each(this._componentsMap.get('series'), function (series) {
  561. series && cb.call(context, series, series.componentIndex);
  562. });
  563. };
  564. /**
  565. * After filtering, series may be different.
  566. * from raw series.
  567. */
  568. GlobalModel.prototype.eachSeriesByType = function (subType, cb, context) {
  569. assertSeriesInitialized(this);
  570. each(this._seriesIndices, function (rawSeriesIndex) {
  571. var series = this._componentsMap.get('series')[rawSeriesIndex];
  572. if (series.subType === subType) {
  573. cb.call(context, series, rawSeriesIndex);
  574. }
  575. }, this);
  576. };
  577. /**
  578. * Iterate raw series before filtered of given type.
  579. */
  580. GlobalModel.prototype.eachRawSeriesByType = function (subType, cb, context) {
  581. return each(this.getSeriesByType(subType), cb, context);
  582. };
  583. GlobalModel.prototype.isSeriesFiltered = function (seriesModel) {
  584. assertSeriesInitialized(this);
  585. return this._seriesIndicesMap.get(seriesModel.componentIndex) == null;
  586. };
  587. GlobalModel.prototype.getCurrentSeriesIndices = function () {
  588. return (this._seriesIndices || []).slice();
  589. };
  590. GlobalModel.prototype.filterSeries = function (cb, context) {
  591. assertSeriesInitialized(this);
  592. var newSeriesIndices = [];
  593. each(this._seriesIndices, function (seriesRawIdx) {
  594. var series = this._componentsMap.get('series')[seriesRawIdx];
  595. cb.call(context, series, seriesRawIdx) && newSeriesIndices.push(seriesRawIdx);
  596. }, this);
  597. this._seriesIndices = newSeriesIndices;
  598. this._seriesIndicesMap = createHashMap(newSeriesIndices);
  599. };
  600. GlobalModel.prototype.restoreData = function (payload) {
  601. reCreateSeriesIndices(this);
  602. var componentsMap = this._componentsMap;
  603. var componentTypes = [];
  604. componentsMap.each(function (components, componentType) {
  605. if (ComponentModel.hasClass(componentType)) {
  606. componentTypes.push(componentType);
  607. }
  608. });
  609. ComponentModel.topologicalTravel(componentTypes, ComponentModel.getAllClassMainTypes(), function (componentType) {
  610. each(componentsMap.get(componentType), function (component) {
  611. if (component && (componentType !== 'series' || !isNotTargetSeries(component, payload))) {
  612. component.restoreData();
  613. }
  614. });
  615. });
  616. };
  617. GlobalModel.internalField = function () {
  618. reCreateSeriesIndices = function (ecModel) {
  619. var seriesIndices = ecModel._seriesIndices = [];
  620. each(ecModel._componentsMap.get('series'), function (series) {
  621. // series may have been removed by `replaceMerge`.
  622. series && seriesIndices.push(series.componentIndex);
  623. });
  624. ecModel._seriesIndicesMap = createHashMap(seriesIndices);
  625. };
  626. assertSeriesInitialized = function (ecModel) {
  627. // Components that use _seriesIndices should depends on series component,
  628. // which make sure that their initialization is after series.
  629. if (process.env.NODE_ENV !== 'production') {
  630. if (!ecModel._seriesIndices) {
  631. throw new Error('Option should contains series.');
  632. }
  633. }
  634. };
  635. initBase = function (ecModel, baseOption) {
  636. // Using OPTION_INNER_KEY to mark that this option cannot be used outside,
  637. // i.e. `chart.setOption(chart.getModel().option);` is forbidden.
  638. ecModel.option = {};
  639. ecModel.option[OPTION_INNER_KEY] = OPTION_INNER_VALUE; // Init with series: [], in case of calling findSeries method
  640. // before series initialized.
  641. ecModel._componentsMap = createHashMap({
  642. series: []
  643. });
  644. ecModel._componentsCount = createHashMap(); // If user spefied `option.aria`, aria will be enable. This detection should be
  645. // performed before theme and globalDefault merge.
  646. var airaOption = baseOption.aria;
  647. if (isObject(airaOption) && airaOption.enabled == null) {
  648. airaOption.enabled = true;
  649. }
  650. mergeTheme(baseOption, ecModel._theme.option); // TODO Needs clone when merging to the unexisted property
  651. merge(baseOption, globalDefault, false);
  652. ecModel._mergeOption(baseOption, null);
  653. };
  654. }();
  655. return GlobalModel;
  656. }(Model);
  657. function isNotTargetSeries(seriesModel, payload) {
  658. if (payload) {
  659. var index = payload.seriesIndex;
  660. var id = payload.seriesId;
  661. var name_1 = payload.seriesName;
  662. return index != null && seriesModel.componentIndex !== index || id != null && seriesModel.id !== id || name_1 != null && seriesModel.name !== name_1;
  663. }
  664. }
  665. function mergeTheme(option, theme) {
  666. // PENDING
  667. // NOT use `colorLayer` in theme if option has `color`
  668. var notMergeColorLayer = option.color && !option.colorLayer;
  669. each(theme, function (themeItem, name) {
  670. if (name === 'colorLayer' && notMergeColorLayer) {
  671. return;
  672. } // If it is component model mainType, the model handles that merge later.
  673. // otherwise, merge them here.
  674. if (!ComponentModel.hasClass(name)) {
  675. if (typeof themeItem === 'object') {
  676. option[name] = !option[name] ? clone(themeItem) : merge(option[name], themeItem, false);
  677. } else {
  678. if (option[name] == null) {
  679. option[name] = themeItem;
  680. }
  681. }
  682. }
  683. });
  684. }
  685. function queryByIdOrName(attr, idOrName, cmpts) {
  686. // Here is a break from echarts4: string and number are
  687. // treated as equal.
  688. if (isArray(idOrName)) {
  689. var keyMap_1 = createHashMap();
  690. each(idOrName, function (idOrNameItem) {
  691. if (idOrNameItem != null) {
  692. var idName = modelUtil.convertOptionIdName(idOrNameItem, null);
  693. idName != null && keyMap_1.set(idOrNameItem, true);
  694. }
  695. });
  696. return filter(cmpts, function (cmpt) {
  697. return cmpt && keyMap_1.get(cmpt[attr]);
  698. });
  699. } else {
  700. var idName_1 = modelUtil.convertOptionIdName(idOrName, null);
  701. return filter(cmpts, function (cmpt) {
  702. return cmpt && idName_1 != null && cmpt[attr] === idName_1;
  703. });
  704. }
  705. }
  706. function filterBySubType(components, condition) {
  707. // Using hasOwnProperty for restrict. Consider
  708. // subType is undefined in user payload.
  709. return condition.hasOwnProperty('subType') ? filter(components, function (cmpt) {
  710. return cmpt && cmpt.subType === condition.subType;
  711. }) : components;
  712. }
  713. function normalizeSetOptionInput(opts) {
  714. var replaceMergeMainTypeMap = createHashMap();
  715. opts && each(modelUtil.normalizeToArray(opts.replaceMerge), function (mainType) {
  716. if (process.env.NODE_ENV !== 'production') {
  717. assert(ComponentModel.hasClass(mainType), '"' + mainType + '" is not valid component main type in "replaceMerge"');
  718. }
  719. replaceMergeMainTypeMap.set(mainType, true);
  720. });
  721. return {
  722. replaceMergeMainTypeMap: replaceMergeMainTypeMap
  723. };
  724. }
  725. mixin(GlobalModel, PaletteMixin);
  726. export default GlobalModel;