@angular/cdk
Version:
Angular Material Component Development Kit
1,240 lines (1,235 loc) • 90.6 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { InjectionToken, Directive, forwardRef, Input, Injectable, NgZone, Optional, SkipSelf, ElementRef, NgModule, IterableDiffers, TemplateRef, ViewContainerRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Output, ViewChild, ViewEncapsulation, defineInjectable, inject } from '@angular/core';
import { coerceNumberProperty } from '@angular/cdk/coercion';
import { Subject, fromEvent, of, Observable, animationFrameScheduler, asapScheduler, merge } from 'rxjs';
import { distinctUntilChanged, auditTime, filter, takeUntil, startWith, pairwise, shareReplay, switchMap } from 'rxjs/operators';
import { Platform, getRtlScrollAxisType, RtlScrollAxisType, supportsScrollBehavior, PlatformModule } from '@angular/cdk/platform';
import { Directionality, BidiModule } from '@angular/cdk/bidi';
import { __extends } from 'tslib';
import { ArrayDataSource, isDataSource } from '@angular/cdk/collections';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* The injection token used to specify the virtual scrolling strategy.
* @type {?}
*/
var VIRTUAL_SCROLL_STRATEGY = new InjectionToken('VIRTUAL_SCROLL_STRATEGY');
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* Virtual scrolling strategy for lists with items of known fixed size.
*/
var /**
* Virtual scrolling strategy for lists with items of known fixed size.
*/
FixedSizeVirtualScrollStrategy = /** @class */ (function () {
/**
* @param itemSize The size of the items in the virtually scrolling list.
* @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
*/
function FixedSizeVirtualScrollStrategy(itemSize, minBufferPx, maxBufferPx) {
this._scrolledIndexChange = new Subject();
/**
* \@docs-private Implemented as part of VirtualScrollStrategy.
*/
this.scrolledIndexChange = this._scrolledIndexChange.pipe(distinctUntilChanged());
/**
* The attached viewport.
*/
this._viewport = null;
this._itemSize = itemSize;
this._minBufferPx = minBufferPx;
this._maxBufferPx = maxBufferPx;
}
/**
* Attaches this scroll strategy to a viewport.
* @param viewport The viewport to attach this strategy to.
*/
/**
* Attaches this scroll strategy to a viewport.
* @param {?} viewport The viewport to attach this strategy to.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.attach = /**
* Attaches this scroll strategy to a viewport.
* @param {?} viewport The viewport to attach this strategy to.
* @return {?}
*/
function (viewport) {
this._viewport = viewport;
this._updateTotalContentSize();
this._updateRenderedRange();
};
/** Detaches this scroll strategy from the currently attached viewport. */
/**
* Detaches this scroll strategy from the currently attached viewport.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.detach = /**
* Detaches this scroll strategy from the currently attached viewport.
* @return {?}
*/
function () {
this._scrolledIndexChange.complete();
this._viewport = null;
};
/**
* Update the item size and buffer size.
* @param itemSize The size of the items in the virtually scrolling list.
* @param minBufferPx The minimum amount of buffer (in pixels) before needing to render more
* @param maxBufferPx The amount of buffer (in pixels) to render when rendering more.
*/
/**
* Update the item size and buffer size.
* @param {?} itemSize The size of the items in the virtually scrolling list.
* @param {?} minBufferPx The minimum amount of buffer (in pixels) before needing to render more
* @param {?} maxBufferPx The amount of buffer (in pixels) to render when rendering more.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.updateItemAndBufferSize = /**
* Update the item size and buffer size.
* @param {?} itemSize The size of the items in the virtually scrolling list.
* @param {?} minBufferPx The minimum amount of buffer (in pixels) before needing to render more
* @param {?} maxBufferPx The amount of buffer (in pixels) to render when rendering more.
* @return {?}
*/
function (itemSize, minBufferPx, maxBufferPx) {
if (maxBufferPx < minBufferPx) {
throw Error('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
}
this._itemSize = itemSize;
this._minBufferPx = minBufferPx;
this._maxBufferPx = maxBufferPx;
this._updateTotalContentSize();
this._updateRenderedRange();
};
/** @docs-private Implemented as part of VirtualScrollStrategy. */
/**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.onContentScrolled = /**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
function () {
this._updateRenderedRange();
};
/** @docs-private Implemented as part of VirtualScrollStrategy. */
/**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.onDataLengthChanged = /**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
function () {
this._updateTotalContentSize();
this._updateRenderedRange();
};
/** @docs-private Implemented as part of VirtualScrollStrategy. */
/**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.onContentRendered = /**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
function () { };
/** @docs-private Implemented as part of VirtualScrollStrategy. */
/**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.onRenderedOffsetChanged = /**
* \@docs-private Implemented as part of VirtualScrollStrategy.
* @return {?}
*/
function () { };
/**
* Scroll to the offset for the given index.
* @param index The index of the element to scroll to.
* @param behavior The ScrollBehavior to use when scrolling.
*/
/**
* Scroll to the offset for the given index.
* @param {?} index The index of the element to scroll to.
* @param {?} behavior The ScrollBehavior to use when scrolling.
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype.scrollToIndex = /**
* Scroll to the offset for the given index.
* @param {?} index The index of the element to scroll to.
* @param {?} behavior The ScrollBehavior to use when scrolling.
* @return {?}
*/
function (index, behavior) {
if (this._viewport) {
this._viewport.scrollToOffset(index * this._itemSize, behavior);
}
};
/** Update the viewport's total content size. */
/**
* Update the viewport's total content size.
* @private
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype._updateTotalContentSize = /**
* Update the viewport's total content size.
* @private
* @return {?}
*/
function () {
if (!this._viewport) {
return;
}
this._viewport.setTotalContentSize(this._viewport.getDataLength() * this._itemSize);
};
/** Update the viewport's rendered range. */
/**
* Update the viewport's rendered range.
* @private
* @return {?}
*/
FixedSizeVirtualScrollStrategy.prototype._updateRenderedRange = /**
* Update the viewport's rendered range.
* @private
* @return {?}
*/
function () {
if (!this._viewport) {
return;
}
/** @type {?} */
var scrollOffset = this._viewport.measureScrollOffset();
/** @type {?} */
var firstVisibleIndex = scrollOffset / this._itemSize;
/** @type {?} */
var renderedRange = this._viewport.getRenderedRange();
/** @type {?} */
var newRange = { start: renderedRange.start, end: renderedRange.end };
/** @type {?} */
var viewportSize = this._viewport.getViewportSize();
/** @type {?} */
var dataLength = this._viewport.getDataLength();
/** @type {?} */
var startBuffer = scrollOffset - newRange.start * this._itemSize;
if (startBuffer < this._minBufferPx && newRange.start != 0) {
/** @type {?} */
var expandStart = Math.ceil((this._maxBufferPx - startBuffer) / this._itemSize);
newRange.start = Math.max(0, newRange.start - expandStart);
newRange.end = Math.min(dataLength, Math.ceil(firstVisibleIndex + (viewportSize + this._minBufferPx) / this._itemSize));
}
else {
/** @type {?} */
var endBuffer = newRange.end * this._itemSize - (scrollOffset + viewportSize);
if (endBuffer < this._minBufferPx && newRange.end != dataLength) {
/** @type {?} */
var expandEnd = Math.ceil((this._maxBufferPx - endBuffer) / this._itemSize);
if (expandEnd > 0) {
newRange.end = Math.min(dataLength, newRange.end + expandEnd);
newRange.start = Math.max(0, Math.floor(firstVisibleIndex - this._minBufferPx / this._itemSize));
}
}
}
this._viewport.setRenderedRange(newRange);
this._viewport.setRenderedContentOffset(this._itemSize * newRange.start);
this._scrolledIndexChange.next(Math.floor(firstVisibleIndex));
};
return FixedSizeVirtualScrollStrategy;
}());
/**
* Provider factory for `FixedSizeVirtualScrollStrategy` that simply extracts the already created
* `FixedSizeVirtualScrollStrategy` from the given directive.
* @param {?} fixedSizeDir The instance of `CdkFixedSizeVirtualScroll` to extract the
* `FixedSizeVirtualScrollStrategy` from.
* @return {?}
*/
function _fixedSizeVirtualScrollStrategyFactory(fixedSizeDir) {
return fixedSizeDir._scrollStrategy;
}
/**
* A virtual scroll strategy that supports fixed-size items.
*/
var CdkFixedSizeVirtualScroll = /** @class */ (function () {
function CdkFixedSizeVirtualScroll() {
this._itemSize = 20;
this._minBufferPx = 100;
this._maxBufferPx = 200;
/**
* The scroll strategy used by this directive.
*/
this._scrollStrategy = new FixedSizeVirtualScrollStrategy(this.itemSize, this.minBufferPx, this.maxBufferPx);
}
Object.defineProperty(CdkFixedSizeVirtualScroll.prototype, "itemSize", {
/** The size of the items in the list (in pixels). */
get: /**
* The size of the items in the list (in pixels).
* @return {?}
*/
function () { return this._itemSize; },
set: /**
* @param {?} value
* @return {?}
*/
function (value) { this._itemSize = coerceNumberProperty(value); },
enumerable: true,
configurable: true
});
Object.defineProperty(CdkFixedSizeVirtualScroll.prototype, "minBufferPx", {
/**
* The minimum amount of buffer rendered beyond the viewport (in pixels).
* If the amount of buffer dips below this number, more items will be rendered. Defaults to 100px.
*/
get: /**
* The minimum amount of buffer rendered beyond the viewport (in pixels).
* If the amount of buffer dips below this number, more items will be rendered. Defaults to 100px.
* @return {?}
*/
function () { return this._minBufferPx; },
set: /**
* @param {?} value
* @return {?}
*/
function (value) { this._minBufferPx = coerceNumberProperty(value); },
enumerable: true,
configurable: true
});
Object.defineProperty(CdkFixedSizeVirtualScroll.prototype, "maxBufferPx", {
/**
* The number of pixels worth of buffer to render for when rendering new items. Defaults to 200px.
*/
get: /**
* The number of pixels worth of buffer to render for when rendering new items. Defaults to 200px.
* @return {?}
*/
function () { return this._maxBufferPx; },
set: /**
* @param {?} value
* @return {?}
*/
function (value) { this._maxBufferPx = coerceNumberProperty(value); },
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
CdkFixedSizeVirtualScroll.prototype.ngOnChanges = /**
* @return {?}
*/
function () {
this._scrollStrategy.updateItemAndBufferSize(this.itemSize, this.minBufferPx, this.maxBufferPx);
};
CdkFixedSizeVirtualScroll.decorators = [
{ type: Directive, args: [{
selector: 'cdk-virtual-scroll-viewport[itemSize]',
providers: [{
provide: VIRTUAL_SCROLL_STRATEGY,
useFactory: _fixedSizeVirtualScrollStrategyFactory,
deps: [forwardRef(function () { return CdkFixedSizeVirtualScroll; })],
}],
},] },
];
CdkFixedSizeVirtualScroll.propDecorators = {
itemSize: [{ type: Input }],
minBufferPx: [{ type: Input }],
maxBufferPx: [{ type: Input }]
};
return CdkFixedSizeVirtualScroll;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* Time in ms to throttle the scrolling events by default.
* @type {?}
*/
var DEFAULT_SCROLL_TIME = 20;
/**
* Service contained all registered Scrollable references and emits an event when any one of the
* Scrollable references emit a scrolled event.
*/
var ScrollDispatcher = /** @class */ (function () {
function ScrollDispatcher(_ngZone, _platform) {
this._ngZone = _ngZone;
this._platform = _platform;
/**
* Subject for notifying that a registered scrollable reference element has been scrolled.
*/
this._scrolled = new Subject();
/**
* Keeps track of the global `scroll` and `resize` subscriptions.
*/
this._globalSubscription = null;
/**
* Keeps track of the amount of subscriptions to `scrolled`. Used for cleaning up afterwards.
*/
this._scrolledCount = 0;
/**
* Map of all the scrollable references that are registered with the service and their
* scroll event subscriptions.
*/
this.scrollContainers = new Map();
}
/**
* Registers a scrollable instance with the service and listens for its scrolled events. When the
* scrollable is scrolled, the service emits the event to its scrolled observable.
* @param scrollable Scrollable instance to be registered.
*/
/**
* Registers a scrollable instance with the service and listens for its scrolled events. When the
* scrollable is scrolled, the service emits the event to its scrolled observable.
* @param {?} scrollable Scrollable instance to be registered.
* @return {?}
*/
ScrollDispatcher.prototype.register = /**
* Registers a scrollable instance with the service and listens for its scrolled events. When the
* scrollable is scrolled, the service emits the event to its scrolled observable.
* @param {?} scrollable Scrollable instance to be registered.
* @return {?}
*/
function (scrollable) {
var _this = this;
if (!this.scrollContainers.has(scrollable)) {
this.scrollContainers.set(scrollable, scrollable.elementScrolled()
.subscribe(function () { return _this._scrolled.next(scrollable); }));
}
};
/**
* Deregisters a Scrollable reference and unsubscribes from its scroll event observable.
* @param scrollable Scrollable instance to be deregistered.
*/
/**
* Deregisters a Scrollable reference and unsubscribes from its scroll event observable.
* @param {?} scrollable Scrollable instance to be deregistered.
* @return {?}
*/
ScrollDispatcher.prototype.deregister = /**
* Deregisters a Scrollable reference and unsubscribes from its scroll event observable.
* @param {?} scrollable Scrollable instance to be deregistered.
* @return {?}
*/
function (scrollable) {
/** @type {?} */
var scrollableReference = this.scrollContainers.get(scrollable);
if (scrollableReference) {
scrollableReference.unsubscribe();
this.scrollContainers.delete(scrollable);
}
};
/**
* Returns an observable that emits an event whenever any of the registered Scrollable
* references (or window, document, or body) fire a scrolled event. Can provide a time in ms
* to override the default "throttle" time.
*
* **Note:** in order to avoid hitting change detection for every scroll event,
* all of the events emitted from this stream will be run outside the Angular zone.
* If you need to update any data bindings as a result of a scroll event, you have
* to run the callback using `NgZone.run`.
*/
/**
* Returns an observable that emits an event whenever any of the registered Scrollable
* references (or window, document, or body) fire a scrolled event. Can provide a time in ms
* to override the default "throttle" time.
*
* **Note:** in order to avoid hitting change detection for every scroll event,
* all of the events emitted from this stream will be run outside the Angular zone.
* If you need to update any data bindings as a result of a scroll event, you have
* to run the callback using `NgZone.run`.
* @param {?=} auditTimeInMs
* @return {?}
*/
ScrollDispatcher.prototype.scrolled = /**
* Returns an observable that emits an event whenever any of the registered Scrollable
* references (or window, document, or body) fire a scrolled event. Can provide a time in ms
* to override the default "throttle" time.
*
* **Note:** in order to avoid hitting change detection for every scroll event,
* all of the events emitted from this stream will be run outside the Angular zone.
* If you need to update any data bindings as a result of a scroll event, you have
* to run the callback using `NgZone.run`.
* @param {?=} auditTimeInMs
* @return {?}
*/
function (auditTimeInMs) {
var _this = this;
if (auditTimeInMs === void 0) { auditTimeInMs = DEFAULT_SCROLL_TIME; }
if (!this._platform.isBrowser) {
return of();
}
return new Observable(function (observer) {
if (!_this._globalSubscription) {
_this._addGlobalListener();
}
// In the case of a 0ms delay, use an observable without auditTime
// since it does add a perceptible delay in processing overhead.
/** @type {?} */
var subscription = auditTimeInMs > 0 ?
_this._scrolled.pipe(auditTime(auditTimeInMs)).subscribe(observer) :
_this._scrolled.subscribe(observer);
_this._scrolledCount++;
return function () {
subscription.unsubscribe();
_this._scrolledCount--;
if (!_this._scrolledCount) {
_this._removeGlobalListener();
}
};
});
};
/**
* @return {?}
*/
ScrollDispatcher.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
var _this = this;
this._removeGlobalListener();
this.scrollContainers.forEach(function (_, container) { return _this.deregister(container); });
this._scrolled.complete();
};
/**
* Returns an observable that emits whenever any of the
* scrollable ancestors of an element are scrolled.
* @param elementRef Element whose ancestors to listen for.
* @param auditTimeInMs Time to throttle the scroll events.
*/
/**
* Returns an observable that emits whenever any of the
* scrollable ancestors of an element are scrolled.
* @param {?} elementRef Element whose ancestors to listen for.
* @param {?=} auditTimeInMs Time to throttle the scroll events.
* @return {?}
*/
ScrollDispatcher.prototype.ancestorScrolled = /**
* Returns an observable that emits whenever any of the
* scrollable ancestors of an element are scrolled.
* @param {?} elementRef Element whose ancestors to listen for.
* @param {?=} auditTimeInMs Time to throttle the scroll events.
* @return {?}
*/
function (elementRef, auditTimeInMs) {
/** @type {?} */
var ancestors = this.getAncestorScrollContainers(elementRef);
return this.scrolled(auditTimeInMs).pipe(filter(function (target) {
return !target || ancestors.indexOf(target) > -1;
}));
};
/** Returns all registered Scrollables that contain the provided element. */
/**
* Returns all registered Scrollables that contain the provided element.
* @param {?} elementRef
* @return {?}
*/
ScrollDispatcher.prototype.getAncestorScrollContainers = /**
* Returns all registered Scrollables that contain the provided element.
* @param {?} elementRef
* @return {?}
*/
function (elementRef) {
var _this = this;
/** @type {?} */
var scrollingContainers = [];
this.scrollContainers.forEach(function (_subscription, scrollable) {
if (_this._scrollableContainsElement(scrollable, elementRef)) {
scrollingContainers.push(scrollable);
}
});
return scrollingContainers;
};
/** Returns true if the element is contained within the provided Scrollable. */
/**
* Returns true if the element is contained within the provided Scrollable.
* @private
* @param {?} scrollable
* @param {?} elementRef
* @return {?}
*/
ScrollDispatcher.prototype._scrollableContainsElement = /**
* Returns true if the element is contained within the provided Scrollable.
* @private
* @param {?} scrollable
* @param {?} elementRef
* @return {?}
*/
function (scrollable, elementRef) {
/** @type {?} */
var element = elementRef.nativeElement;
/** @type {?} */
var scrollableElement = scrollable.getElementRef().nativeElement;
// Traverse through the element parents until we reach null, checking if any of the elements
// are the scrollable's element.
do {
if (element == scrollableElement) {
return true;
}
} while (element = (/** @type {?} */ (element)).parentElement);
return false;
};
/** Sets up the global scroll listeners. */
/**
* Sets up the global scroll listeners.
* @private
* @return {?}
*/
ScrollDispatcher.prototype._addGlobalListener = /**
* Sets up the global scroll listeners.
* @private
* @return {?}
*/
function () {
var _this = this;
this._globalSubscription = this._ngZone.runOutsideAngular(function () {
return fromEvent(window.document, 'scroll').subscribe(function () { return _this._scrolled.next(); });
});
};
/** Cleans up the global scroll listener. */
/**
* Cleans up the global scroll listener.
* @private
* @return {?}
*/
ScrollDispatcher.prototype._removeGlobalListener = /**
* Cleans up the global scroll listener.
* @private
* @return {?}
*/
function () {
if (this._globalSubscription) {
this._globalSubscription.unsubscribe();
this._globalSubscription = null;
}
};
ScrollDispatcher.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] },
];
/** @nocollapse */
ScrollDispatcher.ctorParameters = function () { return [
{ type: NgZone },
{ type: Platform }
]; };
/** @nocollapse */ ScrollDispatcher.ngInjectableDef = defineInjectable({ factory: function ScrollDispatcher_Factory() { return new ScrollDispatcher(inject(NgZone), inject(Platform)); }, token: ScrollDispatcher, providedIn: "root" });
return ScrollDispatcher;
}());
/**
* \@docs-private \@deprecated \@breaking-change 8.0.0
* @param {?} parentDispatcher
* @param {?} ngZone
* @param {?} platform
* @return {?}
*/
function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher, ngZone, platform) {
return parentDispatcher || new ScrollDispatcher(ngZone, platform);
}
/**
* \@docs-private \@deprecated \@breaking-change 8.0.0
* @type {?}
*/
var SCROLL_DISPATCHER_PROVIDER = {
// If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one.
provide: ScrollDispatcher,
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone, Platform],
useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* Sends an event when the directive's element is scrolled. Registers itself with the
* ScrollDispatcher service to include itself as part of its collection of scrolling events that it
* can be listened to through the service.
*/
var CdkScrollable = /** @class */ (function () {
function CdkScrollable(elementRef, scrollDispatcher, ngZone, dir) {
var _this = this;
this.elementRef = elementRef;
this.scrollDispatcher = scrollDispatcher;
this.ngZone = ngZone;
this.dir = dir;
this._destroyed = new Subject();
this._elementScrolled = new Observable(function (observer) {
return _this.ngZone.runOutsideAngular(function () {
return fromEvent(_this.elementRef.nativeElement, 'scroll').pipe(takeUntil(_this._destroyed))
.subscribe(observer);
});
});
}
/**
* @return {?}
*/
CdkScrollable.prototype.ngOnInit = /**
* @return {?}
*/
function () {
this.scrollDispatcher.register(this);
};
/**
* @return {?}
*/
CdkScrollable.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.scrollDispatcher.deregister(this);
this._destroyed.next();
this._destroyed.complete();
};
/** Returns observable that emits when a scroll event is fired on the host element. */
/**
* Returns observable that emits when a scroll event is fired on the host element.
* @return {?}
*/
CdkScrollable.prototype.elementScrolled = /**
* Returns observable that emits when a scroll event is fired on the host element.
* @return {?}
*/
function () {
return this._elementScrolled;
};
/** Gets the ElementRef for the viewport. */
/**
* Gets the ElementRef for the viewport.
* @return {?}
*/
CdkScrollable.prototype.getElementRef = /**
* Gets the ElementRef for the viewport.
* @return {?}
*/
function () {
return this.elementRef;
};
/**
* Scrolls to the specified offsets. This is a normalized version of the browser's native scrollTo
* method, since browsers are not consistent about what scrollLeft means in RTL. For this method
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param options specified the offsets to scroll to.
*/
/**
* Scrolls to the specified offsets. This is a normalized version of the browser's native scrollTo
* method, since browsers are not consistent about what scrollLeft means in RTL. For this method
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param {?} options specified the offsets to scroll to.
* @return {?}
*/
CdkScrollable.prototype.scrollTo = /**
* Scrolls to the specified offsets. This is a normalized version of the browser's native scrollTo
* method, since browsers are not consistent about what scrollLeft means in RTL. For this method
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param {?} options specified the offsets to scroll to.
* @return {?}
*/
function (options) {
/** @type {?} */
var el = this.elementRef.nativeElement;
/** @type {?} */
var isRtl = this.dir && this.dir.value == 'rtl';
// Rewrite start & end offsets as right or left offsets.
options.left = options.left == null ? (isRtl ? options.end : options.start) : options.left;
options.right = options.right == null ? (isRtl ? options.start : options.end) : options.right;
// Rewrite the bottom offset as a top offset.
if (options.bottom != null) {
((/** @type {?} */ (options))).top =
el.scrollHeight - el.clientHeight - options.bottom;
}
// Rewrite the right offset as a left offset.
if (isRtl && getRtlScrollAxisType() != RtlScrollAxisType.NORMAL) {
if (options.left != null) {
((/** @type {?} */ (options))).right =
el.scrollWidth - el.clientWidth - options.left;
}
if (getRtlScrollAxisType() == RtlScrollAxisType.INVERTED) {
options.left = options.right;
}
else if (getRtlScrollAxisType() == RtlScrollAxisType.NEGATED) {
options.left = options.right ? -options.right : options.right;
}
}
else {
if (options.right != null) {
((/** @type {?} */ (options))).left =
el.scrollWidth - el.clientWidth - options.right;
}
}
this._applyScrollToOptions(options);
};
/**
* @private
* @param {?} options
* @return {?}
*/
CdkScrollable.prototype._applyScrollToOptions = /**
* @private
* @param {?} options
* @return {?}
*/
function (options) {
/** @type {?} */
var el = this.elementRef.nativeElement;
if (supportsScrollBehavior()) {
el.scrollTo(options);
}
else {
if (options.top != null) {
el.scrollTop = options.top;
}
if (options.left != null) {
el.scrollLeft = options.left;
}
}
};
/**
* Measures the scroll offset relative to the specified edge of the viewport. This method can be
* used instead of directly checking scrollLeft or scrollTop, since browsers are not consistent
* about what scrollLeft means in RTL. The values returned by this method are normalized such that
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param from The edge to measure from.
*/
/**
* Measures the scroll offset relative to the specified edge of the viewport. This method can be
* used instead of directly checking scrollLeft or scrollTop, since browsers are not consistent
* about what scrollLeft means in RTL. The values returned by this method are normalized such that
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param {?} from The edge to measure from.
* @return {?}
*/
CdkScrollable.prototype.measureScrollOffset = /**
* Measures the scroll offset relative to the specified edge of the viewport. This method can be
* used instead of directly checking scrollLeft or scrollTop, since browsers are not consistent
* about what scrollLeft means in RTL. The values returned by this method are normalized such that
* left and right always refer to the left and right side of the scrolling container irrespective
* of the layout direction. start and end refer to left and right in an LTR context and vice-versa
* in an RTL context.
* @param {?} from The edge to measure from.
* @return {?}
*/
function (from) {
/** @type {?} */
var LEFT = 'left';
/** @type {?} */
var RIGHT = 'right';
/** @type {?} */
var el = this.elementRef.nativeElement;
if (from == 'top') {
return el.scrollTop;
}
if (from == 'bottom') {
return el.scrollHeight - el.clientHeight - el.scrollTop;
}
// Rewrite start & end as left or right offsets.
/** @type {?} */
var isRtl = this.dir && this.dir.value == 'rtl';
if (from == 'start') {
from = isRtl ? RIGHT : LEFT;
}
else if (from == 'end') {
from = isRtl ? LEFT : RIGHT;
}
if (isRtl && getRtlScrollAxisType() == RtlScrollAxisType.INVERTED) {
// For INVERTED, scrollLeft is (scrollWidth - clientWidth) when scrolled all the way left and
// 0 when scrolled all the way right.
if (from == LEFT) {
return el.scrollWidth - el.clientWidth - el.scrollLeft;
}
else {
return el.scrollLeft;
}
}
else if (isRtl && getRtlScrollAxisType() == RtlScrollAxisType.NEGATED) {
// For NEGATED, scrollLeft is -(scrollWidth - clientWidth) when scrolled all the way left and
// 0 when scrolled all the way right.
if (from == LEFT) {
return el.scrollLeft + el.scrollWidth - el.clientWidth;
}
else {
return -el.scrollLeft;
}
}
else {
// For NORMAL, as well as non-RTL contexts, scrollLeft is 0 when scrolled all the way left and
// (scrollWidth - clientWidth) when scrolled all the way right.
if (from == LEFT) {
return el.scrollLeft;
}
else {
return el.scrollWidth - el.clientWidth - el.scrollLeft;
}
}
};
CdkScrollable.decorators = [
{ type: Directive, args: [{
selector: '[cdk-scrollable], [cdkScrollable]'
},] },
];
/** @nocollapse */
CdkScrollable.ctorParameters = function () { return [
{ type: ElementRef },
{ type: ScrollDispatcher },
{ type: NgZone },
{ type: Directionality, decorators: [{ type: Optional }] }
]; };
return CdkScrollable;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* Checks if the given ranges are equal.
* @param {?} r1
* @param {?} r2
* @return {?}
*/
function rangesEqual(r1, r2) {
return r1.start == r2.start && r1.end == r2.end;
}
/**
* Scheduler to be used for scroll events. Needs to fall back to
* something that doesn't rely on requestAnimationFrame on environments
* that don't support it (e.g. server-side rendering).
* @type {?}
*/
var SCROLL_SCHEDULER = typeof requestAnimationFrame !== 'undefined' ? animationFrameScheduler : asapScheduler;
/**
* A viewport that virtualizes it's scrolling with the help of `CdkVirtualForOf`.
*/
var CdkVirtualScrollViewport = /** @class */ (function (_super) {
__extends(CdkVirtualScrollViewport, _super);
function CdkVirtualScrollViewport(elementRef, _changeDetectorRef, ngZone, _scrollStrategy, dir, scrollDispatcher) {
var _this = _super.call(this, elementRef, scrollDispatcher, ngZone, dir) || this;
_this.elementRef = elementRef;
_this._changeDetectorRef = _changeDetectorRef;
_this._scrollStrategy = _scrollStrategy;
/**
* Emits when the viewport is detached from a CdkVirtualForOf.
*/
_this._detachedSubject = new Subject();
/**
* Emits when the rendered range changes.
*/
_this._renderedRangeSubject = new Subject();
/**
* The direction the viewport scrolls.
*/
_this.orientation = 'vertical';
// Note: we don't use the typical EventEmitter here because we need to subscribe to the scroll
// strategy lazily (i.e. only if the user is actually listening to the events). We do this because
// depending on how the strategy calculates the scrolled index, it may come at a cost to
// performance.
/**
* Emits when the index of the first element visible in the viewport changes.
*/
_this.scrolledIndexChange = new Observable(function (observer) {
return _this._scrollStrategy.scrolledIndexChange.subscribe(function (index) {
return Promise.resolve().then(function () { return _this.ngZone.run(function () { return observer.next(index); }); });
});
});
/**
* A stream that emits whenever the rendered range changes.
*/
_this.renderedRangeStream = _this._renderedRangeSubject.asObservable();
/**
* The transform used to scale the spacer to the same size as all content, including content that
* is not currently rendered.
*/
_this._totalContentSizeTransform = '';
/**
* The total size of all content (in pixels), including content that is not currently rendered.
*/
_this._totalContentSize = 0;
/**
* The currently rendered range of indices.
*/
_this._renderedRange = { start: 0, end: 0 };
/**
* The length of the data bound to this viewport (in number of items).
*/
_this._dataLength = 0;
/**
* The size of the viewport (in pixels).
*/
_this._viewportSize = 0;
/**
* The last rendered content offset that was set.
*/
_this._renderedContentOffset = 0;
/**
* Whether the last rendered content offset was to the end of the content (and therefore needs to
* be rewritten as an offset to the start of the content).
*/
_this._renderedContentOffsetNeedsRewrite = false;
/**
* Whether there is a pending change detection cycle.
*/
_this._isChangeDetectionPending = false;
/**
* A list of functions to run after the next change detection cycle.
*/
_this._runAfterChangeDetection = [];
if (!_scrollStrategy) {
throw Error('Error: cdk-virtual-scroll-viewport requires the "itemSize" property to be set.');
}
return _this;
}
/**
* @return {?}
*/
CdkVirtualScrollViewport.prototype.ngOnInit = /**
* @return {?}
*/
function () {
var _this = this;
_super.prototype.ngOnInit.call(this);
// It's still too early to measure the viewport at this point. Deferring with a promise allows
// the Viewport to be rendered with the correct size before we measure. We run this outside the
// zone to avoid causing more change detection cycles. We handle the change detection loop
// ourselves instead.
this.ngZone.runOutsideAngular(function () { return Promise.resolve().then(function () {
_this._measureViewportSize();
_this._scrollStrategy.attach(_this);
_this.elementScrolled()
.pipe(
// Start off with a fake scroll event so we properly detect our initial position.
startWith((/** @type {?} */ (null))),
// Collect multiple events into one until the next animation frame. This way if
// there are multiple scroll events in the same frame we only need to recheck
// our layout once.
auditTime(0, SCROLL_SCHEDULER))
.subscribe(function () { return _this._scrollStrategy.onContentScrolled(); });
_this._markChangeDetectionNeeded();
}); });
};
/**
* @return {?}
*/
CdkVirtualScrollViewport.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.detach();
this._scrollStrategy.detach();
// Complete all subjects
this._renderedRangeSubject.complete();
this._detachedSubject.complete();
_super.prototype.ngOnDestroy.call(this);
};
/** Attaches a `CdkVirtualForOf` to this viewport. */
/**
* Attaches a `CdkVirtualForOf` to this viewport.
* @param {?} forOf
* @return {?}
*/
CdkVirtualScrollViewport.prototype.attach = /**
* Attaches a `CdkVirtualForOf` to this viewport.
* @param {?} forOf
* @return {?}
*/
function (forOf) {
var _this = this;
if (this._forOf) {
throw Error('CdkVirtualScrollViewport is already attached.');
}
// Subscribe to the data stream of the CdkVirtualForOf to keep track of when the data length
// changes. Run outside the zone to avoid triggering change detection, since we're managing the
// change detection loop ourselves.
this.ngZone.runOutsideAngular(function () {
_this._forOf = forOf;
_this._forOf.dataStream.pipe(takeUntil(_this._detachedSubject)).subscribe(function (data) {
/** @type {?} */
var newLength = data.length;
if (newLength !== _this._dataLength) {
_this._dataLength = newLength;
_this._scrollStrategy.onDataLengthChanged();
}
_this._doChangeDetection();
});
});
};
/** Detaches the current `CdkVirtualForOf`. */
/**
* Detaches the current `CdkVirtualForOf`.
* @return {?}
*/
CdkVirtualScrollViewport.prototype.detach = /**
* Detaches the current `CdkVirtualForOf`.
* @return {?}
*/
function () {
this._forOf = null;
this._detachedSubject.next();
};
/** Gets the length of the data bound to this viewport (in number of items). */
/**
* Gets the length of the data bound to this viewport (in number of items).
* @return {?}
*/
CdkVirtualScrollViewport.prototype.getDataLength = /**
* Gets the length of the data bound to this viewport (in number of items).
* @return {?}
*/
function () {
return this._dataLength;
};
/** Gets the size of the viewport (in pixels). */
/**
* Gets the size of the viewport (in pixels).
* @return {?}
*/
CdkVirtualScrollViewport.prototype.getViewportSize = /**
* Gets the size of the viewport (in pixels).
* @return {?}
*/
function () {
return this._viewportSize;
};
// TODO(mmalerba): This is technically out of sync with what's really rendered until a render
// cycle happens. I'm being careful to only call it after the render cycle is complete and before
// setting it to something else, but its error prone and should probably be split into
// `pendingRange` and `renderedRange`, the latter reflecting whats actually in the DOM.
/** Get the current rendered range of items. */
// TODO(mmalerba): This is technically out of sync with what's really rendered until a render
// cycle happens. I'm being careful to only call it after the render cycle is complete and before
// setting it to something else, but its error prone and should probably be split into
// `pendingRange` and `renderedRange`, the latter reflecting whats actually in the DOM.
/**
* Get the current rendered range of items.
* @return {?}
*/
CdkVirtualScrollViewport.prototype.getRenderedRange =
// TODO(mmalerba): This is technically out of sync with what's really rendered until a render
// cycle happens. I'm being careful to only call it after the render cycle is complete and before
// setting it to something else, but its error prone and should probably be split into
// `pendingRange` and `renderedRange`, the latter reflecting whats actually in the DOM.
/**
* Get the current rendered range of items.
* @return {?}
*/
function () {
return this._renderedRange;
};
/**
* Sets the total size of all content (in pixels), including content that is not currently
* rendered.
*/
/**
* Sets the total size of all content (in pixels), including content that is not currently
* rendered.
* @param {?} size
* @return {?}
*/
CdkVirtualScrollViewport.prototype.setTotalContentSize = /**
* Sets the total size of all content (in pixels), including content that is not currently
* rendered.
* @param {?} size
* @return {?}
*/
function (size) {
if (this._totalContentSize !== size) {
this._totalContentSize = size;
/** @type {?} */
var axis = this.orientation == 'horizontal' ? 'X' : 'Y';
this._totalContentSizeTransform = "scale" + axis + "(" + this._totalContentSize + ")";
this._markChangeDetectionNeeded();
}
};
/** Sets the currently rendered range of indices. */
/**
* Sets the currently rendered range of indices.
* @param {?} range
* @return {?}
*/
CdkVirtualScrollViewport.prototype.setRenderedRange = /**
* Sets the currently rendered range of indices.
* @param {?} range
* @return {?}
*/
function (range) {
var _this = this;
if (!rangesEqual(this._renderedRange, range)) {
this._renderedRangeSubject.next(this._renderedRange = range);
this._markChangeDetectionNeeded(function () { return _this._scrollStrategy.onContentRendered(); });
}
};
/**
* Gets the offset from the start of the viewport to the start of the rendered data (in pixels).
*/
/**
* Gets the offset from the start of the viewport to the start of the rendered data (in pixels).
* @return {?}
*/
CdkVirtualScrollViewport.prototype.getOffsetToRenderedContentStart = /**
* Gets the offset from the start of the viewport to the start of the rendered data (in pixels).
* @return {?}
*/
function () {
return this._renderedContentOffsetNeedsRewrite ? null : this._renderedContentOffset;
};
/**
* Sets the offset from the start of the viewport to either the start or end of the rendered data
* (in pixels).
*/
/**
* Sets the offset from the start of the viewport to either the start or end of the rendered data
* (in pixels).
* @param {?} offset
* @param {?=} to
* @return {?}
*/
CdkVirtualScrollViewport.prototype.setRenderedContentOffset = /**
* Sets the offset from the start of the viewport to either the start or end of the rendered data
* (in pixels).
* @param {?} offset
* @param {?=} to
* @return {?}
*/
function (offset, to) {
var _this = this;
if (to === void 0) { to = 'to-start'; }
// For a horizontal viewport in a right-to-left language we need to translate along the x-axis
// in the negative direction.
/** @type {?} */
var isRtl = this.dir && this.dir.value == 'rtl';
/** @type {?} */
var isHorizontal = this.orientation == 'horizontal';
/** @type {?} */
var axis = isHorizontal ? 'X' : 'Y';
/** @type {?} */
var axisDirection = isHorizontal && isRtl ? -1 : 1;
/** @type {?} */
var transform = "translate" + axis + "(" + Number(axisDirection * offset) + "px)";
this._renderedContentOffset = offset;
if (to === 'to-end') {
transform += " translate" + axis + "(-100%)";
// The viewport should rewrite this as a `to-start` offset on the next render cycle. Otherwise
// elements will appear to expand in the wrong direction (e.g. `mat-expansion-panel` would
// expand upward).
this._renderedContentOffsetNeedsRewrite = true;
}
if (this._renderedContentTransform != transform) {
// We know this value is safe because we parse `offset` w