28253c80512c0b85cd0453e7a874a90b1469a10b
[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 { PolicyTypeSchema } from '../interfaces/policy.types';
27 import { PolicyTypeDataSource } from './policy-type/policy-type.datasource';
28 import { getPolicyDialogProperties } from './policy-instance-dialog/policy-instance-dialog.component';
29 import { PolicyInstanceDialogComponent } from './policy-instance-dialog/policy-instance-dialog.component';
30 import { UiService } from '../services/ui/ui.service';
31
32 class PolicyTypeInfo {
33     constructor(public type: PolicyTypeSchema) { }
34
35     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
36 }
37
38 @Component({
39     selector: 'nrcp-policy-control',
40     templateUrl: './policy-control.component.html',
41     styleUrls: ['./policy-control.component.scss'],
42     animations: [
43         trigger('detailExpand', [
44             state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
45             state('expanded', style({ height: '*' })),
46             transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
47             transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
48         ]),
49     ],
50 })
51 export class PolicyControlComponent implements OnInit {
52
53     policyTypeInfo = new Map<string, PolicyTypeInfo>();
54     darkMode: boolean;
55
56     constructor(
57         public policyTypesDataSource: PolicyTypeDataSource,
58         private dialog: MatDialog,
59         private ui: UiService) { }
60
61     ngOnInit() {
62         this.policyTypesDataSource.getPolicyTypes();
63         this.ui.darkModeState.subscribe((isDark) => {
64             this.darkMode = isDark;
65         });
66     }
67
68     createPolicyInstance(policyTypeSchema: PolicyTypeSchema): void {
69         let dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
70             getPolicyDialogProperties(policyTypeSchema, null, this.darkMode));
71         const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyTypeSchema);
72         dialogRef.afterClosed().subscribe(
73             (_) => {
74                 info.isExpanded.next(info.isExpanded.getValue());
75             }
76         );
77     }
78
79     toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
80         const info = this.getPolicyTypeInfo(policyTypeSchema);
81         info.isExpanded.next(!info.isExpanded.getValue());
82     }
83
84     getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
85         let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
86         if (!info) {
87             info = new PolicyTypeInfo(policyTypeSchema);
88             this.policyTypeInfo.set(policyTypeSchema.name, info);
89         }
90         return info;
91     }
92
93     getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
94         if (policyTypeSchema.schemaObject.title) {
95             return policyTypeSchema.schemaObject.title;
96         }
97         return '< No type >';
98     }
99
100     isInstancesShown(policyTypeSchema: PolicyTypeSchema): boolean {
101         return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.getValue();
102     }
103
104     getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
105         return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
106     }
107
108     refreshTables() {
109         this.policyTypesDataSource.getPolicyTypes();
110     }
111 }