-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathComponent.ts
377 lines (320 loc) · 11.4 KB
/
Component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* 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.
*/
import * as zrUtil from 'zrender/src/core/util';
import Model from './Model';
import * as componentUtil from '../util/component';
import {
enableClassManagement,
parseClassType,
isExtendedClass,
ExtendableConstructor,
ClassManager,
mountExtend
} from '../util/clazz';
import {
makeInner, ModelFinderIndexQuery, queryReferringComponents, ModelFinderIdQuery, QueryReferringOpt
} from '../util/model';
import * as layout from '../util/layout';
import GlobalModel from './Global';
import {
ComponentOption,
ComponentMainType,
ComponentSubType,
ComponentFullType,
ComponentLayoutMode,
BoxLayoutOptionMixin
} from '../util/types';
const inner = makeInner<{
defaultOption: ComponentOption
}, ComponentModel>();
class ComponentModel<Opt extends ComponentOption = ComponentOption> extends Model<Opt> {
// [Caution]: Because this class or desecendants can be used as `XXX.extend(subProto)`,
// the class members must not be initialized in constructor or declaration place.
// Otherwise there is bad case:
// class A {xxx = 1;}
// enableClassExtend(A);
// class B extends A {}
// var C = B.extend({xxx: 5});
// var c = new C();
// console.log(c.xxx); // expect 5 but always 1.
/**
* @readonly
*/
type: ComponentFullType;
/**
* @readonly
*/
id: string;
/**
* Because simplified concept is probably better, series.name (or component.name)
* has been having too many responsibilities:
* (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 than one component, which is convenient is some cases).
* (3) Display.
* @readOnly But injected
*/
name: string;
/**
* @readOnly
*/
mainType: ComponentMainType;
/**
* @readOnly
*/
subType: ComponentSubType;
/**
* @readOnly
*/
componentIndex: number;
/**
* @readOnly
*/
protected defaultOption: ComponentOption;
/**
* @readOnly
*/
ecModel: GlobalModel;
/**
* @readOnly
*/
static dependencies: string[];
readonly uid: string;
// // No common coordinateSystem needed. Each sub class implement
// // `CoordinateSystemHostModel` itself.
// coordinateSystem: CoordinateSystemMaster | CoordinateSystemExecutive;
/**
* Support merge layout params.
* Only support 'box' now (left/right/top/bottom/width/height).
*/
static layoutMode: ComponentLayoutMode | ComponentLayoutMode['type'];
/**
* Prevent from auto set z, zlevel, z2 by the framework.
*/
preventAutoZ: boolean;
// Injectable properties:
__viewId: string;
__requireNewView: boolean;
static protoInitialize = (function () {
const proto = ComponentModel.prototype;
proto.type = 'component';
proto.id = '';
proto.name = '';
proto.mainType = '';
proto.subType = '';
proto.componentIndex = 0;
})();
constructor(option: Opt, parentModel: Model, ecModel: GlobalModel) {
super(option, parentModel, ecModel);
this.uid = componentUtil.getUID('ec_cpt_model');
}
init(option: Opt, parentModel: Model, ecModel: GlobalModel): void {
this.mergeDefaultAndTheme(option, ecModel);
}
mergeDefaultAndTheme(option: Opt, ecModel: GlobalModel): void {
const layoutMode = layout.fetchLayoutMode(this);
const inputPositionParams = layoutMode
? layout.getLayoutParams(option as BoxLayoutOptionMixin) : {};
const themeModel = ecModel.getTheme();
zrUtil.merge(option, themeModel.get(this.mainType));
zrUtil.merge(option, this.getDefaultOption());
if (layoutMode) {
layout.mergeLayoutParam(option as BoxLayoutOptionMixin, inputPositionParams, layoutMode);
}
}
mergeOption(option: Opt, ecModel: GlobalModel): void {
zrUtil.merge(this.option, option, true);
const layoutMode = layout.fetchLayoutMode(this);
if (layoutMode) {
layout.mergeLayoutParam(
this.option as BoxLayoutOptionMixin,
option as BoxLayoutOptionMixin,
layoutMode
);
}
}
/**
* Called immediately after `init` or `mergeOption` of this instance called.
*/
optionUpdated(newCptOption: Opt, 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 {
* static type = 'xxx';
* static defaultOption: XxxOption = {
* aaa: 123
* }
* }
* Component.registerClass(XxxModel);
* ```
* ```ts
* import {inheritDefaultOption} from '../util/component';
* import {XxxModel, XxxOption} from './XxxModel';
* export interface XxxSubOption extends XxxOption {
* bbb: number
* }
* class XxxSubModel extends XxxModel {
* static defaultOption: XxxSubOption = inheritDefaultOption(XxxModel.defaultOption, {
* bbb: 456
* })
* fn() {
* let opt = this.getDefaultOption();
* // opt is {aaa: 123, bbb: 456}
* }
* }
* ```
*
* (B) If using class extend (previous approach in echarts 3 & 4):
* ```js
* let XxxComponent = Component.extend({
* defaultOption: {
* xx: 123
* }
* })
* ```
* ```js
* let XxxSubComponent = XxxComponent.extend({
* defaultOption: {
* yy: 456
* },
* fn: function () {
* let opt = this.getDefaultOption();
* // opt is {xx: 123, yy: 456}
* }
* })
* ```
*/
getDefaultOption(): Opt {
const 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)) {
// When using ts class, defaultOption must be declared as static.
return (ctor as any).defaultOption;
}
// FIXME: remove this approach?
const fields = inner(this);
if (!fields.defaultOption) {
const optList = [];
let clz = ctor as ExtendableConstructor;
while (clz) {
const opt = clz.prototype.defaultOption;
opt && optList.push(opt);
clz = clz.superClass;
}
let defaultOption = {};
for (let i = optList.length - 1; i >= 0; i--) {
defaultOption = zrUtil.merge(defaultOption, optList[i], true);
}
fields.defaultOption = defaultOption;
}
return fields.defaultOption as Opt;
}
/**
* Notice: always force to input param `useDefault` in case that forget to consider it.
* The same behavior as `modelUtil.parseFinder`.
*
* @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, opt: QueryReferringOpt): {
// Always be array rather than null/undefined, which is convenient to use.
models: ComponentModel[];
// Whether target component is specified
specified: boolean;
} {
const indexKey = (mainType + 'Index') as keyof Opt;
const idKey = (mainType + 'Id') as keyof Opt;
return queryReferringComponents(
this.ecModel,
mainType,
{
index: this.get(indexKey, true) as unknown as ModelFinderIndexQuery,
id: this.get(idKey, true) as unknown as ModelFinderIdQuery
},
opt
);
}
getBoxLayoutParams() {
// Consider itself having box layout configs.
const boxLayoutModel = this as Model<ComponentOption & BoxLayoutOptionMixin>;
return {
left: boxLayoutModel.get('left'),
top: boxLayoutModel.get('top'),
right: boxLayoutModel.get('right'),
bottom: boxLayoutModel.get('bottom'),
width: boxLayoutModel.get('width'),
height: boxLayoutModel.get('height')
};
}
/**
* Get key for zlevel.
* If developers don't configure zlevel. We will assign zlevel to series based on the key.
* For example, lines with trail effect and progressive series will in an individual zlevel.
*/
getZLevelKey(): string {
return '';
}
setZLevel(zlevel: number) {
this.option.zlevel = zlevel;
}
// // Interfaces for component / series with select ability.
// select(dataIndex?: number[], dataType?: string): void {}
// unSelect(dataIndex?: number[], dataType?: string): void {}
// getSelectedDataIndices(): number[] {
// return [];
// }
static registerClass: ClassManager['registerClass'];
static hasClass: ClassManager['hasClass'];
static registerSubTypeDefaulter: componentUtil.SubTypeDefaulterManager['registerSubTypeDefaulter'];
}
export type ComponentModelConstructor = typeof ComponentModel
& ClassManager
& componentUtil.SubTypeDefaulterManager
& ExtendableConstructor
& componentUtil.TopologicalTravelable<object>;
mountExtend(ComponentModel, Model);
enableClassManagement(ComponentModel as ComponentModelConstructor);
componentUtil.enableSubTypeDefaulter(ComponentModel as ComponentModelConstructor);
componentUtil.enableTopologicalTravel(ComponentModel as ComponentModelConstructor, getDependencies);
function getDependencies(componentType: string): string[] {
let deps: string[] = [];
zrUtil.each((ComponentModel as ComponentModelConstructor).getClassesByMainType(componentType), function (clz) {
deps = deps.concat((clz as any).dependencies || (clz as any).prototype.dependencies || []);
});
// Ensure main type.
deps = zrUtil.map(deps, function (type) {
return parseClassType(type).main;
});
// Hack dataset for convenience.
if (componentType !== 'dataset' && zrUtil.indexOf(deps, 'dataset') <= 0) {
deps.unshift('dataset');
}
return deps;
}
export default ComponentModel;