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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 56× 1× 8945× 1× 196× 1× 56× 56× 56× 33× 56× 373× 56× 136× 136× 136× 136× 97× 1× 1× 96× 10× 10× 10× 86× 86× 39× 39× 39× 136× 136× 56× 44× 44× 44× 44× 44× 43× 53315× 53315× 1× 1× 53315× 2483× 42× 2441× 53315× 838× 838× 53315× 3279× 43× 50036× 1× 1× 2038× 2038× 2038× 1× 2038× 21× 15× 15× 15× 1× 1× 6× 6× 2017× 44× 44× 56× 9× 9× 9× 9× 9× 9× 9× 72× 72× 72× 72× 43× 193× 9× 56× 1× 1× 1× 1× 1× 1× 100× 100× 100× 100× 100× 1× 100× 1× 1× 1× 1× 1× 56× 173× 1× 172× 172× 33× 172× 172× 51× 121× 172× 1× 26× 9× 136× 172× 136× 136× 2× 2× 134× 34× 100× 136× 172× 56× 56× 196× 196× 196× 196× 38× 38× 7× 38× 38× 11× 11× 27× 7× 7× 196× 56× 56× 162× 162× 73× 73× 162× 56× 74× 74× 74× 8× 8× 66× 56× 220× 220× 56× 114× 114× 56× 75× 56× 52× 2× 2× 2× 56× 180× 8× 8000× 56× 140× 137× 137× 103× 137× 137× 137× 137× 31× 31× 31× 31× 26× 5× 5× 5× 60× 60× 5× 31× 106× 106× 137× 137× 137× 253× 1× 168× 1× 4× 1× 18× 1× 107× 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 { Injectable, OnDestroy } from '@angular/core'; import { BehaviorSubject, combineLatest as observableCombineLatest, from as observableFrom, Observable, of as observableOf, ReplaySubject, Subscription, timer as observableTimer } from 'rxjs'; import { combineLatest, debounceTime, distinctUntilChanged, filter, map, switchMap, tap } from 'rxjs/operators'; import { IViewPort, IViewPortItem } from './viewport.service'; export enum ViewportMode { disabled, fixed, variable, auto, } export enum ViewportDirection { vertical, horizontal, } /** Service de gestion du viewport d'une liste. * Ce service permet la gestion du viewport verticalement ou horizontalement. */ @Injectable() export class ViewPortService implements OnDestroy { public static itemDefaultSize = 40; public viewPort$: Observable<IViewPort>; public mode$ = new BehaviorSubject<ViewportMode | string>(ViewportMode.fixed); public items$ = new BehaviorSubject<IViewPortItem[]>([]); public maxSize$ = new BehaviorSubject<number | string>(0); public ensureItem$ = new BehaviorSubject<IViewPortItem | number>(null); public scrollPosition$ = new BehaviorSubject<number>(0); public element$ = new ReplaySubject<HTMLElement>(1); public itemsSize$ = new BehaviorSubject<number>(0); public direction$ = new BehaviorSubject<ViewportDirection | string>(ViewportDirection.vertical); public ensureParams$: Observable<IEnsureParams>; private subscriptions = [] as Subscription[]; private refresh$ = new BehaviorSubject<IViewPortRefreshParams>(null); private deleteSizeCache$ = new BehaviorSubject<boolean>(true); private lastCalculatedSize: number; private emptyViewPort = { beforeSize: 0, afterSize: 0, viewPortSize: 0, listSize: 0, visibleItems: [], startIndex: 0, endIndex: 0, scrollPos: 0, items: [], } as IViewPort; private measureViewPort = { beforeSize: 0, afterSize: 200000, viewPortSize: 0, listSize: 0, visibleItems: [], startIndex: 0, endIndex: 0, scrollPos: undefined, // Do not change the scroll pos in case of refresh is called when the list is scrolling (I.E. dynamic content loading) items: [], } as IViewPort; public viewPortResult$ = new BehaviorSubject<IViewPort>(this.emptyViewPort); private _mode: ViewportMode = ViewportMode.fixed; private _itemsSize = ViewPortService.itemDefaultSize; private _direction = ViewportDirection.vertical; private _scrollPosition = 0; private viewPort: IViewPort; private ignoreScrollCount = 0; public get mode() { return this._mode; } public get itemsSize() { return this._itemsSize; } public get direction() { return this._direction; } constructor() { this.viewPort$ = observableFrom(this.viewPortResult$); // const consoleLog = (_message: string) => { // // console.log(_message); // }; const innerSize = () => { return this._direction === ViewportDirection.horizontal ? window.innerWidth : window.innerHeight; }; const clientSize = (element: HTMLElement) => { return Math.ceil(this._direction === ViewportDirection.horizontal ? element.clientWidth : element.clientHeight); }; const calcFixedSizeViewPort$ = (items: IViewPortItem[], containerSize: number, scrollPos: number, itemDefaultSize: number, ensureParams: IEnsureParams): Observable<IViewPort> => { // consoleLog(`calcFixedSizeViewPort`); const maxCount = Math.ceil(containerSize / itemDefaultSize) + 1; const startRow = Math.floor(scrollPos / itemDefaultSize); const rowsCount = Math.min(items.length - startRow, maxCount); let startIndex: number; let endIndex: number; let newScrollPos: number; if (!ensureParams || ensureParams.index === undefined || !ensureParams.atEnd) { if (rowsCount < 0) { endIndex = items.length - 1; startIndex = endIndex + 1 - Math.min(items.length, maxCount); } else if (ensureParams.index !== undefined) { startIndex = ensureParams.index; endIndex = startIndex + rowsCount - 1; newScrollPos = startIndex * itemDefaultSize; } else { startIndex = startRow; endIndex = startIndex + rowsCount - 1; } } else { // Ensure visible from the end startIndex = Math.max(0, ensureParams.index + 1 - Math.min(items.length, maxCount)); endIndex = Math.max(ensureParams.index, rowsCount - 1); newScrollPos = (endIndex + 1) * itemDefaultSize - containerSize; } const visibleItems = items.slice(startIndex, endIndex + 1); return observableOf({ beforeSize: startIndex * itemDefaultSize, afterSize: (items.length - endIndex - 1) * itemDefaultSize, listSize: containerSize, viewPortSize: visibleItems.length * itemDefaultSize, visibleItems: visibleItems, startIndex: startIndex, endIndex: endIndex, scrollPos: newScrollPos, items: items, } as IViewPort); }; const calcVariableSizeViewPort$ = (items: IViewPortItem[], containerSize: number, scrollPos: number, itemDefaultSize: number, ensureParams: IEnsureParams): Observable<IViewPort> => { // consoleLog(`calcVariableSizeViewPort`); const visibleList = [] as IViewPortItem[]; let startIndex: number; let endIndex: number; let beforeSize = 0; let viewPortSize = 0; let afterSize = 0; let newScrollPos: number; if (!ensureParams || ensureParams.index === undefined || !ensureParams.atEnd) { items.forEach((item: IViewPortItem, index: number) => { const itemSize = item.size || itemDefaultSize; if (ensureParams && ensureParams.index === index) { startIndex = index; newScrollPos = beforeSize; } if (startIndex === undefined) { if (beforeSize + itemSize >= scrollPos) { startIndex = index; } else { beforeSize += itemSize; } } if (startIndex !== undefined && endIndex === undefined) { viewPortSize += itemSize; visibleList.push(item); } if (endIndex === undefined) { if ((beforeSize + viewPortSize > (newScrollPos || scrollPos) + containerSize) || index === items.length - 1) { endIndex = index; } } else { afterSize += itemSize; } }); } else { // Ensure visible from the end let index = items.length; while (--index >= 0) { const item = items[index]; const itemSize = item.size || itemDefaultSize; if (ensureParams.index === index) { endIndex = index; } if (endIndex !== undefined) { if (startIndex === undefined) { viewPortSize += itemSize; visibleList.unshift(item); if (viewPortSize > containerSize || index === 0) { startIndex = index; newScrollPos = viewPortSize - containerSize; } } else { newScrollPos += itemSize; beforeSize += itemSize; } } else { afterSize += itemSize; } } } Iif (ensureParams && ensureParams.index !== undefined && viewPortSize < containerSize && visibleList.length < items.length) { if (ensureParams.atEnd) { return calcVariableSizeViewPort$(items, containerSize, scrollPos, itemDefaultSize, { index: 0, atEnd: false, } as IEnsureParams); } else { return calcVariableSizeViewPort$(items, containerSize, scrollPos, itemDefaultSize, { index: items.length - 1, atEnd: true, } as IEnsureParams); } } return observableOf({ beforeSize: beforeSize, afterSize: afterSize, listSize: containerSize, viewPortSize: viewPortSize, visibleItems: visibleList, startIndex: startIndex || 0, endIndex: endIndex, scrollPos: newScrollPos, items: items, } as IViewPort); }; const calcAutoSizeViewPort$ = (items: IViewPortItem[], containerSize: number, scrollPos: number, element: HTMLElement, itemDefaultSize: number, ensureParams: IEnsureParams, isCalculation?: boolean): Observable<IViewPort> => { // consoleLog(`calcAutoSizeViewPort`); return calcVariableSizeViewPort$(items, containerSize, scrollPos, itemDefaultSize, ensureParams).pipe( switchMap((viewPort: IViewPort) => { const calculationRequired = !isCalculation && viewPort.visibleItems.find((item) => !item.size); Iif (!calculationRequired) { return observableOf(viewPort); } else { // Measure items size this.viewPortResult$.next(viewPort); return observableTimer(1).pipe( tap(() => { const elements = element.getElementsByClassName('listitem'); // tslint:disable-next-line:prefer-for-of for (let i = 0; i < elements.length; i++) { const itemElement = elements[i] as HTMLElement; const index = +itemElement.getAttribute('flat'); const item = viewPort.visibleItems[index - viewPort.startIndex]; if (item) { item.size = clientSize(itemElement); } } // Recalc Viewport size viewPort.viewPortSize = viewPort.visibleItems.reduce((size, item) => size += item.size || itemDefaultSize, 0); }), switchMap(() => calcVariableSizeViewPort$(items, containerSize, viewPort.scrollPos || scrollPos, itemDefaultSize, ensureParams))); } })); }; const calcDisabledViewPort$ = (items: IViewPortItem[], containerSize: number, scrollPos: number, element: HTMLElement, ensureParams: IEnsureParams, bindIfAny: boolean): Observable<IViewPort> => { let viewPortSize = 0; let startIndex: number; let endIndex: number; let newScrollPos: number; const elements = element.getElementsByClassName('listitem'); let viewPort = { beforeSize: 0, afterSize: 0, listSize: containerSize, viewPortSize: 0, visibleItems: [], startIndex: 0, endIndex: 0, scrollPos: 0, items: items, } as IViewPort; Iif (elements.length !== items.length && bindIfAny !== false) { this.viewPortResult$.next(viewPort); return observableTimer(1).pipe( switchMap(() => calcDisabledViewPort$(items, containerSize, scrollPos, element, ensureParams, false))); } Eif (!ensureParams || ensureParams.index === undefined || !ensureParams.atEnd) { for (let i = 0; i < elements.length; i++) { const itemElement = elements[i] as HTMLElement; const itemSize = items[i].size = clientSize(itemElement); Iif (ensureParams && ensureParams.index === i) { startIndex = i; newScrollPos = viewPortSize; } viewPortSize += itemSize; if (startIndex === undefined && viewPortSize > scrollPos) { startIndex = i; } if (endIndex === undefined && viewPortSize > (newScrollPos || scrollPos) + containerSize) { endIndex = i; } } } else { // Ensure visible from the end newScrollPos = 0; let listSize = 0; let i = elements.length; while (--i >= 0) { const itemElement = elements[i] as HTMLElement; const itemSize = items[i].size = clientSize(itemElement); if (ensureParams.index === i) { endIndex = i; } if (endIndex !== undefined) { if (startIndex === undefined) { listSize += itemSize; if (listSize > containerSize) { startIndex = i; newScrollPos = listSize - containerSize; } } else { newScrollPos += itemSize; } } viewPortSize += itemSize; } } startIndex = startIndex || 0; endIndex = endIndex || 0; // consoleLog(`viewPortSize ${viewPortSize}`); viewPort = { beforeSize: 0, afterSize: 0, listSize: containerSize, viewPortSize: viewPortSize, visibleItems: items.slice(startIndex, 1 + endIndex), startIndex: startIndex, endIndex: endIndex, scrollPos: newScrollPos, items: items, } as IViewPort; return observableOf(viewPort); }; const calcViewPort$: any = (items: IViewPortItem[], maxSize: number, scrollPos: number, element: HTMLElement, itemDefaultSize: number, ensureParams: IEnsureParams, fromMeasure?: boolean) => { // consoleLog(`calcViewPort`); if (!items || !items.length) { return observableOf(this.emptyViewPort); } let listSize = maxSize || clientSize(element); if (listSize <= ViewPortService.itemDefaultSize) { listSize = innerSize(); } return observableOf(this.mode).pipe( switchMap((mode) => { if (ensureParams.index !== undefined) { this.ignoreScrollCount++; } else { this.ignoreScrollCount = 0; } switch (mode) { case ViewportMode.disabled: return calcDisabledViewPort$(items, listSize, scrollPos, element, ensureParams, true); case ViewportMode.variable: return calcVariableSizeViewPort$(items, listSize, scrollPos, itemDefaultSize, ensureParams); case ViewportMode.auto: return calcAutoSizeViewPort$(items, listSize, scrollPos, element, itemDefaultSize, ensureParams); case ViewportMode.fixed: return calcFixedSizeViewPort$(items, listSize, scrollPos, itemDefaultSize, ensureParams); default: throw new Error('ViewPortService, invalide mode. The value can be disabled, variable, auto and fixed.'); } }), switchMap((viewPort: IViewPort) => { if (!fromMeasure) { const endScrollPos = viewPort.beforeSize + viewPort.viewPortSize + viewPort.afterSize - viewPort.listSize; if (this.mode !== ViewportMode.disabled && listSize < 2 * ViewPortService.itemDefaultSize) { // Measure again container and recalc viewport this.viewPortResult$.next(this.measureViewPort); return observableTimer(1).pipe(switchMap(() => calcViewPort$(items, maxSize, scrollPos, element, itemDefaultSize, ensureParams, true))); } else if (endScrollPos < 0 || (items.length && endScrollPos > 0 && (viewPort.scrollPos || scrollPos) > endScrollPos)) { // Scroll position is over the last item // Ensure last item visible and recalc viewport return calcViewPort$(items, maxSize, 0, element, itemDefaultSize, { index: items.length - 1, atEnd: true, } as IEnsureParams, true); } else Iif (this.mode === ViewportMode.auto && viewPort.viewPortSize < listSize) { // Rendered viewport is to small, render again to complete return calcViewPort$(items, maxSize, 0, element, itemDefaultSize, { index: items.length - 1, atEnd: true, } as IEnsureParams, true); } } // Return calculated viewport return observableOf(viewPort); }), tap(() => { // consoleLog(`clear ensureParams ${ensureParams && ensureParams.index}`); ensureParams.index = undefined; })); }; const items$ = observableFrom(this.items$); // .do(() => consoleLog('items')); // Ensure item visible by index or instance this.ensureParams$ = observableCombineLatest(this.ensureItem$, items$).pipe( map(([ensureItem, items]) => { this.ignoreScrollCount = 0; const ensureParams = {} as IEnsureParams; if (ensureItem !== undefined && ensureItem !== null && items && items.length) { let ensureIndex = ensureItem as number; if (isNaN(ensureIndex)) { ensureIndex = items.findIndex((itm) => ensureItem === itm); } Eif (ensureIndex >= 0) { if (this.viewPort && ensureIndex <= this.viewPort.startIndex) { ensureParams.index = ensureIndex; ensureParams.atEnd = false; } else if (!this.viewPort || ensureIndex >= this.viewPort.endIndex) { ensureParams.index = ensureIndex; ensureParams.atEnd = true; } } } return ensureParams; })); // .do((ensureParams) => consoleLog(`ensureParams index:${ensureParams && ensureParams.index} atEnd:${ensureParams && ensureParams.atEnd}`)); const maxSize$ = observableFrom(this.maxSize$).pipe( distinctUntilChanged()); // .do((value) => consoleLog(`maxSize ${value}`)); const refresh$ = observableFrom(this.refresh$).pipe( switchMap((params: IViewPortRefreshParams) => { this.ignoreScrollCount = 0; if (params) { Iif (params.clearMeasuredSize) { return observableFrom(this.items$).pipe( tap((items) => { this.lastCalculatedSize = undefined; items.forEach((item) => item.size = undefined); }), map(() => params)); } Iif (params.items) { params.items.forEach((item) => item.size = undefined); } } return observableOf(params); })); // .do(() => consoleLog('refresh')); const scrollPos$ = observableFrom(this.scrollPosition$).pipe( map((scrollPos) => this._scrollPosition = scrollPos || 0), map((scrollPos) => Math.max(scrollPos, 0)), filter(() => { if (this.ignoreScrollCount > 0) { this.ignoreScrollCount--; // consoleLog(`ignoreScrollCount ${this.ignoreScrollCount}`); return false; } else { return true; } }), distinctUntilChanged()); // .do((value) => consoleLog(`scrollPos ${value}`)); const mode$ = observableFrom(this.mode$).pipe( map((mode) => { this._mode = typeof mode === 'string' ? (<any>ViewportMode)[mode] : mode; return this._mode; }), distinctUntilChanged()); // .do((value) => consoleLog(`mode ${value}`)); const direction$ = observableFrom(this.direction$).pipe( map((direction) => { this._direction = typeof direction === 'string' ? (<any>ViewportDirection)[direction] : direction; return this._direction; }), distinctUntilChanged()); // .do((value) => consoleLog(`direction ${value}`)); const itemsSize$ = observableFrom(this.itemsSize$).pipe( distinctUntilChanged(), tap((value) => this._itemsSize = value)); // .do((value) => consoleLog(`itemsSize ${value}`)); const element$ = observableFrom(this.element$).pipe( tap((element) => { if (!element) { this.viewPort = undefined; this.ignoreScrollCount = 0; this.lastCalculatedSize = undefined; } })); // .do(() => consoleLog(`element`)); // Reset items size when direction change in auto mode this.subscriptions.push(observableCombineLatest(direction$, items$, mode$, this.deleteSizeCache$).pipe( filter(([_direction, items, mode]) => items && items.length && mode === ViewportMode.auto), switchMap(([_direction, items]) => items)) .subscribe((item) => { item.size = undefined; })); // Calc view port observable this.subscriptions.push(observableCombineLatest(element$, items$, refresh$, this.ensureParams$).pipe( combineLatest(direction$, mode$, itemsSize$, maxSize$), debounceTime(1), combineLatest(scrollPos$), filter(([[[element]]]) => !!element), // .do(([[[_element, _items, _refresh, ensureParams], _direction, _mode, _itemDefaultSize, _maxSize], _scrollPos]) => consoleLog(`combineLatest ${JSON.stringify(ensureParams)}`)) switchMap(([[[element, items, _refresh, ensureParams], _direction, _mode, itemDefaultSize, maxSize], _scrollPos]) => { // consoleLog(`combineLatest ${ensureParams && ensureParams.index}`); if (!itemDefaultSize) { itemDefaultSize = ViewPortService.itemDefaultSize; } const listSize = this.lastCalculatedSize || maxSize || clientSize(element); const scrollPos = this._scrollPosition; let maxSizeValue = maxSize === 'auto' ? 0 : +maxSize; if (items && items.length && (listSize === 'auto' || listSize < 2 * ViewPortService.itemDefaultSize)) { // Set the viewlist to the maximum height to measure the real max-height defined in the css // Use a blank div to do that // consoleLog(`viewPortResult for measure ${JSON.stringify(this.measureViewPort)}`); this.viewPortResult$.next(this.measureViewPort); // Wait next life cycle for the result return observableTimer(1).pipe( map(() => { // Get mx size from container maxSizeValue = this.lastCalculatedSize = clientSize(element); // Ensure that max size is not more than the items size if (this.mode === ViewportMode.fixed) { Iif (items.length * itemDefaultSize < maxSizeValue) { maxSizeValue = items.length * itemDefaultSize; } } else Eif (this.mode === ViewportMode.variable) { let maxItemsSize = 0; items.find((item) => { maxItemsSize += item.size || itemDefaultSize; return maxItemsSize > maxSizeValue; }); Iif (maxItemsSize < maxSizeValue) { maxSizeValue = maxItemsSize; } } else if (this.mode === ViewportMode.auto) { } return { element, scrollPos, items, maxSizeValue, itemDefaultSize, ensureParams }; })); } else { maxSizeValue = maxSizeValue || this.lastCalculatedSize; return observableOf({ element, scrollPos, items, maxSizeValue, itemDefaultSize, ensureParams }); } }), switchMap(({ element, scrollPos, items, maxSizeValue, itemDefaultSize, ensureParams }) => { // consoleLog(`calcViewPort ${ensureParams && ensureParams.index}`); return calcViewPort$(items, maxSizeValue, scrollPos, element, itemDefaultSize, ensureParams); })) .subscribe((viewPort: IViewPort) => { // consoleLog(`viewPortResult final ${JSON.stringify(viewPort)}`); this.viewPortResult$.next(viewPort); }, ((error) => { console.error(error); }))); // Cache last calculated viewport this.subscriptions.push(observableFrom(this.viewPortResult$).subscribe((viewPort: IViewPort) => this.viewPort = viewPort)); } public ngOnDestroy() { this.subscriptions.forEach((subscription: Subscription) => subscription.unsubscribe()); } public deleteSizeCache() { this.deleteSizeCache$.next(true); } public clear() { this.viewPortResult$.next(this.emptyViewPort); } public refresh(params?: IViewPortRefreshParams) { this.refresh$.next(params || null); } } export interface IEnsureParams { index: number; atEnd: boolean; } export interface IViewPort { beforeSize: number; afterSize: number; visibleItems: IViewPortItem[]; startIndex: number; endIndex: number; viewPortSize: number; listSize: number; scrollPos: number; items: IViewPortItem[]; } export interface IViewPortItem { size?: number; } export interface IViewPortRefreshParams { items: IViewPortItem[]; clearMeasuredSize: boolean; } |