Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private calculateOverlayPosition(): void {
const itemHeight = this.getItemHeight();
const items = this.getItemCount();
const panelHeight = Math.min(items * itemHeight, SELECT_PANEL_MAX_HEIGHT);
const scrollContainerHeight = items * itemHeight;
// The farthest the panel can be scrolled before it hits the bottom
const maxScroll = scrollContainerHeight - panelHeight;
// If no value is selected we open the popup to the first item.
let selectedOptionOffset =
this.empty ? 0 : this.getOptionIndex(this.selectionModel.selected[0])!;
selectedOptionOffset += countGroupLabelsBeforeOption(selectedOptionOffset, this.options, this.optionGroups);
// We must maintain a scroll buffer so the selected option will be scrolled to the
// center of the overlay panel rather than the top.
/* tslint:disable-next-line:no-magic-numbers */
const scrollBuffer = panelHeight / 2;
this.scrollTop = this.calculateOverlayScroll(selectedOptionOffset, scrollBuffer, maxScroll);
this.offsetY = this.calculateOverlayOffsetY();
this.checkOverlayWithinViewport(maxScroll);
}
private scrollActiveOptionIntoView(): void {
const activeOptionIndex = this.keyManager.activeItemIndex || 0;
const labelCount = countGroupLabelsBeforeOption(activeOptionIndex, this.options, this.optionGroups);
this.panel.nativeElement.scrollTop = getOptionScrollPosition(
activeOptionIndex + labelCount,
this.getItemHeight(),
this.panel.nativeElement.scrollTop,
SELECT_PANEL_MAX_HEIGHT
);
}
private scrollToOption(): void {
const index = this.autocomplete.keyManager.activeItemIndex || 0;
const labelCount = countGroupLabelsBeforeOption(index,
this.autocomplete.options, this.autocomplete.optionGroups);
const newScrollPosition = getOptionScrollPosition(
index + labelCount,
AUTOCOMPLETE_OPTION_HEIGHT,
this.autocomplete.getScrollTop(),
AUTOCOMPLETE_PANEL_HEIGHT
);
this.autocomplete.setScrollTop(newScrollPosition);
}
private scrollActiveOptionIntoView(): void {
const activeOptionIndex = this.keyManager.activeItemIndex || 0;
const labelCount = countGroupLabelsBeforeOption(activeOptionIndex, this.options, this.optionGroups);
this.optionsContainer.nativeElement.scrollTop = getOptionScrollPosition(
activeOptionIndex + labelCount,
this.getItemHeight(),
this.optionsContainer.nativeElement.scrollTop,
SELECT_PANEL_MAX_HEIGHT
);
}