2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { MatSort } from '@angular/material';
23 import { merge } from 'rxjs';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { Observable } from 'rxjs/Observable';
26 import { of } from 'rxjs/observable/of';
27 import { catchError, finalize, map } from 'rxjs/operators';
28 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
29 import { XMDeployableApp } from '../interfaces/app-mgr.types';
31 export class CatalogDataSource extends DataSource<XMDeployableApp> {
33 private xAppsSubject = new BehaviorSubject<XMDeployableApp[]>([]);
35 private loadingSubject = new BehaviorSubject<boolean>(false);
37 public loading$ = this.loadingSubject.asObservable();
39 constructor(private appMgrSvc: AppMgrService, private sort: MatSort) {
44 this.loadingSubject.next(true);
45 this.appMgrSvc.getDeployable()
47 catchError(() => of([])),
48 finalize(() => this.loadingSubject.next(false))
50 .subscribe(xApps => this.xAppsSubject.next(xApps));
53 connect(collectionViewer: CollectionViewer): Observable<XMDeployableApp[]> {
54 const dataMutations = [
55 this.xAppsSubject.asObservable(),
58 return merge(...dataMutations).pipe(map(() => {
59 return this.getSortedData([...this.xAppsSubject.getValue()]);
63 disconnect(collectionViewer: CollectionViewer): void {
64 this.xAppsSubject.complete();
65 this.loadingSubject.complete();
68 private getSortedData(data: XMDeployableApp[]) {
69 if (!this.sort.active || this.sort.direction === '') {
72 return data.sort((a: XMDeployableApp, b: XMDeployableApp) => {
73 const isAsc = this.sort.direction === 'asc';
74 switch (this.sort.active) {
75 case 'name': return this.compare(a.name, b.name, isAsc);
76 case 'version': return this.compare(a.version, b.version, isAsc);
82 private compare(a: string, b: string, isAsc: boolean) {
83 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);