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, MatDialogConfig } from '@angular/material/dialog';
22 import { MatSort } from '@angular/material/sort';
23 import { animate, state, style, transition, trigger } from '@angular/animations';
24 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
26 import { PolicyService } from '../services/policy/policy.service';
27 import { PolicyType } from '../interfaces/policy.types';
28 import { PolicyTypeDataSource } from './policy-type.datasource';
29 import { PolicyInstanceDataSource } from './policy-instance.datasource';
30 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
31 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
32 import { PolicyInstance } from '../interfaces/policy.types';
33 import { NotificationService } from '../services/ui/notification.service';
34 import { ErrorDialogService } from '../services/ui/error-dialog.service';
35 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
36 import { BehaviorSubject, Observable } from 'rxjs';
37 import { UiService } from '../services/ui/ui.service';
39 class PolicyTypeInfo {
40 constructor(public type: PolicyType) { }
42 isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
46 selector: 'rd-policy-control',
47 templateUrl: './policy-control.component.html',
48 styleUrls: ['./policy-control.component.scss'],
50 trigger('detailExpand', [
51 state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
52 state('expanded', style({ height: '*' })),
53 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
54 transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
58 export class PolicyControlComponent implements OnInit {
61 policyTypesDataSource: PolicyTypeDataSource;
62 @ViewChild(MatSort, { static: true }) sort: MatSort;
64 policyTypeInfo = new Map<string, PolicyTypeInfo>();
68 private policySvc: PolicyService,
69 private dialog: MatDialog,
70 private errorDialogService: ErrorDialogService,
71 private notificationService: NotificationService,
72 private confirmDialogService: ConfirmDialogService,
73 private ui: UiService) { }
76 this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
77 this.policyTypesDataSource.loadTable();
78 this.ui.darkModeState.subscribe((isDark) => {
79 this.darkMode = isDark;
83 createPolicyInstance(policyType: PolicyType): void {
84 const dialogRef = this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(policyType, null, this.darkMode));
85 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
86 dialogRef.afterClosed().subscribe(
88 info.isExpanded.next(info.isExpanded.getValue());
93 toggleListInstances(policyType: PolicyType): void {
94 console.log('1toggleListInstances ' + + policyType.name + ' ' + this.getPolicyTypeInfo(policyType).isExpanded.getValue());
95 const info = this.getPolicyTypeInfo(policyType);
96 info.isExpanded.next(!info.isExpanded.getValue());
97 console.log('2toggleListInstances ' + + policyType.name + ' ' + this.getPolicyTypeInfo(policyType).isExpanded.getValue());
101 getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
102 let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
104 info = new PolicyTypeInfo(policyType);
105 this.policyTypeInfo.set(policyType.name, info);
110 isInstancesShown(policyType: PolicyType): boolean {
111 return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
114 getExpandedObserver(policyType: PolicyType): Observable<boolean> {
115 return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();