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