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===================================
20 import { animate, state, style, transition, trigger } from '@angular/animations';
21 import { Component, OnInit } from '@angular/core';
22 import { MatDialog } from '@angular/material/dialog';
24 import { BehaviorSubject, Observable } from 'rxjs';
26 import { PolicyTypes, PolicyTypeSchema } from '@interfaces/policy.types';
27 import { PolicyTypeDataSource } from './policy-type/policy-type.datasource';
28 import { getPolicyDialogProperties } from './policy-instance-dialog/policy-instance-dialog.component';
29 import { PolicyInstanceDialogComponent } from './policy-instance-dialog/policy-instance-dialog.component';
30 import { UiService } from '@services/ui/ui.service';
31 import { PolicyService } from '@services/policy/policy.service';
32 import { PolicyTypeComponent } from './policy-type/policy-type.component';
34 class PolicyTypeInfo {
35 constructor(public type: PolicyTypeSchema) { }
37 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
41 selector: 'nrcp-policy-control',
42 templateUrl: './policy-control.component.html',
43 styleUrls: ['./policy-control.component.scss'],
45 trigger('detailExpand', [
46 state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
47 state('expanded', style({ height: '*' })),
48 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
49 transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
53 export class PolicyControlComponent implements OnInit {
55 policyTypeInfo = new Map<string, PolicyTypeInfo>();
56 policyTypeIds: Array<string>;
57 policyTypeComponent = new PolicyTypeComponent(this.policyTypesDataSource);
61 public policyTypesDataSource: PolicyTypeDataSource,
62 private dialog: MatDialog,
63 private policyService: PolicyService,
64 private ui: UiService) { }
67 this.policyTypesDataSource.getPolicyTypes();
68 this.ui.darkModeState.subscribe((isDark) => {
69 this.darkMode = isDark;
71 this.policyService.getPolicyTypes().subscribe((policyType: PolicyTypes) => {
72 this.policyTypeIds = policyType.policytype_ids;
77 toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
78 const info = this.getPolicyTypeInfo(policyTypeSchema);
79 info.isExpanded.next(!info.isExpanded.getValue());
82 getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
83 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
85 info = new PolicyTypeInfo(policyTypeSchema);
86 this.policyTypeInfo.set(policyTypeSchema.name, info);
91 getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
92 if (policyTypeSchema.schemaObject.title) {
93 return policyTypeSchema.schemaObject.title;
98 getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
99 return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
103 this.policyTypesDataSource.getPolicyTypes();
104 this.policyTypeComponent.setIsVisible(false);