UNPKG

3.36 kBPlain TextView Raw
1import {GridOptionsWrapper} from "../gridOptionsWrapper";
2import {Autowired, Bean} from "../context/context";
3import {DragListenerParams, DragService} from "../dragAndDrop/dragService";
4
5export interface HorizontalResizeParams {
6 eResizeBar: HTMLElement;
7 onResizeStart: (shiftKey: boolean)=>void;
8 onResizing: (delta: number)=>void;
9 onResizeEnd: (delta: number)=>void;
10}
11
12@Bean('horizontalResizeService')
13export class HorizontalResizeService {
14
15 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
16 @Autowired('dragService') private dragService: DragService;
17 @Autowired('eGridDiv') private eGridDiv: HTMLElement;
18
19 private draggingStarted: boolean;
20
21 private dragStartX: number;
22
23 private resizeAmount: number;
24
25 private oldBodyCursor: string;
26 private oldMsUserSelect: string;
27 private oldWebkitUserSelect: string;
28
29 public addResizeBar(params: HorizontalResizeParams): ()=>void {
30 let dragSource: DragListenerParams = {
31 dragStartPixels: 0,
32 eElement: params.eResizeBar,
33 onDragStart: this.onDragStart.bind(this, params),
34 onDragStop: this.onDragStop.bind(this, params),
35 onDragging: this.onDragging.bind(this, params)
36 };
37
38 this.dragService.addDragSource(dragSource, true);
39
40 // we pass remove func back to the caller, so call can tell us when they
41 // are finished, and then we remove the listener from the drag source
42 let finishedWithResizeFunc = ()=> this.dragService.removeDragSource(dragSource);
43
44 return finishedWithResizeFunc;
45 }
46
47 private onDragStart(params: HorizontalResizeParams, mouseEvent: MouseEvent|Touch): void {
48 this.draggingStarted = true;
49 this.dragStartX = mouseEvent.clientX;
50
51 this.setResizeIcons();
52
53 let shiftKey = mouseEvent instanceof MouseEvent ? (<MouseEvent>mouseEvent).shiftKey === true : false;
54 params.onResizeStart(shiftKey);
55 }
56
57 private setResizeIcons(): void {
58 this.oldBodyCursor = this.eGridDiv.style.cursor;
59 this.oldMsUserSelect = this.eGridDiv.style.msUserSelect;
60 this.oldWebkitUserSelect = this.eGridDiv.style.webkitUserSelect;
61
62 // change the body cursor, so when drag moves out of the drag bar, the cursor is still 'resize' (or 'move'
63 this.eGridDiv.style.cursor = 'col-resize';
64 // we don't want text selection outside the grid (otherwise it looks weird as text highlights when we move)
65 this.eGridDiv.style.msUserSelect = 'none';
66 this.eGridDiv.style.webkitUserSelect = 'none';
67 }
68
69 private onDragStop(params: HorizontalResizeParams, mouseEvent: MouseEvent|Touch): void {
70 params.onResizeEnd(this.resizeAmount);
71 this.resetIcons();
72 }
73
74 private resetIcons(): void {
75 // we don't want text selection outside the grid (otherwise it looks weird as text highlights when we move)
76 this.eGridDiv.style.cursor = this.oldBodyCursor;
77 this.eGridDiv.style.msUserSelect = this.oldMsUserSelect;
78 this.eGridDiv.style.webkitUserSelect = this.oldWebkitUserSelect;
79 }
80
81 private onDragging(params: HorizontalResizeParams, mouseEvent: MouseEvent|Touch): void {
82 this.resizeAmount = mouseEvent.clientX - this.dragStartX;
83 params.onResizing(this.resizeAmount);
84 }
85
86}