| 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 |
1×
1×
1×
1×
3×
1×
1×
3×
3×
3×
3×
3×
2×
36×
2×
36×
2×
36×
1×
15×
3×
3×
3×
3×
3×
3×
1×
6×
3×
3×
3×
1×
3×
1×
3×
1×
6×
6×
1×
15×
15×
1×
6×
6×
6×
6×
6×
6×
3×
3×
3×
3×
6×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
3×
1×
12×
12×
12×
6×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
1×
15×
18×
1×
3×
3×
3×
1×
3×
3×
3×
3×
3×
3×
3×
3×
1×
1×
2×
1×
1×
1×
1×
3×
3×
3×
3×
1×
18×
15×
6×
3×
15×
15×
15×
3×
3×
3×
3×
3×
1×
18×
9×
3×
3×
3×
1×
| /*
* @license
* Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
*
* Use of this source code is governed by an Apache-2.0 license that can be
* found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
*/
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, HostBinding, Input, OnChanges, OnDestroy, Output, Renderer, SimpleChanges, ViewEncapsulation } from '@angular/core';
import { IAreaData } from './area-data.model';
import { Point } from './point.model';
import { SplitAreaDirective } from './split-area.directive';
const gutterSize = 'gutterSize';
const disabled = 'disabled';
/**
* Splitter Component for Angular
*
* The splitter component allows to split horizontally or vertically, a container in N resizable part.
*/
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
selector: 'deja-splitter',
styles: [require('./splitter.component.scss')],
template: require('./splitter.component.html'),
})
export class DejaSplitterComponent implements OnChanges, OnDestroy {
/**
* Direction of the split
* Can be `horizontal` or `vertical`
*/
@Input() public direction = 'horizontal';
/**
* Width in percent of the component
* By default `100%`
*/
@Input() public width: number;
/**
* Height in percent of the component
* By default `100%`
*/
@Input() public height: number;
/**
* Separator width
*/
@Input() public gutterSize = 10;
/**
* Disable the component
*/
@Input() public disabled = false;
/**
* Event triggered when the user start to drag the cursor
*/
@Output() public dragStart = new EventEmitter<number[]>(false);
/**
* Event triggered during the cursor's drag
*/
@Output() public dragProgress = new EventEmitter<number[]>(false);
/**
* Event triggered when the user stop to drag the cursor
*/
@Output() public dragEnd = new EventEmitter<number[]>(false);
/**
* Host Binding
*/
@HostBinding('style.flex-direction') public get styleFlexDirection() {
return this.direction === 'horizontal' ? 'row' : 'column';
}
/**
* Host Binding
*/
@HostBinding('style.width') public get styleWidth() {
return (this.width && !isNaN(this.width) && this.width > 0) ? `${this.width}px` : '100%';
}
/**
* Host Binding
*/
@HostBinding('style.height') public get styleHeight() {
return (this.height && !isNaN(this.height) && this.height > 0) ? `${this.height}px` : '100%';
}
private get nbGutters(): number {
return this._areas.length - 1;
}
private _areas: IAreaData[] = [];
private isDragging = false;
private containerSize = 0;
private areaASize = 0;
private areaBSize = 0;
private eventsDragFct: Function[] = [];
public get areas() {
return this._areas;
}
/**
* Constructor
*/
constructor(private cdRef: ChangeDetectorRef,
private elementRef: ElementRef,
private renderer: Renderer) {
}
/**
* Lifecycle hook that is called when any data-bound property of a directive changes.
*/
public ngOnChanges(changes: SimpleChanges) {
Iif (changes[gutterSize] || changes[disabled]) {
this.refresh();
}
}
/**
* Lifecycle hook that is called when a directive, pipe or service is destroyed.
*/
public ngOnDestroy() {
this.stopDragging();
}
/**
* Add a new area into the component
* @param component Area to add
* @param orderUser Position of the new area into the component
* @param sizeUser Size of the new area
* @param minPixel Min size of the new area
*/
public addArea(component: SplitAreaDirective, orderUser: number | null, sizeUser: number | null, minPixel: number) {
this._areas.push({
component,
orderUser,
order: -1,
sizeUser,
size: -1,
minPixel,
});
this.refresh();
}
/**
* Update an existing area into the component
* @param component Area to update
* @param orderUser Position of the area into the component
* @param sizeUser Size of the area
* @param minPixel Min size of the area
*/
public updateArea(component: SplitAreaDirective, orderUser: number | null, sizeUser: number | null, minPixel: number) {
const item = this._areas.find((a) => a.component === component);
Iif (item) {
item.orderUser = orderUser;
item.sizeUser = sizeUser;
item.minPixel = minPixel;
this.refresh();
}
}
/**
* Delete an existing area into the component
* @param area Area to delete
*/
public removeArea(area: SplitAreaDirective) {
const item = this._areas.find((a) => a.component === area);
Eif (item) {
const index = this._areas.indexOf(item);
this._areas.splice(index, 1);
this._areas.forEach((a, i) => a.order = i * 2);
this.refresh();
}
}
/**
* Function called when the user start to drag the cursor
* @param startEvent drag event
* @param gutterOrder separator number
*/
public startDragging(startEvent: MouseEvent, gutterOrder: number) {
startEvent.preventDefault();
Iif (this.disabled) {
return;
}
const areaA = this._areas.find((a) => a.order === gutterOrder - 1);
const areaB = this._areas.find((a) => a.order === gutterOrder + 1);
Iif (!areaA || !areaB) {
return;
}
const prop = (this.direction === 'horizontal') ? 'offsetWidth' : 'offsetHeight';
this.containerSize = this.elementRef.nativeElement[prop];
this.areaASize = this.containerSize * areaA.size / 100;
this.areaBSize = this.containerSize * areaB.size / 100;
const start: Point = {
x: startEvent.screenX,
y: startEvent.screenY,
};
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'mousemove', (e: MouseEvent) => this.dragEvent(e, start, areaA, areaB)));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchmove', (e: MouseEvent) => this.dragEvent(e, start, areaA, areaB)));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'mouseup', () => this.stopDragging()));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchend', () => this.stopDragging()));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchcancel', () => this.stopDragging()));
areaA.component.lockEvents();
areaB.component.lockEvents();
this.isDragging = true;
this.notify('start');
}
public refresh() {
this.stopDragging();
// ORDERS: Set css 'order' property depending on user input or added order
const nbCorrectOrder = this._areas.filter((a) => a.orderUser !== null && !isNaN(a.orderUser)).length;
if (nbCorrectOrder === this._areas.length) {
this._areas.sort((a, b) => +a.orderUser - +b.orderUser);
}
this._areas.forEach((a, i) => {
a.order = i * 2;
a.component.setStyle('order', a.order);
});
// SIZES: Set css 'flex-basis' property depending on user input or equal sizes
const totalSize = this._areas.map((a) => a.sizeUser).reduce((acc, s) => acc + s, 0);
const toBeDefined = this._areas.filter((a) => !a.sizeUser || isNaN(a.sizeUser));
Iif ((totalSize < 99.99 || totalSize > 100.01) && this._areas.length > 1) {
if (toBeDefined.length === 0) {
// Map to 100%
this._areas.forEach((a) => {
const adjustedSize = Number(a.sizeUser) * 100 / totalSize;
a.size = adjustedSize;
});
} else if (totalSize < 99.99) {
// Share the remaining size to the undefined areas
let remain = (100 - totalSize);
let toBeDefinedLength = toBeDefined.length;
toBeDefined.forEach((a) => {
const size = remain / toBeDefinedLength--;
a.size = size;
remain -= size;
});
this._areas
.filter((a) => a.sizeUser && !isNaN(a.sizeUser))
.forEach((a) => a.size = Number(a.sizeUser));
} else {
const size = Number((100 / this._areas.length).toFixed(3));
this._areas.forEach((a) => a.size = size);
}
} else Iif (totalSize === 0 && this._areas.length === 1) {
this._areas[0].size = 100;
} else {
this._areas.forEach((a) => a.size = Number(a.sizeUser));
}
this.refreshStyleSizes();
this.cdRef.markForCheck();
}
private refreshStyleSizes() {
const f = this.gutterSize * this.nbGutters / this._areas.length;
this._areas.forEach((a) => a.component.setStyle('flex-basis', `calc( ${a.size}% - ${f}px )`));
}
private dragEvent(event: MouseEvent, start: Point, areaA: IAreaData, areaB: IAreaData) {
Iif (!this.isDragging) {
return;
}
const end: Point = {
x: event.screenX,
y: event.screenY,
};
this.drag(start, end, areaA, areaB);
}
private drag(start: Point, end: Point, areaA: IAreaData, areaB: IAreaData) {
const offsetPixel = (this.direction === 'horizontal') ? (start.x - end.x) : (start.y - end.y);
const newSizePixelA = this.areaASize - offsetPixel;
const newSizePixelB = this.areaBSize + offsetPixel;
const minPercentA = areaA.minPixel ? (areaA.minPixel + 5) / this.containerSize * 100 : 0;
const minPercentB = areaB.minPixel ? (areaB.minPixel + 5) / this.containerSize * 100 : 0;
let newSizePercentA = newSizePixelA / this.containerSize * 100;
let newSizePercentB = newSizePixelB / this.containerSize * 100;
if (newSizePercentA <= minPercentA) {
newSizePercentA = minPercentA;
newSizePercentB = areaA.size + areaB.size - minPercentA;
} else if (newSizePercentB <= minPercentB) {
newSizePercentB = minPercentB;
newSizePercentA = areaA.size + areaB.size - minPercentB;
} else {
newSizePercentA = Number(newSizePercentA.toFixed(3));
newSizePercentB = Number((areaA.size + areaB.size - newSizePercentA).toFixed(3));
}
areaA.size = newSizePercentA;
areaB.size = newSizePercentB;
this.refreshStyleSizes();
this.notify('progress');
}
private stopDragging() {
if (!this.isDragging) {
return;
}
this._areas.forEach((a) => a.component.unlockEvents());
while (this.eventsDragFct.length > 0) {
const fct = this.eventsDragFct.pop();
Eif (fct) {
fct();
}
}
this.containerSize = 0;
this.areaASize = 0;
this.areaBSize = 0;
this.isDragging = false;
this.notify('end');
}
private notify(type: string) {
const data: number[] = this._areas.map((a) => a.size);
switch (type) {
case 'start':
return this.dragStart.emit(data);
case 'progress':
return this.dragProgress.emit(data);
case 'end':
return this.dragEnd.emit(data);
default:
return null;
}
}
}
|