Scrollbar directive improvements

This commit is contained in:
Adina Țeudan 2021-10-12 23:21:49 +03:00
parent 4040db5fba
commit 0de03537c4
3 changed files with 26 additions and 10 deletions

View File

@ -3,6 +3,7 @@
:host cdk-virtual-scroll-viewport {
height: calc(100vh - 50px - 31px - 111px);
overflow-y: hidden !important;
@include mixins.scroll-bar;
&.no-data {
display: none;
@ -131,7 +132,6 @@
&:hover {
overflow-y: auto !important;
@include mixins.scroll-bar;
&.has-scrollbar {
.table-item {

View File

@ -12,10 +12,11 @@ import {
ViewContainerRef,
} from '@angular/core';
import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
import { Required } from '../../utils';
import { AutoUnsubscribe, Required } from '../../utils';
import { IListable, ListingModes, TableColumnConfig } from '../models';
import { ListingComponent } from '../listing-component.directive';
import { EntitiesService } from '../services';
import { HasScrollbarDirective } from '../../scrollbar';
const SCROLLBAR_WIDTH = 11;
@ -25,7 +26,7 @@ const SCROLLBAR_WIDTH = 11;
styleUrls: ['./table.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableComponent<T extends IListable> implements OnInit {
export class TableComponent<T extends IListable> extends AutoUnsubscribe implements OnInit {
readonly listingModes = ListingModes;
@Input() bulkActions?: TemplateRef<unknown>;
@ -48,12 +49,15 @@ export class TableComponent<T extends IListable> implements OnInit {
@Input() itemMouseEnterFn?: (entity: T) => void;
@Input() itemMouseLeaveFn?: (entity: T) => void;
@ViewChild(CdkVirtualScrollViewport, { static: true }) readonly scrollViewport!: CdkVirtualScrollViewport;
@ViewChild(HasScrollbarDirective, { static: true }) hasScrollbarDirective!: HasScrollbarDirective;
constructor(
@Inject(forwardRef(() => ListingComponent)) readonly listingComponent: ListingComponent<T>,
private readonly _hostRef: ViewContainerRef,
readonly entitiesService: EntitiesService<T>,
) {}
) {
super();
}
get tableColumnConfigs(): readonly TableColumnConfig<T>[] {
return this.listingComponent.tableColumnConfigs;
@ -68,7 +72,12 @@ export class TableComponent<T extends IListable> implements OnInit {
}
ngOnInit(): void {
this.listingComponent.noContent$.subscribe(() => {
this.addSubscription = this.entitiesService.allLength$.subscribe(() => {
setTimeout(() => {
this.hasScrollbarDirective.process();
}, 0);
});
this.addSubscription = this.listingComponent.noContent$.subscribe(() => {
setTimeout(() => {
this.scrollViewport?.checkViewportSize();
}, 0);

View File

@ -1,10 +1,10 @@
import { AfterContentChecked, Directive, ElementRef, HostBinding } from '@angular/core';
import { Directive, ElementRef, HostBinding, HostListener, OnChanges, OnInit } from '@angular/core';
@Directive({
selector: '[iqserHasScrollbar]',
exportAs: 'iqserHasScrollbar',
})
export class HasScrollbarDirective implements AfterContentChecked {
export class HasScrollbarDirective implements OnInit, OnChanges {
@HostBinding('class') class = '';
constructor(private readonly _elementRef: ElementRef) {}
@ -14,14 +14,21 @@ export class HasScrollbarDirective implements AfterContentChecked {
return element.clientHeight < element.scrollHeight;
}
ngAfterContentChecked(): void {
this._process();
ngOnInit(): void {
setTimeout(() => {
this.process();
}, 0);
}
_process(): void {
@HostListener('window:resize')
process(): void {
const newClass = this.hasScrollbar ? 'has-scrollbar' : '';
if (this.class !== newClass) {
this.class = newClass;
}
}
ngOnChanges(): void {
this.process();
}
}