| 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 |
1×
1×
1×
1×
1×
1×
1×
1×
16×
16×
16×
16×
1×
132×
1×
66×
16×
16×
16×
16×
16×
1×
1×
2×
32×
2×
32×
2×
6000×
2×
16×
16×
1×
16×
2×
1×
1×
2×
1×
1×
1×
1×
2×
5×
5×
196×
16×
2×
16×
16×
16×
2×
2×
2×
2×
485×
1×
2×
2×
1×
1×
1×
1×
18×
2×
14×
4×
16×
16×
18×
2×
2×
2×
16×
49×
49×
48×
48×
48×
1×
1×
49×
6×
6×
6×
6×
6×
43×
43×
43×
43×
43×
49×
17×
17×
1×
1×
1×
17×
49×
17×
17×
1×
1×
17×
17×
16×
1×
1×
1×
32×
16×
32×
32×
2×
1×
30×
16×
32×
32×
2×
1×
1×
1×
1×
30×
31×
16×
32×
32×
2×
1×
1×
1×
1×
30×
31×
16×
64×
2×
1×
16×
16×
1×
16×
1×
16×
1×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
485×
485×
1×
970×
400×
570×
144×
426×
374×
52×
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, ContentChild, ElementRef, HostBinding, Input, OnDestroy, ViewChild } from '@angular/core';
import { from as observableFrom, fromEvent as observableFromEvent, interval as observableInterval, merge as observableMerge, Subject, Subscription, timer as observableTimer } from 'rxjs';
import { debounceTime, delay, first, map, takeUntil, takeWhile } from 'rxjs/operators';
import { IViewPort, IViewPortItem, IViewPortRefreshParams, ViewportDirection, ViewportMode, ViewPortService } from '../../common/core/item-list/viewport.service';
export enum DejaViewPortScrollStyle {
scrollbar,
buttons,
}
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [ViewPortService],
selector: 'deja-viewport',
styles: [require('./viewport.component.scss')],
template: require('./viewport.component.html'),
})
export class DejaViewPortComponent implements OnDestroy {
protected beforeSize: number;
protected afterSize: number;
protected vpItems: IDejaViewPortItem[];
protected vpStartIndex: number;
protected vpEndIndex: number;
protected startOffset: number; // Buttons mode only
@HostBinding('attr.hasUpBtn') protected hasUpButton = false;
@HostBinding('attr.hasDownBtn') protected hasDownButton = false;
@HostBinding('attr.horizontal') protected _isHorizontal = false;
@HostBinding('attr.buttons') protected _hasButtons = false;
public get hasButtons() {
return this._hasButtons;
}
public get isHorizontal() {
return this._isHorizontal;
}
private _items: IDejaViewPortItem[];
private element: HTMLElement;
private isAlive = true;
private downButton$ = new Subject<HTMLElement>();
private upButton$ = new Subject<HTMLElement>();
private buttonsStep = 20;
private downButton$Sub: Subscription;
private upButton$Sub: Subscription;
private mouseWheel$Sub: Subscription;
private scrollPosition = 0;
/** Permet de définir un template d'élément par binding */
@Input() public itemTemplateExternal: any;
@ContentChild('itemTemplate') private itemTemplateInternal: any;
@ViewChild('down')
public set downButton(element: ElementRef) {
this.downButton$.next((element && element.nativeElement) || null);
}
@ViewChild('up')
public set upButton(element: ElementRef) {
this.upButton$.next((element && element.nativeElement) || null);
}
/** Set the list of models to render inside the viewport control */
@Input()
public set models(models: any[]) {
this.items = models ? models.map((model) => ({
model: model,
} as IDejaViewPortItem)) : [];
}
/** Set the list of items to render inside the viewport control */
@Input()
public set items(items: any[]) {
this._items = items || [];
if (this.viewPort.mode === ViewportMode.disabled) {
this.vpItems = this._items;
}
this.viewPort.items$.next(this._items);
}
/** Set the scrolling style
* scrollbar: Classic scrollbars
* buttons: A button before is placed at the top or at the left of the list, and a button after is placed at the right or the bottom of the list.
*/
@Input()
public set scrollingStyle(value: DejaViewPortScrollStyle | string) {
const scrollingStyle = typeof value === 'string' ? DejaViewPortScrollStyle[value as any] : value;
this._hasButtons = scrollingStyle === DejaViewPortScrollStyle.buttons;
}
/** Set the direction of the items rendering
* vertical: The item are displayed vertically
* horizontal: The item are displayed horizontally
*/
@Input()
public set direction(value: ViewportDirection | string) {
const direction = typeof value === 'string' ? ViewportDirection[value as any] : value;
this.viewPort.direction$.next(direction);
this._isHorizontal = direction === ViewportDirection.horizontal;
this.changeDetectorRef.markForCheck();
}
/** Set the item size in fixed mode or the default item size before rendering in auto mode */
@Input()
public set itemSize(value: number | string) {
Eif (value) {
this.viewPort.itemsSize$.next(+value);
}
}
public get itemSize() {
return this.viewPort.itemsSize;
}
@ViewChild('wrapper')
public set wrapperElement(element: ElementRef) {
this.element = element.nativeElement as HTMLElement;
this.viewPort.element$.next(this.element);
observableFromEvent(this.element, 'scroll').pipe(
takeWhile(() => this.isAlive),
map((event: Event) => event.target as HTMLElement),
map((target) => Math.round(this._isHorizontal ? target.scrollLeft : target.scrollTop)))
.subscribe((scrollPos) => {
this.viewPort.scrollPosition$.next(scrollPos);
});
}
public get itemTemplate() { return this.itemTemplateExternal || this.itemTemplateInternal; }
private get clientSize() {
Iif (!this.element) {
return 0;
}
return this._isHorizontal ? this.element.clientWidth : this.element.clientHeight;
}
private set scrollPos(value: number) {
const scrollPos = Math.max(value, 0);
this.scrollPosition = scrollPos;
this.viewPort.scrollPosition$.next(scrollPos);
}
private get scrollPos() {
return this.scrollPosition;
}
/**
* Définit la méthode de calcul de la taille des éléments. Les valuers acceptées sont
* disabled: Tous les éléments sont rendus. (< 100 éléments)
* fixed: Seul les éléments visibles sont rendus. La taille des éléments est constante et définie par itemsSize. (performances ++)
* variable: Seul les éléments visibles sont rendus. La taille des éléments est variable et définie par item.size. (performances +-)
* auto: Seul les éléments visibles sont rendus. La taille des éléments est calculée automatiquement (performances --)
*/
@Input()
public set viewportMode(mode: ViewportMode | string) {
this.viewPort.mode$.next(mode);
}
public get viewportMode() {
return this.viewPort.mode;
}
constructor(private changeDetectorRef: ChangeDetectorRef, private viewPort: ViewPortService) {
observableFromEvent(window, 'resize').pipe(
takeWhile(() => this.isAlive),
debounceTime(5))
.subscribe(() => {
this.viewPort.deleteSizeCache();
this.viewPort.refresh();
this.changeDetectorRef.markForCheck();
});
viewPort.viewPort$.pipe(
takeWhile(() => this.isAlive))
.subscribe((viewPortResult: IViewPort) => {
if (viewPort.mode !== ViewportMode.disabled) {
this.vpItems = viewPortResult.visibleItems as IDejaViewPortItem[];
this.vpStartIndex = viewPortResult.startIndex;
this.vpEndIndex = viewPortResult.endIndex;
} else {
this.vpStartIndex = 0;
this.vpEndIndex = 0;
}
if (this.hasButtons) {
this.startOffset = this.scrollPos - viewPortResult.beforeSize;
this.beforeSize = null;
this.afterSize = null;
this.hasUpButton = this.scrollPos > 0;
this.hasDownButton = this.scrollPos + viewPortResult.listSize < viewPortResult.beforeSize + viewPortResult.viewPortSize + viewPortResult.afterSize;
} else {
this.startOffset = 0;
this.beforeSize = viewPortResult.beforeSize || null;
this.afterSize = viewPortResult.afterSize || null;
this.hasUpButton = false;
this.hasDownButton = false;
}
const scroll = (vp: IViewPort) => {
Eif (!this.hasButtons) {
if (this.element) {
Iif (this._isHorizontal) {
this.element.scrollLeft = vp.scrollPos;
} else {
this.element.scrollTop = vp.scrollPos;
}
this.scrollPosition = vp.scrollPos;
}
} else {
this.scrollPos = vp.scrollPos;
this.startOffset = this.scrollPos - vp.beforeSize;
}
this.changeDetectorRef.markForCheck();
};
if (viewPortResult.scrollPos !== undefined) {
let length = 0;
if (this.element) {
const listItems = this.element.getElementsByClassName('listitem');
length = listItems.length;
}
const rebind = length !== viewPortResult.visibleItems.length;
if (!rebind) {
scroll(viewPortResult);
} else {
this.changeDetectorRef.markForCheck();
observableTimer(1).pipe(
first())
.subscribe(() => scroll(viewPortResult));
}
} else {
this.changeDetectorRef.markForCheck();
}
});
observableFrom(this.downButton$).pipe(
takeWhile(() => this.isAlive))
.subscribe((downButton) => {
if (downButton) {
if (!this.mouseWheel$Sub) {
this.mouseWheel$Sub = observableFromEvent(this.element, 'mousewheel')
.subscribe((event: MouseWheelEvent) => {
this.scrollPos = this.scrollPos + event.deltaY;
});
}
} else Iif (this.mouseWheel$Sub) {
this.mouseWheel$Sub.unsubscribe();
delete this.mouseWheel$Sub;
this.scrollPos = 0;
}
});
const downButton$ = observableFrom(this.downButton$).pipe(
takeWhile(() => this.isAlive),
map((downButton) => {
if (downButton) {
if (!this.downButton$Sub) {
const mousedown$ = observableFromEvent(downButton, 'mousedown');
const mouseup$ = observableMerge(
observableFromEvent(downButton, 'mouseup'),
observableFromEvent(downButton, 'mouseleave'));
this.downButton$Sub = mousedown$.subscribe((event: MouseEvent) => {
mouseup$.pipe(first())
.subscribe((upEvent: MouseEvent) => {
this.scrollPos += upEvent.ctrlKey ? this.clientSize : this.buttonsStep;
});
observableTimer(750).pipe(
takeUntil(mouseup$))
.subscribe(() => {
observableInterval(50).pipe(
takeUntil(mouseup$))
.subscribe(() => {
this.scrollPos += event.ctrlKey ? this.clientSize : this.buttonsStep * 2;
});
});
});
return true;
}
} else Iif (this.downButton$Sub) {
this.downButton$Sub.unsubscribe();
delete this.downButton$Sub;
return true;
}
return false;
}));
const upButton$ = observableFrom(this.upButton$).pipe(
takeWhile(() => this.isAlive),
map((upButton) => {
if (upButton) {
if (!this.upButton$Sub) {
const mousedown$ = observableFromEvent(upButton, 'mousedown');
const mouseup$ = observableMerge(
observableFromEvent(upButton, 'mouseup'),
observableFromEvent(upButton, 'mouseleave'));
this.upButton$Sub = mousedown$.subscribe((event: MouseEvent) => {
mouseup$.pipe(
first())
.subscribe((upEvent: MouseEvent) => {
this.scrollPos -= upEvent.ctrlKey ? this.clientSize : this.buttonsStep;
});
observableTimer(750).pipe(
takeUntil(mouseup$))
.subscribe(() => {
observableInterval(50).pipe(
takeUntil(mouseup$))
.subscribe(() => {
this.scrollPos -= event.ctrlKey ? this.clientSize : this.buttonsStep * 2;
});
});
});
return true;
}
} else Iif (this.upButton$Sub) {
this.upButton$Sub.unsubscribe();
delete this.upButton$Sub;
return true;
}
return false;
}));
observableMerge(downButton$, upButton$).pipe(
delay(10))
.subscribe((needToRefresh) => {
if (needToRefresh) {
this.viewPort.refresh();
}
});
}
public ngOnDestroy() {
this.isAlive = false;
if (this.downButton$Sub) {
this.downButton$Sub.unsubscribe();
}
if (this.upButton$Sub) {
this.upButton$Sub.unsubscribe();
}
if (this.mouseWheel$Sub) {
this.mouseWheel$Sub.unsubscribe();
}
}
public refresh() {
this.changeDetectorRef.markForCheck();
}
/** Recalcule le viewport. */
public refreshViewPort(item?: IViewPortItem, clearMeasuredHeight?: boolean) {
const refreshParams = {} as IViewPortRefreshParams;
Iif (item) {
refreshParams.items = [item];
}
Iif (clearMeasuredHeight) {
refreshParams.clearMeasuredSize = clearMeasuredHeight;
}
this.viewPort.refresh(refreshParams);
this.changeDetectorRef.markForCheck();
}
public ensureVisible(item: any) {
this.viewPort.ensureItem$.next(item);
}
protected getCssSize(item: IViewPortItem) {
const itemSize = this.getItemSize(item);
return itemSize ? `${itemSize}px` : 'auto';
}
protected getItemSize(item: IViewPortItem) {
if (this.viewPort.mode === ViewportMode.disabled) {
return null;
} else if (this.viewPort.mode === ViewportMode.fixed) {
return this.itemSize;
} else if (this.viewPort.mode === ViewportMode.auto) {
return item.size || null;
} else {
return (item.size && item.size > ViewPortService.itemDefaultSize) ? item.size : this.itemSize;
}
}
}
export interface IDejaViewPortItem extends IViewPortItem {
model: any;
}
|