Component.ts 6.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 21 22 23 24
/**
 * Component model
 *
 * @module echarts/model/Component
 */
L
lang 已提交
25

S
sushuang 已提交
26
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
27 28
import Model from './Model';
import * as componentUtil from '../util/component';
S
sushuang 已提交
29 30
import {enableClassManagement, parseClassType} from '../util/clazz';
import {makeInner} from '../util/model';
S
sushuang 已提交
31 32 33
import * as layout from '../util/layout';
import boxLayoutMixin from './mixin/boxLayout';

S
sushuang 已提交
34
var inner = makeInner();
S
sushuang 已提交
35 36 37 38 39 40 41 42 43 44 45

/**
 * @alias module:echarts/model/Component
 * @constructor
 * @param {Object} option
 * @param {module:echarts/model/Model} parentModel
 * @param {module:echarts/model/Model} ecModel
 */
var ComponentModel = Model.extend({

    type: 'component',
P
pah100 已提交
46

L
lang 已提交
47
    /**
S
sushuang 已提交
48 49
     * @readOnly
     * @type {string}
L
lang 已提交
50
     */
S
sushuang 已提交
51
    id: '',
P
pah100 已提交
52

S
sushuang 已提交
53
    /**
S
sushuang 已提交
54 55 56 57 58 59
     * 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.
S
sushuang 已提交
60 61 62
     * @readOnly
     */
    name: '',
P
pah100 已提交
63

S
sushuang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    /**
     * @readOnly
     * @type {string}
     */
    mainType: '',

    /**
     * @readOnly
     * @type {string}
     */
    subType: '',

    /**
     * @readOnly
     * @type {number}
     */
    componentIndex: 0,

    /**
     * @type {Object}
     * @protected
     */
    defaultOption: null,

    /**
     * @type {module:echarts/model/Global}
     * @readOnly
     */
    ecModel: null,

    /**
     * key: componentType
     * value:  Component model list, can not be null.
     * @type {Object.<string, Array.<module:echarts/model/Model>>}
     * @readOnly
     */
    dependentModels: [],

    /**
     * @type {string}
     * @readOnly
     */
    uid: null,

    /**
     * Support merge layout params.
     * Only support 'box' now (left/right/top/bottom/width/height).
     * @type {string|Object} Object can be {ignoreSize: true}
     * @readOnly
     */
    layoutMode: null,

    $constructor: function (option, parentModel, ecModel, extraOpt) {
        Model.call(this, option, parentModel, ecModel, extraOpt);

S
sushuang 已提交
119
        this.uid = componentUtil.getUID('ec_cpt_model');
S
sushuang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    },

    init: function (option, parentModel, ecModel, extraOpt) {
        this.mergeDefaultAndTheme(option, ecModel);
    },

    mergeDefaultAndTheme: function (option, ecModel) {
        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 已提交
137
        }
S
sushuang 已提交
138
    },
139

S
sushuang 已提交
140 141
    mergeOption: function (option, extraOpt) {
        zrUtil.merge(this.option, option, true);
L
lang 已提交
142

S
sushuang 已提交
143 144 145 146 147 148 149 150 151 152
        var layoutMode = this.layoutMode;
        if (layoutMode) {
            layout.mergeLayoutParam(this.option, option, layoutMode);
        }
    },

    // Hooker after init or mergeOption
    optionUpdated: function (newCptOption, isInit) {},

    getDefaultOption: function () {
S
sushuang 已提交
153 154
        var fields = inner(this);
        if (!fields.defaultOption) {
S
sushuang 已提交
155 156 157 158 159 160 161 162 163 164 165 166
            var optList = [];
            var Class = this.constructor;
            while (Class) {
                var opt = Class.prototype.defaultOption;
                opt && optList.push(opt);
                Class = Class.superClass;
            }

            var defaultOption = {};
            for (var i = optList.length - 1; i >= 0; i--) {
                defaultOption = zrUtil.merge(defaultOption, optList[i], true);
            }
S
sushuang 已提交
167
            fields.defaultOption = defaultOption;
S
sushuang 已提交
168
        }
S
sushuang 已提交
169
        return fields.defaultOption;
S
sushuang 已提交
170 171 172 173 174 175 176
    },

    getReferringComponents: function (mainType) {
        return this.ecModel.queryComponents({
            mainType: mainType,
            index: this.get(mainType + 'Index', true),
            id: this.get(mainType + 'Id', true)
P
pah100 已提交
177
        });
P
pah100 已提交
178 179
    }

S
sushuang 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
});

// 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'
//         // ]);
//     }
// );

// Add capability of registerClass, getClass, hasClass, registerSubTypeDefaulter and so on.
S
sushuang 已提交
199
enableClassManagement(
S
sushuang 已提交
200 201 202 203 204 205 206 207 208 209
    ComponentModel, {registerWhenExtend: true}
);
componentUtil.enableSubTypeDefaulter(ComponentModel);

// Add capability of ComponentModel.topologicalTravel.
componentUtil.enableTopologicalTravel(ComponentModel, getDependencies);

function getDependencies(componentType) {
    var deps = [];
    zrUtil.each(ComponentModel.getClassesByMainType(componentType), function (Clazz) {
S
sushuang 已提交
210
        deps = deps.concat(Clazz.prototype.dependencies || []);
S
sushuang 已提交
211
    });
S
sushuang 已提交
212 213 214

    // Ensure main type.
    deps = zrUtil.map(deps, function (type) {
S
sushuang 已提交
215
        return parseClassType(type).main;
S
sushuang 已提交
216
    });
S
sushuang 已提交
217 218 219 220 221 222 223

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

    return deps;
S
sushuang 已提交
224 225
}

S
sushuang 已提交
226
zrUtil.mixin(ComponentModel, boxLayoutMixin);
227

S
sushuang 已提交
228
export default ComponentModel;