f1629e0cc8056a2fef6fc1ff9d92de78475327d4
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-control.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
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';
23
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';
32
33 class PolicyTypeInfo {
34     constructor(public type: PolicyType) { }
35
36     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
37 }
38
39 @Component({
40     selector: 'rd-policy-control',
41     templateUrl: './policy-control.component.html',
42     styleUrls: ['./policy-control.component.scss'],
43     animations: [
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)'))
49         ]),
50     ],
51 })
52 export class PolicyControlComponent implements OnInit {
53
54     policyTypeInfo = new Map<string, PolicyTypeInfo>();
55     darkMode: boolean;
56
57     constructor(
58         private policyTypesDataSource: PolicyTypeDataSource,
59         private dialog: MatDialog,
60         private notificationService: NotificationService,
61         private ui: UiService) { }
62
63     ngOnInit() {
64         this.policyTypesDataSource.loadTable();
65         this.ui.darkModeState.subscribe((isDark) => {
66             this.darkMode = isDark;
67         });
68     }
69
70     createPolicyInstance(policyType: PolicyType): void {
71         let dialogRef;
72         if (this.isSchemaEmpty(policyType)) {
73             dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
74                 getPolicyDialogProperties(policyType, null, this.darkMode));
75         } else {
76             dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
77                 getPolicyDialogProperties(policyType, null, this.darkMode));
78         }
79         const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
80         dialogRef.afterClosed().subscribe(
81             (_) => {
82                 info.isExpanded.next(info.isExpanded.getValue());
83             }
84         );
85     }
86
87     toggleListInstances(policyType: PolicyType): void {
88         const info = this.getPolicyTypeInfo(policyType);
89         info.isExpanded.next(!info.isExpanded.getValue());
90     }
91
92     private isSchemaEmpty(policyType: PolicyType): boolean {
93         return Object.keys(policyType.schemaObject).length === 0;
94     }
95
96     getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
97         let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
98         if (!info) {
99             info = new PolicyTypeInfo(policyType);
100             this.policyTypeInfo.set(policyType.name, info);
101         }
102         return info;
103     }
104
105     getDisplayName(policyType: PolicyType): string {
106         if (policyType.schemaObject.title) {
107             return policyType.schemaObject.title;
108         }
109         return '< No type >';
110     }
111
112     isInstancesShown(policyType: PolicyType): boolean {
113         return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
114     }
115
116     getExpandedObserver(policyType: PolicyType): Observable<boolean> {
117         return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();
118     }
119
120     refreshTables() {
121         this.policyTypesDataSource.loadTable();
122     }
123 }