-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathplayer.ts
More file actions
461 lines (409 loc) · 13.2 KB
/
Copy pathplayer.ts
File metadata and controls
461 lines (409 loc) · 13.2 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { Factory } from './../../core/factory';
import type { INode, IGroup, IGraphic } from '@visactor/vrender-core';
import type { ContinuousPlayerAttributes, DiscretePlayerAttributes } from '@visactor/vrender-components';
// eslint-disable-next-line no-duplicate-imports
import { DiscretePlayer, ContinuousPlayer, PlayerEventEnum } from '@visactor/vrender-components';
// eslint-disable-next-line no-duplicate-imports
import { isNumber, array, isEqual, isValidNumber } from '@visactor/vutils';
import type { IModelRenderOption } from '../../model/interface';
import type { IRegion } from '../../region/interface';
import type { DirectionType, IPlayer } from './interface';
// eslint-disable-next-line no-duplicate-imports
import type { IComponent } from '../interface';
import type { IPoint, IOrientType, ILayoutRect, IChartSpec, IDataValues } from '../../typings';
// eslint-disable-next-line no-duplicate-imports
import { ComponentTypeEnum } from '../interface/type';
import { BaseComponent } from '../base/base-component';
import { transformContinuousSpecToAttrs, transformDiscreteSpecToAttrs } from './utils/transform';
import { isHorizontal, isVertical } from './utils/orient';
import { LayoutLevel, LayoutZIndex } from '../../constant/layout';
import { ChartEvent } from '../../constant/event';
import { player } from '../../theme/builtin/common/component/player';
export class Player extends BaseComponent<IPlayer> implements IComponent {
layoutZIndex: number = LayoutZIndex.Player;
layoutLevel: number = LayoutLevel.Player;
static readonly builtInTheme = {
player
};
static specKey = 'player';
specKey: string = 'player';
static type = ComponentTypeEnum.player;
type = ComponentTypeEnum.player;
protected _orient: IOrientType = 'bottom';
private _specs: Partial<IChartSpec>[];
private _playerComponent: DiscretePlayer | ContinuousPlayer;
private _cacheAttrs: ContinuousPlayerAttributes | DiscretePlayerAttributes;
private _visible: boolean;
private _direction: DirectionType;
private _alternate: boolean;
private _dx: number;
private _dy: number;
private _width: number;
private _height: number;
private _position: 'start' | 'middle' | 'end';
get orient() {
return this._orient;
}
set layoutOrient(v: IOrientType) {
this._orient = v;
}
/**
* 设置Attr
*/
setAttrFromSpec() {
super.setAttrFromSpec();
this._orient = this._spec.orient ?? 'bottom';
this._specs = this._spec.specs ?? [];
this._direction = this._spec.direction ?? 'default';
this._alternate = this._spec.alternate ?? false;
this._dx = this._spec.dx ?? 0;
this._dy = this._spec.dy ?? 0;
this._position = this._spec.position ?? 'middle';
this._visible = this._spec.visible ?? true;
}
_compareSpec(spec: IPlayer, prevSpec: IPlayer) {
const result = super._compareSpec(spec, prevSpec);
const specChanged = !isEqual(prevSpec, spec);
if (prevSpec?.type !== spec?.type) {
result.reMake = true;
return result;
}
if (specChanged && !result.reMake && !result.reCompile) {
result.change = true;
result.reRender = true;
result.effects = {
...result.effects,
component: true,
layout: true,
render: true
};
}
return result;
}
/**
* 计算组件位置(布局的左上角起点)
* @param pos
*/
afterSetLayoutStartPoint(pos: IPoint) {
super.afterSetLayoutStartPoint(pos);
if (isValidNumber(pos.x)) {
const offsetX = isVertical(this._orient) ? pos.x + this._sliderExceededSize() / 2 : pos.x;
this._playerComponent && this._playerComponent.setAttribute('x', offsetX);
}
if (isValidNumber(pos.y)) {
const offsetY = isHorizontal(this._orient) ? pos.y + this._sliderExceededSize() / 2 : pos.y;
this._playerComponent && this._playerComponent.setAttribute('y', offsetY);
}
}
/**
* 计算组件占用的bound box
* @param rect
* @returns
*/
getBoundsInRect(rect: ILayoutRect, fullSpace: ILayoutRect) {
this._width = this._computeWidth(rect);
this._height = this._computeHeight(rect);
this._dx = this._computeDx(fullSpace);
this._dy = this._computeDy(fullSpace);
const bounds = this._computeLayoutRect(rect, this._width, this._height);
this._createOrUpdatePlayerComponent();
return bounds;
}
protected _getNeedClearVRenderComponents(): IGraphic[] {
return [this._playerComponent] as unknown as IGroup[];
}
/**
* 播放器属性
*/
private _getPlayerAttrs = () => {
const type = this._spec.type;
const layoutAttrs = {
size: {
width: this._width,
height: this._height
},
dx: this._spec.dx ?? 0 + this._dx,
dy: this._spec.dy ?? 0 + this._dy
};
// 离散类型Attrs
if (type === 'discrete') {
return {
...transformDiscreteSpecToAttrs(this._spec, this._specs),
...layoutAttrs,
disableTriggerEvent: this._option.disableTriggerEvent,
loop: this._spec?.loop ?? true
};
}
// 连续类型Attrs
return {
...transformContinuousSpecToAttrs(this._spec, this._specs),
...layoutAttrs,
disableTriggerEvent: this._option.disableTriggerEvent,
loop: this._spec?.loop ?? true
};
};
/**
* 创建或更新播放器组件
*/
private _createOrUpdatePlayerComponent = () => {
const attrs = { ...this._getPlayerAttrs() };
const container = this.getContainer();
if (this._playerComponent) {
if (!isEqual(attrs, this._cacheAttrs)) {
this._cacheAttrs = attrs;
this._playerComponent.setAttributes(attrs);
// FIXME: player 组件没有重写 setAttributes 方法,因此不能正常更新样式。以下两句模拟执行了 setAttributes 方法,但是应在 vrender-component 的后续版本中实现 setAttributes 方法
this._playerComponent._initAttributes();
this._playerComponent.render();
}
} else {
if (attrs.type === 'discrete') {
this._playerComponent = new DiscretePlayer(attrs);
} else {
this._playerComponent = new ContinuousPlayer(attrs);
}
this._cacheAttrs = attrs;
this._playerComponent.name = `player`;
container.add(this._playerComponent as unknown as INode);
this._initEvent();
}
};
/**
* 计算起点
*/
private _computeLayoutRect(rect: ILayoutRect, width: number, height: number) {
// don't set bounds when player hidden
if (this._visible === false) {
return { x1: 0, x2: 0, y1: 0, y2: 0 };
}
// set bounds by 4 kinds of orient
switch (this._orient) {
case 'top': {
return { x1: 0, y1: 0, x2: width, y2: height };
}
case 'right': {
return { x1: rect.width - width, y1: 0, x2: rect.width, y2: rect.height };
}
case 'left': {
return { x1: 0, y1: 0, x2: width, y2: height };
}
case 'bottom':
default: {
return { x1: 0, y1: rect.height - height, x2: rect.width, y2: rect.height };
}
}
}
/**
* 计算组件宽度
*/
private _computeWidth(rect: ILayoutRect) {
// 若设置的是数值则直接返回
if (isNumber(this._spec.width)) {
return Math.min(rect.width, Number(this._spec.width));
}
if (isVertical(this._orient)) {
return this._maxSize();
}
return rect.width;
}
/**
* 计算组件高度
*/
private _computeHeight(rect: ILayoutRect) {
// 若设置的是数值则直接返回
if (isNumber(this._spec.height)) {
this._height = this._spec.height;
return Math.min(rect.height, Number(this._spec.height));
}
if (isHorizontal(this._orient)) {
return this._maxSize();
}
return rect.height;
}
/**
* 计算x方向的偏移, 用于实现对齐
*/
private _computeDx(rect: ILayoutRect) {
// 垂直时, x不偏移
if (isVertical(this._orient)) {
return 0;
}
// start
if (this._position === 'start') {
return 0;
}
// middle
else if (this._position === 'middle') {
return (rect.width - this._width) / 2;
}
// end
return rect.width - this._width;
}
/**
* 计算y方向的偏移, 用于实现对齐
*/
private _computeDy(rect: ILayoutRect) {
// 水平时, y不偏移
if (isHorizontal(this._orient)) {
return 0;
}
// start
if (this._position === 'start') {
return 0;
}
// middle
else if (this._position === 'middle') {
return (rect.height - this._height) / 2;
}
// end
return rect.height - this._height;
}
/**
* 播放器宽度取计算子组件中最高的一个
*/
private _maxSize = () => {
return Math.max(
...array(this._spec.controller.start?.style?.size),
...array(this._spec.controller.pause?.style?.size),
...array(this._spec.controller.backward?.style?.size),
...array(this._spec.controller.forward?.style?.size),
(isVertical(this._orient) ? this._spec.slider.railStyle.width : this._spec.slider.railStyle.height) ?? 10
);
};
/**
* 滑动条超过按钮的高度
*/
private _sliderExceededSize = () => {
const sliderHeight =
(isVertical(this._orient) ? this._spec.slider.railStyle.width : this._spec.slider.railStyle.height) ?? 10;
const controllersHeight = Math.max(
...array(this._spec.controller.start?.style?.size),
...array(this._spec.controller.pause?.style?.size),
...array(this._spec.controller.backward?.style?.size),
...array(this._spec.controller.forward?.style?.size)
);
if (sliderHeight >= controllersHeight) {
return sliderHeight - controllersHeight;
}
return 0;
};
changePlayerIndex(index: number) {
const spec = this._specs?.[index];
if (!spec || !this._option?.globalInstance) {
return;
}
this._option.globalInstance.updateFullData((spec as any).data);
this.event.emit(ChartEvent.playerChange, {
model: this,
value: {
spec: spec,
index: index,
specs: this._specs
}
});
}
autoPlayCallback = () => {
if (this._spec?.auto && this._playerComponent) {
this._playerComponent.pause();
this._playerComponent.play();
}
};
/**
* 事件
*/
private _initEvent = () => {
if (this._option.disableTriggerEvent) {
return;
}
// 自动播放
this._option.globalInstance.off(ChartEvent.rendered, this.autoPlayCallback);
// 自动播放
this._option.globalInstance.on(ChartEvent.rendered, this.autoPlayCallback);
// 循环播放 与 交替方向
this._playerComponent.addEventListener(PlayerEventEnum.end, () => {
this.event.emit(ChartEvent.playerEnd, { model: this });
// 交替方向, 仅离散轴支持
if (this._alternate && this._spec.type === 'discrete') {
this._direction = this._direction === 'default' ? 'reverse' : 'default';
this._playerComponent.setAttributes({
direction: this._direction,
dataIndex: this._direction === 'reverse' ? this._specs.length - 2 : 1
});
}
// 循环播放
if (this._spec?.loop) {
this._playerComponent.play();
}
});
// 数据更新
this._playerComponent.addEventListener(PlayerEventEnum.change, (e: { detail: { index: number } }) => {
// 更新data
const { index } = e.detail;
this.changePlayerIndex(index);
});
// 后退
this._playerComponent.addEventListener(PlayerEventEnum.backward, (e: { detail: { index: number } }) => {
const { index } = e.detail;
const spec = this._specs[index];
this.event.emit(ChartEvent.playerBackward, {
model: this,
value: {
spec: spec,
index: index,
specs: this._specs
}
});
});
// 前进
this._playerComponent.addEventListener(PlayerEventEnum.forward, (e: { detail: { index: number } }) => {
const { index } = e.detail;
const spec = this._specs[index];
this.event.emit(ChartEvent.playerForward, {
model: this,
value: {
spec: spec,
index: index,
specs: this._specs
}
});
});
// 播放
this._playerComponent.addEventListener(PlayerEventEnum.play, (e: { detail: { index: number } }) => {
const { index } = e.detail;
const spec = this._specs[index];
this.event.emit(ChartEvent.playerPlay, {
model: this,
value: {
spec: spec,
index: index,
specs: this._specs
}
});
});
// 暂停
this._playerComponent.addEventListener(PlayerEventEnum.pause, (e: { detail: { index: number } }) => {
const { index } = e.detail;
const spec = this._specs[index];
this.event.emit(ChartEvent.playerPause, {
model: this,
value: {
spec: spec,
index: index,
specs: this._specs
}
});
});
};
release(): void {
this._playerComponent?.pause?.();
// 自动播放
this._option?.globalInstance?.off(ChartEvent.rendered, this.autoPlayCallback);
super.release();
this._playerComponent = null as unknown as DiscretePlayer | ContinuousPlayer;
this._cacheAttrs = null as unknown as ContinuousPlayerAttributes | DiscretePlayerAttributes;
this._specs = [];
}
}
export const registerPlayer = () => {
Factory.registerComponent(Player.type, Player);
};