de3f57879109119c6e27e74a534c7626823b9939
[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 { PolicyTypes, 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 import { PolicyService } from '@services/policy/policy.service';
32 import { PolicyTypeComponent } from './policy-type/policy-type.component';
33
34 class PolicyTypeInfo {
35     constructor(public type: PolicyTypeSchema) { }
36
37     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
38 }
39
40 @Component({
41     selector: 'nrcp-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     policyTypeInfo = new Map<string, PolicyTypeInfo>();
56     policyTypeIds: Array<string>;
57     policyTypeComponent = new PolicyTypeComponent(this.policyTypesDataSource);
58     darkMode: boolean;
59
60     constructor(
61         public policyTypesDataSource: PolicyTypeDataSource,
62         private dialog: MatDialog,
63         private policyService: PolicyService,
64         private ui: UiService) { }
65
66     ngOnInit() {
67         this.policyTypesDataSource.getPolicyTypes();
68         this.ui.darkModeState.subscribe((isDark) => {
69             this.darkMode = isDark;
70         });
71         this.policyService.getPolicyTypes().subscribe((policyType: PolicyTypes) => {
72             this.policyTypeIds = policyType.policytype_ids;
73             }
74         );
75     }
76
77     toggleListInstances(policyTypeSchema: PolicyTypeSchema): void {
78         const info = this.getPolicyTypeInfo(policyTypeSchema);
79         info.isExpanded.next(!info.isExpanded.getValue());
80     }
81
82     getPolicyTypeInfo(policyTypeSchema: PolicyTypeSchema): PolicyTypeInfo {
83         let info: PolicyTypeInfo = this.policyTypeInfo.get(policyTypeSchema.name);
84         if (!info) {
85             info = new PolicyTypeInfo(policyTypeSchema);
86             this.policyTypeInfo.set(policyTypeSchema.name, info);
87         }
88         return info;
89     }
90
91     getDisplayName(policyTypeSchema: PolicyTypeSchema): string {
92         if (policyTypeSchema.schemaObject.title) {
93             return policyTypeSchema.schemaObject.title;
94         }
95         return '< No type >';
96     }
97
98     getExpandedObserver(policyTypeSchema: PolicyTypeSchema): Observable<boolean> {
99         return this.getPolicyTypeInfo(policyTypeSchema).isExpanded.asObservable();
100     }
101
102     refreshTables() {
103         this.policyTypesDataSource.getPolicyTypes();
104         this.policyTypeComponent.setIsVisible(false);
105     }
106 }