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';
34 class PolicyTypeInfo {
35 constructor(public type: PolicyType) { }
37 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
41 selector: 'rd-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 {
56 policyTypesDataSource: PolicyTypeDataSource;
57 @ViewChild(MatSort, { static: true }) sort: MatSort;
59 policyTypeInfo = new Map<string, PolicyTypeInfo>();
63 private policySvc: PolicyService,
64 private dialog: MatDialog,
65 private notificationService: NotificationService,
66 private ui: UiService) { }
69 this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
70 this.policyTypesDataSource.loadTable();
71 this.ui.darkModeState.subscribe((isDark) => {
72 this.darkMode = isDark;
76 createPolicyInstance(policyType: PolicyType): void {
77 const dialogRef = this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(policyType, null, this.darkMode));
78 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
79 dialogRef.afterClosed().subscribe(
81 info.isExpanded.next(info.isExpanded.getValue());
86 toggleListInstances(policyType: PolicyType): void {
87 const info = this.getPolicyTypeInfo(policyType);
88 info.isExpanded.next(!info.isExpanded.getValue());
91 getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
92 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
94 info = new PolicyTypeInfo(policyType);
95 this.policyTypeInfo.set(policyType.name, info);
100 getName(policyType: PolicyType): string {
101 if (policyType.schemaObject.title) { return policyType.schemaObject.title; }
102 return policyType.name;
105 isInstancesShown(policyType: PolicyType): boolean {
106 return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
109 getExpandedObserver(policyType: PolicyType): Observable<boolean> {
110 return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();