Update selected on displayed change

This commit is contained in:
Adina Țeudan 2021-10-13 20:09:10 +03:00
parent 019072dfd2
commit de6415965a

View File

@ -5,10 +5,10 @@ import { FilterService, getFilteredEntities } from '../../filtering';
import { SearchService } from '../../search';
import { IListable } from '../models';
import { EntitiesService } from './entities.service';
import { getLength } from '../../utils';
import { AutoUnsubscribe, getLength } from '../../utils';
@Injectable()
export class ListingService<E extends IListable> {
export class ListingService<E extends IListable> extends AutoUnsubscribe {
readonly displayed$: Observable<E[]>;
readonly displayedLength$: Observable<number>;
readonly areAllSelected$: Observable<boolean>;
@ -25,6 +25,8 @@ export class ListingService<E extends IListable> {
private readonly _searchService: SearchService<E>,
private readonly _entitiesService: EntitiesService<E>,
) {
super();
this.displayed$ = this._getDisplayed$.pipe(shareReplay());
this.displayedLength$ = this.displayed$.pipe(getLength, distinctUntilChanged(), shareReplay());
@ -38,6 +40,10 @@ export class ListingService<E extends IListable> {
this.areAllSelected$ = this._areAllSelected$;
this.areSomeSelected$ = this._areSomeSelected$;
this.notAllSelected$ = this._notAllSelected$;
this.addSubscription = this.displayed$.subscribe(() => {
this._updateSelection();
});
}
get selected(): E[] {
@ -116,7 +122,7 @@ export class ListingService<E extends IListable> {
this.setSelected(this.selected.filter((el, idx) => idx !== currentEntityIdx));
}
updateSelection(): void {
private _updateSelection(): void {
const items = this._displayed.filter(item => this.selected.includes(item));
this.setSelected(items);
}