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 { MatSort } from '@angular/material/sort';
23 import { animate, state, style, transition, trigger } from '@angular/animations';
25 import { PolicyService } from '../services/policy/policy.service';
26 import { PolicyType } from '../interfaces/policy.types';
27 import { PolicyTypeDataSource } from './policy-type.datasource';
28 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
29 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
30 import { NotificationService } from '../services/ui/notification.service';
31 import { BehaviorSubject, Observable } from 'rxjs';
32 import { UiService } from '../services/ui/ui.service';
33 import { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog.component';
35 class PolicyTypeInfo {
36 constructor(public type: PolicyType) { }
38 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
42 selector: 'rd-policy-control',
43 templateUrl: './policy-control.component.html',
44 styleUrls: ['./policy-control.component.scss'],
46 trigger('detailExpand', [
47 state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
48 state('expanded', style({ height: '*' })),
49 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
50 transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
54 export class PolicyControlComponent implements OnInit {
57 policyTypesDataSource: PolicyTypeDataSource;
58 @ViewChild(MatSort, { static: true }) sort: MatSort;
60 policyTypeInfo = new Map<string, PolicyTypeInfo>();
64 private policySvc: PolicyService,
65 private dialog: MatDialog,
66 private notificationService: NotificationService,
67 private ui: UiService) { }
70 this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
71 this.policyTypesDataSource.loadTable();
72 this.ui.darkModeState.subscribe((isDark) => {
73 this.darkMode = isDark;
77 createPolicyInstance(policyType: PolicyType): void {
79 if (this.isSchemaEmpty(policyType)) {
80 dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
81 getPolicyDialogProperties(policyType, null, this.darkMode));
83 dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
84 getPolicyDialogProperties(policyType, null, this.darkMode));
86 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
87 dialogRef.afterClosed().subscribe(
89 info.isExpanded.next(info.isExpanded.getValue());
94 toggleListInstances(policyType: PolicyType): void {
95 const info = this.getPolicyTypeInfo(policyType);
96 info.isExpanded.next(!info.isExpanded.getValue());
99 private isSchemaEmpty(policyType: PolicyType): boolean {
100 return Object.keys(policyType.schemaObject).length === 0;
103 getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
104 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
106 info = new PolicyTypeInfo(policyType);
107 this.policyTypeInfo.set(policyType.name, info);
112 getDisplayName(policyType: PolicyType): string {
113 if (policyType.schemaObject.title) {
114 return policyType.schemaObject.title;
116 return '< No type >';
119 isInstancesShown(policyType: PolicyType): boolean {
120 return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
123 getExpandedObserver(policyType: PolicyType): Observable<boolean> {
124 return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();
128 this.policyTypesDataSource.loadTable();