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 { 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';
32 class PolicyTypeInfo {
33 constructor(public type: PolicyTypeSchema) { }
35 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
39 selector: 'nrcp-policy-control',
40 templateUrl: './policy-control.component.html',
41 styleUrls: ['./policy-control.component.scss'],
43 trigger('detailExpand', [
44 state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
45 state('expanded', style({ height: '*' })),
46 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
47 transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
51 export class PolicyControlComponent implements OnInit {
53 policyTypeInfo = new Map<string, PolicyTypeInfo>();
57 public policyTypesDataSource: PolicyTypeDataSource,
58 private dialog: MatDialog,
59 private ui: UiService) { }
62 this.policyTypesDataSource.getPolicyTypes();
63 this.ui.darkModeState.subscribe((isDark) => {
64 this.darkMode = isDark;
68 createPolicyInstance(policyTypeSchema: PolicyTypeSchema): void {
69 let dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
70 getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
71 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyTypeSchema);
72 dialogRef.afterClosed().subscribe(
74 info.isExpanded.next(info.isExpanded.getValue());
79 toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
80 const info = this.getPolicyTypeInfo(policyTypeSchema);
81 info.isExpanded.next(!info.isExpanded.getValue());
84 getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
85 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
87 info = new PolicyTypeInfo(policyTypeSchema);
88 this.policyTypeInfo.set(policyTypeSchema.name, info);
93 getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
94 if (policyTypeSchema.schemaObject.title) {
95 return policyTypeSchema.schemaObject.title;
100 isInstancesShown(policyTypeSchema: PolicyTypeSchema): boolean {
101 return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.getValue();
104 getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
105 return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
109 this.policyTypesDataSource.getPolicyTypes();