UNPKG

2.63 kBPlain TextView Raw
1import {Bean, PostConstruct} from "../context/context";
2import {Autowired} from "../context/context";
3import {NumberSequence, Utils as _} from '../utils';
4import {GridCell} from "../entities/gridCell";
5import {GridOptionsWrapper} from "../gridOptionsWrapper";
6import {CellComp} from "../rendering/cellComp";
7
8@Bean('mouseEventService')
9export class MouseEventService {
10
11 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
12 @Autowired('eGridDiv') private eGridDiv: HTMLElement;
13
14 private static gridInstanceSequence = new NumberSequence();
15 private static GRID_DOM_KEY = '__ag_grid_instance';
16
17 private gridInstanceId = MouseEventService.gridInstanceSequence.next();
18
19 @PostConstruct
20 private init(): void {
21 this.stampDomElementWithGridInstance();
22 }
23
24 // we put the instance id onto the main DOM element. this is used for events, when grids are inside grids,
25 // so the grid can work out if the even came from this grid or a grid inside this one. see the ctrl+v logic
26 // for where this is used.
27 private stampDomElementWithGridInstance(): void {
28 (<any>this.eGridDiv)[MouseEventService.GRID_DOM_KEY] = this.gridInstanceId;
29 }
30
31 public getRenderedCellForEvent(event: Event): CellComp {
32
33 let sourceElement = _.getTarget(event);
34
35 while (sourceElement) {
36 let renderedCell = this.gridOptionsWrapper.getDomData(sourceElement, CellComp.DOM_DATA_KEY_CELL_COMP);
37 if (renderedCell) {
38 return <CellComp> renderedCell;
39 }
40 sourceElement = sourceElement.parentElement;
41 }
42
43 return null;
44 }
45
46 // walks the path of the event, and returns true if this grid is the first one that it finds. if doing
47 // master / detail grids, and a child grid is found, then it returns false. this stops things like copy/paste
48 // getting executed on many grids at the same time.
49 public isEventFromThisGrid(event: MouseEvent | KeyboardEvent): boolean {
50
51 let path = _.getEventPath(event);
52
53 for (let i = 0; i<path.length; i++) {
54 let element = path[i];
55 let instanceId = (<any>element)[MouseEventService.GRID_DOM_KEY];
56 if (_.exists(instanceId)) {
57 let eventFromThisGrid = instanceId === this.gridInstanceId;
58 return eventFromThisGrid;
59 }
60 }
61
62 return false;
63 }
64
65 public getGridCellForEvent(event: MouseEvent | KeyboardEvent): GridCell {
66 let cellComp = this.getRenderedCellForEvent(event);
67 return cellComp ? cellComp.getGridCell() : null;
68 }
69
70}