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