-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathresize-handle.directive.ts
186 lines (155 loc) · 6.31 KB
/
resize-handle.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { DOCUMENT } from '@angular/common';
import { Directive, ElementRef, EventEmitter, HostListener, Inject, Input, NgZone, Output, Renderer2 } from '@angular/core';
import { fromEvent, Subscription } from 'rxjs';
@Directive({
selector: '[dResizeHandle]',
})
export class ResizeHandleDirective {
@Input() containerElement: Element;
@Input() minWidth: string;
@Input() maxWidth: string;
@Output() resizeStartEvent: EventEmitter<any> = new EventEmitter<any>();
@Output() resizingEvent: EventEmitter<any> = new EventEmitter<any>();
@Output() resizeEndEvent: EventEmitter<any> = new EventEmitter<any>();
@Output() collapseEvent: EventEmitter<any> = new EventEmitter<any>();
element: HTMLElement;
moveCount: number;
initialWidth: number;
totalWidth: number;
mouseDownScreenX: number;
resizeHandle: HTMLElement;
resizeOverlay: HTMLElement;
resizeBarRefElement: HTMLElement;
mouseUpSubscription: Subscription;
resizeHandleEnter: any;
resizeHandleLeave: any;
resizeHandleClick: any;
preventRemoveHandle = false;
document: Document;
constructor(element: ElementRef, private renderer2: Renderer2, private zone: NgZone, @Inject(DOCUMENT) private doc: any) {
this.element = element.nativeElement;
this.document = this.doc;
}
@HostListener('mouseenter', ['$event'])
onMouseEnter(event: MouseEvent) {
if (!this.resizeHandle) {
this.resizeHandle = this.renderer2.createElement('div');
this.renderer2.appendChild(this.containerElement, this.resizeHandle);
this.renderer2.addClass(this.resizeHandle, 'resize-handle');
const left = this.element.getBoundingClientRect().right - this.containerElement.getBoundingClientRect().left;
this.renderer2.setStyle(this.resizeHandle, 'left', left + 'px');
this.resizeHandleEnter = this.renderer2.listen(this.resizeHandle, 'mouseenter', this.onHandleMouseEnter.bind(this));
this.resizeHandleLeave = this.renderer2.listen(this.resizeHandle, 'mouseleave', this.onHandleMouseLeave.bind(this));
this.resizeHandleClick = this.renderer2.listen(this.resizeHandle, 'mousedown', this.onMousedown.bind(this));
}
}
@HostListener('mouseleave', ['$event'])
onMouseLeave(event: any) {
setTimeout(() => {
if (!this.preventRemoveHandle) {
if (this.resizeHandle) {
this.renderer2.removeChild(this.containerElement, this.resizeHandle);
this.resizeHandle = null;
}
if (this.resizeHandleClick) {
this.resizeHandleClick();
}
}
}, 100);
}
private onHandleMouseEnter() {
this.preventRemoveHandle = true;
}
private onHandleMouseLeave() {
this.preventRemoveHandle = false;
if (this.resizeHandle) {
this.renderer2.removeChild(this.containerElement, this.resizeHandle);
this.resizeHandle = null;
}
if (this.resizeHandleClick) {
this.resizeHandleClick();
}
}
onMousedown(event: MouseEvent): void {
this.moveCount = 0;
this.resizeStartEvent.emit(event); // emit begin resize event
this.initialWidth = this.element.clientWidth;
const initialOffset = this.element.getBoundingClientRect().left - this.containerElement.getBoundingClientRect().left;
this.mouseDownScreenX = event.clientX;
event.stopPropagation();
// create resizeOverlay
this.resizeOverlay = this.renderer2.createElement('div');
this.renderer2.appendChild(this.containerElement, this.resizeOverlay);
this.renderer2.addClass(this.resizeOverlay, 'resize-overlay');
this.renderer2.listen(this.resizeOverlay, 'click', (clickEvent: Event) => clickEvent.stopPropagation());
const resizeBar = this.renderer2.createElement('div');
this.renderer2.addClass(resizeBar, 'resize-bar');
this.resizeBarRefElement = resizeBar;
this.renderer2.appendChild(this.containerElement, resizeBar);
this.renderer2.setStyle(this.resizeBarRefElement, 'display', 'block');
this.renderer2.setStyle(this.resizeBarRefElement, 'left', initialOffset + this.initialWidth + 'px');
const mouseup = fromEvent(document, 'mouseup');
this.mouseUpSubscription = mouseup.subscribe((ev: MouseEvent) => this.onMouseup(ev));
this.zone.runOutsideAngular(() => {
this.document.addEventListener('mousemove', this.bindMousemove);
});
}
onMouseup(event: MouseEvent): void {
const movementX = event.clientX - this.mouseDownScreenX;
const newWidth = this.initialWidth + movementX;
const finalWidth = this.getFinalWidth(newWidth);
// destroy overlay
this.renderer2.removeChild(this.element, this.resizeOverlay);
this.renderer2.removeChild(this.containerElement, this.resizeBarRefElement);
this.resizeEndEvent.emit({ width: finalWidth });
if (this.mouseUpSubscription && !this.mouseUpSubscription.closed) {
this._destroySubscription();
}
this.document.removeEventListener('mousemove', this.bindMousemove);
}
bindMousemove = (e) => {
this.move(e);
};
move(event: MouseEvent): void {
this.moveCount++;
if (this.moveCount % 2 === 0) {
return;
}
const movementX = event.clientX - this.mouseDownScreenX;
const newWidth = this.initialWidth + movementX;
const finalWidth = this.getFinalWidth(newWidth);
this.renderer2.setStyle(
this.resizeBarRefElement,
'left',
`${finalWidth + this.element.getBoundingClientRect().left - this.containerElement.getBoundingClientRect().left}px`
);
this.resizingEvent.emit({ width: finalWidth });
}
private getFinalWidth(newWidth: number): number {
const minWidth = this.handleWidth(this.minWidth);
const maxWidth = this.handleWidth(this.maxWidth);
const overMinWidth = !this.minWidth || newWidth >= minWidth;
const underMaxWidth = !this.maxWidth || newWidth <= maxWidth;
const finalWidth = !overMinWidth ? minWidth : !underMaxWidth ? maxWidth : newWidth;
return finalWidth;
}
private handleWidth(width: string | number) {
if (!width) {
return;
}
if (typeof width === 'number') {
return width;
}
if (width.includes('%')) {
const tableWidth = this.containerElement.clientWidth;
return (tableWidth * parseInt(width, 10)) / 100;
}
return parseInt(width.replace(/[^\d]+/, ''), 10);
}
private _destroySubscription() {
if (this.mouseUpSubscription) {
this.mouseUpSubscription.unsubscribe();
this.mouseUpSubscription = undefined;
}
}
}