01c38b6d8c9697b7c3085ed38786e28cb9b3b8f3
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / 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 { animate, state, style, transition, trigger } from '@angular/animations';
21 import { Component, OnInit } from '@angular/core';
22 import { MatDialog } from '@angular/material/dialog';
23
24 import { BehaviorSubject, Observable } from 'rxjs';
25
26 import { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog/no-type-policy-instance-dialog.component';
27 import { PolicyTypeSchema } from '../interfaces/policy.types';
28 import { PolicyTypeDataSource } from './policy-type/policy-type.datasource';
29 import { getPolicyDialogProperties } from './policy-instance-dialog/policy-instance-dialog.component';
30 import { PolicyInstanceDialogComponent } from './policy-instance-dialog/policy-instance-dialog.component';
31 import { UiService } from '../services/ui/ui.service';
32
33 class PolicyTypeInfo {
34     constructor(public type: PolicyTypeSchema) { }
35
36     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
37 }
38
39 @Component({
40     selector: 'nrcp-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         public policyTypesDataSource: PolicyTypeDataSource,
59         private dialog: MatDialog,
60         private ui: UiService) { }
61
62     ngOnInit() {
63         this.policyTypesDataSource.getPolicyTypes();
64         this.ui.darkModeState.subscribe((isDark) => {
65             this.darkMode = isDark;
66         });
67     }
68
69     createPolicyInstance(policyTypeSchema: PolicyTypeSchema): void {
70         let dialogRef;
71         if (this.isSchemaEmpty(policyTypeSchema)) {
72             dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
73                 getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
74         } else {
75             dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
76                 getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
77         }
78         const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyTypeSchema);
79         dialogRef.afterClosed().subscribe(
80             (_) => {
81                 info.isExpanded.next(info.isExpanded.getValue());
82             }
83         );
84     }
85
86     toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
87         const info = this.getPolicyTypeInfo(policyTypeSchema);
88         info.isExpanded.next(!info.isExpanded.getValue());
89     }
90
91     private isSchemaEmpty(policyTypeSchema: PolicyTypeSchema): boolean {
92         return policyTypeSchema.schemaObject === '{}';
93     }
94
95     getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
96         let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
97         if (!info) {
98             info = new PolicyTypeInfo(policyTypeSchema);
99             this.policyTypeInfo.set(policyTypeSchema.name, info);
100         }
101         return info;
102     }
103
104     getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
105         if (policyTypeSchema.schemaObject.title) {
106             return policyTypeSchema.schemaObject.title;
107         }
108         return '< No type >';
109     }
110
111     isInstancesShown(policyTypeSchema: PolicyTypeSchema): boolean {
112         return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.getValue();
113     }
114
115     getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
116         return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
117     }
118
119     refreshTables() {
120         this.policyTypesDataSource.getPolicyTypes();
121     }
122 }