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 { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog/no-type-policy-instance-dialog.component';
27 import { PolicyTypeSchema } from '../interfaces/policy.types';
28 import { PolicyTypeDataSource } from './policy-type/policy-type.datasource';
29 import { getPolicyDialogProperties } from './policy-instance-dialog/policy-instance-dialog.component';
30 import { PolicyInstanceDialogComponent } from './policy-instance-dialog/policy-instance-dialog.component';
31 import { UiService } from '../services/ui/ui.service';
33 class PolicyTypeInfo {
34 constructor(public type: PolicyTypeSchema) { }
36 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
40 selector: 'nrcp-policy-control',
41 templateUrl: './policy-control.component.html',
42 styleUrls: ['./policy-control.component.scss'],
44 trigger('detailExpand', [
45 state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
46 state('expanded', style({ height: '*' })),
47 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
48 transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
52 export class PolicyControlComponent implements OnInit {
54 policyTypeInfo = new Map<string, PolicyTypeInfo>();
58 public policyTypesDataSource: PolicyTypeDataSource,
59 private dialog: MatDialog,
60 private ui: UiService) { }
63 this.policyTypesDataSource.getPolicyTypes();
64 this.ui.darkModeState.subscribe((isDark) => {
65 this.darkMode = isDark;
69 createPolicyInstance(policyTypeSchema: PolicyTypeSchema): void {
71 if (this.isSchemaEmpty(policyTypeSchema)) {
72 dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
73 getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
75 dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
76 getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
78 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyTypeSchema);
79 dialogRef.afterClosed().subscribe(
81 info.isExpanded.next(info.isExpanded.getValue());
86 toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
87 const info = this.getPolicyTypeInfo(policyTypeSchema);
88 info.isExpanded.next(!info.isExpanded.getValue());
91 private isSchemaEmpty(policyTypeSchema: PolicyTypeSchema): boolean {
92 return policyTypeSchema.schemaObject === '{}';
95 getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
96 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
98 info = new PolicyTypeInfo(policyTypeSchema);
99 this.policyTypeInfo.set(policyTypeSchema.name, info);
104 getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
105 if (policyTypeSchema.schemaObject.title) {
106 return policyTypeSchema.schemaObject.title;
108 return '< No type >';
111 isInstancesShown(policyTypeSchema: PolicyTypeSchema): boolean {
112 return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.getValue();
115 getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
116 return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
120 this.policyTypesDataSource.getPolicyTypes();