Merge "First version of policy editor components"
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / jobs-list / jobs-list.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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 } from '@angular/core';
21 import { FormControl, FormGroup } from '@angular/forms';
22 import { Sort } from '@angular/material/sort';
23 import { MatTableDataSource } from '@angular/material/table';
24 import { EIJob } from 'src/app/interfaces/ei.types';
25 import { UiService } from 'src/app/services/ui/ui.service';
26 import { EIJobDataSource } from '../ei-job.datasource';
27
28 @Component({
29   selector: 'nrcp-jobs-list',
30   templateUrl: './jobs-list.component.html',
31   styleUrls: ['./jobs-list.component.scss']
32 })
33 export class JobsListComponent implements OnInit {
34   darkMode: boolean;
35   jobsDataSource: MatTableDataSource<EIJob> = new MatTableDataSource<EIJob>();
36
37   jobForm: FormGroup;
38
39   constructor(
40     private eiJobsDataSource: EIJobDataSource,
41     private ui: UiService
42   ) {
43     this.jobForm = new FormGroup({
44       id: new FormControl(''),
45       typeId: new FormControl(''),
46       owner: new FormControl(''),
47       targetUri: new FormControl('')
48     });
49
50   }
51
52   ngOnInit(): void {
53     this.refresh();
54
55     this.jobForm.valueChanges.subscribe(value => {
56       const filter = { ...value, id: value.id.trim().toLowerCase() } as string;
57       this.jobsDataSource.filter = filter;
58     });
59
60     this.jobsDataSource.filterPredicate = ((data: EIJob, filter) => {
61       return this.isDataIncluding(data.ei_job_identity, filter.id)
62         && this.isDataIncluding(data.target_uri, filter.targetUri)
63         && this.isDataIncluding(data.owner, filter.owner)
64         && this.isDataIncluding(data.ei_type_identity, filter.typeId);
65     }) as (data: EIJob, filter: any) => boolean;
66
67     this.ui.darkModeState.subscribe((isDark) => {
68       this.darkMode = isDark;
69     });
70   }
71
72   sortJobs(sort: Sort) {
73     const data = this.jobsDataSource.data
74     data.sort((a: EIJob, b: EIJob) => {
75       const isAsc = sort.direction === 'asc';
76       switch (sort.active) {
77         case 'id': return this.compare(a.ei_job_identity, b.ei_job_identity, isAsc);
78         case 'typeId': return this.compare(a.ei_type_identity, b.ei_type_identity, isAsc);
79         case 'owner': return this.compare(a.owner, b.owner, isAsc);
80         case 'targetUri': return this.compare(a.target_uri, b.owner, isAsc);
81         default: return 0;
82       }
83     });
84     this.jobsDataSource.data = data;
85   }
86
87   compare(a: any, b: any, isAsc: boolean) {
88     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
89   }
90
91   stopSort(event: any) {
92     event.stopPropagation();
93   }
94
95   isDataIncluding(data: string, filter: string): boolean {
96     return !filter || data.toLowerCase().includes(filter);
97   }
98
99   getJobTypeId(eiJob: EIJob): string {
100     if (eiJob.ei_type_identity) {
101       return eiJob.ei_type_identity;
102     }
103     return '< No type >';
104   }
105
106   getJobOwner(eiJob: EIJob): string {
107     if (eiJob.owner) {
108       return eiJob.owner;
109     }
110     return '< No owner >';
111   }
112
113   refresh() {
114     this.eiJobsDataSource.loadJobs();
115
116     this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
117       this.jobsDataSource.data = data;
118     });
119   }
120
121 }