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 { Injectable } from '@angular/core';
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { of } from 'rxjs/observable/of';
25 import { Observable } from 'rxjs/Observable';
27 import { PolicyTypeSchema } from '../interfaces/policy.types';
28 import { PolicyService } from '../services/policy/policy.service';
29 import { NotificationService } from '../services/ui/notification.service';
35 export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
37 policyTypes: PolicyTypeSchema[] = [];
39 private policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
41 public rowCount = 1; // hide footer during intial load
43 constructor(private policySvc: PolicyService,
44 private notificationService: NotificationService) {
48 public getPolicyTypes() {
49 this.policyTypes = [] as PolicyTypeSchema[];
50 this.policySvc.getPolicyTypes().subscribe(policyType => {
51 if (policyType.policytype_ids.length != 0) {
52 policyType.policytype_ids.forEach(policyTypeId => {
53 var policyTypeSchema = {} as PolicyTypeSchema
54 if (policyTypeId === "") {
55 policyTypeSchema.id = '';
56 policyTypeSchema.name = '';
57 policyTypeSchema.schemaObject = '{}';
58 this.policyTypes.push(policyTypeSchema);
61 this.policySvc.getPolicyType(policyTypeId).subscribe(policyType => {
62 policyTypeSchema.id = policyTypeId;
63 policyTypeSchema.schemaObject = policyType.policy_schema;
64 policyTypeSchema.name = policyType.policy_schema.title;
65 this.policyTypes.push(policyTypeSchema);
68 this.policyTypeSubject.next(this.policyTypes);
74 connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
75 return of(this.policyTypeSubject.getValue());
78 disconnect(collectionViewer: CollectionViewer): void {
79 this.policyTypeSubject.complete();