2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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 { HttpErrorResponse } from '@angular/common/http';
23 import { Injectable } from '@angular/core';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { of } from 'rxjs/observable/of';
26 import { catchError, finalize, map } from 'rxjs/operators';
27 import { Observable } from 'rxjs/Observable';
29 import { PolicyType } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
37 export class PolicyTypeDataSource extends DataSource<PolicyType> {
39 private policyTypeSubject = new BehaviorSubject<PolicyType[]>([]);
41 private loadingSubject = new BehaviorSubject<boolean>(false);
43 public loading$ = this.loadingSubject.asObservable();
45 public rowCount = 1; // hide footer during intial load
47 constructor(private policySvc: PolicyService,
48 private notificationService: NotificationService) {
53 this.loadingSubject.next(true);
54 this.policySvc.getPolicyTypes()
56 catchError((her: HttpErrorResponse) => {
57 this.notificationService.error('Failed to get policy types: ' + her.statusText + ', ' + her.error);
60 finalize(() => this.loadingSubject.next(false))
62 .subscribe((types: PolicyType[]) => {
63 this.rowCount = types.length;
64 for (let i = 0; i < types.length; i++) {
65 const policyType = types[i];
67 policyType.schemaObject = JSON.parse(policyType.schema);
69 console.error('Could not parse schema: ' + policyType.schema);
70 policyType.schemaObject = { description: 'Incorrect schema: ' + jsonError };
73 this.policyTypeSubject.next(types);
77 connect(collectionViewer: CollectionViewer): Observable<PolicyType[]> {
78 return of(this.policyTypeSubject.getValue());
81 disconnect(collectionViewer: CollectionViewer): void {
82 this.policyTypeSubject.complete();
83 this.loadingSubject.complete();