ng-virtual-list 0.7.2 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +44 -71
  2. package/esm2020/lib/components/ng-virtual-list-item.component.mjs +80 -0
  3. package/esm2020/lib/const/index.mjs +34 -0
  4. package/esm2020/lib/enums/direction.mjs +2 -0
  5. package/esm2020/lib/enums/directions.mjs +18 -0
  6. package/esm2020/lib/enums/index.mjs +3 -0
  7. package/esm2020/lib/models/collection.model.mjs +3 -0
  8. package/esm2020/lib/models/index.mjs +2 -0
  9. package/esm2020/lib/models/item.model.mjs +3 -0
  10. package/esm2020/lib/models/render-collection.model.mjs +3 -0
  11. package/esm2020/lib/models/render-item-config.model.mjs +2 -0
  12. package/esm2020/lib/models/render-item.model.mjs +3 -0
  13. package/esm2020/lib/models/sticky-map.model.mjs +2 -0
  14. package/esm2020/lib/ng-virtual-list.component.mjs +510 -0
  15. package/esm2020/lib/ng-virtual-list.module.mjs +20 -0
  16. package/esm2020/lib/types/id.mjs +2 -0
  17. package/esm2020/lib/types/index.mjs +2 -0
  18. package/esm2020/lib/types/rect.mjs +2 -0
  19. package/esm2020/lib/types/size.mjs +2 -0
  20. package/esm2020/lib/utils/cacheMap.mjs +52 -0
  21. package/esm2020/lib/utils/debounce.mjs +31 -0
  22. package/esm2020/lib/utils/disposableComponent.mjs +29 -0
  23. package/esm2020/lib/utils/eventEmitter.mjs +106 -0
  24. package/esm2020/lib/utils/index.mjs +8 -0
  25. package/esm2020/lib/utils/isDirection.mjs +15 -0
  26. package/esm2020/lib/utils/toggleClassName.mjs +15 -0
  27. package/esm2020/lib/utils/trackBox.mjs +352 -0
  28. package/esm2020/lib/utils/tracker.mjs +108 -0
  29. package/esm2020/ng-virtual-list.mjs +5 -0
  30. package/esm2020/public-api.mjs +8 -0
  31. package/fesm2015/ng-virtual-list.mjs +1360 -0
  32. package/fesm2015/ng-virtual-list.mjs.map +1 -0
  33. package/fesm2020/ng-virtual-list.mjs +1359 -0
  34. package/fesm2020/ng-virtual-list.mjs.map +1 -0
  35. package/index.d.ts +5 -5
  36. package/lib/components/ng-virtual-list-item.component.d.ts +28 -35
  37. package/lib/const/index.d.ts +32 -31
  38. package/lib/enums/direction.d.ts +8 -2
  39. package/lib/enums/directions.d.ts +16 -4
  40. package/lib/enums/index.d.ts +4 -4
  41. package/lib/models/collection.model.d.ts +9 -3
  42. package/lib/models/index.d.ts +4 -4
  43. package/lib/models/item.model.d.ts +14 -5
  44. package/lib/models/render-collection.model.d.ts +9 -3
  45. package/lib/models/render-item-config.model.d.ts +33 -7
  46. package/lib/models/render-item.model.d.ts +28 -10
  47. package/lib/models/sticky-map.model.d.ts +12 -6
  48. package/lib/ng-virtual-list.component.d.ts +151 -110
  49. package/lib/ng-virtual-list.module.d.ts +9 -0
  50. package/lib/types/id.d.ts +7 -1
  51. package/lib/types/index.d.ts +4 -4
  52. package/lib/types/rect.d.ts +17 -5
  53. package/lib/types/size.d.ts +16 -4
  54. package/lib/utils/cacheMap.d.ts +34 -31
  55. package/lib/utils/debounce.d.ts +16 -10
  56. package/lib/utils/disposableComponent.d.ts +15 -0
  57. package/lib/utils/eventEmitter.d.ts +40 -37
  58. package/lib/utils/index.d.ts +7 -6
  59. package/lib/utils/isDirection.d.ts +8 -2
  60. package/lib/utils/toggleClassName.d.ts +7 -1
  61. package/lib/utils/trackBox.d.ts +113 -73
  62. package/lib/utils/tracker.d.ts +38 -38
  63. package/package.json +18 -6
  64. package/public-api.d.ts +4 -3
  65. package/fesm2022/ng-virtual-list.mjs +0 -942
  66. package/fesm2022/ng-virtual-list.mjs.map +0 -1
@@ -0,0 +1,1360 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Component, ChangeDetectionStrategy, EventEmitter as EventEmitter$1, ViewContainerRef, ElementRef, ViewEncapsulation, ViewChild, Output, Input, NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
3
+ import * as i1 from '@angular/common';
4
+ import { CommonModule } from '@angular/common';
5
+ import { Subject, BehaviorSubject, filter, map, takeUntil, tap, combineLatest, distinctUntilChanged, debounceTime, switchMap, of } from 'rxjs';
6
+
7
+ /**
8
+ * Axis of the arrangement of virtual list elements.
9
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/enums/directions.ts
10
+ * @author Evgenii Grebennikov
11
+ * @email djonnyx@gmail.com
12
+ */
13
+ var Directions;
14
+ (function (Directions) {
15
+ /**
16
+ * Horizontal axis.
17
+ */
18
+ Directions["HORIZONTAL"] = "horizontal";
19
+ /**
20
+ * Vertical axis.
21
+ */
22
+ Directions["VERTICAL"] = "vertical";
23
+ })(Directions || (Directions = {}));
24
+
25
+ const DEFAULT_ITEM_SIZE = 24;
26
+ const DEFAULT_ITEMS_OFFSET = 2;
27
+ const DEFAULT_LIST_SIZE = 400;
28
+ const DEFAULT_SNAP = false;
29
+ const DEFAULT_SNAP_TO_ITEM = false;
30
+ const DEFAULT_DYNAMIC_SIZE = false;
31
+ const TRACK_BY_PROPERTY_NAME = 'id';
32
+ const DEFAULT_DIRECTION = Directions.VERTICAL;
33
+ const DISPLAY_OBJECTS_LENGTH_MESUREMENT_ERROR = 1;
34
+ // presets
35
+ const BEHAVIOR_AUTO = 'auto';
36
+ const BEHAVIOR_INSTANT = 'instant';
37
+ const BEHAVIOR_SMOOTH = 'smooth';
38
+ const VISIBILITY_VISIBLE = 'visible';
39
+ const VISIBILITY_HIDDEN = 'hidden';
40
+ const SIZE_100_PERSENT = '100%';
41
+ const SIZE_AUTO = 'auto';
42
+ const POSITION_ABSOLUTE = 'absolute';
43
+ const POSITION_STICKY = 'sticky';
44
+ const TRANSLATE_3D = 'translate3d';
45
+ const ZEROS_TRANSLATE_3D = `${TRANSLATE_3D}(0,0,0)`;
46
+ const TOP_PROP_NAME = 'top';
47
+ const LEFT_PROP_NAME = 'left';
48
+ const X_PROP_NAME = 'x';
49
+ const Y_PROP_NAME = 'y';
50
+ const WIDTH_PROP_NAME = 'width';
51
+ const HEIGHT_PROP_NAME = 'height';
52
+ const PX = 'px';
53
+ const SCROLL = 'scroll';
54
+ const SCROLL_END = 'scrollend';
55
+ const CLASS_LIST_VERTICAL = 'vertical';
56
+ const CLASS_LIST_HORIZONTAL = 'horizontal';
57
+
58
+ /**
59
+ * Virtual list item component
60
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/components/ng-virtual-list-item.component.ts
61
+ * @author Evgenii Grebennikov
62
+ * @email djonnyx@gmail.com
63
+ */
64
+ class NgVirtualListItemComponent {
65
+ constructor(_cdr, _elementRef) {
66
+ this._cdr = _cdr;
67
+ this._elementRef = _elementRef;
68
+ this._id = NgVirtualListItemComponent.__nextId = NgVirtualListItemComponent.__nextId === Number.MAX_SAFE_INTEGER
69
+ ? 0 : NgVirtualListItemComponent.__nextId + 1;
70
+ }
71
+ get id() {
72
+ return this._id;
73
+ }
74
+ set item(v) {
75
+ if (this.data === v) {
76
+ return;
77
+ }
78
+ const data = this.data = v;
79
+ if (data) {
80
+ const styles = this._elementRef.nativeElement.style;
81
+ styles.zIndex = String(data.config.sticky);
82
+ if (data.config.snapped) {
83
+ styles.transform = ZEROS_TRANSLATE_3D;
84
+ styles.position = POSITION_STICKY;
85
+ }
86
+ else {
87
+ styles.position = POSITION_ABSOLUTE;
88
+ styles.transform = `${TRANSLATE_3D}(${data.config.isVertical ? 0 : data.measures.x}${PX}, ${data.config.isVertical ? data.measures.y : 0}${PX} , 0)`;
89
+ }
90
+ styles.height = data.config.isVertical ? data.config.dynamic ? SIZE_AUTO : `${data.measures.height}${PX}` : SIZE_100_PERSENT;
91
+ styles.width = data.config.isVertical ? SIZE_100_PERSENT : data.config.dynamic ? SIZE_AUTO : `${data.measures.width}${PX}`;
92
+ }
93
+ this._cdr.detectChanges();
94
+ }
95
+ get itemId() {
96
+ var _a;
97
+ return (_a = this.data) === null || _a === void 0 ? void 0 : _a.id;
98
+ }
99
+ set renderer(v) {
100
+ if (this.itemRenderer === v) {
101
+ return;
102
+ }
103
+ this.itemRenderer = v;
104
+ this._cdr.markForCheck();
105
+ }
106
+ getBounds() {
107
+ const el = this._elementRef.nativeElement, { width, height, left, top } = el.getBoundingClientRect();
108
+ return { width, height, x: left, y: top };
109
+ }
110
+ showIfNeed() {
111
+ const styles = this._elementRef.nativeElement.style;
112
+ if (styles.visibility === VISIBILITY_VISIBLE) {
113
+ return;
114
+ }
115
+ styles.visibility = VISIBILITY_VISIBLE;
116
+ }
117
+ hide() {
118
+ const styles = this._elementRef.nativeElement.style;
119
+ if (styles.visibility === VISIBILITY_HIDDEN) {
120
+ return;
121
+ }
122
+ styles.visibility = VISIBILITY_HIDDEN;
123
+ }
124
+ }
125
+ NgVirtualListItemComponent.__nextId = 0;
126
+ NgVirtualListItemComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListItemComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
127
+ NgVirtualListItemComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.3.0", type: NgVirtualListItemComponent, selector: "ng-virtual-list-item", host: { classAttribute: "ngvl__item" }, ngImport: i0, template: "<ng-container *ngIf=\"data\">\r\n <li #listItem part=\"item\" class=\"ngvl-item__container\" [ngClass]=\"{'snapped': data.config.snapped,\r\n 'snapped-out': data.config.snappedOut}\">\r\n <ng-container *ngIf=\"itemRenderer\">\r\n <ng-container [ngTemplateOutlet]=\"itemRenderer\"\r\n [ngTemplateOutletContext]=\"{data: data.data || {}, config: data.config}\"></ng-container>\r\n </ng-container>\r\n </li>\r\n</ng-container>", styles: [":host{display:block;position:absolute;left:0;top:0;box-sizing:border-box;overflow:hidden}.ngvl-item__container{margin:0;padding:0;overflow:hidden;background-color:#fff;width:inherit;height:inherit}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
128
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListItemComponent, decorators: [{
129
+ type: Component,
130
+ args: [{ selector: 'ng-virtual-list-item', host: {
131
+ 'class': 'ngvl__item',
132
+ }, changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-container *ngIf=\"data\">\r\n <li #listItem part=\"item\" class=\"ngvl-item__container\" [ngClass]=\"{'snapped': data.config.snapped,\r\n 'snapped-out': data.config.snappedOut}\">\r\n <ng-container *ngIf=\"itemRenderer\">\r\n <ng-container [ngTemplateOutlet]=\"itemRenderer\"\r\n [ngTemplateOutletContext]=\"{data: data.data || {}, config: data.config}\"></ng-container>\r\n </ng-container>\r\n </li>\r\n</ng-container>", styles: [":host{display:block;position:absolute;left:0;top:0;box-sizing:border-box;overflow:hidden}.ngvl-item__container{margin:0;padding:0;overflow:hidden;background-color:#fff;width:inherit;height:inherit}\n"] }]
133
+ }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }]; } });
134
+
135
+ const HORIZONTAL_ALIASES = [Directions.HORIZONTAL, 'horizontal'], VERTICAL_ALIASES = [Directions.VERTICAL, 'vertical'];
136
+ /**
137
+ * Determines the axis membership of a virtual list
138
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/isDirection.ts
139
+ * @author Evgenii Grebennikov
140
+ * @email djonnyx@gmail.com
141
+ */
142
+ const isDirection = (src, expected) => {
143
+ if (HORIZONTAL_ALIASES.includes(expected)) {
144
+ return HORIZONTAL_ALIASES.includes(src);
145
+ }
146
+ return VERTICAL_ALIASES.includes(src);
147
+ };
148
+
149
+ /**
150
+ * Simple debounce function.
151
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/debounce.ts
152
+ * @author Evgenii Grebennikov
153
+ * @email djonnyx@gmail.com
154
+ */
155
+ const debounce = (cb, debounceTime = 0) => {
156
+ let timeout;
157
+ const dispose = () => {
158
+ if (timeout !== undefined) {
159
+ clearTimeout(timeout);
160
+ }
161
+ };
162
+ const execute = (...args) => {
163
+ dispose();
164
+ timeout = setTimeout(() => {
165
+ cb(...args);
166
+ }, debounceTime);
167
+ };
168
+ return {
169
+ /**
170
+ * Call handling method
171
+ */
172
+ execute,
173
+ /**
174
+ * Method of destroying handlers
175
+ */
176
+ dispose,
177
+ };
178
+ };
179
+
180
+ /**
181
+ * Switch css classes
182
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/toggleClassName.ts
183
+ * @author Evgenii Grebennikov
184
+ * @email djonnyx@gmail.com
185
+ */
186
+ const toggleClassName = (el, className, remove = false) => {
187
+ if (!el.classList.contains(className)) {
188
+ el.classList.add(className);
189
+ }
190
+ else if (remove) {
191
+ el.classList.remove(className);
192
+ }
193
+ };
194
+
195
+ /**
196
+ * Tracks display items by property
197
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/tracker.ts
198
+ * @author Evgenii Grebennikov
199
+ * @email djonnyx@gmail.com
200
+ */
201
+ class Tracker {
202
+ constructor(trackingPropertyName) {
203
+ /**
204
+ * display objects dictionary of indexes by id
205
+ */
206
+ this._displayObjectIndexMapById = {};
207
+ /**
208
+ * Dictionary displayItems propertyNameId by items propertyNameId
209
+ */
210
+ this._trackMap = {};
211
+ this._trackingPropertyName = trackingPropertyName;
212
+ }
213
+ set displayObjectIndexMapById(v) {
214
+ if (this._displayObjectIndexMapById === v) {
215
+ return;
216
+ }
217
+ this._displayObjectIndexMapById = v;
218
+ }
219
+ get displayObjectIndexMapById() {
220
+ return this._displayObjectIndexMapById;
221
+ }
222
+ get trackMap() {
223
+ return this._trackMap;
224
+ }
225
+ /**
226
+ * tracking by propName
227
+ */
228
+ track(items, components, afterComponentSetup) {
229
+ var _a;
230
+ if (!items) {
231
+ return;
232
+ }
233
+ const idPropName = this._trackingPropertyName, untrackedItems = [...components];
234
+ for (let i = 0, l = items.length; i < l; i++) {
235
+ const item = items[i], itemTrackingProperty = item[idPropName];
236
+ if (this._trackMap) {
237
+ const diId = this._trackMap[itemTrackingProperty];
238
+ if (this._trackMap.hasOwnProperty(itemTrackingProperty)) {
239
+ const lastIndex = this._displayObjectIndexMapById[diId], el = components[lastIndex];
240
+ this._checkComponentProperty(el === null || el === void 0 ? void 0 : el.instance);
241
+ const elId = (_a = el === null || el === void 0 ? void 0 : el.instance) === null || _a === void 0 ? void 0 : _a[itemTrackingProperty];
242
+ if (el && elId === diId) {
243
+ const indexByUntrackedItems = untrackedItems.findIndex(v => {
244
+ this._checkComponentProperty(v.instance);
245
+ return v.instance[itemTrackingProperty] === elId;
246
+ });
247
+ if (indexByUntrackedItems > -1) {
248
+ el.instance.item = item;
249
+ if (afterComponentSetup !== undefined) {
250
+ afterComponentSetup(el.instance, item);
251
+ }
252
+ untrackedItems.splice(indexByUntrackedItems, 1);
253
+ continue;
254
+ }
255
+ }
256
+ delete this._trackMap[itemTrackingProperty];
257
+ }
258
+ }
259
+ if (untrackedItems.length > 0) {
260
+ const el = untrackedItems.shift(), item = items[i];
261
+ if (el) {
262
+ el.instance.item = item;
263
+ if (this._trackMap) {
264
+ this._checkComponentProperty(el.instance);
265
+ this._trackMap[itemTrackingProperty] = el.instance[itemTrackingProperty];
266
+ }
267
+ if (afterComponentSetup !== undefined) {
268
+ afterComponentSetup(el.instance, item);
269
+ }
270
+ }
271
+ }
272
+ }
273
+ if (untrackedItems.length) {
274
+ throw Error('Tracking by id caused an error.');
275
+ }
276
+ }
277
+ untrackComponentByIdProperty(component) {
278
+ if (!component) {
279
+ return;
280
+ }
281
+ const propertyIdName = this._trackingPropertyName;
282
+ this._checkComponentProperty(component);
283
+ if (this._trackMap && component[propertyIdName] !== undefined) {
284
+ delete this._trackMap[propertyIdName];
285
+ }
286
+ }
287
+ _checkComponentProperty(component) {
288
+ if (!component) {
289
+ return;
290
+ }
291
+ const propertyIdName = this._trackingPropertyName;
292
+ try {
293
+ component[propertyIdName];
294
+ }
295
+ catch (err) {
296
+ throw Error(`Property ${propertyIdName} does not exist.`);
297
+ }
298
+ }
299
+ dispose() {
300
+ this._trackMap = null;
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Simple event emitter
306
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/eventEmitter.ts
307
+ * @author Evgenii Grebennikov
308
+ * @email djonnyx@gmail.com
309
+ */
310
+ class EventEmitter {
311
+ constructor() {
312
+ this._listeners = {};
313
+ this._disposed = false;
314
+ }
315
+ /**
316
+ * Emits the event
317
+ */
318
+ dispatch(event, ...args) {
319
+ const ctx = this;
320
+ const listeners = this._listeners[event];
321
+ if (Array.isArray(listeners)) {
322
+ for (let i = 0, l = listeners.length; i < l; i++) {
323
+ const listener = listeners[i];
324
+ if (listener) {
325
+ listener.apply(ctx, args);
326
+ }
327
+ }
328
+ }
329
+ }
330
+ /**
331
+ * Emits the event async
332
+ */
333
+ dispatchAsync(event, ...args) {
334
+ queueMicrotask(() => {
335
+ if (this._disposed) {
336
+ return;
337
+ }
338
+ this.dispatch(event, ...args);
339
+ });
340
+ }
341
+ /**
342
+ * Returns true if the event listener is already subscribed.
343
+ */
344
+ hasEventListener(eventName, handler) {
345
+ const event = eventName;
346
+ if (this._listeners.hasOwnProperty(event)) {
347
+ const listeners = this._listeners[event];
348
+ const index = listeners.findIndex(v => v === handler);
349
+ if (index > -1) {
350
+ return true;
351
+ }
352
+ }
353
+ return false;
354
+ }
355
+ /**
356
+ * Add event listener
357
+ */
358
+ addEventListener(eventName, handler) {
359
+ const event = eventName;
360
+ if (!this._listeners.hasOwnProperty(event)) {
361
+ this._listeners[event] = [];
362
+ }
363
+ this._listeners[event].push(handler);
364
+ }
365
+ /**
366
+ * Remove event listener
367
+ */
368
+ removeEventListener(eventName, handler) {
369
+ const event = eventName;
370
+ if (!this._listeners.hasOwnProperty(event)) {
371
+ return;
372
+ }
373
+ const listeners = this._listeners[event], index = listeners.findIndex(v => v === handler);
374
+ if (index > -1) {
375
+ listeners.splice(index, 1);
376
+ if (listeners.length === 0) {
377
+ delete this._listeners[event];
378
+ }
379
+ }
380
+ }
381
+ /**
382
+ * Remove all listeners
383
+ */
384
+ removeAllListeners() {
385
+ const events = Object.keys(this._listeners);
386
+ while (events.length > 0) {
387
+ const event = events.pop();
388
+ if (event) {
389
+ const listeners = this._listeners[event];
390
+ if (Array.isArray(listeners)) {
391
+ while (listeners.length > 0) {
392
+ const listener = listeners.pop();
393
+ if (listener) {
394
+ this.removeEventListener(event, listener);
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+ }
401
+ /**
402
+ * Method of destroying handlers
403
+ */
404
+ dispose() {
405
+ this._disposed = true;
406
+ this.removeAllListeners();
407
+ }
408
+ }
409
+
410
+ /**
411
+ * Cache map.
412
+ * Emits a change event on each mutation.
413
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/cacheMap.ts
414
+ * @author Evgenii Grebennikov
415
+ * @email djonnyx@gmail.com
416
+ */
417
+ class CacheMap extends EventEmitter {
418
+ constructor() {
419
+ super();
420
+ this._map = new Map();
421
+ this._version = 0;
422
+ this._previouseFullHeigh = 0;
423
+ this._delta = 0;
424
+ }
425
+ get delta() {
426
+ return this._delta;
427
+ }
428
+ get version() {
429
+ return this._version;
430
+ }
431
+ bumpVersion() {
432
+ this._version = this._version === Number.MAX_SAFE_INTEGER ? 0 : this._version + 1;
433
+ }
434
+ fireChange() {
435
+ this.dispatch('change', this.version);
436
+ }
437
+ set(id, bounds) {
438
+ if (this._map.has(id) && JSON.stringify(this._map.get(id)) === JSON.stringify(bounds)) {
439
+ return this._map;
440
+ }
441
+ const v = this._map.set(id, bounds);
442
+ this.bumpVersion();
443
+ this.fireChange();
444
+ return v;
445
+ }
446
+ has(id) {
447
+ return this._map.has(id);
448
+ }
449
+ get(id) {
450
+ return this._map.get(id);
451
+ }
452
+ forEach(callbackfn, thisArg) {
453
+ return this._map.forEach(callbackfn, thisArg);
454
+ }
455
+ dispose() {
456
+ super.dispose();
457
+ this._map.clear();
458
+ }
459
+ }
460
+
461
+ const TRACK_BOX_CHANGE_EVENT_NAME = 'change';
462
+ /**
463
+ * An object that performs tracking, calculations and caching.
464
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/trackBox.ts
465
+ * @author Evgenii Grebennikov
466
+ * @email djonnyx@gmail.com
467
+ */
468
+ class TrackBox extends CacheMap {
469
+ constructor(trackingPropertyName) {
470
+ super();
471
+ this._fireChanges = (version) => {
472
+ this.dispatch(TRACK_BOX_CHANGE_EVENT_NAME, version);
473
+ };
474
+ this._debounceChanges = debounce(this._fireChanges, 0);
475
+ this._tracker = new Tracker(trackingPropertyName);
476
+ }
477
+ set items(v) {
478
+ if (this._items === v) {
479
+ return;
480
+ }
481
+ this._items = v;
482
+ }
483
+ set displayComponents(v) {
484
+ if (this._displayComponents === v) {
485
+ return;
486
+ }
487
+ this._displayComponents = v;
488
+ }
489
+ set(id, bounds) {
490
+ if (this._map.has(id) && JSON.stringify(this._map.get(id)) === JSON.stringify(bounds)) {
491
+ return this._map;
492
+ }
493
+ const v = this._map.set(id, bounds);
494
+ this.bumpVersion();
495
+ this.fireChange();
496
+ return v;
497
+ }
498
+ fireChange() {
499
+ this._debounceChanges.execute(this._version);
500
+ }
501
+ getItemPosition(id, stickyMap, options) {
502
+ const opt = Object.assign({ fromItemId: id, stickyMap, scrollDirection: undefined }, options);
503
+ const { scrollSize } = this.recalculateMetrics(opt);
504
+ return scrollSize;
505
+ }
506
+ updateCollection(items, stickyMap, options) {
507
+ const opt = Object.assign({ stickyMap, scrollDirection: undefined }, options);
508
+ this.cacheElements();
509
+ const metrics = this.recalculateMetrics(Object.assign(Object.assign({}, opt), { collection: items }));
510
+ const displayItems = this.generateDisplayCollection(items, stickyMap, metrics);
511
+ return { displayItems, totalSize: metrics.totalSize, delta: metrics.delta };
512
+ }
513
+ /**
514
+ * Calculates list metrics
515
+ */
516
+ recalculateMetrics(options) {
517
+ const { scrollDirection = 0, fromItemId, bounds, collection, dynamicSize, isVertical, itemSize, itemsOffset, scrollSize, snap, stickyMap } = options;
518
+ const { width, height } = bounds, sizeProperty = isVertical ? HEIGHT_PROP_NAME : WIDTH_PROP_NAME, size = isVertical ? height : width, totalLength = collection.length, typicalItemSize = itemSize, w = isVertical ? width : typicalItemSize, h = isVertical ? typicalItemSize : height, totalSize = dynamicSize ? this.getBoundsFromCache(collection, typicalItemSize, isVertical) : totalLength * typicalItemSize, snippedPos = Math.floor(scrollSize), leftItemsWeights = [], isFromId = fromItemId !== undefined && (typeof fromItemId === 'number' && fromItemId > -1)
519
+ || (typeof fromItemId === 'string' && fromItemId > '-1');
520
+ let itemsFromStartToScrollEnd = -1, itemsFromDisplayEndToOffsetEnd = 0, itemsFromStartToDisplayEnd = -1, leftItemLength = 0, rightItemLength = 0, leftItemsWeight = 0, rightItemsWeight = 0, leftHiddenItemsWeight = 0, totalItemsToDisplayEndWeight = 0, itemById = undefined, itemByIdPos = 0, lastDisplayItemId = undefined, actualScrollSize = itemByIdPos, fullHeight = 0, startIndex;
521
+ if (dynamicSize) {
522
+ let y = 0, stickyCollectionItem = undefined, stickyComponentSize = 0;
523
+ for (let i = 0, l = collection.length; i < l; i++) {
524
+ const ii = i + 1, collectionItem = collection[i], map = this._map;
525
+ let componentSize = 0;
526
+ if (map.has(collectionItem.id)) {
527
+ const bounds = map.get(collectionItem.id);
528
+ componentSize = bounds ? bounds[sizeProperty] : typicalItemSize;
529
+ }
530
+ else {
531
+ componentSize = typicalItemSize;
532
+ }
533
+ if (isFromId) {
534
+ if (itemById === undefined) {
535
+ leftItemsWeights.push(componentSize);
536
+ leftHiddenItemsWeight += componentSize;
537
+ itemsFromStartToScrollEnd = ii;
538
+ if (stickyMap && stickyMap[collectionItem.id] > 0) {
539
+ stickyComponentSize = componentSize;
540
+ stickyCollectionItem = collectionItem;
541
+ }
542
+ if (collectionItem.id === fromItemId) {
543
+ itemById = collectionItem;
544
+ itemByIdPos = y;
545
+ if (stickyCollectionItem && stickyMap && stickyMap[stickyCollectionItem.id] > 0) {
546
+ itemByIdPos = itemByIdPos - stickyComponentSize;
547
+ }
548
+ }
549
+ }
550
+ }
551
+ else if (y < scrollSize - componentSize) {
552
+ leftItemsWeights.push(componentSize);
553
+ leftHiddenItemsWeight += componentSize;
554
+ itemsFromStartToScrollEnd = ii;
555
+ }
556
+ if (isFromId) {
557
+ if (!lastDisplayItemId) {
558
+ if (itemById === undefined || y < itemByIdPos + size + componentSize) {
559
+ itemsFromStartToDisplayEnd = ii;
560
+ totalItemsToDisplayEndWeight += componentSize;
561
+ itemsFromDisplayEndToOffsetEnd = itemsFromStartToDisplayEnd + itemsOffset;
562
+ }
563
+ }
564
+ }
565
+ else if (y < scrollSize + size + componentSize) {
566
+ itemsFromStartToDisplayEnd = ii;
567
+ totalItemsToDisplayEndWeight += componentSize;
568
+ itemsFromDisplayEndToOffsetEnd = itemsFromStartToDisplayEnd + itemsOffset;
569
+ }
570
+ else if (i < itemsFromDisplayEndToOffsetEnd) {
571
+ rightItemsWeight += componentSize;
572
+ }
573
+ y += componentSize;
574
+ }
575
+ fullHeight = y;
576
+ if (itemsFromStartToScrollEnd === -1) {
577
+ itemsFromStartToScrollEnd = 0;
578
+ }
579
+ if (itemsFromStartToDisplayEnd === -1) {
580
+ itemsFromStartToDisplayEnd = 0;
581
+ }
582
+ actualScrollSize = isFromId ? itemByIdPos : scrollSize;
583
+ leftItemsWeights.splice(0, leftItemsWeights.length - itemsOffset);
584
+ leftItemsWeights.forEach(v => {
585
+ leftItemsWeight += v;
586
+ });
587
+ leftItemLength = Math.min(itemsFromStartToScrollEnd, itemsOffset);
588
+ rightItemLength = itemsFromStartToDisplayEnd + itemsOffset > totalLength
589
+ ? totalLength - itemsFromStartToDisplayEnd : itemsOffset;
590
+ }
591
+ else {
592
+ itemsFromStartToScrollEnd = Math.floor(scrollSize / typicalItemSize);
593
+ itemsFromStartToDisplayEnd = Math.ceil((scrollSize + size) / typicalItemSize);
594
+ leftItemLength = Math.min(itemsFromStartToScrollEnd, itemsOffset);
595
+ rightItemLength = itemsFromStartToDisplayEnd + itemsOffset > totalLength
596
+ ? totalLength - itemsFromStartToDisplayEnd : itemsOffset;
597
+ leftItemsWeight = leftItemLength * typicalItemSize;
598
+ rightItemsWeight = rightItemLength * typicalItemSize,
599
+ leftHiddenItemsWeight = itemsFromStartToScrollEnd * typicalItemSize,
600
+ totalItemsToDisplayEndWeight = itemsFromStartToDisplayEnd * typicalItemSize;
601
+ actualScrollSize = scrollSize,
602
+ fullHeight = totalLength * typicalItemSize;
603
+ }
604
+ startIndex = Math.min(itemsFromStartToScrollEnd - leftItemLength, totalLength > 0 ? totalLength - 1 : 0);
605
+ const itemsOnDisplay = totalItemsToDisplayEndWeight - leftHiddenItemsWeight, itemsOnDisplayLength = itemsFromStartToDisplayEnd - itemsFromStartToScrollEnd, startPosition = leftHiddenItemsWeight - leftItemsWeight, renderItems = itemsOnDisplayLength + leftItemLength + rightItemLength, delta = fullHeight - this._previouseFullHeigh;
606
+ if (scrollDirection === -1) {
607
+ this._delta += delta;
608
+ }
609
+ const metrics = {
610
+ delta: this._delta,
611
+ normalizedItemWidth: w,
612
+ normalizedItemHeight: h,
613
+ width,
614
+ height,
615
+ dynamicSize,
616
+ itemSize,
617
+ itemsFromStartToScrollEnd,
618
+ itemsFromStartToDisplayEnd,
619
+ itemsOnDisplay,
620
+ itemsOnDisplayLength,
621
+ isVertical,
622
+ leftHiddenItemsWeight,
623
+ leftItemLength,
624
+ leftItemsWeight,
625
+ renderItems,
626
+ rightItemLength,
627
+ rightItemsWeight,
628
+ scrollSize: actualScrollSize,
629
+ sizeProperty,
630
+ snap,
631
+ snippedPos,
632
+ startIndex,
633
+ startPosition,
634
+ totalItemsToDisplayEndWeight,
635
+ totalLength,
636
+ totalSize,
637
+ typicalItemSize,
638
+ };
639
+ if (scrollDirection !== 0) {
640
+ this._previouseFullHeigh = fullHeight;
641
+ }
642
+ return metrics;
643
+ }
644
+ clearDelta() {
645
+ this._delta = 0;
646
+ }
647
+ generateDisplayCollection(items, stickyMap, metrics) {
648
+ var _a, _b;
649
+ const {
650
+ // delta,
651
+ normalizedItemWidth, normalizedItemHeight,
652
+ // width,
653
+ // height,
654
+ dynamicSize,
655
+ // itemSize,
656
+ itemsFromStartToScrollEnd,
657
+ // itemsFromStartToDisplayEnd,
658
+ // itemsOnDisplay,
659
+ // itemsOnDisplayLength,
660
+ isVertical,
661
+ // leftHiddenItemsWeight,
662
+ // leftItemLength,
663
+ // leftItemsWeight,
664
+ renderItems: renderItemsLength,
665
+ // rightItemLength,
666
+ // rightItemsWeight,
667
+ scrollSize, sizeProperty, snap, snippedPos, startPosition,
668
+ // totalItemsToDisplayEndWeight,
669
+ totalLength,
670
+ // totalSize,
671
+ startIndex, typicalItemSize, } = metrics, displayItems = [];
672
+ if (items.length) {
673
+ const actualSnippedPosition = snippedPos;
674
+ let pos = startPosition, renderItems = renderItemsLength, stickyItem, nextSticky, stickyItemIndex = -1, stickyItemSize = 0;
675
+ if (snap) {
676
+ for (let i = Math.min(itemsFromStartToScrollEnd > 0 ? itemsFromStartToScrollEnd : 0, totalLength - 1); i >= 0; i--) {
677
+ const id = items[i].id, sticky = stickyMap[id], size = dynamicSize ? ((_a = this.get(id)) === null || _a === void 0 ? void 0 : _a[sizeProperty]) || typicalItemSize : typicalItemSize;
678
+ stickyItemSize = size;
679
+ if (sticky > 0) {
680
+ const measures = {
681
+ x: isVertical ? 0 : actualSnippedPosition,
682
+ y: isVertical ? actualSnippedPosition : 0,
683
+ width: normalizedItemWidth,
684
+ height: normalizedItemHeight,
685
+ }, config = {
686
+ isVertical,
687
+ sticky,
688
+ snap,
689
+ snapped: true,
690
+ snappedOut: false,
691
+ dynamic: dynamicSize,
692
+ };
693
+ const itemData = items[i];
694
+ stickyItem = { id, measures, data: itemData, config };
695
+ stickyItemIndex = i;
696
+ displayItems.push(stickyItem);
697
+ break;
698
+ }
699
+ }
700
+ }
701
+ let i = startIndex;
702
+ while (renderItems > 0) {
703
+ if (i >= totalLength) {
704
+ break;
705
+ }
706
+ const id = items[i].id, size = dynamicSize ? ((_b = this.get(id)) === null || _b === void 0 ? void 0 : _b[sizeProperty]) || typicalItemSize : typicalItemSize;
707
+ if (id !== (stickyItem === null || stickyItem === void 0 ? void 0 : stickyItem.id)) {
708
+ const snapped = snap && stickyMap[id] > 0 && pos <= scrollSize, measures = {
709
+ x: isVertical ? 0 : pos,
710
+ y: isVertical ? pos : 0,
711
+ width: normalizedItemWidth,
712
+ height: normalizedItemHeight,
713
+ }, config = {
714
+ isVertical,
715
+ sticky: stickyMap[id],
716
+ snap,
717
+ snapped: false,
718
+ snappedOut: false,
719
+ dynamic: dynamicSize,
720
+ };
721
+ const itemData = items[i];
722
+ const item = { id, measures, data: itemData, config };
723
+ if (!nextSticky && stickyItemIndex < i && snap && stickyMap[id] > 0 && pos <= scrollSize + size) {
724
+ item.measures.x = isVertical ? 0 : snapped ? actualSnippedPosition : pos;
725
+ item.measures.y = isVertical ? snapped ? actualSnippedPosition : pos : 0;
726
+ nextSticky = item;
727
+ nextSticky.config.snapped = snapped;
728
+ }
729
+ displayItems.push(item);
730
+ }
731
+ renderItems -= 1;
732
+ pos += size;
733
+ i++;
734
+ }
735
+ const axis = isVertical ? Y_PROP_NAME : X_PROP_NAME;
736
+ if (nextSticky && stickyItem && nextSticky.measures[axis] <= scrollSize + stickyItemSize) {
737
+ if (nextSticky.measures[axis] > scrollSize) {
738
+ stickyItem.measures[axis] = nextSticky.measures[axis] - stickyItemSize;
739
+ stickyItem.config.snapped = nextSticky.config.snapped = false;
740
+ stickyItem.config.snappedOut = true;
741
+ stickyItem.config.sticky = 1;
742
+ }
743
+ else {
744
+ nextSticky.config.snapped = true;
745
+ }
746
+ }
747
+ }
748
+ return displayItems;
749
+ }
750
+ /**
751
+ * tracking by propName
752
+ */
753
+ track() {
754
+ if (!this._items || !this._displayComponents) {
755
+ return;
756
+ }
757
+ this._tracker.track(this._items, this._displayComponents);
758
+ }
759
+ setDisplayObjectIndexMapById(v) {
760
+ this._tracker.displayObjectIndexMapById = v;
761
+ }
762
+ untrackComponentByIdProperty(component) {
763
+ this._tracker.untrackComponentByIdProperty(component);
764
+ }
765
+ cacheElements() {
766
+ if (!this._displayComponents) {
767
+ return;
768
+ }
769
+ for (let i = 0, l = this._displayComponents.length; i < l; i++) {
770
+ const component = this._displayComponents[i], itemId = component.instance.itemId;
771
+ if (itemId === undefined) {
772
+ continue;
773
+ }
774
+ const bounds = component.instance.getBounds();
775
+ this.set(itemId, bounds);
776
+ }
777
+ }
778
+ /**
779
+ * Returns calculated bounds from cache
780
+ */
781
+ getBoundsFromCache(items, typicalItemSize, isVertical) {
782
+ const sizeProperty = isVertical ? HEIGHT_PROP_NAME : WIDTH_PROP_NAME, map = this._map;
783
+ let size = 0;
784
+ for (let i = 0, l = items.length; i < l; i++) {
785
+ const item = items[i];
786
+ if (map.has(item.id)) {
787
+ const bounds = map.get(item.id);
788
+ size += bounds ? bounds[sizeProperty] : typicalItemSize;
789
+ }
790
+ else {
791
+ size += typicalItemSize;
792
+ }
793
+ }
794
+ return size;
795
+ }
796
+ dispose() {
797
+ super.dispose();
798
+ if (this._debounceChanges) {
799
+ this._debounceChanges.dispose();
800
+ }
801
+ if (this._tracker) {
802
+ this._tracker.dispose();
803
+ }
804
+ }
805
+ }
806
+
807
+ /**
808
+ * Base disposable component
809
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/utils/disposableComponent.ts
810
+ * @author Evgenii Grebennikov
811
+ * @email djonnyx@gmail.com
812
+ */
813
+ class DisposableComponent {
814
+ constructor() {
815
+ this._$unsubscribe = new Subject();
816
+ }
817
+ ngOnDestroy() {
818
+ if (this._$unsubscribe) {
819
+ this._$unsubscribe.next();
820
+ this._$unsubscribe.complete();
821
+ }
822
+ }
823
+ }
824
+ DisposableComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DisposableComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
825
+ DisposableComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.3.0", type: DisposableComponent, selector: "ng-component", ngImport: i0, template: ``, isInline: true });
826
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DisposableComponent, decorators: [{
827
+ type: Component,
828
+ args: [{
829
+ template: ``,
830
+ }]
831
+ }] });
832
+
833
+ /**
834
+ * Virtual list component.
835
+ * Maximum performance for extremely large lists.
836
+ * It is based on algorithms for virtualization of screen objects.
837
+ * @link https://github.com/DjonnyX/ng-virtual-list/blob/main/projects/ng-virtual-list/src/lib/ng-virtual-list.component.ts
838
+ * @author Evgenii Grebennikov
839
+ * @email djonnyx@gmail.com
840
+ */
841
+ class NgVirtualListComponent extends DisposableComponent {
842
+ constructor(_cdr, _elementRef) {
843
+ super();
844
+ this._cdr = _cdr;
845
+ this._elementRef = _elementRef;
846
+ this._id = NgVirtualListComponent.__nextId;
847
+ /**
848
+ * Fires when the list has been scrolled.
849
+ */
850
+ this.onScroll = new EventEmitter$1();
851
+ /**
852
+ * Fires when the list has completed scrolling.
853
+ */
854
+ this.onScrollEnd = new EventEmitter$1();
855
+ this._$items = new BehaviorSubject(undefined);
856
+ this.$items = this._$items.asObservable();
857
+ this._$snap = new BehaviorSubject(DEFAULT_SNAP);
858
+ this.$snap = this._$snap.asObservable();
859
+ this._$snapToItem = new BehaviorSubject(DEFAULT_SNAP_TO_ITEM);
860
+ this.$snapToItem = this._$snapToItem.asObservable();
861
+ this._$itemRenderer = new BehaviorSubject(undefined);
862
+ this.$itemRenderer = this._$itemRenderer.asObservable();
863
+ this._$stickyMap = new BehaviorSubject({});
864
+ this.$stickyMap = this._$stickyMap.asObservable();
865
+ this._itemSizeOptions = (v) => {
866
+ if (v === undefined) {
867
+ return DEFAULT_ITEM_SIZE;
868
+ }
869
+ const val = Number(v);
870
+ return Number.isNaN(val) || val <= 0 ? DEFAULT_ITEM_SIZE : val;
871
+ };
872
+ this._$itemSize = new BehaviorSubject(DEFAULT_ITEM_SIZE);
873
+ this.$itemSize = this._$itemSize.asObservable();
874
+ this._$dynamicSize = new BehaviorSubject(DEFAULT_DYNAMIC_SIZE);
875
+ this.$dynamicSize = this._$dynamicSize.asObservable();
876
+ this._$direction = new BehaviorSubject(DEFAULT_DIRECTION);
877
+ this.$direction = this._$direction.asObservable();
878
+ this._$itemsOffset = new BehaviorSubject(DEFAULT_ITEMS_OFFSET);
879
+ this.$itemsOffset = this._$itemsOffset.asObservable();
880
+ this._isVertical = this.getIsVertical();
881
+ this._displayComponents = [];
882
+ this._$bounds = new BehaviorSubject(null);
883
+ this._$scrollSize = new BehaviorSubject(0);
884
+ this._resizeObserver = null;
885
+ /**
886
+ * only dynamic
887
+ */
888
+ this._$scrolledItemId = new BehaviorSubject(undefined);
889
+ this._onResizeHandler = () => {
890
+ var _a, _b, _c;
891
+ this._$bounds.next((_c = (_b = (_a = this._container) === null || _a === void 0 ? void 0 : _a.nativeElement) === null || _b === void 0 ? void 0 : _b.getBoundingClientRect()) !== null && _c !== void 0 ? _c : null);
892
+ };
893
+ this._scrollDirection = 0;
894
+ this._onScrollHandler = (e) => {
895
+ var _a;
896
+ this._$scrolledItemId.next(undefined);
897
+ const container = (_a = this._container) === null || _a === void 0 ? void 0 : _a.nativeElement;
898
+ if (container) {
899
+ const dynamicSize = this.dynamicSize, delta = this._trackBox.delta, scrollSize = (this._isVertical ? container.scrollTop : container.scrollLeft), previouseScrollSize = this._$scrollSize.getValue();
900
+ let actualScrollSize = scrollSize;
901
+ this._scrollDirection = previouseScrollSize > scrollSize ? -1 : 1;
902
+ if (dynamicSize && delta !== 0) {
903
+ actualScrollSize = scrollSize + delta;
904
+ const params = {
905
+ [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: actualScrollSize,
906
+ behavior: BEHAVIOR_INSTANT
907
+ };
908
+ const container = this._container;
909
+ if (container) {
910
+ this.scrollImmediately(container, params);
911
+ this._trackBox.clearDelta();
912
+ }
913
+ }
914
+ this._$scrollSize.next(actualScrollSize);
915
+ this.onScroll.emit(actualScrollSize);
916
+ }
917
+ };
918
+ this._scrollImmediatelyHandler = undefined;
919
+ this._onScrollEndHandler = (e, fireEvent = true) => {
920
+ const container = this._container;
921
+ if (container) {
922
+ const itemSize = this.itemSize, snapToItem = this.snapToItem, dynamicSize = this.dynamicSize, delta = this._trackBox.delta, scrollSize = (this._isVertical ? container.nativeElement.scrollTop : container.nativeElement.scrollLeft);
923
+ let actualScrollSize = scrollSize;
924
+ if (dynamicSize && delta !== 0) {
925
+ actualScrollSize = scrollSize + delta;
926
+ if (scrollSize !== actualScrollSize) {
927
+ const params = {
928
+ [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: actualScrollSize,
929
+ behavior: BEHAVIOR_INSTANT
930
+ };
931
+ this._trackBox.clearDelta();
932
+ this._$scrollSize.next(actualScrollSize);
933
+ container.nativeElement.scroll(params);
934
+ return;
935
+ }
936
+ }
937
+ else {
938
+ const scrollItems = Math.round(scrollSize / itemSize);
939
+ actualScrollSize = snapToItem ? scrollItems * itemSize : scrollSize;
940
+ if (scrollSize !== actualScrollSize) {
941
+ const params = {
942
+ [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: actualScrollSize,
943
+ behavior: BEHAVIOR_INSTANT
944
+ };
945
+ container.nativeElement.scroll(params);
946
+ }
947
+ }
948
+ this._$scrollSize.next(actualScrollSize);
949
+ if (fireEvent) {
950
+ this.onScrollEnd.emit(actualScrollSize);
951
+ }
952
+ }
953
+ };
954
+ this._$initialized = new BehaviorSubject(false);
955
+ /**
956
+ * Dictionary of element sizes by their id
957
+ */
958
+ this._trackBox = new TrackBox(TRACK_BY_PROPERTY_NAME);
959
+ this._onTrackBoxChangeHandler = (v) => {
960
+ this._$cacheVersion.next(v);
961
+ };
962
+ this._$cacheVersion = new BehaviorSubject(-1);
963
+ NgVirtualListComponent.__nextId = NgVirtualListComponent.__nextId + 1 === Number.MAX_SAFE_INTEGER
964
+ ? 0 : NgVirtualListComponent.__nextId + 1;
965
+ this._id = NgVirtualListComponent.__nextId;
966
+ this._$initialized = new BehaviorSubject(false);
967
+ this.$initialized = this._$initialized.asObservable();
968
+ this._trackBox.displayComponents = this._displayComponents;
969
+ const $bounds = this._$bounds.asObservable().pipe(filter(b => !!b)), $items = this.$items.pipe(map(i => !i ? [] : i)), $scrollSize = this._$scrollSize.asObservable(), $itemSize = this.$itemSize.pipe(map(v => v <= 0 ? DEFAULT_ITEM_SIZE : v)), $itemsOffset = this.$itemsOffset.pipe(map(v => v < 0 ? DEFAULT_ITEMS_OFFSET : v)), $stickyMap = this.$stickyMap.pipe(map(v => !v ? {} : v)), $snap = this.$snap, $isVertical = this.$direction.pipe(map(v => this.getIsVertical(v || DEFAULT_DIRECTION))), $dynamicSize = this.$dynamicSize, $cacheVersion = this.$cacheVersion, $scrolledItemId = this._$scrolledItemId.asObservable();
970
+ $isVertical.pipe(takeUntil(this._$unsubscribe), tap(v => {
971
+ this._isVertical = v;
972
+ const el = this._elementRef.nativeElement;
973
+ toggleClassName(el, v ? CLASS_LIST_VERTICAL : CLASS_LIST_HORIZONTAL, true);
974
+ })).subscribe();
975
+ $dynamicSize.pipe(takeUntil(this._$unsubscribe), tap(dynamicSize => {
976
+ this.listenCacheChangesIfNeed(dynamicSize);
977
+ })).subscribe();
978
+ combineLatest([this.$initialized, $scrolledItemId, $bounds, $items, $stickyMap, $scrollSize, $itemSize,
979
+ $itemsOffset, $snap, $isVertical, $dynamicSize, $cacheVersion,
980
+ ]).pipe(takeUntil(this._$unsubscribe), distinctUntilChanged(), debounceTime(0), filter(([initialized]) => !!initialized), switchMap(([, scrolledItemId, bounds, items, stickyMap, scrollSize, itemSize, itemsOffset, snap, isVertical, dynamicSize, cacheVersion,]) => {
981
+ const { width, height } = bounds;
982
+ let actualScrollSize = scrollSize;
983
+ const opts = {
984
+ bounds: { width, height }, collection: items, dynamicSize, isVertical, itemSize,
985
+ itemsOffset, scrollSize: scrollSize, snap, fromItemId: scrolledItemId,
986
+ };
987
+ if (dynamicSize && scrolledItemId !== undefined) {
988
+ const scrollSize = this._trackBox.getItemPosition(scrolledItemId, stickyMap, Object.assign(Object.assign({}, opts), { scrollSize: actualScrollSize }));
989
+ actualScrollSize = scrollSize;
990
+ this._$scrollSize.next(actualScrollSize);
991
+ }
992
+ const scrollDirection = this._scrollDirection, { displayItems, totalSize } = this._trackBox.updateCollection(items, stickyMap, Object.assign(Object.assign({}, opts), { scrollSize: actualScrollSize, scrollDirection }));
993
+ this.resetBoundsSize(isVertical, totalSize);
994
+ this.createDisplayComponentsIfNeed(displayItems);
995
+ this.tracking();
996
+ if (dynamicSize && scrolledItemId !== undefined) {
997
+ const container = this._container;
998
+ if (container) {
999
+ const params = { [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: scrollSize, behavior: BEHAVIOR_AUTO };
1000
+ this.scrollImmediately(container, params);
1001
+ }
1002
+ }
1003
+ return of(displayItems);
1004
+ })).subscribe();
1005
+ combineLatest([this.$initialized, this.$itemRenderer]).pipe(takeUntil(this._$unsubscribe), distinctUntilChanged(), filter(([initialized]) => !!initialized), tap(([, itemRenderer]) => {
1006
+ this.resetRenderers(itemRenderer);
1007
+ }));
1008
+ }
1009
+ /**
1010
+ * Readonly. Returns the unique identifier of the component.
1011
+ */
1012
+ get id() { return this._id; }
1013
+ /**
1014
+ * Collection of list items.
1015
+ */
1016
+ set items(v) {
1017
+ if (this._$items.getValue() === v) {
1018
+ return;
1019
+ }
1020
+ this._$items.next(v);
1021
+ this._cdr.markForCheck();
1022
+ }
1023
+ ;
1024
+ get items() { return this._$items.getValue(); }
1025
+ /**
1026
+ * Determines whether elements will snap. Default value is "true".
1027
+ */
1028
+ set snap(v) {
1029
+ if (this._$snap.getValue() === v) {
1030
+ return;
1031
+ }
1032
+ this._$snap.next(v);
1033
+ this._cdr.markForCheck();
1034
+ }
1035
+ ;
1036
+ get snap() { return this._$snap.getValue(); }
1037
+ /**
1038
+ * Determines whether scroll positions will be snapped to the element. Default value is "false".
1039
+ */
1040
+ set snapToItem(v) {
1041
+ if (this._$snapToItem.getValue() === v) {
1042
+ return;
1043
+ }
1044
+ this._$snapToItem.next(v);
1045
+ this._cdr.markForCheck();
1046
+ }
1047
+ ;
1048
+ get snapToItem() { return this._$snapToItem.getValue(); }
1049
+ /**
1050
+ * Rendering element template.
1051
+ */
1052
+ set itemRenderer(v) {
1053
+ if (this._$itemRenderer.getValue() === v) {
1054
+ return;
1055
+ }
1056
+ this._$itemRenderer.next(v);
1057
+ this._cdr.markForCheck();
1058
+ }
1059
+ ;
1060
+ get itemRenderer() { return this._$itemRenderer.getValue(); }
1061
+ /**
1062
+ * Dictionary zIndex by id of the list element. If the value is not set or equal to 0,
1063
+ * then a simple element is displayed, if the value is greater than 0, then the sticky position mode is enabled for the element.
1064
+ */
1065
+ set stickyMap(v) {
1066
+ if (this._$stickyMap.getValue() === v) {
1067
+ return;
1068
+ }
1069
+ this._$stickyMap.next(v);
1070
+ this._cdr.markForCheck();
1071
+ }
1072
+ ;
1073
+ get stickyMap() { return this._$stickyMap.getValue(); }
1074
+ /**
1075
+ * If direction = 'vertical', then the height of a typical element. If direction = 'horizontal', then the width of a typical element.
1076
+ * Ignored if the dynamicSize property is true.
1077
+ */
1078
+ set itemSize(v) {
1079
+ if (this._$itemSize.getValue() === v) {
1080
+ return;
1081
+ }
1082
+ this._$itemSize.next(this._itemSizeOptions(v));
1083
+ this._cdr.markForCheck();
1084
+ }
1085
+ ;
1086
+ get itemSize() { return this._$itemSize.getValue(); }
1087
+ /**
1088
+ * If true then the items in the list can have different sizes and the itemSize property is ignored.
1089
+ * If false then the items in the list have a fixed size specified by the itemSize property. The default value is false.
1090
+ */
1091
+ set dynamicSize(v) {
1092
+ if (this._$dynamicSize.getValue() === v) {
1093
+ return;
1094
+ }
1095
+ this._$dynamicSize.next(v);
1096
+ this._cdr.markForCheck();
1097
+ }
1098
+ ;
1099
+ get dynamicSize() { return this._$dynamicSize.getValue(); }
1100
+ /**
1101
+ * Determines the direction in which elements are placed. Default value is "vertical".
1102
+ */
1103
+ set direction(v) {
1104
+ if (this._$direction.getValue() === v) {
1105
+ return;
1106
+ }
1107
+ this._$direction.next(v);
1108
+ this._cdr.markForCheck();
1109
+ }
1110
+ ;
1111
+ get direction() { return this._$direction.getValue(); }
1112
+ /**
1113
+ * Number of elements outside the scope of visibility. Default value is 2.
1114
+ */
1115
+ set itemsOffset(v) {
1116
+ if (this._$itemsOffset.getValue() === v) {
1117
+ return;
1118
+ }
1119
+ this._$itemsOffset.next(v);
1120
+ }
1121
+ ;
1122
+ get itemsOffset() { return this._$itemsOffset.getValue(); }
1123
+ scrollImmediately(container, params) {
1124
+ this.clearScrollImmediately();
1125
+ container.nativeElement.removeEventListener(SCROLL_END, this._onScrollEndHandler);
1126
+ const handler = () => {
1127
+ if (container) {
1128
+ container.nativeElement.removeEventListener(SCROLL_END, handler);
1129
+ container.nativeElement.scroll(params);
1130
+ container.nativeElement.addEventListener(SCROLL_END, this._onScrollEndHandler);
1131
+ }
1132
+ };
1133
+ container.nativeElement.addEventListener(SCROLL_END, handler);
1134
+ container.nativeElement.scroll(params);
1135
+ this._scrollImmediatelyHandler = handler;
1136
+ }
1137
+ clearScrollImmediately() {
1138
+ if (this._scrollImmediatelyHandler === undefined) {
1139
+ return;
1140
+ }
1141
+ const container = this._container;
1142
+ if (container) {
1143
+ container.nativeElement.removeEventListener(SCROLL_END, this._scrollImmediatelyHandler);
1144
+ }
1145
+ }
1146
+ get $cacheVersion() { return this._$cacheVersion.asObservable(); }
1147
+ ngOnInit() {
1148
+ this._$initialized.next(true);
1149
+ }
1150
+ listenCacheChangesIfNeed(value) {
1151
+ if (value) {
1152
+ if (!this._trackBox.hasEventListener(TRACK_BOX_CHANGE_EVENT_NAME, this._onTrackBoxChangeHandler)) {
1153
+ this._trackBox.addEventListener(TRACK_BOX_CHANGE_EVENT_NAME, this._onTrackBoxChangeHandler);
1154
+ }
1155
+ }
1156
+ else {
1157
+ if (this._trackBox.hasEventListener(TRACK_BOX_CHANGE_EVENT_NAME, this._onTrackBoxChangeHandler)) {
1158
+ this._trackBox.removeEventListener(TRACK_BOX_CHANGE_EVENT_NAME, this._onTrackBoxChangeHandler);
1159
+ }
1160
+ }
1161
+ }
1162
+ getIsVertical(d) {
1163
+ const dir = d || this.direction;
1164
+ return isDirection(dir, Directions.VERTICAL);
1165
+ }
1166
+ createDisplayComponentsIfNeed(displayItems) {
1167
+ var _a;
1168
+ if (!displayItems || !this._listContainerRef) {
1169
+ this._trackBox.setDisplayObjectIndexMapById({});
1170
+ return;
1171
+ }
1172
+ this._trackBox.items = displayItems;
1173
+ const _listContainerRef = this._listContainerRef;
1174
+ while (this._displayComponents.length < displayItems.length) {
1175
+ if (_listContainerRef) {
1176
+ const comp = _listContainerRef.createComponent(NgVirtualListItemComponent);
1177
+ this._displayComponents.push(comp);
1178
+ }
1179
+ }
1180
+ const maxLength = displayItems.length;
1181
+ while (this._displayComponents.length > maxLength) {
1182
+ const comp = this._displayComponents.pop();
1183
+ comp === null || comp === void 0 ? void 0 : comp.destroy();
1184
+ const id = (_a = comp === null || comp === void 0 ? void 0 : comp.instance.item) === null || _a === void 0 ? void 0 : _a.id;
1185
+ if (id !== undefined) {
1186
+ this._trackBox.untrackComponentByIdProperty(comp === null || comp === void 0 ? void 0 : comp.instance);
1187
+ }
1188
+ }
1189
+ this.resetRenderers();
1190
+ }
1191
+ resetRenderers(itemRenderer) {
1192
+ const doMap = {};
1193
+ for (let i = 0, l = this._displayComponents.length; i < l; i++) {
1194
+ const item = this._displayComponents[i];
1195
+ item.instance.renderer = itemRenderer || this.itemRenderer;
1196
+ doMap[item.instance.id] = i;
1197
+ }
1198
+ this._trackBox.setDisplayObjectIndexMapById(doMap);
1199
+ }
1200
+ /**
1201
+ * Tracking by id
1202
+ */
1203
+ tracking() {
1204
+ this._trackBox.track();
1205
+ }
1206
+ resetBoundsSize(isVertical, totalSize) {
1207
+ const l = this._list;
1208
+ if (l) {
1209
+ l.nativeElement.style[isVertical ? HEIGHT_PROP_NAME : WIDTH_PROP_NAME] = `${totalSize}${PX}`;
1210
+ }
1211
+ }
1212
+ /**
1213
+ * The method scrolls the list to the element with the given id and returns the value of the scrolled area.
1214
+ * Behavior accepts the values ​​"auto", "instant" and "smooth".
1215
+ */
1216
+ scrollTo(id, behavior = BEHAVIOR_AUTO) {
1217
+ const items = this.items;
1218
+ if (!items || !items.length) {
1219
+ return;
1220
+ }
1221
+ const dynamicSize = this.dynamicSize, container = this._container, itemSize = this.itemSize;
1222
+ if (container) {
1223
+ if (dynamicSize) {
1224
+ if (container) {
1225
+ container.nativeElement.removeEventListener(SCROLL, this._onScrollHandler);
1226
+ container.nativeElement.removeEventListener(SCROLL_END, this._onScrollEndHandler);
1227
+ }
1228
+ const { width, height } = this._$bounds.getValue() || { width: 0, height: 0 }, stickyMap = this.stickyMap, items = this.items, opts = {
1229
+ bounds: { width, height }, collection: items, dynamicSize, isVertical: this._isVertical, itemSize,
1230
+ itemsOffset: this.itemsOffset, scrollSize: this._isVertical ? container.nativeElement.scrollTop : container.nativeElement.scrollLeft,
1231
+ snap: this.snap, fromItemId: id,
1232
+ }, scrollSize = this._trackBox.getItemPosition(id, stickyMap, opts), params = { [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: scrollSize, behavior };
1233
+ this._$scrolledItemId.next(id);
1234
+ this._$scrollSize.next(scrollSize);
1235
+ if (container) {
1236
+ const handler = () => {
1237
+ if (container) {
1238
+ container.nativeElement.removeEventListener(SCROLL_END, handler);
1239
+ clearTimeout(this._scrollToTimeout);
1240
+ this._scrollToTimeout = setTimeout(() => {
1241
+ container.nativeElement.addEventListener(SCROLL, this._onScrollHandler);
1242
+ container.nativeElement.addEventListener(SCROLL_END, this._onScrollEndHandler);
1243
+ }, 100);
1244
+ this.listenCacheChangesIfNeed(dynamicSize);
1245
+ this.onScroll.emit(scrollSize);
1246
+ }
1247
+ };
1248
+ container.nativeElement.addEventListener(SCROLL_END, handler);
1249
+ }
1250
+ container.nativeElement.scroll(params);
1251
+ }
1252
+ else {
1253
+ const index = items.findIndex(item => item.id === id), scrollSize = index * this.itemSize;
1254
+ const params = { [this._isVertical ? TOP_PROP_NAME : LEFT_PROP_NAME]: scrollSize, behavior };
1255
+ container.nativeElement.scroll(params);
1256
+ }
1257
+ }
1258
+ }
1259
+ scrollToEnd(behavior = BEHAVIOR_INSTANT) {
1260
+ const items = this.items, latItem = items[items.length > 0 ? items.length - 1 : 0];
1261
+ this.scrollTo(latItem.id, behavior);
1262
+ }
1263
+ ngAfterViewInit() {
1264
+ const containerEl = this._container;
1265
+ if (containerEl) {
1266
+ containerEl.nativeElement.addEventListener(SCROLL, this._onScrollHandler);
1267
+ containerEl.nativeElement.addEventListener(SCROLL_END, this._onScrollEndHandler);
1268
+ this._resizeObserver = new ResizeObserver(this._onResizeHandler);
1269
+ this._resizeObserver.observe(containerEl.nativeElement);
1270
+ this._onResizeHandler();
1271
+ }
1272
+ }
1273
+ ngOnDestroy() {
1274
+ super.ngOnDestroy();
1275
+ if (this._scrollToTimeout) {
1276
+ clearTimeout(this._scrollToTimeout);
1277
+ }
1278
+ if (this._trackBox) {
1279
+ this._trackBox.dispose();
1280
+ }
1281
+ const containerEl = this._container;
1282
+ if (containerEl) {
1283
+ containerEl.nativeElement.removeEventListener(SCROLL, this._onScrollHandler);
1284
+ containerEl.nativeElement.removeEventListener(SCROLL_END, this._onScrollEndHandler);
1285
+ if (this._resizeObserver) {
1286
+ this._resizeObserver.unobserve(containerEl.nativeElement);
1287
+ }
1288
+ }
1289
+ if (this._displayComponents) {
1290
+ while (this._displayComponents.length > 0) {
1291
+ const comp = this._displayComponents.pop();
1292
+ comp === null || comp === void 0 ? void 0 : comp.destroy();
1293
+ }
1294
+ }
1295
+ }
1296
+ }
1297
+ NgVirtualListComponent.__nextId = 0;
1298
+ NgVirtualListComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
1299
+ NgVirtualListComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.3.0", type: NgVirtualListComponent, selector: "ng-virtual-list", inputs: { items: "items", snap: "snap", snapToItem: "snapToItem", itemRenderer: "itemRenderer", stickyMap: "stickyMap", itemSize: "itemSize", dynamicSize: "dynamicSize", direction: "direction", itemsOffset: "itemsOffset" }, outputs: { onScroll: "onScroll", onScrollEnd: "onScrollEnd" }, viewQueries: [{ propertyName: "_listContainerRef", first: true, predicate: ["renderersContainer"], descendants: true, read: ViewContainerRef }, { propertyName: "_container", first: true, predicate: ["container"], descendants: true, read: (ElementRef) }, { propertyName: "_list", first: true, predicate: ["list"], descendants: true, read: (ElementRef) }], usesInheritance: true, ngImport: i0, template: "<div #container part=\"scroller\" class=\"ngvl__container\">\r\n <ul #list part=\"list\" class=\"ngvl__list\">\r\n <ng-container #renderersContainer></ng-container>\r\n </ul>\r\n</div>", styles: [":host{display:block;width:400px;overflow:hidden}:host(.horizontal){height:48px}:host(.vertical){height:320px}.ngvl__container{overflow:auto;width:100%;height:100%}.ngvl__list{position:relative;list-style:none;padding:0;margin:0;width:100%;height:100%}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.ShadowDom });
1300
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListComponent, decorators: [{
1301
+ type: Component,
1302
+ args: [{ selector: 'ng-virtual-list', changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.ShadowDom, template: "<div #container part=\"scroller\" class=\"ngvl__container\">\r\n <ul #list part=\"list\" class=\"ngvl__list\">\r\n <ng-container #renderersContainer></ng-container>\r\n </ul>\r\n</div>", styles: [":host{display:block;width:400px;overflow:hidden}:host(.horizontal){height:48px}:host(.vertical){height:320px}.ngvl__container{overflow:auto;width:100%;height:100%}.ngvl__list{position:relative;list-style:none;padding:0;margin:0;width:100%;height:100%}\n"] }]
1303
+ }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }]; }, propDecorators: { _listContainerRef: [{
1304
+ type: ViewChild,
1305
+ args: ['renderersContainer', { read: ViewContainerRef }]
1306
+ }], _container: [{
1307
+ type: ViewChild,
1308
+ args: ['container', { read: (ElementRef) }]
1309
+ }], _list: [{
1310
+ type: ViewChild,
1311
+ args: ['list', { read: (ElementRef) }]
1312
+ }], onScroll: [{
1313
+ type: Output
1314
+ }], onScrollEnd: [{
1315
+ type: Output
1316
+ }], items: [{
1317
+ type: Input
1318
+ }], snap: [{
1319
+ type: Input
1320
+ }], snapToItem: [{
1321
+ type: Input
1322
+ }], itemRenderer: [{
1323
+ type: Input
1324
+ }], stickyMap: [{
1325
+ type: Input
1326
+ }], itemSize: [{
1327
+ type: Input
1328
+ }], dynamicSize: [{
1329
+ type: Input
1330
+ }], direction: [{
1331
+ type: Input
1332
+ }], itemsOffset: [{
1333
+ type: Input
1334
+ }] } });
1335
+
1336
+ class NgVirtualListModule {
1337
+ }
1338
+ NgVirtualListModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
1339
+ NgVirtualListModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListModule, declarations: [NgVirtualListComponent, NgVirtualListItemComponent], imports: [CommonModule], exports: [NgVirtualListComponent] });
1340
+ NgVirtualListModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListModule, imports: [CommonModule] });
1341
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: NgVirtualListModule, decorators: [{
1342
+ type: NgModule,
1343
+ args: [{
1344
+ declarations: [NgVirtualListComponent, NgVirtualListItemComponent],
1345
+ exports: [NgVirtualListComponent],
1346
+ imports: [CommonModule],
1347
+ schemas: [NO_ERRORS_SCHEMA],
1348
+ }]
1349
+ }] });
1350
+
1351
+ /*
1352
+ * Public API Surface of ng-virtual-list
1353
+ */
1354
+
1355
+ /**
1356
+ * Generated bundle index. Do not edit.
1357
+ */
1358
+
1359
+ export { NgVirtualListComponent, NgVirtualListModule };
1360
+ //# sourceMappingURL=ng-virtual-list.mjs.map