scrolling.mjs 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. import * as i0 from '@angular/core';
  2. import { InjectionToken, forwardRef, Directive, Input, inject, NgZone, RendererFactory2, Injectable, ElementRef, Renderer2, ChangeDetectorRef, Injector, afterNextRender, booleanAttribute, Optional, Inject, Component, ViewEncapsulation, ChangeDetectionStrategy, Output, ViewChild, ViewContainerRef, TemplateRef, IterableDiffers, NgModule } from '@angular/core';
  3. import { Subject, of, Observable, Subscription, animationFrameScheduler, asapScheduler, isObservable } from 'rxjs';
  4. import { distinctUntilChanged, auditTime, filter, startWith, takeUntil, pairwise, switchMap, shareReplay } from 'rxjs/operators';
  5. import { c as coerceNumberProperty, a as coerceElement } from './element-x4z00URv.mjs';
  6. import { P as Platform } from './platform-DmdVEw_C.mjs';
  7. import { D as Directionality } from './directionality-CBXD4hga.mjs';
  8. import { g as getRtlScrollAxisType, R as RtlScrollAxisType, s as supportsScrollBehavior } from './scrolling-BkvA05C8.mjs';
  9. import { BidiModule } from './bidi.mjs';
  10. export { Dir as ɵɵDir } from './bidi.mjs';
  11. import { DOCUMENT } from '@angular/common';
  12. import { b as _VIEW_REPEATER_STRATEGY, A as ArrayDataSource, _ as _RecycleViewRepeaterStrategy } from './recycle-view-repeater-strategy-DoWdPqVw.mjs';
  13. import { i as isDataSource } from './data-source-D34wiQZj.mjs';
  14. /** The injection token used to specify the virtual scrolling strategy. */
  15. const VIRTUAL_SCROLL_STRATEGY = new InjectionToken('VIRTUAL_SCROLL_STRATEGY');
  16. /** Virtual scrolling strategy for lists with items of known fixed size. */
  17. class FixedSizeVirtualScrollStrategy {
  18. _scrolledIndexChange = new Subject();
  19. /** @docs-private Implemented as part of VirtualScrollStrategy. */
  20. scrolledIndexChange = this._scrolledIndexChange.pipe(distinctUntilChanged());
  21. /** The attached viewport. */
  22. _viewport = null;
  23. /** The size of the items in the virtually scrolling list. */
  24. _itemSize;
  25. /** The minimum amount of buffer rendered beyond the viewport (in pixels). */
  26. _minBufferPx;
  27. /** The number of buffer items to render beyond the edge of the viewport (in pixels). */
  28. _maxBufferPx;
  29. /**
  30. * @param itemSize The size of the items in the virtually scrolling list.
  31. * @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
  32. * @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
  33. */
  34. constructor(itemSize, minBufferPx, maxBufferPx) {
  35. this._itemSize = itemSize;
  36. this._minBufferPx = minBufferPx;
  37. this._maxBufferPx = maxBufferPx;
  38. }
  39. /**
  40. * Attaches this scroll strategy to a viewport.
  41. * @param viewport The viewport to attach this strategy to.
  42. */
  43. attach(viewport) {
  44. this._viewport = viewport;
  45. this._updateTotalContentSize();
  46. this._updateRenderedRange();
  47. }
  48. /** Detaches this scroll strategy from the currently attached viewport. */
  49. detach() {
  50. this._scrolledIndexChange.complete();
  51. this._viewport = null;
  52. }
  53. /**
  54. * Update the item size and buffer size.
  55. * @param itemSize The size of the items in the virtually scrolling list.
  56. * @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
  57. * @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
  58. */
  59. updateItemAndBufferSize(itemSize, minBufferPx, maxBufferPx) {
  60. if (maxBufferPx < minBufferPx && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  61. throw Error('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
  62. }
  63. this._itemSize = itemSize;
  64. this._minBufferPx = minBufferPx;
  65. this._maxBufferPx = maxBufferPx;
  66. this._updateTotalContentSize();
  67. this._updateRenderedRange();
  68. }
  69. /** @docs-private Implemented as part of VirtualScrollStrategy. */
  70. onContentScrolled() {
  71. this._updateRenderedRange();
  72. }
  73. /** @docs-private Implemented as part of VirtualScrollStrategy. */
  74. onDataLengthChanged() {
  75. this._updateTotalContentSize();
  76. this._updateRenderedRange();
  77. }
  78. /** @docs-private Implemented as part of VirtualScrollStrategy. */
  79. onContentRendered() {
  80. /* no-op */
  81. }
  82. /** @docs-private Implemented as part of VirtualScrollStrategy. */
  83. onRenderedOffsetChanged() {
  84. /* no-op */
  85. }
  86. /**
  87. * Scroll to the offset for the given index.
  88. * @param index The index of the element to scroll to.
  89. * @param behavior The ScrollBehavior to use when scrolling.
  90. */
  91. scrollToIndex(index, behavior) {
  92. if (this._viewport) {
  93. this._viewport.scrollToOffset(index * this._itemSize, behavior);
  94. }
  95. }
  96. /** Update the viewport's total content size. */
  97. _updateTotalContentSize() {
  98. if (!this._viewport) {
  99. return;
  100. }
  101. this._viewport.setTotalContentSize(this._viewport.getDataLength() * this._itemSize);
  102. }
  103. /** Update the viewport's rendered range. */
  104. _updateRenderedRange() {
  105. if (!this._viewport) {
  106. return;
  107. }
  108. const renderedRange = this._viewport.getRenderedRange();
  109. const newRange = { start: renderedRange.start, end: renderedRange.end };
  110. const viewportSize = this._viewport.getViewportSize();
  111. const dataLength = this._viewport.getDataLength();
  112. let scrollOffset = this._viewport.measureScrollOffset();
  113. // Prevent NaN as result when dividing by zero.
  114. let firstVisibleIndex = this._itemSize > 0 ? scrollOffset / this._itemSize : 0;
  115. // If user scrolls to the bottom of the list and data changes to a smaller list
  116. if (newRange.end > dataLength) {
  117. // We have to recalculate the first visible index based on new data length and viewport size.
  118. const maxVisibleItems = Math.ceil(viewportSize / this._itemSize);
  119. const newVisibleIndex = Math.max(0, Math.min(firstVisibleIndex, dataLength - maxVisibleItems));
  120. // If first visible index changed we must update scroll offset to handle start/end buffers
  121. // Current range must also be adjusted to cover the new position (bottom of new list).
  122. if (firstVisibleIndex != newVisibleIndex) {
  123. firstVisibleIndex = newVisibleIndex;
  124. scrollOffset = newVisibleIndex * this._itemSize;
  125. newRange.start = Math.floor(firstVisibleIndex);
  126. }
  127. newRange.end = Math.max(0, Math.min(dataLength, newRange.start + maxVisibleItems));
  128. }
  129. const startBuffer = scrollOffset - newRange.start * this._itemSize;
  130. if (startBuffer < this._minBufferPx && newRange.start != 0) {
  131. const expandStart = Math.ceil((this._maxBufferPx - startBuffer) / this._itemSize);
  132. newRange.start = Math.max(0, newRange.start - expandStart);
  133. newRange.end = Math.min(dataLength, Math.ceil(firstVisibleIndex + (viewportSize + this._minBufferPx) / this._itemSize));
  134. }
  135. else {
  136. const endBuffer = newRange.end * this._itemSize - (scrollOffset + viewportSize);
  137. if (endBuffer < this._minBufferPx && newRange.end != dataLength) {
  138. const expandEnd = Math.ceil((this._maxBufferPx - endBuffer) / this._itemSize);
  139. if (expandEnd > 0) {
  140. newRange.end = Math.min(dataLength, newRange.end + expandEnd);
  141. newRange.start = Math.max(0, Math.floor(firstVisibleIndex - this._minBufferPx / this._itemSize));
  142. }
  143. }
  144. }
  145. this._viewport.setRenderedRange(newRange);
  146. this._viewport.setRenderedContentOffset(this._itemSize * newRange.start);
  147. this._scrolledIndexChange.next(Math.floor(firstVisibleIndex));
  148. }
  149. }
  150. /**
  151. * Provider factory for `FixedSizeVirtualScrollStrategy` that simply extracts the already created
  152. * `FixedSizeVirtualScrollStrategy` from the given directive.
  153. * @param fixedSizeDir The instance of `CdkFixedSizeVirtualScroll` to extract the
  154. * `FixedSizeVirtualScrollStrategy` from.
  155. */
  156. function _fixedSizeVirtualScrollStrategyFactory(fixedSizeDir) {
  157. return fixedSizeDir._scrollStrategy;
  158. }
  159. /** A virtual scroll strategy that supports fixed-size items. */
  160. class CdkFixedSizeVirtualScroll {
  161. /** The size of the items in the list (in pixels). */
  162. get itemSize() {
  163. return this._itemSize;
  164. }
  165. set itemSize(value) {
  166. this._itemSize = coerceNumberProperty(value);
  167. }
  168. _itemSize = 20;
  169. /**
  170. * The minimum amount of buffer rendered beyond the viewport (in pixels).
  171. * If the amount of buffer dips below this number, more items will be rendered. Defaults to 100px.
  172. */
  173. get minBufferPx() {
  174. return this._minBufferPx;
  175. }
  176. set minBufferPx(value) {
  177. this._minBufferPx = coerceNumberProperty(value);
  178. }
  179. _minBufferPx = 100;
  180. /**
  181. * The number of pixels worth of buffer to render for when rendering new items. Defaults to 200px.
  182. */
  183. get maxBufferPx() {
  184. return this._maxBufferPx;
  185. }
  186. set maxBufferPx(value) {
  187. this._maxBufferPx = coerceNumberProperty(value);
  188. }
  189. _maxBufferPx = 200;
  190. /** The scroll strategy used by this directive. */
  191. _scrollStrategy = new FixedSizeVirtualScrollStrategy(this.itemSize, this.minBufferPx, this.maxBufferPx);
  192. ngOnChanges() {
  193. this._scrollStrategy.updateItemAndBufferSize(this.itemSize, this.minBufferPx, this.maxBufferPx);
  194. }
  195. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkFixedSizeVirtualScroll, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  196. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkFixedSizeVirtualScroll, isStandalone: true, selector: "cdk-virtual-scroll-viewport[itemSize]", inputs: { itemSize: "itemSize", minBufferPx: "minBufferPx", maxBufferPx: "maxBufferPx" }, providers: [
  197. {
  198. provide: VIRTUAL_SCROLL_STRATEGY,
  199. useFactory: _fixedSizeVirtualScrollStrategyFactory,
  200. deps: [forwardRef(() => CdkFixedSizeVirtualScroll)],
  201. },
  202. ], usesOnChanges: true, ngImport: i0 });
  203. }
  204. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkFixedSizeVirtualScroll, decorators: [{
  205. type: Directive,
  206. args: [{
  207. selector: 'cdk-virtual-scroll-viewport[itemSize]',
  208. providers: [
  209. {
  210. provide: VIRTUAL_SCROLL_STRATEGY,
  211. useFactory: _fixedSizeVirtualScrollStrategyFactory,
  212. deps: [forwardRef(() => CdkFixedSizeVirtualScroll)],
  213. },
  214. ],
  215. }]
  216. }], propDecorators: { itemSize: [{
  217. type: Input
  218. }], minBufferPx: [{
  219. type: Input
  220. }], maxBufferPx: [{
  221. type: Input
  222. }] } });
  223. /** Time in ms to throttle the scrolling events by default. */
  224. const DEFAULT_SCROLL_TIME = 20;
  225. /**
  226. * Service contained all registered Scrollable references and emits an event when any one of the
  227. * Scrollable references emit a scrolled event.
  228. */
  229. class ScrollDispatcher {
  230. _ngZone = inject(NgZone);
  231. _platform = inject(Platform);
  232. _renderer = inject(RendererFactory2).createRenderer(null, null);
  233. _cleanupGlobalListener;
  234. constructor() { }
  235. /** Subject for notifying that a registered scrollable reference element has been scrolled. */
  236. _scrolled = new Subject();
  237. /** Keeps track of the amount of subscriptions to `scrolled`. Used for cleaning up afterwards. */
  238. _scrolledCount = 0;
  239. /**
  240. * Map of all the scrollable references that are registered with the service and their
  241. * scroll event subscriptions.
  242. */
  243. scrollContainers = new Map();
  244. /**
  245. * Registers a scrollable instance with the service and listens for its scrolled events. When the
  246. * scrollable is scrolled, the service emits the event to its scrolled observable.
  247. * @param scrollable Scrollable instance to be registered.
  248. */
  249. register(scrollable) {
  250. if (!this.scrollContainers.has(scrollable)) {
  251. this.scrollContainers.set(scrollable, scrollable.elementScrolled().subscribe(() => this._scrolled.next(scrollable)));
  252. }
  253. }
  254. /**
  255. * De-registers a Scrollable reference and unsubscribes from its scroll event observable.
  256. * @param scrollable Scrollable instance to be deregistered.
  257. */
  258. deregister(scrollable) {
  259. const scrollableReference = this.scrollContainers.get(scrollable);
  260. if (scrollableReference) {
  261. scrollableReference.unsubscribe();
  262. this.scrollContainers.delete(scrollable);
  263. }
  264. }
  265. /**
  266. * Returns an observable that emits an event whenever any of the registered Scrollable
  267. * references (or window, document, or body) fire a scrolled event. Can provide a time in ms
  268. * to override the default "throttle" time.
  269. *
  270. * **Note:** in order to avoid hitting change detection for every scroll event,
  271. * all of the events emitted from this stream will be run outside the Angular zone.
  272. * If you need to update any data bindings as a result of a scroll event, you have
  273. * to run the callback using `NgZone.run`.
  274. */
  275. scrolled(auditTimeInMs = DEFAULT_SCROLL_TIME) {
  276. if (!this._platform.isBrowser) {
  277. return of();
  278. }
  279. return new Observable((observer) => {
  280. if (!this._cleanupGlobalListener) {
  281. this._cleanupGlobalListener = this._ngZone.runOutsideAngular(() => this._renderer.listen('document', 'scroll', () => this._scrolled.next()));
  282. }
  283. // In the case of a 0ms delay, use an observable without auditTime
  284. // since it does add a perceptible delay in processing overhead.
  285. const subscription = auditTimeInMs > 0
  286. ? this._scrolled.pipe(auditTime(auditTimeInMs)).subscribe(observer)
  287. : this._scrolled.subscribe(observer);
  288. this._scrolledCount++;
  289. return () => {
  290. subscription.unsubscribe();
  291. this._scrolledCount--;
  292. if (!this._scrolledCount) {
  293. this._cleanupGlobalListener?.();
  294. this._cleanupGlobalListener = undefined;
  295. }
  296. };
  297. });
  298. }
  299. ngOnDestroy() {
  300. this._cleanupGlobalListener?.();
  301. this._cleanupGlobalListener = undefined;
  302. this.scrollContainers.forEach((_, container) => this.deregister(container));
  303. this._scrolled.complete();
  304. }
  305. /**
  306. * Returns an observable that emits whenever any of the
  307. * scrollable ancestors of an element are scrolled.
  308. * @param elementOrElementRef Element whose ancestors to listen for.
  309. * @param auditTimeInMs Time to throttle the scroll events.
  310. */
  311. ancestorScrolled(elementOrElementRef, auditTimeInMs) {
  312. const ancestors = this.getAncestorScrollContainers(elementOrElementRef);
  313. return this.scrolled(auditTimeInMs).pipe(filter(target => !target || ancestors.indexOf(target) > -1));
  314. }
  315. /** Returns all registered Scrollables that contain the provided element. */
  316. getAncestorScrollContainers(elementOrElementRef) {
  317. const scrollingContainers = [];
  318. this.scrollContainers.forEach((_subscription, scrollable) => {
  319. if (this._scrollableContainsElement(scrollable, elementOrElementRef)) {
  320. scrollingContainers.push(scrollable);
  321. }
  322. });
  323. return scrollingContainers;
  324. }
  325. /** Returns true if the element is contained within the provided Scrollable. */
  326. _scrollableContainsElement(scrollable, elementOrElementRef) {
  327. let element = coerceElement(elementOrElementRef);
  328. let scrollableElement = scrollable.getElementRef().nativeElement;
  329. // Traverse through the element parents until we reach null, checking if any of the elements
  330. // are the scrollable's element.
  331. do {
  332. if (element == scrollableElement) {
  333. return true;
  334. }
  335. } while ((element = element.parentElement));
  336. return false;
  337. }
  338. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollDispatcher, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  339. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollDispatcher, providedIn: 'root' });
  340. }
  341. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollDispatcher, decorators: [{
  342. type: Injectable,
  343. args: [{ providedIn: 'root' }]
  344. }], ctorParameters: () => [] });
  345. /**
  346. * Sends an event when the directive's element is scrolled. Registers itself with the
  347. * ScrollDispatcher service to include itself as part of its collection of scrolling events that it
  348. * can be listened to through the service.
  349. */
  350. class CdkScrollable {
  351. elementRef = inject(ElementRef);
  352. scrollDispatcher = inject(ScrollDispatcher);
  353. ngZone = inject(NgZone);
  354. dir = inject(Directionality, { optional: true });
  355. _scrollElement = this.elementRef.nativeElement;
  356. _destroyed = new Subject();
  357. _renderer = inject(Renderer2);
  358. _cleanupScroll;
  359. _elementScrolled = new Subject();
  360. constructor() { }
  361. ngOnInit() {
  362. this._cleanupScroll = this.ngZone.runOutsideAngular(() => this._renderer.listen(this._scrollElement, 'scroll', event => this._elementScrolled.next(event)));
  363. this.scrollDispatcher.register(this);
  364. }
  365. ngOnDestroy() {
  366. this._cleanupScroll?.();
  367. this._elementScrolled.complete();
  368. this.scrollDispatcher.deregister(this);
  369. this._destroyed.next();
  370. this._destroyed.complete();
  371. }
  372. /** Returns observable that emits when a scroll event is fired on the host element. */
  373. elementScrolled() {
  374. return this._elementScrolled;
  375. }
  376. /** Gets the ElementRef for the viewport. */
  377. getElementRef() {
  378. return this.elementRef;
  379. }
  380. /**
  381. * Scrolls to the specified offsets. This is a normalized version of the browser's native scrollTo
  382. * method, since browsers are not consistent about what scrollLeft means in RTL. For this method
  383. * left and right always refer to the left and right side of the scrolling container irrespective
  384. * of the layout direction. start and end refer to left and right in an LTR context and vice-versa
  385. * in an RTL context.
  386. * @param options specified the offsets to scroll to.
  387. */
  388. scrollTo(options) {
  389. const el = this.elementRef.nativeElement;
  390. const isRtl = this.dir && this.dir.value == 'rtl';
  391. // Rewrite start & end offsets as right or left offsets.
  392. if (options.left == null) {
  393. options.left = isRtl ? options.end : options.start;
  394. }
  395. if (options.right == null) {
  396. options.right = isRtl ? options.start : options.end;
  397. }
  398. // Rewrite the bottom offset as a top offset.
  399. if (options.bottom != null) {
  400. options.top =
  401. el.scrollHeight - el.clientHeight - options.bottom;
  402. }
  403. // Rewrite the right offset as a left offset.
  404. if (isRtl && getRtlScrollAxisType() != RtlScrollAxisType.NORMAL) {
  405. if (options.left != null) {
  406. options.right =
  407. el.scrollWidth - el.clientWidth - options.left;
  408. }
  409. if (getRtlScrollAxisType() == RtlScrollAxisType.INVERTED) {
  410. options.left = options.right;
  411. }
  412. else if (getRtlScrollAxisType() == RtlScrollAxisType.NEGATED) {
  413. options.left = options.right ? -options.right : options.right;
  414. }
  415. }
  416. else {
  417. if (options.right != null) {
  418. options.left =
  419. el.scrollWidth - el.clientWidth - options.right;
  420. }
  421. }
  422. this._applyScrollToOptions(options);
  423. }
  424. _applyScrollToOptions(options) {
  425. const el = this.elementRef.nativeElement;
  426. if (supportsScrollBehavior()) {
  427. el.scrollTo(options);
  428. }
  429. else {
  430. if (options.top != null) {
  431. el.scrollTop = options.top;
  432. }
  433. if (options.left != null) {
  434. el.scrollLeft = options.left;
  435. }
  436. }
  437. }
  438. /**
  439. * Measures the scroll offset relative to the specified edge of the viewport. This method can be
  440. * used instead of directly checking scrollLeft or scrollTop, since browsers are not consistent
  441. * about what scrollLeft means in RTL. The values returned by this method are normalized such that
  442. * left and right always refer to the left and right side of the scrolling container irrespective
  443. * of the layout direction. start and end refer to left and right in an LTR context and vice-versa
  444. * in an RTL context.
  445. * @param from The edge to measure from.
  446. */
  447. measureScrollOffset(from) {
  448. const LEFT = 'left';
  449. const RIGHT = 'right';
  450. const el = this.elementRef.nativeElement;
  451. if (from == 'top') {
  452. return el.scrollTop;
  453. }
  454. if (from == 'bottom') {
  455. return el.scrollHeight - el.clientHeight - el.scrollTop;
  456. }
  457. // Rewrite start & end as left or right offsets.
  458. const isRtl = this.dir && this.dir.value == 'rtl';
  459. if (from == 'start') {
  460. from = isRtl ? RIGHT : LEFT;
  461. }
  462. else if (from == 'end') {
  463. from = isRtl ? LEFT : RIGHT;
  464. }
  465. if (isRtl && getRtlScrollAxisType() == RtlScrollAxisType.INVERTED) {
  466. // For INVERTED, scrollLeft is (scrollWidth - clientWidth) when scrolled all the way left and
  467. // 0 when scrolled all the way right.
  468. if (from == LEFT) {
  469. return el.scrollWidth - el.clientWidth - el.scrollLeft;
  470. }
  471. else {
  472. return el.scrollLeft;
  473. }
  474. }
  475. else if (isRtl && getRtlScrollAxisType() == RtlScrollAxisType.NEGATED) {
  476. // For NEGATED, scrollLeft is -(scrollWidth - clientWidth) when scrolled all the way left and
  477. // 0 when scrolled all the way right.
  478. if (from == LEFT) {
  479. return el.scrollLeft + el.scrollWidth - el.clientWidth;
  480. }
  481. else {
  482. return -el.scrollLeft;
  483. }
  484. }
  485. else {
  486. // For NORMAL, as well as non-RTL contexts, scrollLeft is 0 when scrolled all the way left and
  487. // (scrollWidth - clientWidth) when scrolled all the way right.
  488. if (from == LEFT) {
  489. return el.scrollLeft;
  490. }
  491. else {
  492. return el.scrollWidth - el.clientWidth - el.scrollLeft;
  493. }
  494. }
  495. }
  496. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollable, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  497. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkScrollable, isStandalone: true, selector: "[cdk-scrollable], [cdkScrollable]", ngImport: i0 });
  498. }
  499. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollable, decorators: [{
  500. type: Directive,
  501. args: [{
  502. selector: '[cdk-scrollable], [cdkScrollable]',
  503. }]
  504. }], ctorParameters: () => [] });
  505. /** Time in ms to throttle the resize events by default. */
  506. const DEFAULT_RESIZE_TIME = 20;
  507. /**
  508. * Simple utility for getting the bounds of the browser viewport.
  509. * @docs-private
  510. */
  511. class ViewportRuler {
  512. _platform = inject(Platform);
  513. _listeners;
  514. /** Cached viewport dimensions. */
  515. _viewportSize;
  516. /** Stream of viewport change events. */
  517. _change = new Subject();
  518. /** Used to reference correct document/window */
  519. _document = inject(DOCUMENT, { optional: true });
  520. constructor() {
  521. const ngZone = inject(NgZone);
  522. const renderer = inject(RendererFactory2).createRenderer(null, null);
  523. ngZone.runOutsideAngular(() => {
  524. if (this._platform.isBrowser) {
  525. const changeListener = (event) => this._change.next(event);
  526. this._listeners = [
  527. renderer.listen('window', 'resize', changeListener),
  528. renderer.listen('window', 'orientationchange', changeListener),
  529. ];
  530. }
  531. // Clear the cached position so that the viewport is re-measured next time it is required.
  532. // We don't need to keep track of the subscription, because it is completed on destroy.
  533. this.change().subscribe(() => (this._viewportSize = null));
  534. });
  535. }
  536. ngOnDestroy() {
  537. this._listeners?.forEach(cleanup => cleanup());
  538. this._change.complete();
  539. }
  540. /** Returns the viewport's width and height. */
  541. getViewportSize() {
  542. if (!this._viewportSize) {
  543. this._updateViewportSize();
  544. }
  545. const output = { width: this._viewportSize.width, height: this._viewportSize.height };
  546. // If we're not on a browser, don't cache the size since it'll be mocked out anyway.
  547. if (!this._platform.isBrowser) {
  548. this._viewportSize = null;
  549. }
  550. return output;
  551. }
  552. /** Gets a DOMRect for the viewport's bounds. */
  553. getViewportRect() {
  554. // Use the document element's bounding rect rather than the window scroll properties
  555. // (e.g. pageYOffset, scrollY) due to in issue in Chrome and IE where window scroll
  556. // properties and client coordinates (boundingClientRect, clientX/Y, etc.) are in different
  557. // conceptual viewports. Under most circumstances these viewports are equivalent, but they
  558. // can disagree when the page is pinch-zoomed (on devices that support touch).
  559. // See https://bugs.chromium.org/p/chromium/issues/detail?id=489206#c4
  560. // We use the documentElement instead of the body because, by default (without a css reset)
  561. // browsers typically give the document body an 8px margin, which is not included in
  562. // getBoundingClientRect().
  563. const scrollPosition = this.getViewportScrollPosition();
  564. const { width, height } = this.getViewportSize();
  565. return {
  566. top: scrollPosition.top,
  567. left: scrollPosition.left,
  568. bottom: scrollPosition.top + height,
  569. right: scrollPosition.left + width,
  570. height,
  571. width,
  572. };
  573. }
  574. /** Gets the (top, left) scroll position of the viewport. */
  575. getViewportScrollPosition() {
  576. // While we can get a reference to the fake document
  577. // during SSR, it doesn't have getBoundingClientRect.
  578. if (!this._platform.isBrowser) {
  579. return { top: 0, left: 0 };
  580. }
  581. // The top-left-corner of the viewport is determined by the scroll position of the document
  582. // body, normally just (scrollLeft, scrollTop). However, Chrome and Firefox disagree about
  583. // whether `document.body` or `document.documentElement` is the scrolled element, so reading
  584. // `scrollTop` and `scrollLeft` is inconsistent. However, using the bounding rect of
  585. // `document.documentElement` works consistently, where the `top` and `left` values will
  586. // equal negative the scroll position.
  587. const document = this._document;
  588. const window = this._getWindow();
  589. const documentElement = document.documentElement;
  590. const documentRect = documentElement.getBoundingClientRect();
  591. const top = -documentRect.top ||
  592. document.body.scrollTop ||
  593. window.scrollY ||
  594. documentElement.scrollTop ||
  595. 0;
  596. const left = -documentRect.left ||
  597. document.body.scrollLeft ||
  598. window.scrollX ||
  599. documentElement.scrollLeft ||
  600. 0;
  601. return { top, left };
  602. }
  603. /**
  604. * Returns a stream that emits whenever the size of the viewport changes.
  605. * This stream emits outside of the Angular zone.
  606. * @param throttleTime Time in milliseconds to throttle the stream.
  607. */
  608. change(throttleTime = DEFAULT_RESIZE_TIME) {
  609. return throttleTime > 0 ? this._change.pipe(auditTime(throttleTime)) : this._change;
  610. }
  611. /** Use defaultView of injected document if available or fallback to global window reference */
  612. _getWindow() {
  613. return this._document.defaultView || window;
  614. }
  615. /** Updates the cached viewport size. */
  616. _updateViewportSize() {
  617. const window = this._getWindow();
  618. this._viewportSize = this._platform.isBrowser
  619. ? { width: window.innerWidth, height: window.innerHeight }
  620. : { width: 0, height: 0 };
  621. }
  622. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ViewportRuler, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  623. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ViewportRuler, providedIn: 'root' });
  624. }
  625. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ViewportRuler, decorators: [{
  626. type: Injectable,
  627. args: [{ providedIn: 'root' }]
  628. }], ctorParameters: () => [] });
  629. const VIRTUAL_SCROLLABLE = new InjectionToken('VIRTUAL_SCROLLABLE');
  630. /**
  631. * Extending the {@link CdkScrollable} to be used as scrolling container for virtual scrolling.
  632. */
  633. class CdkVirtualScrollable extends CdkScrollable {
  634. constructor() {
  635. super();
  636. }
  637. /**
  638. * Measure the viewport size for the provided orientation.
  639. *
  640. * @param orientation The orientation to measure the size from.
  641. */
  642. measureViewportSize(orientation) {
  643. const viewportEl = this.elementRef.nativeElement;
  644. return orientation === 'horizontal' ? viewportEl.clientWidth : viewportEl.clientHeight;
  645. }
  646. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollable, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  647. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkVirtualScrollable, isStandalone: true, usesInheritance: true, ngImport: i0 });
  648. }
  649. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollable, decorators: [{
  650. type: Directive
  651. }], ctorParameters: () => [] });
  652. /** Checks if the given ranges are equal. */
  653. function rangesEqual(r1, r2) {
  654. return r1.start == r2.start && r1.end == r2.end;
  655. }
  656. /**
  657. * Scheduler to be used for scroll events. Needs to fall back to
  658. * something that doesn't rely on requestAnimationFrame on environments
  659. * that don't support it (e.g. server-side rendering).
  660. */
  661. const SCROLL_SCHEDULER = typeof requestAnimationFrame !== 'undefined' ? animationFrameScheduler : asapScheduler;
  662. /** A viewport that virtualizes its scrolling with the help of `CdkVirtualForOf`. */
  663. class CdkVirtualScrollViewport extends CdkVirtualScrollable {
  664. elementRef = inject(ElementRef);
  665. _changeDetectorRef = inject(ChangeDetectorRef);
  666. _scrollStrategy = inject(VIRTUAL_SCROLL_STRATEGY, {
  667. optional: true,
  668. });
  669. scrollable = inject(VIRTUAL_SCROLLABLE, { optional: true });
  670. _platform = inject(Platform);
  671. /** Emits when the viewport is detached from a CdkVirtualForOf. */
  672. _detachedSubject = new Subject();
  673. /** Emits when the rendered range changes. */
  674. _renderedRangeSubject = new Subject();
  675. /** The direction the viewport scrolls. */
  676. get orientation() {
  677. return this._orientation;
  678. }
  679. set orientation(orientation) {
  680. if (this._orientation !== orientation) {
  681. this._orientation = orientation;
  682. this._calculateSpacerSize();
  683. }
  684. }
  685. _orientation = 'vertical';
  686. /**
  687. * Whether rendered items should persist in the DOM after scrolling out of view. By default, items
  688. * will be removed.
  689. */
  690. appendOnly = false;
  691. // Note: we don't use the typical EventEmitter here because we need to subscribe to the scroll
  692. // strategy lazily (i.e. only if the user is actually listening to the events). We do this because
  693. // depending on how the strategy calculates the scrolled index, it may come at a cost to
  694. // performance.
  695. /** Emits when the index of the first element visible in the viewport changes. */
  696. scrolledIndexChange = new Observable((observer) => this._scrollStrategy.scrolledIndexChange.subscribe(index => Promise.resolve().then(() => this.ngZone.run(() => observer.next(index)))));
  697. /** The element that wraps the rendered content. */
  698. _contentWrapper;
  699. /** A stream that emits whenever the rendered range changes. */
  700. renderedRangeStream = this._renderedRangeSubject;
  701. /**
  702. * The total size of all content (in pixels), including content that is not currently rendered.
  703. */
  704. _totalContentSize = 0;
  705. /** A string representing the `style.width` property value to be used for the spacer element. */
  706. _totalContentWidth = '';
  707. /** A string representing the `style.height` property value to be used for the spacer element. */
  708. _totalContentHeight = '';
  709. /**
  710. * The CSS transform applied to the rendered subset of items so that they appear within the bounds
  711. * of the visible viewport.
  712. */
  713. _renderedContentTransform;
  714. /** The currently rendered range of indices. */
  715. _renderedRange = { start: 0, end: 0 };
  716. /** The length of the data bound to this viewport (in number of items). */
  717. _dataLength = 0;
  718. /** The size of the viewport (in pixels). */
  719. _viewportSize = 0;
  720. /** the currently attached CdkVirtualScrollRepeater. */
  721. _forOf;
  722. /** The last rendered content offset that was set. */
  723. _renderedContentOffset = 0;
  724. /**
  725. * Whether the last rendered content offset was to the end of the content (and therefore needs to
  726. * be rewritten as an offset to the start of the content).
  727. */
  728. _renderedContentOffsetNeedsRewrite = false;
  729. /** Whether there is a pending change detection cycle. */
  730. _isChangeDetectionPending = false;
  731. /** A list of functions to run after the next change detection cycle. */
  732. _runAfterChangeDetection = [];
  733. /** Subscription to changes in the viewport size. */
  734. _viewportChanges = Subscription.EMPTY;
  735. _injector = inject(Injector);
  736. _isDestroyed = false;
  737. constructor() {
  738. super();
  739. const viewportRuler = inject(ViewportRuler);
  740. if (!this._scrollStrategy && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  741. throw Error('Error: cdk-virtual-scroll-viewport requires the "itemSize" property to be set.');
  742. }
  743. this._viewportChanges = viewportRuler.change().subscribe(() => {
  744. this.checkViewportSize();
  745. });
  746. if (!this.scrollable) {
  747. // No scrollable is provided, so the virtual-scroll-viewport needs to become a scrollable
  748. this.elementRef.nativeElement.classList.add('cdk-virtual-scrollable');
  749. this.scrollable = this;
  750. }
  751. }
  752. ngOnInit() {
  753. // Scrolling depends on the element dimensions which we can't get during SSR.
  754. if (!this._platform.isBrowser) {
  755. return;
  756. }
  757. if (this.scrollable === this) {
  758. super.ngOnInit();
  759. }
  760. // It's still too early to measure the viewport at this point. Deferring with a promise allows
  761. // the Viewport to be rendered with the correct size before we measure. We run this outside the
  762. // zone to avoid causing more change detection cycles. We handle the change detection loop
  763. // ourselves instead.
  764. this.ngZone.runOutsideAngular(() => Promise.resolve().then(() => {
  765. this._measureViewportSize();
  766. this._scrollStrategy.attach(this);
  767. this.scrollable
  768. .elementScrolled()
  769. .pipe(
  770. // Start off with a fake scroll event so we properly detect our initial position.
  771. startWith(null),
  772. // Collect multiple events into one until the next animation frame. This way if
  773. // there are multiple scroll events in the same frame we only need to recheck
  774. // our layout once.
  775. auditTime(0, SCROLL_SCHEDULER),
  776. // Usually `elementScrolled` is completed when the scrollable is destroyed, but
  777. // that may not be the case if a `CdkVirtualScrollableElement` is used so we have
  778. // to unsubscribe here just in case.
  779. takeUntil(this._destroyed))
  780. .subscribe(() => this._scrollStrategy.onContentScrolled());
  781. this._markChangeDetectionNeeded();
  782. }));
  783. }
  784. ngOnDestroy() {
  785. this.detach();
  786. this._scrollStrategy.detach();
  787. // Complete all subjects
  788. this._renderedRangeSubject.complete();
  789. this._detachedSubject.complete();
  790. this._viewportChanges.unsubscribe();
  791. this._isDestroyed = true;
  792. super.ngOnDestroy();
  793. }
  794. /** Attaches a `CdkVirtualScrollRepeater` to this viewport. */
  795. attach(forOf) {
  796. if (this._forOf && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  797. throw Error('CdkVirtualScrollViewport is already attached.');
  798. }
  799. // Subscribe to the data stream of the CdkVirtualForOf to keep track of when the data length
  800. // changes. Run outside the zone to avoid triggering change detection, since we're managing the
  801. // change detection loop ourselves.
  802. this.ngZone.runOutsideAngular(() => {
  803. this._forOf = forOf;
  804. this._forOf.dataStream.pipe(takeUntil(this._detachedSubject)).subscribe(data => {
  805. const newLength = data.length;
  806. if (newLength !== this._dataLength) {
  807. this._dataLength = newLength;
  808. this._scrollStrategy.onDataLengthChanged();
  809. }
  810. this._doChangeDetection();
  811. });
  812. });
  813. }
  814. /** Detaches the current `CdkVirtualForOf`. */
  815. detach() {
  816. this._forOf = null;
  817. this._detachedSubject.next();
  818. }
  819. /** Gets the length of the data bound to this viewport (in number of items). */
  820. getDataLength() {
  821. return this._dataLength;
  822. }
  823. /** Gets the size of the viewport (in pixels). */
  824. getViewportSize() {
  825. return this._viewportSize;
  826. }
  827. // TODO(mmalerba): This is technically out of sync with what's really rendered until a render
  828. // cycle happens. I'm being careful to only call it after the render cycle is complete and before
  829. // setting it to something else, but its error prone and should probably be split into
  830. // `pendingRange` and `renderedRange`, the latter reflecting whats actually in the DOM.
  831. /** Get the current rendered range of items. */
  832. getRenderedRange() {
  833. return this._renderedRange;
  834. }
  835. measureBoundingClientRectWithScrollOffset(from) {
  836. return this.getElementRef().nativeElement.getBoundingClientRect()[from];
  837. }
  838. /**
  839. * Sets the total size of all content (in pixels), including content that is not currently
  840. * rendered.
  841. */
  842. setTotalContentSize(size) {
  843. if (this._totalContentSize !== size) {
  844. this._totalContentSize = size;
  845. this._calculateSpacerSize();
  846. this._markChangeDetectionNeeded();
  847. }
  848. }
  849. /** Sets the currently rendered range of indices. */
  850. setRenderedRange(range) {
  851. if (!rangesEqual(this._renderedRange, range)) {
  852. if (this.appendOnly) {
  853. range = { start: 0, end: Math.max(this._renderedRange.end, range.end) };
  854. }
  855. this._renderedRangeSubject.next((this._renderedRange = range));
  856. this._markChangeDetectionNeeded(() => this._scrollStrategy.onContentRendered());
  857. }
  858. }
  859. /**
  860. * Gets the offset from the start of the viewport to the start of the rendered data (in pixels).
  861. */
  862. getOffsetToRenderedContentStart() {
  863. return this._renderedContentOffsetNeedsRewrite ? null : this._renderedContentOffset;
  864. }
  865. /**
  866. * Sets the offset from the start of the viewport to either the start or end of the rendered data
  867. * (in pixels).
  868. */
  869. setRenderedContentOffset(offset, to = 'to-start') {
  870. // In appendOnly, we always start from the top
  871. offset = this.appendOnly && to === 'to-start' ? 0 : offset;
  872. // For a horizontal viewport in a right-to-left language we need to translate along the x-axis
  873. // in the negative direction.
  874. const isRtl = this.dir && this.dir.value == 'rtl';
  875. const isHorizontal = this.orientation == 'horizontal';
  876. const axis = isHorizontal ? 'X' : 'Y';
  877. const axisDirection = isHorizontal && isRtl ? -1 : 1;
  878. let transform = `translate${axis}(${Number(axisDirection * offset)}px)`;
  879. this._renderedContentOffset = offset;
  880. if (to === 'to-end') {
  881. transform += ` translate${axis}(-100%)`;
  882. // The viewport should rewrite this as a `to-start` offset on the next render cycle. Otherwise
  883. // elements will appear to expand in the wrong direction (e.g. `mat-expansion-panel` would
  884. // expand upward).
  885. this._renderedContentOffsetNeedsRewrite = true;
  886. }
  887. if (this._renderedContentTransform != transform) {
  888. // We know this value is safe because we parse `offset` with `Number()` before passing it
  889. // into the string.
  890. this._renderedContentTransform = transform;
  891. this._markChangeDetectionNeeded(() => {
  892. if (this._renderedContentOffsetNeedsRewrite) {
  893. this._renderedContentOffset -= this.measureRenderedContentSize();
  894. this._renderedContentOffsetNeedsRewrite = false;
  895. this.setRenderedContentOffset(this._renderedContentOffset);
  896. }
  897. else {
  898. this._scrollStrategy.onRenderedOffsetChanged();
  899. }
  900. });
  901. }
  902. }
  903. /**
  904. * Scrolls to the given offset from the start of the viewport. Please note that this is not always
  905. * the same as setting `scrollTop` or `scrollLeft`. In a horizontal viewport with right-to-left
  906. * direction, this would be the equivalent of setting a fictional `scrollRight` property.
  907. * @param offset The offset to scroll to.
  908. * @param behavior The ScrollBehavior to use when scrolling. Default is behavior is `auto`.
  909. */
  910. scrollToOffset(offset, behavior = 'auto') {
  911. const options = { behavior };
  912. if (this.orientation === 'horizontal') {
  913. options.start = offset;
  914. }
  915. else {
  916. options.top = offset;
  917. }
  918. this.scrollable.scrollTo(options);
  919. }
  920. /**
  921. * Scrolls to the offset for the given index.
  922. * @param index The index of the element to scroll to.
  923. * @param behavior The ScrollBehavior to use when scrolling. Default is behavior is `auto`.
  924. */
  925. scrollToIndex(index, behavior = 'auto') {
  926. this._scrollStrategy.scrollToIndex(index, behavior);
  927. }
  928. /**
  929. * Gets the current scroll offset from the start of the scrollable (in pixels).
  930. * @param from The edge to measure the offset from. Defaults to 'top' in vertical mode and 'start'
  931. * in horizontal mode.
  932. */
  933. measureScrollOffset(from) {
  934. // This is to break the call cycle
  935. let measureScrollOffset;
  936. if (this.scrollable == this) {
  937. measureScrollOffset = (_from) => super.measureScrollOffset(_from);
  938. }
  939. else {
  940. measureScrollOffset = (_from) => this.scrollable.measureScrollOffset(_from);
  941. }
  942. return Math.max(0, measureScrollOffset(from ?? (this.orientation === 'horizontal' ? 'start' : 'top')) -
  943. this.measureViewportOffset());
  944. }
  945. /**
  946. * Measures the offset of the viewport from the scrolling container
  947. * @param from The edge to measure from.
  948. */
  949. measureViewportOffset(from) {
  950. let fromRect;
  951. const LEFT = 'left';
  952. const RIGHT = 'right';
  953. const isRtl = this.dir?.value == 'rtl';
  954. if (from == 'start') {
  955. fromRect = isRtl ? RIGHT : LEFT;
  956. }
  957. else if (from == 'end') {
  958. fromRect = isRtl ? LEFT : RIGHT;
  959. }
  960. else if (from) {
  961. fromRect = from;
  962. }
  963. else {
  964. fromRect = this.orientation === 'horizontal' ? 'left' : 'top';
  965. }
  966. const scrollerClientRect = this.scrollable.measureBoundingClientRectWithScrollOffset(fromRect);
  967. const viewportClientRect = this.elementRef.nativeElement.getBoundingClientRect()[fromRect];
  968. return viewportClientRect - scrollerClientRect;
  969. }
  970. /** Measure the combined size of all of the rendered items. */
  971. measureRenderedContentSize() {
  972. const contentEl = this._contentWrapper.nativeElement;
  973. return this.orientation === 'horizontal' ? contentEl.offsetWidth : contentEl.offsetHeight;
  974. }
  975. /**
  976. * Measure the total combined size of the given range. Throws if the range includes items that are
  977. * not rendered.
  978. */
  979. measureRangeSize(range) {
  980. if (!this._forOf) {
  981. return 0;
  982. }
  983. return this._forOf.measureRangeSize(range, this.orientation);
  984. }
  985. /** Update the viewport dimensions and re-render. */
  986. checkViewportSize() {
  987. // TODO: Cleanup later when add logic for handling content resize
  988. this._measureViewportSize();
  989. this._scrollStrategy.onDataLengthChanged();
  990. }
  991. /** Measure the viewport size. */
  992. _measureViewportSize() {
  993. this._viewportSize = this.scrollable.measureViewportSize(this.orientation);
  994. }
  995. /** Queue up change detection to run. */
  996. _markChangeDetectionNeeded(runAfter) {
  997. if (runAfter) {
  998. this._runAfterChangeDetection.push(runAfter);
  999. }
  1000. // Use a Promise to batch together calls to `_doChangeDetection`. This way if we set a bunch of
  1001. // properties sequentially we only have to run `_doChangeDetection` once at the end.
  1002. if (!this._isChangeDetectionPending) {
  1003. this._isChangeDetectionPending = true;
  1004. this.ngZone.runOutsideAngular(() => Promise.resolve().then(() => {
  1005. this._doChangeDetection();
  1006. }));
  1007. }
  1008. }
  1009. /** Run change detection. */
  1010. _doChangeDetection() {
  1011. if (this._isDestroyed) {
  1012. return;
  1013. }
  1014. this.ngZone.run(() => {
  1015. // Apply changes to Angular bindings. Note: We must call `markForCheck` to run change detection
  1016. // from the root, since the repeated items are content projected in. Calling `detectChanges`
  1017. // instead does not properly check the projected content.
  1018. this._changeDetectorRef.markForCheck();
  1019. // Apply the content transform. The transform can't be set via an Angular binding because
  1020. // bypassSecurityTrustStyle is banned in Google. However the value is safe, it's composed of
  1021. // string literals, a variable that can only be 'X' or 'Y', and user input that is run through
  1022. // the `Number` function first to coerce it to a numeric value.
  1023. this._contentWrapper.nativeElement.style.transform = this._renderedContentTransform;
  1024. afterNextRender(() => {
  1025. this._isChangeDetectionPending = false;
  1026. const runAfterChangeDetection = this._runAfterChangeDetection;
  1027. this._runAfterChangeDetection = [];
  1028. for (const fn of runAfterChangeDetection) {
  1029. fn();
  1030. }
  1031. }, { injector: this._injector });
  1032. });
  1033. }
  1034. /** Calculates the `style.width` and `style.height` for the spacer element. */
  1035. _calculateSpacerSize() {
  1036. this._totalContentHeight =
  1037. this.orientation === 'horizontal' ? '' : `${this._totalContentSize}px`;
  1038. this._totalContentWidth =
  1039. this.orientation === 'horizontal' ? `${this._totalContentSize}px` : '';
  1040. }
  1041. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollViewport, deps: [], target: i0.ɵɵFactoryTarget.Component });
  1042. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "19.2.6", type: CdkVirtualScrollViewport, isStandalone: true, selector: "cdk-virtual-scroll-viewport", inputs: { orientation: "orientation", appendOnly: ["appendOnly", "appendOnly", booleanAttribute] }, outputs: { scrolledIndexChange: "scrolledIndexChange" }, host: { properties: { "class.cdk-virtual-scroll-orientation-horizontal": "orientation === \"horizontal\"", "class.cdk-virtual-scroll-orientation-vertical": "orientation !== \"horizontal\"" }, classAttribute: "cdk-virtual-scroll-viewport" }, providers: [
  1043. {
  1044. provide: CdkScrollable,
  1045. useFactory: (virtualScrollable, viewport) => virtualScrollable || viewport,
  1046. deps: [[new Optional(), new Inject(VIRTUAL_SCROLLABLE)], CdkVirtualScrollViewport],
  1047. },
  1048. ], viewQueries: [{ propertyName: "_contentWrapper", first: true, predicate: ["contentWrapper"], descendants: true, static: true }], usesInheritance: true, ngImport: i0, template: "<!--\n Wrap the rendered content in an element that will be used to offset it based on the scroll\n position.\n-->\n<div #contentWrapper class=\"cdk-virtual-scroll-content-wrapper\">\n <ng-content></ng-content>\n</div>\n<!--\n Spacer used to force the scrolling container to the correct size for the *total* number of items\n so that the scrollbar captures the size of the entire data set.\n-->\n<div class=\"cdk-virtual-scroll-spacer\"\n [style.width]=\"_totalContentWidth\" [style.height]=\"_totalContentHeight\"></div>\n", styles: ["cdk-virtual-scroll-viewport{display:block;position:relative;transform:translateZ(0)}.cdk-virtual-scrollable{overflow:auto;will-change:scroll-position;contain:strict}.cdk-virtual-scroll-content-wrapper{position:absolute;top:0;left:0;contain:content}[dir=rtl] .cdk-virtual-scroll-content-wrapper{right:0;left:auto}.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper{min-height:100%}.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>dl:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>ol:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>table:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>ul:not([cdkVirtualFor]){padding-left:0;padding-right:0;margin-left:0;margin-right:0;border-left-width:0;border-right-width:0;outline:none}.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper{min-width:100%}.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>dl:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>ol:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>table:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>ul:not([cdkVirtualFor]){padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;border-top-width:0;border-bottom-width:0;outline:none}.cdk-virtual-scroll-spacer{height:1px;transform-origin:0 0;flex:0 0 auto}[dir=rtl] .cdk-virtual-scroll-spacer{transform-origin:100% 0}\n"], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  1049. }
  1050. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollViewport, decorators: [{
  1051. type: Component,
  1052. args: [{ selector: 'cdk-virtual-scroll-viewport', host: {
  1053. 'class': 'cdk-virtual-scroll-viewport',
  1054. '[class.cdk-virtual-scroll-orientation-horizontal]': 'orientation === "horizontal"',
  1055. '[class.cdk-virtual-scroll-orientation-vertical]': 'orientation !== "horizontal"',
  1056. }, encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, providers: [
  1057. {
  1058. provide: CdkScrollable,
  1059. useFactory: (virtualScrollable, viewport) => virtualScrollable || viewport,
  1060. deps: [[new Optional(), new Inject(VIRTUAL_SCROLLABLE)], CdkVirtualScrollViewport],
  1061. },
  1062. ], template: "<!--\n Wrap the rendered content in an element that will be used to offset it based on the scroll\n position.\n-->\n<div #contentWrapper class=\"cdk-virtual-scroll-content-wrapper\">\n <ng-content></ng-content>\n</div>\n<!--\n Spacer used to force the scrolling container to the correct size for the *total* number of items\n so that the scrollbar captures the size of the entire data set.\n-->\n<div class=\"cdk-virtual-scroll-spacer\"\n [style.width]=\"_totalContentWidth\" [style.height]=\"_totalContentHeight\"></div>\n", styles: ["cdk-virtual-scroll-viewport{display:block;position:relative;transform:translateZ(0)}.cdk-virtual-scrollable{overflow:auto;will-change:scroll-position;contain:strict}.cdk-virtual-scroll-content-wrapper{position:absolute;top:0;left:0;contain:content}[dir=rtl] .cdk-virtual-scroll-content-wrapper{right:0;left:auto}.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper{min-height:100%}.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>dl:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>ol:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>table:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper>ul:not([cdkVirtualFor]){padding-left:0;padding-right:0;margin-left:0;margin-right:0;border-left-width:0;border-right-width:0;outline:none}.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper{min-width:100%}.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>dl:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>ol:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>table:not([cdkVirtualFor]),.cdk-virtual-scroll-orientation-vertical .cdk-virtual-scroll-content-wrapper>ul:not([cdkVirtualFor]){padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;border-top-width:0;border-bottom-width:0;outline:none}.cdk-virtual-scroll-spacer{height:1px;transform-origin:0 0;flex:0 0 auto}[dir=rtl] .cdk-virtual-scroll-spacer{transform-origin:100% 0}\n"] }]
  1063. }], ctorParameters: () => [], propDecorators: { orientation: [{
  1064. type: Input
  1065. }], appendOnly: [{
  1066. type: Input,
  1067. args: [{ transform: booleanAttribute }]
  1068. }], scrolledIndexChange: [{
  1069. type: Output
  1070. }], _contentWrapper: [{
  1071. type: ViewChild,
  1072. args: ['contentWrapper', { static: true }]
  1073. }] } });
  1074. /** Helper to extract the offset of a DOM Node in a certain direction. */
  1075. function getOffset(orientation, direction, node) {
  1076. const el = node;
  1077. if (!el.getBoundingClientRect) {
  1078. return 0;
  1079. }
  1080. const rect = el.getBoundingClientRect();
  1081. if (orientation === 'horizontal') {
  1082. return direction === 'start' ? rect.left : rect.right;
  1083. }
  1084. return direction === 'start' ? rect.top : rect.bottom;
  1085. }
  1086. /**
  1087. * A directive similar to `ngForOf` to be used for rendering data inside a virtual scrolling
  1088. * container.
  1089. */
  1090. class CdkVirtualForOf {
  1091. _viewContainerRef = inject(ViewContainerRef);
  1092. _template = inject(TemplateRef);
  1093. _differs = inject(IterableDiffers);
  1094. _viewRepeater = inject(_VIEW_REPEATER_STRATEGY);
  1095. _viewport = inject(CdkVirtualScrollViewport, { skipSelf: true });
  1096. /** Emits when the rendered view of the data changes. */
  1097. viewChange = new Subject();
  1098. /** Subject that emits when a new DataSource instance is given. */
  1099. _dataSourceChanges = new Subject();
  1100. /** The DataSource to display. */
  1101. get cdkVirtualForOf() {
  1102. return this._cdkVirtualForOf;
  1103. }
  1104. set cdkVirtualForOf(value) {
  1105. this._cdkVirtualForOf = value;
  1106. if (isDataSource(value)) {
  1107. this._dataSourceChanges.next(value);
  1108. }
  1109. else {
  1110. // If value is an an NgIterable, convert it to an array.
  1111. this._dataSourceChanges.next(new ArrayDataSource(isObservable(value) ? value : Array.from(value || [])));
  1112. }
  1113. }
  1114. _cdkVirtualForOf;
  1115. /**
  1116. * The `TrackByFunction` to use for tracking changes. The `TrackByFunction` takes the index and
  1117. * the item and produces a value to be used as the item's identity when tracking changes.
  1118. */
  1119. get cdkVirtualForTrackBy() {
  1120. return this._cdkVirtualForTrackBy;
  1121. }
  1122. set cdkVirtualForTrackBy(fn) {
  1123. this._needsUpdate = true;
  1124. this._cdkVirtualForTrackBy = fn
  1125. ? (index, item) => fn(index + (this._renderedRange ? this._renderedRange.start : 0), item)
  1126. : undefined;
  1127. }
  1128. _cdkVirtualForTrackBy;
  1129. /** The template used to stamp out new elements. */
  1130. set cdkVirtualForTemplate(value) {
  1131. if (value) {
  1132. this._needsUpdate = true;
  1133. this._template = value;
  1134. }
  1135. }
  1136. /**
  1137. * The size of the cache used to store templates that are not being used for re-use later.
  1138. * Setting the cache size to `0` will disable caching. Defaults to 20 templates.
  1139. */
  1140. get cdkVirtualForTemplateCacheSize() {
  1141. return this._viewRepeater.viewCacheSize;
  1142. }
  1143. set cdkVirtualForTemplateCacheSize(size) {
  1144. this._viewRepeater.viewCacheSize = coerceNumberProperty(size);
  1145. }
  1146. /** Emits whenever the data in the current DataSource changes. */
  1147. dataStream = this._dataSourceChanges.pipe(
  1148. // Start off with null `DataSource`.
  1149. startWith(null),
  1150. // Bundle up the previous and current data sources so we can work with both.
  1151. pairwise(),
  1152. // Use `_changeDataSource` to disconnect from the previous data source and connect to the
  1153. // new one, passing back a stream of data changes which we run through `switchMap` to give
  1154. // us a data stream that emits the latest data from whatever the current `DataSource` is.
  1155. switchMap(([prev, cur]) => this._changeDataSource(prev, cur)),
  1156. // Replay the last emitted data when someone subscribes.
  1157. shareReplay(1));
  1158. /** The differ used to calculate changes to the data. */
  1159. _differ = null;
  1160. /** The most recent data emitted from the DataSource. */
  1161. _data;
  1162. /** The currently rendered items. */
  1163. _renderedItems;
  1164. /** The currently rendered range of indices. */
  1165. _renderedRange;
  1166. /** Whether the rendered data should be updated during the next ngDoCheck cycle. */
  1167. _needsUpdate = false;
  1168. _destroyed = new Subject();
  1169. constructor() {
  1170. const ngZone = inject(NgZone);
  1171. this.dataStream.subscribe(data => {
  1172. this._data = data;
  1173. this._onRenderedDataChange();
  1174. });
  1175. this._viewport.renderedRangeStream.pipe(takeUntil(this._destroyed)).subscribe(range => {
  1176. this._renderedRange = range;
  1177. if (this.viewChange.observers.length) {
  1178. ngZone.run(() => this.viewChange.next(this._renderedRange));
  1179. }
  1180. this._onRenderedDataChange();
  1181. });
  1182. this._viewport.attach(this);
  1183. }
  1184. /**
  1185. * Measures the combined size (width for horizontal orientation, height for vertical) of all items
  1186. * in the specified range. Throws an error if the range includes items that are not currently
  1187. * rendered.
  1188. */
  1189. measureRangeSize(range, orientation) {
  1190. if (range.start >= range.end) {
  1191. return 0;
  1192. }
  1193. if ((range.start < this._renderedRange.start || range.end > this._renderedRange.end) &&
  1194. (typeof ngDevMode === 'undefined' || ngDevMode)) {
  1195. throw Error(`Error: attempted to measure an item that isn't rendered.`);
  1196. }
  1197. // The index into the list of rendered views for the first item in the range.
  1198. const renderedStartIndex = range.start - this._renderedRange.start;
  1199. // The length of the range we're measuring.
  1200. const rangeLen = range.end - range.start;
  1201. // Loop over all the views, find the first and land node and compute the size by subtracting
  1202. // the top of the first node from the bottom of the last one.
  1203. let firstNode;
  1204. let lastNode;
  1205. // Find the first node by starting from the beginning and going forwards.
  1206. for (let i = 0; i < rangeLen; i++) {
  1207. const view = this._viewContainerRef.get(i + renderedStartIndex);
  1208. if (view && view.rootNodes.length) {
  1209. firstNode = lastNode = view.rootNodes[0];
  1210. break;
  1211. }
  1212. }
  1213. // Find the last node by starting from the end and going backwards.
  1214. for (let i = rangeLen - 1; i > -1; i--) {
  1215. const view = this._viewContainerRef.get(i + renderedStartIndex);
  1216. if (view && view.rootNodes.length) {
  1217. lastNode = view.rootNodes[view.rootNodes.length - 1];
  1218. break;
  1219. }
  1220. }
  1221. return firstNode && lastNode
  1222. ? getOffset(orientation, 'end', lastNode) - getOffset(orientation, 'start', firstNode)
  1223. : 0;
  1224. }
  1225. ngDoCheck() {
  1226. if (this._differ && this._needsUpdate) {
  1227. // TODO(mmalerba): We should differentiate needs update due to scrolling and a new portion of
  1228. // this list being rendered (can use simpler algorithm) vs needs update due to data actually
  1229. // changing (need to do this diff).
  1230. const changes = this._differ.diff(this._renderedItems);
  1231. if (!changes) {
  1232. this._updateContext();
  1233. }
  1234. else {
  1235. this._applyChanges(changes);
  1236. }
  1237. this._needsUpdate = false;
  1238. }
  1239. }
  1240. ngOnDestroy() {
  1241. this._viewport.detach();
  1242. this._dataSourceChanges.next(undefined);
  1243. this._dataSourceChanges.complete();
  1244. this.viewChange.complete();
  1245. this._destroyed.next();
  1246. this._destroyed.complete();
  1247. this._viewRepeater.detach();
  1248. }
  1249. /** React to scroll state changes in the viewport. */
  1250. _onRenderedDataChange() {
  1251. if (!this._renderedRange) {
  1252. return;
  1253. }
  1254. this._renderedItems = this._data.slice(this._renderedRange.start, this._renderedRange.end);
  1255. if (!this._differ) {
  1256. // Use a wrapper function for the `trackBy` so any new values are
  1257. // picked up automatically without having to recreate the differ.
  1258. this._differ = this._differs.find(this._renderedItems).create((index, item) => {
  1259. return this.cdkVirtualForTrackBy ? this.cdkVirtualForTrackBy(index, item) : item;
  1260. });
  1261. }
  1262. this._needsUpdate = true;
  1263. }
  1264. /** Swap out one `DataSource` for another. */
  1265. _changeDataSource(oldDs, newDs) {
  1266. if (oldDs) {
  1267. oldDs.disconnect(this);
  1268. }
  1269. this._needsUpdate = true;
  1270. return newDs ? newDs.connect(this) : of();
  1271. }
  1272. /** Update the `CdkVirtualForOfContext` for all views. */
  1273. _updateContext() {
  1274. const count = this._data.length;
  1275. let i = this._viewContainerRef.length;
  1276. while (i--) {
  1277. const view = this._viewContainerRef.get(i);
  1278. view.context.index = this._renderedRange.start + i;
  1279. view.context.count = count;
  1280. this._updateComputedContextProperties(view.context);
  1281. view.detectChanges();
  1282. }
  1283. }
  1284. /** Apply changes to the DOM. */
  1285. _applyChanges(changes) {
  1286. this._viewRepeater.applyChanges(changes, this._viewContainerRef, (record, _adjustedPreviousIndex, currentIndex) => this._getEmbeddedViewArgs(record, currentIndex), record => record.item);
  1287. // Update $implicit for any items that had an identity change.
  1288. changes.forEachIdentityChange((record) => {
  1289. const view = this._viewContainerRef.get(record.currentIndex);
  1290. view.context.$implicit = record.item;
  1291. });
  1292. // Update the context variables on all items.
  1293. const count = this._data.length;
  1294. let i = this._viewContainerRef.length;
  1295. while (i--) {
  1296. const view = this._viewContainerRef.get(i);
  1297. view.context.index = this._renderedRange.start + i;
  1298. view.context.count = count;
  1299. this._updateComputedContextProperties(view.context);
  1300. }
  1301. }
  1302. /** Update the computed properties on the `CdkVirtualForOfContext`. */
  1303. _updateComputedContextProperties(context) {
  1304. context.first = context.index === 0;
  1305. context.last = context.index === context.count - 1;
  1306. context.even = context.index % 2 === 0;
  1307. context.odd = !context.even;
  1308. }
  1309. _getEmbeddedViewArgs(record, index) {
  1310. // Note that it's important that we insert the item directly at the proper index,
  1311. // rather than inserting it and the moving it in place, because if there's a directive
  1312. // on the same node that injects the `ViewContainerRef`, Angular will insert another
  1313. // comment node which can throw off the move when it's being repeated for all items.
  1314. return {
  1315. templateRef: this._template,
  1316. context: {
  1317. $implicit: record.item,
  1318. // It's guaranteed that the iterable is not "undefined" or "null" because we only
  1319. // generate views for elements if the "cdkVirtualForOf" iterable has elements.
  1320. cdkVirtualForOf: this._cdkVirtualForOf,
  1321. index: -1,
  1322. count: -1,
  1323. first: false,
  1324. last: false,
  1325. odd: false,
  1326. even: false,
  1327. },
  1328. index,
  1329. };
  1330. }
  1331. static ngTemplateContextGuard(directive, context) {
  1332. return true;
  1333. }
  1334. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualForOf, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  1335. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkVirtualForOf, isStandalone: true, selector: "[cdkVirtualFor][cdkVirtualForOf]", inputs: { cdkVirtualForOf: "cdkVirtualForOf", cdkVirtualForTrackBy: "cdkVirtualForTrackBy", cdkVirtualForTemplate: "cdkVirtualForTemplate", cdkVirtualForTemplateCacheSize: "cdkVirtualForTemplateCacheSize" }, providers: [{ provide: _VIEW_REPEATER_STRATEGY, useClass: _RecycleViewRepeaterStrategy }], ngImport: i0 });
  1336. }
  1337. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualForOf, decorators: [{
  1338. type: Directive,
  1339. args: [{
  1340. selector: '[cdkVirtualFor][cdkVirtualForOf]',
  1341. providers: [{ provide: _VIEW_REPEATER_STRATEGY, useClass: _RecycleViewRepeaterStrategy }],
  1342. }]
  1343. }], ctorParameters: () => [], propDecorators: { cdkVirtualForOf: [{
  1344. type: Input
  1345. }], cdkVirtualForTrackBy: [{
  1346. type: Input
  1347. }], cdkVirtualForTemplate: [{
  1348. type: Input
  1349. }], cdkVirtualForTemplateCacheSize: [{
  1350. type: Input
  1351. }] } });
  1352. /**
  1353. * Provides a virtual scrollable for the element it is attached to.
  1354. */
  1355. class CdkVirtualScrollableElement extends CdkVirtualScrollable {
  1356. constructor() {
  1357. super();
  1358. }
  1359. measureBoundingClientRectWithScrollOffset(from) {
  1360. return (this.getElementRef().nativeElement.getBoundingClientRect()[from] -
  1361. this.measureScrollOffset(from));
  1362. }
  1363. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollableElement, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  1364. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkVirtualScrollableElement, isStandalone: true, selector: "[cdkVirtualScrollingElement]", host: { classAttribute: "cdk-virtual-scrollable" }, providers: [{ provide: VIRTUAL_SCROLLABLE, useExisting: CdkVirtualScrollableElement }], usesInheritance: true, ngImport: i0 });
  1365. }
  1366. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollableElement, decorators: [{
  1367. type: Directive,
  1368. args: [{
  1369. selector: '[cdkVirtualScrollingElement]',
  1370. providers: [{ provide: VIRTUAL_SCROLLABLE, useExisting: CdkVirtualScrollableElement }],
  1371. host: {
  1372. 'class': 'cdk-virtual-scrollable',
  1373. },
  1374. }]
  1375. }], ctorParameters: () => [] });
  1376. /**
  1377. * Provides as virtual scrollable for the global / window scrollbar.
  1378. */
  1379. class CdkVirtualScrollableWindow extends CdkVirtualScrollable {
  1380. constructor() {
  1381. super();
  1382. const document = inject(DOCUMENT);
  1383. this.elementRef = new ElementRef(document.documentElement);
  1384. this._scrollElement = document;
  1385. }
  1386. measureBoundingClientRectWithScrollOffset(from) {
  1387. return this.getElementRef().nativeElement.getBoundingClientRect()[from];
  1388. }
  1389. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollableWindow, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  1390. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.6", type: CdkVirtualScrollableWindow, isStandalone: true, selector: "cdk-virtual-scroll-viewport[scrollWindow]", providers: [{ provide: VIRTUAL_SCROLLABLE, useExisting: CdkVirtualScrollableWindow }], usesInheritance: true, ngImport: i0 });
  1391. }
  1392. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkVirtualScrollableWindow, decorators: [{
  1393. type: Directive,
  1394. args: [{
  1395. selector: 'cdk-virtual-scroll-viewport[scrollWindow]',
  1396. providers: [{ provide: VIRTUAL_SCROLLABLE, useExisting: CdkVirtualScrollableWindow }],
  1397. }]
  1398. }], ctorParameters: () => [] });
  1399. class CdkScrollableModule {
  1400. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollableModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  1401. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollableModule, imports: [CdkScrollable], exports: [CdkScrollable] });
  1402. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollableModule });
  1403. }
  1404. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: CdkScrollableModule, decorators: [{
  1405. type: NgModule,
  1406. args: [{
  1407. exports: [CdkScrollable],
  1408. imports: [CdkScrollable],
  1409. }]
  1410. }] });
  1411. /**
  1412. * @docs-primary-export
  1413. */
  1414. class ScrollingModule {
  1415. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  1416. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.6", ngImport: i0, type: ScrollingModule, imports: [BidiModule, CdkScrollableModule, CdkVirtualScrollViewport,
  1417. CdkFixedSizeVirtualScroll,
  1418. CdkVirtualForOf,
  1419. CdkVirtualScrollableWindow,
  1420. CdkVirtualScrollableElement], exports: [BidiModule, CdkScrollableModule, CdkFixedSizeVirtualScroll,
  1421. CdkVirtualForOf,
  1422. CdkVirtualScrollViewport,
  1423. CdkVirtualScrollableWindow,
  1424. CdkVirtualScrollableElement] });
  1425. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollingModule, imports: [BidiModule,
  1426. CdkScrollableModule, BidiModule, CdkScrollableModule] });
  1427. }
  1428. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: ScrollingModule, decorators: [{
  1429. type: NgModule,
  1430. args: [{
  1431. imports: [
  1432. BidiModule,
  1433. CdkScrollableModule,
  1434. CdkVirtualScrollViewport,
  1435. CdkFixedSizeVirtualScroll,
  1436. CdkVirtualForOf,
  1437. CdkVirtualScrollableWindow,
  1438. CdkVirtualScrollableElement,
  1439. ],
  1440. exports: [
  1441. BidiModule,
  1442. CdkScrollableModule,
  1443. CdkFixedSizeVirtualScroll,
  1444. CdkVirtualForOf,
  1445. CdkVirtualScrollViewport,
  1446. CdkVirtualScrollableWindow,
  1447. CdkVirtualScrollableElement,
  1448. ],
  1449. }]
  1450. }] });
  1451. export { CdkFixedSizeVirtualScroll, CdkScrollable, CdkScrollableModule, CdkVirtualForOf, CdkVirtualScrollViewport, CdkVirtualScrollable, CdkVirtualScrollableElement, CdkVirtualScrollableWindow, DEFAULT_RESIZE_TIME, DEFAULT_SCROLL_TIME, FixedSizeVirtualScrollStrategy, ScrollDispatcher, ScrollingModule, VIRTUAL_SCROLLABLE, VIRTUAL_SCROLL_STRATEGY, ViewportRuler, _fixedSizeVirtualScrollStrategyFactory };
  1452. //# sourceMappingURL=scrolling.mjs.map