Possibility to create and edit typeless policies
[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 import { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog.component';
34
35 class PolicyTypeInfo {
36     constructor(public type: PolicyType) { }
37
38     isExpanded: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
39 }
40
41 @Component({
42     selector: 'rd-policy-control',
43     templateUrl: './policy-control.component.html',
44     styleUrls: ['./policy-control.component.scss'],
45     animations: [
46         trigger('detailExpand', [
47             state('collapsed, void', style({ height: '0px', minHeight: '0', display: 'none' })),
48             state('expanded', style({ height: '*' })),
49             transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
50             transition('expanded <=> void', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
51         ]),
52     ],
53 })
54 export class PolicyControlComponent implements OnInit {
55
56
57     policyTypesDataSource: PolicyTypeDataSource;
58     @ViewChild(MatSort, { static: true }) sort: MatSort;
59
60     policyTypeInfo = new Map<string, PolicyTypeInfo>();
61     darkMode: boolean;
62
63     constructor(
64         private policySvc: PolicyService,
65         private dialog: MatDialog,
66         private notificationService: NotificationService,
67         private ui: UiService) { }
68
69     ngOnInit() {
70         this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
71         this.policyTypesDataSource.loadTable();
72         this.ui.darkModeState.subscribe((isDark) => {
73             this.darkMode = isDark;
74         });
75     }
76
77     createPolicyInstance(policyType: PolicyType): void {
78         let dialogRef;
79         if (this.isSchemaEmpty(policyType)) {
80             dialogRef = this.dialog.open(NoTypePolicyInstanceDialogComponent,
81                 getPolicyDialogProperties(policyType, null, this.darkMode));
82         } else {
83             dialogRef = this.dialog.open(PolicyInstanceDialogComponent,
84                 getPolicyDialogProperties(policyType, null, this.darkMode));
85         }
86         const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
87         dialogRef.afterClosed().subscribe(
88             (_) => {
89                 info.isExpanded.next(info.isExpanded.getValue());
90             }
91         );
92     }
93
94     toggleListInstances(policyType: PolicyType): void {
95         const info = this.getPolicyTypeInfo(policyType);
96         info.isExpanded.next(!info.isExpanded.getValue());
97     }
98
99     private isSchemaEmpty(policyType: PolicyType): boolean {
100         return Object.keys(policyType.schemaObject).length === 0;
101     }
102
103     getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
104         let info: PolicyTypeInfo = this.policyTypeInfo.get(policyType.name);
105         if (!info) {
106             info = new PolicyTypeInfo(policyType);
107             this.policyTypeInfo.set(policyType.name, info);
108         }
109         return info;
110     }
111
112     getDisplayName(policyType: PolicyType): string {
113         if (policyType.schemaObject.title) {
114             return policyType.schemaObject.title;
115         }
116         return '< No type >';
117     }
118
119     isInstancesShown(policyType: PolicyType): boolean {
120         return this.getPolicyTypeInfo(policyType).isExpanded.getValue();
121     }
122
123     getExpandedObserver(policyType: PolicyType): Observable<boolean> {
124         return this.getPolicyTypeInfo(policyType).isExpanded.asObservable();
125     }
126 }