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(data => {
51 if (data.policytype_ids.length != 0) {
52 data.policytype_ids.forEach(policyId => {
53 var policyTypeSchema = {} as PolicyTypeSchema
54 if (policyId === "") {
55 policyTypeSchema.name = '';
56 policyTypeSchema.schemaObject = '{}';
57 this.policyTypes.push(policyTypeSchema);
60 this.policySvc.getPolicyType(policyId).subscribe(policyType => {
61 policyTypeSchema.schemaObject = policyType.policy_schema;
62 policyTypeSchema.name = policyType.policy_schema.title;
63 this.policyTypes.push(policyTypeSchema);
66 this.policyTypeSubject.next(this.policyTypes);
72 connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
73 return of(this.policyTypeSubject.getValue());
76 disconnect(collectionViewer: CollectionViewer): void {
77 this.policyTypeSubject.complete();