forked from angular-split/angular-split
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.component.js
More file actions
313 lines (313 loc) · 14.5 KB
/
split.component.js
File metadata and controls
313 lines (313 loc) · 14.5 KB
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
import { Component, ChangeDetectorRef, Input, Output, HostBinding, ElementRef, ChangeDetectionStrategy, EventEmitter, Renderer } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/debounceTime';
var SplitComponent = (function () {
function SplitComponent(cdRef, elementRef, renderer) {
this.cdRef = cdRef;
this.elementRef = elementRef;
this.renderer = renderer;
this.direction = 'horizontal';
this.gutterSize = 10;
this.disabled = false;
this.visibleTransition = false;
this.dragStart = new EventEmitter(false);
this.dragProgress = new EventEmitter(false);
this.dragEnd = new EventEmitter(false);
this.visibleTransitionEndInternal = new Subject();
this.visibleTransitionEnd = this.visibleTransitionEndInternal.asObservable().debounceTime(20);
this.areas = [];
this.minPercent = 5;
this.isDragging = false;
this.containerSize = 0;
this.areaASize = 0;
this.areaBSize = 0;
this.eventsDragFct = [];
}
Object.defineProperty(SplitComponent.prototype, "styleFlexDirection", {
get: function () {
return this.direction === 'vertical';
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "styleFlexDirectionStyle", {
get: function () {
return this.direction === 'horizontal' ? 'row' : 'column';
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "dragging", {
get: function () {
// prevent animation of areas when visibleTransition is false, or resizing
return !this.visibleTransition || this.isDragging;
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "styleWidth", {
get: function () {
return (this.width && !isNaN(this.width) && this.width > 0) ? this.width + 'px' : '100%';
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "styleHeight", {
get: function () {
return (this.height && !isNaN(this.height) && this.height > 0) ? this.height + 'px' : '100%';
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "visibleAreas", {
get: function () {
return this.areas.filter(function (a) { return a.component.visible; });
},
enumerable: true,
configurable: true
});
Object.defineProperty(SplitComponent.prototype, "nbGutters", {
get: function () {
return this.visibleAreas.length - 1;
},
enumerable: true,
configurable: true
});
SplitComponent.prototype.ngOnChanges = function (changes) {
if (changes['gutterSize'] || changes['disabled']) {
this.refresh();
}
};
SplitComponent.prototype.addArea = function (component, orderUser, sizeUser, minPixel) {
this.areas.push({
component: component,
orderUser: orderUser,
order: -1,
sizeUser: sizeUser,
size: -1,
minPixel: minPixel
});
this.refresh();
};
SplitComponent.prototype.updateArea = function (component, orderUser, sizeUser, minPixel) {
var item = this.areas.find(function (a) { return a.component === component; });
if (item) {
item.orderUser = orderUser;
item.sizeUser = sizeUser;
item.minPixel = minPixel;
this.refresh();
}
};
SplitComponent.prototype.removeArea = function (area) {
var item = this.areas.find(function (a) { return a.component === area; });
if (item) {
var index = this.areas.indexOf(item);
this.areas.splice(index, 1);
this.areas.forEach(function (a, i) { return a.order = i * 2; });
this.refresh();
}
};
SplitComponent.prototype.hideArea = function (area) {
var item = this.areas.find(function (a) { return a.component === area; });
if (item) {
this.refresh();
}
};
SplitComponent.prototype.showArea = function (area) {
var item = this.areas.find(function (a) { return a.component === area; });
if (item) {
this.refresh();
}
};
SplitComponent.prototype.isLastVisibleArea = function (area) {
var visibleAreas = this.visibleAreas;
return visibleAreas.length > 0 ? area === visibleAreas[visibleAreas.length - 1] : false;
};
SplitComponent.prototype.refresh = function () {
var _this = this;
this.stopDragging();
var visibleAreas = this.visibleAreas;
// ORDERS: Set css 'order' property depending on user input or added order
var nbCorrectOrder = this.areas.filter(function (a) { return a.orderUser !== null && !isNaN(a.orderUser); }).length;
if (nbCorrectOrder === this.areas.length) {
this.areas.sort(function (a, b) { return +a.orderUser - +b.orderUser; });
}
this.areas.forEach(function (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
var totalSize = visibleAreas.map(function (a) { return a.sizeUser; }).reduce(function (acc, s) { return acc + s; }, 0);
var nbCorrectSize = visibleAreas.filter(function (a) { return a.sizeUser !== null && !isNaN(a.sizeUser) && a.sizeUser >= _this.minPercent; }).length;
if (totalSize < 99.99 || totalSize > 100.01 || nbCorrectSize !== visibleAreas.length) {
var size_1 = Number((100 / visibleAreas.length).toFixed(3));
visibleAreas.forEach(function (a) { return a.size = size_1; });
}
else {
visibleAreas.forEach(function (a) { return a.size = Number(a.sizeUser); });
}
this.refreshStyleSizes();
this.cdRef.markForCheck();
};
SplitComponent.prototype.refreshStyleSizes = function () {
var visibleAreas = this.visibleAreas;
var f = this.gutterSize * this.nbGutters / visibleAreas.length;
visibleAreas.forEach(function (a) { return a.component.setStyle('flex-basis', "calc( " + a.size + "% - " + f + "px )"); });
};
SplitComponent.prototype.startDragging = function (startEvent, gutterOrder) {
var _this = this;
startEvent.preventDefault();
if (this.disabled) {
return;
}
var areaA = this.areas.find(function (a) { return a.order === gutterOrder - 1; });
var areaB = this.areas.find(function (a) { return a.order === gutterOrder + 1; });
if (!areaA || !areaB) {
return;
}
var 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;
var start;
if (startEvent instanceof MouseEvent) {
start = {
x: startEvent.screenX,
y: startEvent.screenY
};
}
else if (startEvent instanceof TouchEvent) {
start = {
x: startEvent.touches[0].screenX,
y: startEvent.touches[0].screenY
};
}
else {
return;
}
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'mousemove', function (e) { return _this.dragEvent(e, start, areaA, areaB); }));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchmove', function (e) { return _this.dragEvent(e, start, areaA, areaB); }));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'mouseup', function (e) { return _this.stopDragging(); }));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchend', function (e) { return _this.stopDragging(); }));
this.eventsDragFct.push(this.renderer.listenGlobal('document', 'touchcancel', function (e) { return _this.stopDragging(); }));
areaA.component.lockEvents();
areaB.component.lockEvents();
this.isDragging = true;
this.notify('start');
};
SplitComponent.prototype.dragEvent = function (event, start, areaA, areaB) {
if (!this.isDragging) {
return;
}
var end;
if (event instanceof MouseEvent) {
end = {
x: event.screenX,
y: event.screenY
};
}
else if (event instanceof TouchEvent) {
end = {
x: event.touches[0].screenX,
y: event.touches[0].screenY
};
}
else {
return;
}
this.drag(start, end, areaA, areaB);
};
SplitComponent.prototype.drag = function (start, end, areaA, areaB) {
var offsetPixel = (this.direction === 'horizontal') ? (start.x - end.x) : (start.y - end.y);
var newSizePixelA = this.areaASize - offsetPixel;
var newSizePixelB = this.areaBSize + offsetPixel;
if (newSizePixelA <= areaA.minPixel && newSizePixelB < areaB.minPixel) {
return;
}
var newSizePercentA = newSizePixelA / this.containerSize * 100;
var newSizePercentB = newSizePixelB / this.containerSize * 100;
if (newSizePercentA <= this.minPercent) {
newSizePercentA = this.minPercent;
newSizePercentB = areaA.size + areaB.size - this.minPercent;
}
else if (newSizePercentB <= this.minPercent) {
newSizePercentB = this.minPercent;
newSizePercentA = areaA.size + areaB.size - this.minPercent;
}
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');
};
SplitComponent.prototype.stopDragging = function () {
if (!this.isDragging) {
return;
}
this.areas.forEach(function (a) { return a.component.unlockEvents(); });
while (this.eventsDragFct.length > 0) {
var fct = this.eventsDragFct.pop();
if (fct) {
fct();
}
}
this.containerSize = 0;
this.areaASize = 0;
this.areaBSize = 0;
this.isDragging = false;
this.notify('end');
};
SplitComponent.prototype.notify = function (type) {
var data = this.visibleAreas.map(function (a) { return 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);
case 'visibleTransitionEnd':
return this.visibleTransitionEndInternal.next(data);
}
};
SplitComponent.prototype.ngOnDestroy = function () {
this.stopDragging();
};
return SplitComponent;
}());
export { SplitComponent };
SplitComponent.decorators = [
{ type: Component, args: [{
selector: 'split',
changeDetection: ChangeDetectionStrategy.OnPush,
styles: ["\n :host {\n display: flex;\n flex-wrap: nowrap;\n justify-content: flex-start;\n align-items: stretch;\n flex-direction: row;\n }\n\n :host.vertical {\n flex-direction: column;\n }\n\n split-gutter {\n flex-grow: 0;\n flex-shrink: 0;\n background-color: #eeeeee;\n background-position: center center;\n background-repeat: no-repeat;\n }\n\n :host.vertical split-gutter {\n width: 100%;\n }\n\n :host /deep/ split-area {\n transition: flex-basis 0.3s;\n } \n\n :host.notransition /deep/ split-area {\n transition: none !important;\n } \n\n :host /deep/ split-area.hided {\n flex-basis: 0 !important;\n overflow: hidden !important;\n } \n\n :host.vertical /deep/ split-area.hided {\n max-width: 0;\n }\n "],
template: "\n <ng-content></ng-content>\n <ng-template ngFor let-area [ngForOf]=\"areas\" let-index=\"index\" let-last=\"last\">\n <split-gutter *ngIf=\"last === false && area.component.visible === true && !isLastVisibleArea(area)\" \n [order]=\"index*2+1\"\n [direction]=\"direction\"\n [size]=\"gutterSize\"\n [disabled]=\"disabled\"\n (mousedown)=\"startDragging($event, index*2+1)\"\n (touchstart)=\"startDragging($event, index*2+1)\"></split-gutter>\n </ng-template>",
},] },
];
/** @nocollapse */
SplitComponent.ctorParameters = function () { return [
{ type: ChangeDetectorRef, },
{ type: ElementRef, },
{ type: Renderer, },
]; };
SplitComponent.propDecorators = {
'direction': [{ type: Input },],
'width': [{ type: Input },],
'height': [{ type: Input },],
'gutterSize': [{ type: Input },],
'disabled': [{ type: Input },],
'visibleTransition': [{ type: Input },],
'dragStart': [{ type: Output },],
'dragProgress': [{ type: Output },],
'dragEnd': [{ type: Output },],
'visibleTransitionEnd': [{ type: Output },],
'styleFlexDirection': [{ type: HostBinding, args: ['class.vertical',] },],
'styleFlexDirectionStyle': [{ type: HostBinding, args: ['style.flex-direction',] },],
'dragging': [{ type: HostBinding, args: ['class.notransition',] },],
'styleWidth': [{ type: HostBinding, args: ['style.width',] },],
'styleHeight': [{ type: HostBinding, args: ['style.height',] },],
};
//# sourceMappingURL=split.component.js.map