UNPKG

1.98 kBPlain TextView Raw
1import { Event as GEvent, IShape } from '../dependents';
2import { Datum } from '../interface';
3import View from './view';
4
5/**
6 * @todo Whether it can(or necessary to) keep consistent with the structure of G.Event or directly use the structure of G.Event
7 * G2 事件的事件包装类,基于 G.Event
8 */
9export default class Event {
10 /** 当前 target 归属的 view 实例 */
11 public view: View;
12 /** 被包装的原声 G 事件 */
13 public gEvent: GEvent;
14 /** 原始数据 */
15 public data?: Datum;
16 /** 事件类型 */
17 public type: string;
18
19 constructor(view: View, gEvent: GEvent, data?: Datum) {
20 this.view = view;
21 this.gEvent = gEvent;
22 this.data = data;
23 this.type = gEvent.type;
24 }
25
26 /**
27 * 非交互产生的事件
28 * @param view
29 * @param type
30 * @param data
31 */
32 public static fromData(view: View, type: string, data: Datum) {
33 return new Event(view, new GEvent(type, {}), data);
34 }
35
36 // below props are proxy props of G.event convenient
37
38 /** the real trigger shape of the event */
39 public get target(): IShape {
40 // @todo G 中事件定义为 object 不正确,这里先 ignore
41 // @ts-ignore
42 return this.gEvent.target;
43 }
44
45 /** 获取对应的 dom 原生时间 */
46 public get event(): any {
47 return this.gEvent.originalEvent;
48 }
49
50 /** x 画布坐标 */
51 public get x(): number {
52 return this.gEvent.x;
53 }
54
55 /** y 画布坐标 */
56 public get y(): number {
57 return this.gEvent.y;
58 }
59
60 /** x 窗口坐标 */
61 public get clientX(): number {
62 return this.gEvent.clientX;
63 }
64
65 /** y 窗口坐标 */
66 public get clientY(): number {
67 return this.gEvent.clientY;
68 }
69 // end for proxy events
70
71 /**
72 * event string
73 * @returns string
74 */
75 public toString(): string {
76 return `[Event (type=${this.type})]`;
77 }
78
79 /**
80 * clone a new event with same attributes
81 * @returns [[Event]]
82 */
83 public clone(): Event {
84 return new Event(this.view, this.gEvent, this.data);
85 }
86}