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 { PolicyType, PolicyTypes, PolicyTypeSchema } from '../../interfaces/policy.types';
28 import { PolicyService } from '../../services/policy/policy.service';
34 export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
36 policyTypes: PolicyTypeSchema[] = [];
38 policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
40 public rowCount = 1; // hide footer during intial load
42 constructor(public policySvc: PolicyService) {
46 public getPolicyTypes() {
47 this.policyTypes = [] as PolicyTypeSchema[];
48 this.policySvc.getPolicyTypes()
49 .subscribe((policyType: PolicyTypes) => {
50 this.rowCount = policyType.policytype_ids.length;
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)
62 .subscribe((policyType: PolicyType) => {
63 policyTypeSchema.id = policyTypeId;
64 policyTypeSchema.schemaObject = policyType.policy_schema;
65 policyTypeSchema.name = policyType.policy_schema.title;
66 this.policyTypes.push(policyTypeSchema);
69 this.policyTypeSubject.next(this.policyTypes);
75 connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
76 return of(this.policyTypeSubject.getValue());
79 disconnect(collectionViewer: CollectionViewer): void {
80 this.policyTypeSubject.complete();