Add column sorting on ANR page
[portal/ric-dashboard.git] / webapp-frontend / src / app / anr-xapp / anr-xapp.datasource.ts
index bca766c..bc04b6b 100644 (file)
  * ========================LICENSE_END===================================
  */
 
-import { CollectionViewer, DataSource} from '@angular/cdk/collections';
+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 { catchError, finalize, map } from 'rxjs/operators';
 import { ANRNeighborCellRelation } from '../interfaces/anr-xapp.types';
 import { ANRXappService } from '../services/anr-xapp/anr-xapp.service';
 
@@ -35,7 +37,7 @@ export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
 
     public loading$ = this.loadingSubject.asObservable();
 
-    constructor(private anrXappService: ANRXappService) {
+  constructor(private anrXappService: ANRXappService, private sort: MatSort) {
         super();
     }
 
@@ -50,7 +52,13 @@ export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
     }
 
     connect(collectionViewer: CollectionViewer): Observable<ANRNeighborCellRelation[]> {
-        return this.relationsSubject.asObservable();
+      const dataMutations = [
+        this.relationsSubject.asObservable(),
+        this.sort.sortChange
+      ];
+      return merge(...dataMutations).pipe(map(() => {
+        return this.getSortedData([...this.relationsSubject.getValue()]);
+      }));
     }
 
     disconnect(collectionViewer: CollectionViewer): void {
@@ -58,4 +66,27 @@ export class ANRXappDataSource extends DataSource<ANRNeighborCellRelation> {
         this.loadingSubject.complete();
     }
 
+  private getSortedData(data: ANRNeighborCellRelation[]) {
+    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 'cellIdentifierNrcgi': return compare(a.servingCellNrcgi, b.servingCellNrcgi, isAsc);
+        case 'neighborCellNrpci': return compare(a.neighborCellNrpci, b.neighborCellNrpci, isAsc);
+        case 'neighborCellNrcgi': return compare(a.neighborCellNrcgi, b.neighborCellNrcgi, isAsc);
+        case 'flagNoHo': return compare(a.flagNoHo, b.flagNoHo, isAsc);
+        case 'flagNoXn': return compare(a.flagNoXn, b.flagNoXn, isAsc);
+        case 'flagNoRemove': return compare(a.flagNoRemove, b.flagNoRemove, isAsc);
+        default: return 0;
+      }
+    });
+  }
+
+}
+
+function compare(a, b, isAsc) {
+  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
 }