UNPKG

4.98 kBPlain TextView Raw
1import {DragAndDropService, DraggingEvent, DragSourceType, DropTarget} from "../dragAndDrop/dragAndDropService";
2import {Autowired, Context, PostConstruct} from "../context/context";
3import {MoveColumnController} from "./moveColumnController";
4import {Column} from "../entities/column";
5import {GridPanel} from "../gridPanel/gridPanel";
6import {BodyDropPivotTarget} from "./bodyDropPivotTarget";
7import {ColumnController} from "../columnController/columnController";
8
9export interface DropListener {
10 getIconName(): string;
11 onDragEnter(params: DraggingEvent): void;
12 onDragLeave(params: DraggingEvent): void;
13 onDragging(params: DraggingEvent): void;
14 onDragStop(params: DraggingEvent): void;
15}
16
17enum DropType { ColumnMove, Pivot }
18
19export class BodyDropTarget implements DropTarget {
20
21 @Autowired('context') private context: Context;
22 @Autowired('dragAndDropService') private dragAndDropService: DragAndDropService;
23 @Autowired('columnController') private columnController: ColumnController;
24
25 private gridPanel: GridPanel;
26
27 private pinned: string;
28
29 // public because it's part of the DropTarget interface
30 private eContainer: HTMLElement;
31
32 // public because it's part of the DropTarget interface
33 private eSecondaryContainers: HTMLElement[];
34
35 private dropListeners: {[type: number]: DropListener} = {};
36
37 private currentDropListener: DropListener;
38
39 private moveColumnController: MoveColumnController;
40
41 constructor(pinned: string, eContainer: HTMLElement) {
42 this.pinned = pinned;
43 this.eContainer = eContainer;
44 }
45
46 public registerGridComp(gridPanel: GridPanel): void {
47 this.gridPanel = gridPanel;
48
49 this.moveColumnController.registerGridComp(gridPanel);
50
51 switch (this.pinned) {
52 case Column.PINNED_LEFT: this.eSecondaryContainers = this.gridPanel.getDropTargetLeftContainers(); break;
53 case Column.PINNED_RIGHT: this.eSecondaryContainers = this.gridPanel.getDropTargetRightContainers(); break;
54 default: this.eSecondaryContainers = this.gridPanel.getDropTargetBodyContainers(); break;
55 }
56 }
57
58 public isInterestedIn(type: DragSourceType): boolean {
59 // not interested in row drags
60 return type === DragSourceType.HeaderCell || type === DragSourceType.ToolPanel;
61 }
62
63 public getSecondaryContainers(): HTMLElement[] {
64 return this.eSecondaryContainers;
65 }
66
67 public getContainer(): HTMLElement {
68 return this.eContainer;
69 }
70
71 @PostConstruct
72 private init(): void {
73
74 this.moveColumnController = new MoveColumnController(this.pinned, this.eContainer);
75 this.context.wireBean(this.moveColumnController);
76
77 let bodyDropPivotTarget = new BodyDropPivotTarget(this.pinned);
78 this.context.wireBean(bodyDropPivotTarget);
79
80 this.dropListeners[DropType.ColumnMove] = this.moveColumnController;
81 this.dropListeners[DropType.Pivot] = bodyDropPivotTarget;
82
83 this.dragAndDropService.addDropTarget(this);
84 }
85
86 public getIconName(): string {
87 return this.currentDropListener.getIconName();
88 }
89
90 // we want to use the bodyPivotTarget if the user is dragging columns in from the toolPanel
91 // and we are in pivot mode, as it has to logic to set pivot/value/group on the columns when
92 // dropped into the grid's body.
93 private getDropType(draggingEvent: DraggingEvent): DropType {
94
95 if (this.columnController.isPivotMode()) {
96 // in pivot mode, then if moving a column (ie didn't come from toolpanel) then it's
97 // a standard column move, however if it came from teh toolpanel, then we are introducing
98 // dimensions or values to the grid
99 if (draggingEvent.dragSource.type === DragSourceType.ToolPanel) {
100 return DropType.Pivot;
101 } else {
102 return DropType.ColumnMove;
103 }
104 } else {
105 // it's a column, and not pivot mode, so always moving
106 return DropType.ColumnMove;
107 }
108 }
109
110 public onDragEnter(draggingEvent: DraggingEvent): void {
111 // we pick the drop listener depending on whether we are in pivot mode are not. if we are
112 // in pivot mode, then dropping cols changes the row group, pivot, value stats. otherwise
113 // we change visibility state and position.
114
115 // if (this.columnController.isPivotMode()) {
116 let dropType: DropType = this.getDropType(draggingEvent);
117 this.currentDropListener = this.dropListeners[dropType];
118
119 this.currentDropListener.onDragEnter(draggingEvent);
120 }
121
122 public onDragLeave(params: DraggingEvent): void {
123 this.currentDropListener.onDragLeave(params);
124 }
125
126 public onDragging(params: DraggingEvent): void {
127 this.currentDropListener.onDragging(params);
128 }
129
130 public onDragStop(params: DraggingEvent): void {
131 this.currentDropListener.onDragStop(params);
132 }
133
134}
\No newline at end of file