Component.ts 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

L
lang 已提交
20

S
sushuang 已提交
21
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
22 23
import Model from './Model';
import * as componentUtil from '../util/component';
24 25 26 27 28 29 30 31
import {
    enableClassManagement,
    parseClassType,
    isExtendedClass,
    ExtendableConstructor,
    ClassManager,
    mountExtend
} from '../util/clazz';
S
sushuang 已提交
32
import {makeInner} from '../util/model';
S
sushuang 已提交
33 34
import * as layout from '../util/layout';
import boxLayoutMixin from './mixin/boxLayout';
35 36
import { CoordinateSystem } from '../coord/CoordinateSystem';
import GlobalModel from './Global';
37
import { ComponentOption, ComponentMainType, ComponentSubType, ComponentFullType } from '../util/types';
S
sushuang 已提交
38

S
sushuang 已提交
39
var inner = makeInner();
S
sushuang 已提交
40

41
class ComponentModel extends Model {
S
sushuang 已提交
42

43 44 45 46 47
    // [Caution]: for compat the previous "class extend"
    // publich and protected fields must be initialized on
    // prototype rather than in constructor. Otherwise the
    // subclass overrided filed will be overwritten by this
    // class. That is, they should not be initialized here.
P
pah100 已提交
48

L
lang 已提交
49
    /**
50 51
     * @readonly
     */
52
    type: ComponentFullType;
53 54 55

    /**
     * @readonly
L
lang 已提交
56
     */
57
    id: string;
P
pah100 已提交
58

S
sushuang 已提交
59
    /**
S
sushuang 已提交
60 61 62 63 64 65
     * Because simplified concept is probably better, series.name (or component.name)
     * has been having too many resposibilities:
     * (1) Generating id (which requires name in option should not be modified).
     * (2) As an index to mapping series when merging option or calling API (a name
     * can refer to more then one components, which is convinient is some case).
     * (3) Display.
66 67 68 69 70
     * @readOnly But injected
     */
    name: string;

    /**
S
sushuang 已提交
71 72
     * @readOnly
     */
73
    mainType: ComponentMainType;
P
pah100 已提交
74

S
sushuang 已提交
75 76 77
    /**
     * @readOnly
     */
78
    subType: ComponentSubType;
S
sushuang 已提交
79 80 81 82

    /**
     * @readOnly
     */
83
    componentIndex: number;
S
sushuang 已提交
84 85 86 87

    /**
     * @readOnly
     */
88
    protected defaultOption: ComponentOption;
S
sushuang 已提交
89 90

    /**
91
     * @readOnly
S
sushuang 已提交
92
     */
93
    ecModel: GlobalModel;
S
sushuang 已提交
94 95 96 97

    /**
     * @readOnly
     */
98
    dependencies: string[];
S
sushuang 已提交
99 100 101

    /**
     * key: componentType
102
     * value: Component model list, can not be null.
S
sushuang 已提交
103 104
     * @readOnly
     */
105 106 107
    dependentModels: {[componentType: string]: ComponentModel[]} = {};

    readonly uid: string;
S
sushuang 已提交
108 109

    /**
110
     * @readonly
S
sushuang 已提交
111
     */
112
    coordinateSystem: CoordinateSystem;
S
sushuang 已提交
113 114 115 116 117

    /**
     * Support merge layout params.
     * Only support 'box' now (left/right/top/bottom/width/height).
     */
118 119 120 121 122 123 124 125 126 127 128 129 130 131
    readonly layoutMode: string | {ignoreSize: boolean};

    // Injectable properties:
    __viewId: string;

    static protoInitialize = (function () {
        var proto = ComponentModel.prototype;
        proto.type = 'component';
        proto.id = '';
        proto.name = '';
        proto.mainType = '';
        proto.subType = '';
        proto.componentIndex = 0;
    })();
S
sushuang 已提交
132 133


134 135
    constructor(option: ComponentOption, parentModel: Model, ecModel: GlobalModel) {
        super(option, parentModel, ecModel);
S
sushuang 已提交
136
        this.uid = componentUtil.getUID('ec_cpt_model');
137
    }
S
sushuang 已提交
138

139
    init(option: ComponentOption, parentModel: Model, ecModel: GlobalModel): void {
S
sushuang 已提交
140
        this.mergeDefaultAndTheme(option, ecModel);
141
    }
S
sushuang 已提交
142

143
    mergeDefaultAndTheme(option: ComponentOption, ecModel: GlobalModel): void {
S
sushuang 已提交
144 145 146 147 148 149 150 151 152 153
        var layoutMode = this.layoutMode;
        var inputPositionParams = layoutMode
            ? layout.getLayoutParams(option) : {};

        var themeModel = ecModel.getTheme();
        zrUtil.merge(option, themeModel.get(this.mainType));
        zrUtil.merge(option, this.getDefaultOption());

        if (layoutMode) {
            layout.mergeLayoutParam(option, inputPositionParams, layoutMode);
L
lang 已提交
154
        }
155
    }
156

157
    mergeOption(option: ComponentOption, ecModel: GlobalModel): void {
S
sushuang 已提交
158
        zrUtil.merge(this.option, option, true);
L
lang 已提交
159

S
sushuang 已提交
160 161 162 163
        var layoutMode = this.layoutMode;
        if (layoutMode) {
            layout.mergeLayoutParam(this.option, option, layoutMode);
        }
164
    }
S
sushuang 已提交
165 166

    // Hooker after init or mergeOption
167 168 169 170 171 172 173 174 175 176 177 178
    optionUpdated(newCptOption: ComponentOption, isInit: boolean): void {}

    /**
     * [How to declare defaultOption]:
     *
     * (A) If using class declaration in typescript (since echarts 5):
     * ```ts
     * import {ComponentOption} from '../model/option';
     * export interface XxxOption extends ComponentOption {
     *     aaa: number
     * }
     * export class XxxModel extends Component {
179 180
     *     static type = 'xxx';
     *     static defaultOption: XxxOption = {
181 182 183
     *         aaa: 123
     *     }
     * }
184
     * Component.registerClass(XxxModel);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
     * ```
     * ```ts
     * import {mergeOption} from '../model/util';
     * import {XxxModel, XxxOption} from './XxxModel';
     * export interface XxxSubOption extends XxxOption {
     *     bbb: number
     * }
     * class XxxSubModel extends XxxModel {
     *     readonly defaultOption: XxxSubOption = mergeOption({
     *         bbb: 456
     *     }, XxxModel.prototype.defaultOption)
     *     fn() {
     *         var opt = this.getDefaultOption();
     *         // opt is {aaa: 123, bbb: 456}
     *     }
     * }
     * ```
     *
     * (B) If using class extend (previous approach in echarts 3 & 4):
     * ```js
     * var XxxComponent = Component.extend({
     *     defaultOption: {
     *         xx: 123
     *     }
     * })
     * ```
     * ```js
     * var XxxSubComponent = XxxComponent.extend({
     *     defaultOption: {
     *         yy: 456
     *     },
     *     fn: function () {
     *         var opt = this.getDefaultOption();
     *         // opt is {xx: 123, yy: 456}
     *     }
     * })
     * ```
     */
    getDefaultOption(): ComponentOption {
        var ctor = this.constructor;

        // If using class declaration, it is different to travel super class
        // in legacy env and auto merge defaultOption. So if using class
        // declaration, defaultOption should be merged manually.
        if (!isExtendedClass(ctor)) {
230 231
            // When using ts class, defaultOption must be declared as static.
            return (ctor as any).defaultOption;
232
        }
S
sushuang 已提交
233

234
        // FIXME: remove this approach?
S
sushuang 已提交
235 236
        var fields = inner(this);
        if (!fields.defaultOption) {
S
sushuang 已提交
237
            var optList = [];
238 239 240
            var clz = ctor as ExtendableConstructor;
            while (clz) {
                var opt = clz.prototype.defaultOption;
S
sushuang 已提交
241
                opt && optList.push(opt);
242
                clz = clz.superClass;
S
sushuang 已提交
243 244 245 246 247 248
            }

            var defaultOption = {};
            for (var i = optList.length - 1; i >= 0; i--) {
                defaultOption = zrUtil.merge(defaultOption, optList[i], true);
            }
S
sushuang 已提交
249
            fields.defaultOption = defaultOption;
S
sushuang 已提交
250
        }
S
sushuang 已提交
251
        return fields.defaultOption;
252
    }
S
sushuang 已提交
253

254
    getReferringComponents(mainType: ComponentMainType): ComponentModel[] {
S
sushuang 已提交
255 256 257 258
        return this.ecModel.queryComponents({
            mainType: mainType,
            index: this.get(mainType + 'Index', true),
            id: this.get(mainType + 'Id', true)
P
pah100 已提交
259
        });
P
pah100 已提交
260 261
    }

262
    static registerClass: ClassManager['registerClass'];
263
}
S
sushuang 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

// Reset ComponentModel.extend, add preConstruct.
// clazzUtil.enableClassExtend(
//     ComponentModel,
//     function (option, parentModel, ecModel, extraOpt) {
//         // Set dependentModels, componentIndex, name, id, mainType, subType.
//         zrUtil.extend(this, extraOpt);

//         this.uid = componentUtil.getUID('componentModel');

//         // this.setReadOnly([
//         //     'type', 'id', 'uid', 'name', 'mainType', 'subType',
//         //     'dependentModels', 'componentIndex'
//         // ]);
//     }
// );

281 282 283 284 285 286 287 288 289 290
type ComponentModelConstructor = typeof ComponentModel
    & ClassManager
    & componentUtil.SubTypeDefaulterManager
    & ExtendableConstructor
    & componentUtil.TopologicalTravelable<object>;

mountExtend(ComponentModel, Model);
enableClassManagement(ComponentModel as ComponentModelConstructor, {registerWhenExtend: true})
componentUtil.enableSubTypeDefaulter(ComponentModel as ComponentModelConstructor);
componentUtil.enableTopologicalTravel(ComponentModel as ComponentModelConstructor, getDependencies);
S
sushuang 已提交
291 292


293 294 295 296
function getDependencies(componentType: string): string[] {
    var deps: string[] = [];
    zrUtil.each((ComponentModel as ComponentModelConstructor).getClassesByMainType(componentType), function (clz) {
        deps = deps.concat((clz as any).prototype.dependencies || []);
S
sushuang 已提交
297
    });
S
sushuang 已提交
298 299 300

    // Ensure main type.
    deps = zrUtil.map(deps, function (type) {
S
sushuang 已提交
301
        return parseClassType(type).main;
S
sushuang 已提交
302
    });
S
sushuang 已提交
303 304 305 306 307 308 309

    // Hack dataset for convenience.
    if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
        deps.unshift('dataset');
    }

    return deps;
S
sushuang 已提交
310 311
}

S
sushuang 已提交
312
zrUtil.mixin(ComponentModel, boxLayoutMixin);
313

314 315 316
export {ComponentModelConstructor};

export default ComponentModel;