X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fcatalog%2Fcatalog.datasource.ts;h=f6ae32d486b46fd8e63b3a600d4a05f601aa0d10;hb=dd3e10c0442cb16c08e9a8ecee429576396accac;hp=6ee6e92f6303941d542a741cc60d40e8677ef905;hpb=4347fbc44c279856d5b83a1768638fe571daf4d5;p=portal%2Fric-dashboard.git diff --git a/webapp-frontend/src/app/catalog/catalog.datasource.ts b/webapp-frontend/src/app/catalog/catalog.datasource.ts index 6ee6e92f..f6ae32d4 100644 --- a/webapp-frontend/src/app/catalog/catalog.datasource.ts +++ b/webapp-frontend/src/app/catalog/catalog.datasource.ts @@ -19,12 +19,14 @@ */ import { CollectionViewer, DataSource } from '@angular/cdk/collections'; +import { MatSort } from '@angular/material'; +import { merge } from 'rxjs'; +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Observable } from 'rxjs/Observable'; -import { catchError, finalize } from 'rxjs/operators'; import { of } from 'rxjs/observable/of'; -import { BehaviorSubject } from 'rxjs/BehaviorSubject'; -import { XappMgrService } from '../services/xapp-mgr/xapp-mgr.service'; +import { catchError, finalize, map } from 'rxjs/operators'; import { XMXapp } from '../interfaces/xapp-mgr.types'; +import { XappMgrService } from '../services/xapp-mgr/xapp-mgr.service'; export class CatalogDataSource extends DataSource { @@ -34,7 +36,7 @@ export class CatalogDataSource extends DataSource { public loading$ = this.loadingSubject.asObservable(); - constructor(private xappMgrSvc: XappMgrService) { + constructor(private xappMgrSvc: XappMgrService, private sort: MatSort ) { super(); }; @@ -49,11 +51,38 @@ export class CatalogDataSource extends DataSource { } connect(collectionViewer: CollectionViewer): Observable { - return this.xAppsSubject.asObservable(); + const dataMutations = [ + this.xAppsSubject.asObservable(), + this.sort.sortChange + ]; + return merge(...dataMutations).pipe(map(() => { + return this.getSortedData([...this.xAppsSubject.getValue()]); + })); } disconnect(collectionViewer: CollectionViewer): void { this.xAppsSubject.complete(); this.loadingSubject.complete(); } + + private getSortedData(data: XMXapp[]) { + if (!this.sort.active || this.sort.direction === '') { + return data; + } + + return data.sort((a, b) => { + const isAsc = this.sort.direction === 'asc'; + switch (this.sort.active) { + case 'name': return compare(a.name, b.name, isAsc); + case 'version': return compare(a.version, b.version, isAsc); + case 'status': return compare(a.status, b.status, isAsc); + default: return 0; + } + }); + } } + +function compare(a, b, isAsc) { + return (a < b ? -1 : 1) * (isAsc ? 1 : -1); +} +