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 { Component, OnInit, ViewChild } from '@angular/core';
21 import { MatDialog } from '@angular/material/dialog';
22 import { animate, state, style, transition, trigger } from '@angular/animations';
24 import { PolicyType } from '../interfaces/policy.types';
25 import { PolicyTypeDataSource } from './policy-type.datasource';
26 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
27 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
28 import { NotificationService } from '../services/ui/notification.service';
29 import { BehaviorSubject, Observable } from 'rxjs';
30 import { UiService } from '../services/ui/ui.service';
31 import { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog.component';
33 class PolicyTypeInfo {
34 constructor(public type: PolicyType) { }
36 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
40 selector: 'rd-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 private policyTypesDataSource: PolicyTypeDataSource,
59 private dialog: MatDialog,
60 private notificationService: NotificationService,
61 private ui: UiService) { }
64 this.policyTypesDataSource.loadTable();
65 this.ui.darkModeState.subscribe((isDark) => {
66 this.darkMode = isDark;
70 createPolicyInstance(policyType: PolicyType): void {
72 if (this.isSchemaEmpty(policyType)) {
73 dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
74 getPolicyDialogProperties(policyType, null, this.darkMode));
76 dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
77 getPolicyDialogProperties(policyType, null, this.darkMode));
79 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
80 dialogRef.afterClosed().subscribe(
82 info.isExpanded.next(info.isExpanded.getValue());
87 toggleListInstances(policyType: PolicyType): void {
88 const info = this.getPolicyTypeInfo(policyType);
89 info.isExpanded.next(!info.isExpanded.getValue());
92 private isSchemaEmpty(policyType: PolicyType): boolean {
93 return Object.keys(policyType.schemaObject).length === 0;
96 getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
97 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
99 info = new PolicyTypeInfo(policyType);
100 this.policyTypeInfo.set(policyType.name, info);
105 getDisplayName(policyType: PolicyType): string {
106 if (policyType.schemaObject.title) {
107 return policyType.schemaObject.title;
109 return '< No type >';
112 isInstancesShown(policyType: PolicyType): boolean {
113 return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
116 getExpandedObserver(policyType: PolicyType): Observable<boolean> {
117 return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();
121 this.policyTypesDataSource.loadTable();