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 { Subject } from 'rxjs';
38 class PolicyTypeInfo {
39 constructor(public type: PolicyType, public isExpanded: boolean) { }
41 isExpandedObservers: Subject<boolean> = new Subject<boolean>();
45 selector: 'rd-policy-control',
46 templateUrl: './policy-control.component.html',
47 styleUrls: ['./policy-control.component.scss'],
49 trigger('detailExpand', [
50 state('collapsed', style({ height: '0px', minHeight: '0', visibility: 'hidden' })),
51 state('expanded', style({ height: '*', visibility: 'visible' })),
52 transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
56 export class PolicyControlComponent implements OnInit {
58 policyTypesDataSource: PolicyTypeDataSource;
59 @ViewChild(MatSort, { static: true }) sort: MatSort;
61 expandedTypes = new Map<string, PolicyTypeInfo>();
64 private policySvc: PolicyService,
65 private dialog: MatDialog,
66 private errorDialogService: ErrorDialogService,
67 private notificationService: NotificationService,
68 private confirmDialogService: ConfirmDialogService) { }
71 this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
72 this.policyTypesDataSource.loadTable();
75 createPolicyInstance(policyType: PolicyType): void {
76 const dialogRef = this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(policyType, null));
77 const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
78 dialogRef.afterClosed().subscribe(
80 info.isExpandedObservers.next(info.isExpanded);
85 toggleListInstances(policyType: PolicyType): void {
86 let info = this.getPolicyTypeInfo(policyType);
87 info.isExpanded = !info.isExpanded;
88 info.isExpandedObservers.next(info.isExpanded);
91 getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
92 let info: PolicyTypeInfo = this.expandedTypes.get(policyType.name);
94 info = new PolicyTypeInfo(policyType, false);
95 this.expandedTypes.set(policyType.name, info);
100 isInstancesShown(policyType: PolicyType): boolean {
101 return this.getPolicyTypeInfo(policyType).isExpanded;
104 getPolicyTypeName(type: PolicyType): string {
105 const schema = JSON.parse(type.create_schema);
112 getObservable(policyType: PolicyType): Subject<boolean> {
113 return this.getPolicyTypeInfo(policyType).isExpandedObservers;