You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by su...@apache.org on 2020/07/17 11:39:06 UTC

[incubator-echarts] 07/16: fix: fix type and tweak component.getReferingComponent.

This is an automated email from the ASF dual-hosted git repository.

sushuang pushed a commit to branch remove-component
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git

commit af079802ecd260265bd9ca5575726442615ff82f
Author: 100pah <su...@gmail.com>
AuthorDate: Tue Jul 14 17:30:37 2020 +0800

    fix: fix type and tweak component.getReferingComponent.
---
 src/chart/themeRiver/ThemeRiverSeries.ts   |  2 +-
 src/coord/cartesian/Grid.ts                |  4 +--
 src/coord/cartesian/cartesianAxisHelper.ts |  2 +-
 src/coord/geo/Geo.ts                       |  2 +-
 src/echarts.ts                             |  2 +-
 src/model/Component.ts                     | 41 ++++++++++++++++++++++++++----
 src/model/referHelper.ts                   |  8 +++---
 src/util/model.ts                          | 28 ++++++++++++++++----
 8 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/src/chart/themeRiver/ThemeRiverSeries.ts b/src/chart/themeRiver/ThemeRiverSeries.ts
index 5e28bf2..3f0cc49 100644
--- a/src/chart/themeRiver/ThemeRiverSeries.ts
+++ b/src/chart/themeRiver/ThemeRiverSeries.ts
@@ -83,7 +83,7 @@ class ThemeRiverSeriesModel extends SeriesModel<ThemeRiverSeriesOption> {
 
     static readonly dependencies = ['singleAxis'];
 
-    nameMap: zrUtil.HashMap<number>;
+    nameMap: zrUtil.HashMap<number, string>;
 
     coordinateSystem: Single;
 
diff --git a/src/coord/cartesian/Grid.ts b/src/coord/cartesian/Grid.ts
index 6c803f9..91e540b 100644
--- a/src/coord/cartesian/Grid.ts
+++ b/src/coord/cartesian/Grid.ts
@@ -254,9 +254,9 @@ class Grid implements CoordinateSystemMaster {
     } {
         const seriesModel = finder.seriesModel;
         const xAxisModel = finder.xAxisModel
-            || (seriesModel && seriesModel.getReferringComponents('xAxis')[0]);
+            || (seriesModel && seriesModel.getReferringComponents('xAxis', true).models[0]);
         const yAxisModel = finder.yAxisModel
-            || (seriesModel && seriesModel.getReferringComponents('yAxis')[0]);
+            || (seriesModel && seriesModel.getReferringComponents('yAxis', true).models[0]);
         const gridModel = finder.gridModel;
         const coordsList = this._coordsList;
         let cartesian: Cartesian2D;
diff --git a/src/coord/cartesian/cartesianAxisHelper.ts b/src/coord/cartesian/cartesianAxisHelper.ts
index f732f36..badc476 100644
--- a/src/coord/cartesian/cartesianAxisHelper.ts
+++ b/src/coord/cartesian/cartesianAxisHelper.ts
@@ -112,7 +112,7 @@ export function findAxisModels(seriesModel: SeriesModel): {
     } as ReturnType<typeof findAxisModels>;
     zrUtil.each(axisModelMap, function (v, key) {
         const axisType = key.replace(/Model$/, '');
-        const axisModel = seriesModel.getReferringComponents(axisType)[0] as CartesianAxisModel;
+        const axisModel = seriesModel.getReferringComponents(axisType, true).models[0] as CartesianAxisModel;
 
         if (__DEV__) {
             if (!axisModel) {
diff --git a/src/coord/geo/Geo.ts b/src/coord/geo/Geo.ts
index 9627ba1..df84083 100644
--- a/src/coord/geo/Geo.ts
+++ b/src/coord/geo/Geo.ts
@@ -173,7 +173,7 @@ function getCoordSys(finder: ParsedModelFinder): Geo {
         : seriesModel
         ? (
             seriesModel.coordinateSystem as Geo // For map series.
-            || ((seriesModel.getReferringComponents('geo')[0] || {}) as GeoModel).coordinateSystem
+            || ((seriesModel.getReferringComponents('geo', true).models[0] || {}) as GeoModel).coordinateSystem
         )
         : null;
 }
diff --git a/src/echarts.ts b/src/echarts.ts
index 581bf53..63a2100 100644
--- a/src/echarts.ts
+++ b/src/echarts.ts
@@ -1298,7 +1298,7 @@ class ECharts extends Eventful {
             subType && (condition.subType = subType); // subType may be '' by parseClassType;
 
             const excludeSeriesId = payload.excludeSeriesId;
-            let excludeSeriesIdMap: zrUtil.HashMap<string[]>;
+            let excludeSeriesIdMap: zrUtil.HashMap<string[], string>;
             if (excludeSeriesId != null) {
                 excludeSeriesIdMap = zrUtil.createHashMap(modelUtil.normalizeToArray(excludeSeriesId));
             }
diff --git a/src/model/Component.ts b/src/model/Component.ts
index 74fccf0..2e1b1a8 100644
--- a/src/model/Component.ts
+++ b/src/model/Component.ts
@@ -177,7 +177,9 @@ class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Mode
         }
     }
 
-    // Hooker after init or mergeOption
+    /**
+     * Called immediately after `init` or `mergeOption` of this instance called.
+     */
     optionUpdated(newCptOption: Opt, isInit: boolean): void {}
 
     /**
@@ -265,14 +267,43 @@ class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Mode
         return fields.defaultOption as Opt;
     }
 
-    getReferringComponents(mainType: ComponentMainType): ComponentModel[] {
+    /**
+     * Notice: always force to input param `useDefault` in case that forget to consider it.
+     *
+     * @param useDefault In many cases like series refer axis and axis refer grid,
+     *        If axis index / axis id not specified, use the first target as default.
+     *        In other cases like dataZoom refer axis, if not specified, measn no refer.
+     */
+    getReferringComponents(mainType: ComponentMainType, useDefault: boolean): {
+        // Always be array rather than null/undefined, which is convenient to use.
+        models: ComponentModel[],
+        // Whether index or id are specified in option.
+        specified: boolean
+    } {
         const indexKey = (mainType + 'Index') as keyof Opt;
         const idKey = (mainType + 'Id') as keyof Opt;
-        return this.ecModel.queryComponents({
+        const indexOption = this.get(indexKey, true);
+        const idOption = this.get(idKey, true);
+
+        const models = this.ecModel.queryComponents({
             mainType: mainType,
-            index: this.get(indexKey, true) as unknown as number,
-            id: this.get(idKey, true) as unknown as string
+            index: indexOption as any,
+            id: idOption as any
         });
+
+        // `queryComponents` will return all components if
+        // both index and id are null/undefined
+        let specified = true;
+        if (indexOption == null && idOption == null) {
+            specified = false;
+            // Use the first as default if `useDefault`.
+            models.length = (useDefault && models.length) ? 1 : 0;
+        }
+
+        return {
+            models: models,
+            specified: specified
+        };
     }
 
     getBoxLayoutParams() {
diff --git a/src/model/referHelper.ts b/src/model/referHelper.ts
index 287eec5..0f6d46c 100644
--- a/src/model/referHelper.ts
+++ b/src/model/referHelper.ts
@@ -94,8 +94,8 @@ const fetchers: Record<SupportedCoordSys, Fetcher> = {
     cartesian2d: function (
         seriesModel: SeriesModel<SeriesOption & SeriesOnCartesianOptionMixin>, result, axisMap, categoryAxisMap
     ) {
-        const xAxisModel = seriesModel.getReferringComponents('xAxis')[0] as AxisBaseModel;
-        const yAxisModel = seriesModel.getReferringComponents('yAxis')[0] as AxisBaseModel;
+        const xAxisModel = seriesModel.getReferringComponents('xAxis', true).models[0] as AxisBaseModel;
+        const yAxisModel = seriesModel.getReferringComponents('yAxis', true).models[0] as AxisBaseModel;
 
         if (__DEV__) {
             if (!xAxisModel) {
@@ -129,7 +129,7 @@ const fetchers: Record<SupportedCoordSys, Fetcher> = {
     },
 
     singleAxis: function (seriesModel, result, axisMap, categoryAxisMap) {
-        const singleAxisModel = seriesModel.getReferringComponents('singleAxis')[0] as AxisBaseModel;
+        const singleAxisModel = seriesModel.getReferringComponents('singleAxis', true).models[0] as AxisBaseModel;
 
         if (__DEV__) {
             if (!singleAxisModel) {
@@ -147,7 +147,7 @@ const fetchers: Record<SupportedCoordSys, Fetcher> = {
     },
 
     polar: function (seriesModel, result, axisMap, categoryAxisMap) {
-        const polarModel = seriesModel.getReferringComponents('polar')[0] as PolarModel;
+        const polarModel = seriesModel.getReferringComponents('polar', true).models[0] as PolarModel;
         const radiusAxisModel = polarModel.findAxisModel('radiusAxis');
         const angleAxisModel = polarModel.findAxisModel('angleAxis');
 
diff --git a/src/util/model.ts b/src/util/model.ts
index 826c23f..7e21a2e 100644
--- a/src/util/model.ts
+++ b/src/util/model.ts
@@ -47,6 +47,7 @@ import SeriesModel from '../model/Series';
 import CartesianAxisModel from '../coord/cartesian/AxisModel';
 import GridModel from '../coord/cartesian/GridModel';
 import { __DEV__ } from '../config';
+import { isNumeric } from './number';
 
 /**
  * Make the name displayable. But we should
@@ -219,6 +220,9 @@ function mappingToExistsInNormalMerge<T extends MappingExistingItem>(
             return;
         }
 
+        cmptOption.id == null || validateIdOrName(cmptOption.id);
+        cmptOption.name == null || validateIdOrName(cmptOption.name);
+
         // id has highest priority.
         for (let i = 0; i < result.length; i++) {
             const existing = result[i].existing;
@@ -304,9 +308,10 @@ function mappingToExistsInReplaceMerge<T extends MappingExistingItem>(
             newCmptOptions[index] = null;
             return;
         }
-        if (cmptOption.id == null) {
-            return;
-        }
+
+        cmptOption.id == null || validateIdOrName(cmptOption.id);
+        cmptOption.name == null || validateIdOrName(cmptOption.name);
+
         const optionId = makeComparableKey(cmptOption.id);
         const existingIdx = existingIdIdxMap.get(optionId);
         if (existingIdx != null) {
@@ -482,6 +487,19 @@ function makeComparableKey(val: string | number): string {
     return val + '';
 }
 
+export function validateIdOrName(idOrName: unknown) {
+    if (__DEV__) {
+        assert(
+            isValidIdOrName(idOrName),
+            '`' + idOrName + '` is invalid id or name. Must be a string.'
+        );
+    }
+}
+
+function isValidIdOrName(idOrName: unknown): boolean {
+    return isString(idOrName) || isNumeric(idOrName);
+}
+
 export function isNameSpecified(componentModel: ComponentModel): boolean {
     const name = componentModel.name;
     // Is specified when `indexOf` get -1 or > 0.
@@ -810,9 +828,9 @@ export function groupData<T, R extends string | number>(
     getKey: (item: T) => R // return key
 ): {
     keys: R[],
-    buckets: HashMap<T[]> // hasmap key: the key returned by `getKey`.
+    buckets: HashMap<T[], R> // hasmap key: the key returned by `getKey`.
 } {
-    const buckets = createHashMap<T[]>();
+    const buckets = createHashMap<T[], R>();
     const keys: R[] = [];
 
     each(array, function (item) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org