1 | import {Bean, PreDestroy, Autowired, PostConstruct, Optional} from "../context/context";
|
2 | import {LoggerFactory, Logger} from "../logger";
|
3 | import {Utils as _} from "../utils";
|
4 | import {EventService} from "../eventService";
|
5 | import {DragStartedEvent, DragStoppedEvent, Events} from "../events";
|
6 | import {GridOptionsWrapper} from "../gridOptionsWrapper";
|
7 | import {ColumnApi} from "../columnController/columnApi";
|
8 | import {GridApi} from "../gridApi";
|
9 |
|
10 |
|
11 |
|
12 | @Bean('dragService')
|
13 | export class DragService {
|
14 |
|
15 | @Autowired('loggerFactory') private loggerFactory: LoggerFactory;
|
16 | @Autowired('eventService') private eventService: EventService;
|
17 | @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
|
18 | @Autowired('columnApi') private columnApi: ColumnApi;
|
19 | @Autowired('gridApi') private gridApi: GridApi;
|
20 |
|
21 | private currentDragParams: DragListenerParams;
|
22 | private dragging: boolean;
|
23 | private mouseEventLastTime: MouseEvent;
|
24 | private mouseStartEvent: MouseEvent;
|
25 | private touchLastTime: Touch;
|
26 | private touchStart: Touch;
|
27 |
|
28 | private onMouseUpListener = this.onMouseUp.bind(this);
|
29 | private onMouseMoveListener = this.onMouseMove.bind(this);
|
30 |
|
31 | private onTouchEndListener = this.onTouchUp.bind(this);
|
32 | private onTouchMoveListener = this.onTouchMove.bind(this);
|
33 |
|
34 | private logger: Logger;
|
35 |
|
36 | private dragEndFunctions: Function[] = [];
|
37 |
|
38 | private dragSources: DragSourceAndListener[] = [];
|
39 |
|
40 | @PostConstruct
|
41 | private init(): void {
|
42 | this.logger = this.loggerFactory.create('DragService');
|
43 | }
|
44 |
|
45 | @PreDestroy
|
46 | private destroy(): void {
|
47 | this.dragSources.forEach( this.removeListener.bind(this) );
|
48 | this.dragSources.length = 0;
|
49 | }
|
50 |
|
51 | private removeListener(dragSourceAndListener: DragSourceAndListener): void {
|
52 | let element = dragSourceAndListener.dragSource.eElement;
|
53 | let mouseDownListener = dragSourceAndListener.mouseDownListener;
|
54 | element.removeEventListener('mousedown', mouseDownListener);
|
55 |
|
56 |
|
57 | if (dragSourceAndListener.touchEnabled) {
|
58 | let touchStartListener = dragSourceAndListener.touchStartListener;
|
59 | element.removeEventListener('touchstart', touchStartListener, <any>{passive:true});
|
60 | }
|
61 | }
|
62 |
|
63 | public removeDragSource(params: DragListenerParams): void {
|
64 | let dragSourceAndListener = _.find( this.dragSources, item => item.dragSource === params);
|
65 |
|
66 | if (!dragSourceAndListener) { return; }
|
67 |
|
68 | this.removeListener(dragSourceAndListener);
|
69 | _.removeFromArray(this.dragSources, dragSourceAndListener);
|
70 | }
|
71 |
|
72 | private setNoSelectToBody(noSelect: boolean): void {
|
73 | let usrDocument = this.gridOptionsWrapper.getDocument();
|
74 | let eBody = <HTMLElement> usrDocument.querySelector('body');
|
75 | if (_.exists(eBody)) {
|
76 | _.addOrRemoveCssClass(eBody, 'ag-body-no-select', noSelect);
|
77 | }
|
78 | }
|
79 |
|
80 | public addDragSource(params: DragListenerParams, includeTouch: boolean = false): void {
|
81 |
|
82 | let mouseListener = this.onMouseDown.bind(this, params);
|
83 | params.eElement.addEventListener('mousedown', mouseListener);
|
84 |
|
85 | let touchListener: (touchEvent: TouchEvent)=>void = null;
|
86 |
|
87 | let suppressTouch = this.gridOptionsWrapper.isSuppressTouch();
|
88 |
|
89 | let reallyIncludeTouch = includeTouch && !suppressTouch;
|
90 |
|
91 | if (reallyIncludeTouch) {
|
92 | touchListener = this.onTouchStart.bind(this, params);
|
93 | params.eElement.addEventListener('touchstart', touchListener, <any>{passive:false});
|
94 | }
|
95 |
|
96 | this.dragSources.push({
|
97 | dragSource: params,
|
98 | mouseDownListener: mouseListener,
|
99 | touchStartListener: touchListener,
|
100 | touchEnabled: includeTouch
|
101 | });
|
102 | }
|
103 |
|
104 |
|
105 | private onTouchStart(params: DragListenerParams, touchEvent: TouchEvent): void {
|
106 |
|
107 | this.currentDragParams = params;
|
108 | this.dragging = false;
|
109 |
|
110 | let touch = touchEvent.touches[0];
|
111 |
|
112 | this.touchLastTime = touch;
|
113 | this.touchStart = touch;
|
114 |
|
115 | touchEvent.preventDefault();
|
116 |
|
117 |
|
118 |
|
119 | params.eElement.addEventListener('touchmove', this.onTouchMoveListener, <any>{passive:true});
|
120 | params.eElement.addEventListener('touchend', this.onTouchEndListener, <any>{passive:true});
|
121 | params.eElement.addEventListener('touchcancel', this.onTouchEndListener, <any>{passive:true});
|
122 |
|
123 | this.dragEndFunctions.push( ()=> {
|
124 | params.eElement.removeEventListener('touchmove', this.onTouchMoveListener, <any>{passive:true});
|
125 | params.eElement.removeEventListener('touchend', this.onTouchEndListener, <any>{passive:true});
|
126 | params.eElement.removeEventListener('touchcancel', this.onTouchEndListener, <any>{passive:true});
|
127 | });
|
128 |
|
129 |
|
130 | if (params.dragStartPixels===0) {
|
131 | this.onCommonMove(touch, this.touchStart);
|
132 | }
|
133 | }
|
134 |
|
135 |
|
136 | private onMouseDown(params: DragListenerParams, mouseEvent: MouseEvent): void {
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | if (params.skipMouseEvent) {
|
142 | if (params.skipMouseEvent(mouseEvent)) { return; }
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | if ((<any>mouseEvent)._alreadyProcessedByDragService) { return; }
|
149 | (<any>mouseEvent)._alreadyProcessedByDragService = true;
|
150 |
|
151 |
|
152 | if (mouseEvent.button!==0) { return; }
|
153 |
|
154 | this.currentDragParams = params;
|
155 | this.dragging = false;
|
156 |
|
157 | this.mouseEventLastTime = mouseEvent;
|
158 | this.mouseStartEvent = mouseEvent;
|
159 |
|
160 | let usrDocument = this.gridOptionsWrapper.getDocument();
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | usrDocument.addEventListener('mousemove', this.onMouseMoveListener);
|
166 | usrDocument.addEventListener('mouseup', this.onMouseUpListener);
|
167 |
|
168 | this.dragEndFunctions.push( ()=> {
|
169 | usrDocument.removeEventListener('mousemove', this.onMouseMoveListener);
|
170 | usrDocument.removeEventListener('mouseup', this.onMouseUpListener);
|
171 | });
|
172 |
|
173 |
|
174 | if (params.dragStartPixels===0) {
|
175 | this.onMouseMove(mouseEvent);
|
176 | }
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 | private isEventNearStartEvent(currentEvent: MouseEvent|Touch, startEvent: MouseEvent|Touch): boolean {
|
182 |
|
183 | let requiredPixelDiff = _.exists(this.currentDragParams.dragStartPixels) ? this.currentDragParams.dragStartPixels : 4;
|
184 | return _.areEventsNear(currentEvent, startEvent, requiredPixelDiff);
|
185 | }
|
186 |
|
187 | private getFirstActiveTouch(touchList: TouchList): Touch {
|
188 | for (let i = 0; i<touchList.length; i++) {
|
189 | let matches = touchList[i].identifier === this.touchStart.identifier;
|
190 | if (matches) {
|
191 | return touchList[i];
|
192 | }
|
193 | }
|
194 |
|
195 | return null;
|
196 | }
|
197 |
|
198 | private onCommonMove(currentEvent: MouseEvent|Touch, startEvent: MouseEvent|Touch): void {
|
199 |
|
200 | if (!this.dragging) {
|
201 |
|
202 | let toEarlyToDrag = !this.dragging && this.isEventNearStartEvent(currentEvent, startEvent);
|
203 | if (toEarlyToDrag) {
|
204 | return;
|
205 | } else {
|
206 |
|
207 | this.dragging = true;
|
208 | let event: DragStartedEvent = {
|
209 | type: Events.EVENT_DRAG_STARTED,
|
210 | api: this.gridApi,
|
211 | columnApi: this.columnApi
|
212 | };
|
213 | this.eventService.dispatchEvent(event);
|
214 | this.currentDragParams.onDragStart(startEvent);
|
215 | this.setNoSelectToBody(true);
|
216 | }
|
217 | }
|
218 |
|
219 | this.currentDragParams.onDragging(currentEvent);
|
220 | }
|
221 |
|
222 | private onTouchMove(touchEvent: TouchEvent): void {
|
223 |
|
224 | let touch = this.getFirstActiveTouch(touchEvent.touches);
|
225 | if (!touch) { return; }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | this.onCommonMove(touch, this.touchStart);
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 | private onMouseMove(mouseEvent: MouseEvent): void {
|
240 | this.onCommonMove(mouseEvent, this.mouseStartEvent);
|
241 | }
|
242 |
|
243 | public onTouchUp(touchEvent: TouchEvent): void {
|
244 | let touch = this.getFirstActiveTouch(touchEvent.changedTouches);
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | if (!touch) {
|
252 | touch = this.touchLastTime;
|
253 | }
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | this.onUpCommon(touch);
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | }
|
268 |
|
269 | public onMouseUp(mouseEvent: MouseEvent): void {
|
270 | this.onUpCommon(mouseEvent);
|
271 | }
|
272 |
|
273 | public onUpCommon(eventOrTouch: MouseEvent|Touch): void {
|
274 |
|
275 | if (this.dragging) {
|
276 | this.dragging = false;
|
277 | this.currentDragParams.onDragStop(eventOrTouch);
|
278 | let event: DragStoppedEvent = {
|
279 | type: Events.EVENT_DRAG_STOPPED,
|
280 | api: this.gridApi,
|
281 | columnApi: this.columnApi
|
282 | };
|
283 | this.eventService.dispatchEvent(event);
|
284 | }
|
285 |
|
286 | this.setNoSelectToBody(false);
|
287 |
|
288 | this.mouseStartEvent = null;
|
289 | this.mouseEventLastTime = null;
|
290 | this.touchStart = null;
|
291 | this.touchLastTime = null;
|
292 | this.currentDragParams = null;
|
293 |
|
294 | this.dragEndFunctions.forEach( func => func() );
|
295 | this.dragEndFunctions.length = 0;
|
296 | }
|
297 | }
|
298 |
|
299 | interface DragSourceAndListener {
|
300 | dragSource: DragListenerParams;
|
301 | mouseDownListener: (mouseEvent: MouseEvent)=>void;
|
302 | touchEnabled: boolean;
|
303 | touchStartListener: (touchEvent: TouchEvent)=>void;
|
304 | }
|
305 |
|
306 | export interface DragListenerParams {
|
307 |
|
308 | dragStartPixels?: number;
|
309 |
|
310 | eElement: HTMLElement;
|
311 |
|
312 | skipMouseEvent?: (mouseEvent: MouseEvent) => boolean;
|
313 |
|
314 | onDragStart: (mouseEvent: MouseEvent|Touch) => void;
|
315 |
|
316 | onDragStop: (mouseEvent: MouseEvent|Touch) => void;
|
317 |
|
318 | onDragging: (mouseEvent: MouseEvent|Touch) => void;
|
319 | } |
\ | No newline at end of file |