1 | import {GridOptionsWrapper} from "../gridOptionsWrapper";
|
2 | import {Autowired, Bean} from "../context/context";
|
3 | import {DragListenerParams, DragService} from "../dragAndDrop/dragService";
|
4 |
|
5 | export 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')
|
13 | export 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 |
|
41 |
|
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 |
|
63 | this.eGridDiv.style.cursor = 'col-resize';
|
64 |
|
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 |
|
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 | }
|