UNPKG

1.05 kBPlain TextView Raw
1import EE from '@antv/event-emitter';
2
3interface BaseCfg {
4 visible?: boolean;
5}
6
7/**
8 * G2 Chart、View、Geometry 以及 Element 等的基类,提供事件以及一些通用的方法。
9 */
10export default class Base extends EE {
11 /** 是否可见 */
12 public visible: boolean;
13 /** 标识对象是否已销毁 */
14 public destroyed: boolean = false;
15
16 constructor(cfg: BaseCfg) {
17 super();
18 const { visible = true } = cfg;
19 this.visible = visible;
20 }
21
22 /**
23 * 显示。
24 */
25 public show() {
26 const visible = this.visible;
27 if (!visible) {
28 this.changeVisible(true);
29 }
30 }
31
32 /**
33 * 隐藏。
34 */
35 public hide() {
36 const visible = this.visible;
37 if (visible) {
38 this.changeVisible(false);
39 }
40 }
41
42 /**
43 * 销毁。
44 */
45 public destroy() {
46 this.off();
47 this.destroyed = true;
48 }
49
50 /**
51 * 显示或者隐藏。
52 * @param visible
53 * @returns
54 */
55 public changeVisible(visible: boolean) {
56 if (this.visible === visible) {
57 return;
58 }
59 this.visible = visible;
60 }
61}