2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
29 selector: 'nrcp-jobs-list',
30 templateUrl: './jobs-list.component.html',
31 styleUrls: ['./jobs-list.component.scss']
33 export class JobsListComponent implements OnInit {
35 jobsDataSource: MatTableDataSource<EIJob> = new MatTableDataSource<EIJob>();
40 private eiJobsDataSource: EIJobDataSource,
43 this.jobForm = new FormGroup({
44 id: new FormControl(''),
45 typeId: new FormControl(''),
46 owner: new FormControl(''),
47 targetUri: new FormControl('')
55 this.jobForm.valueChanges.subscribe(value => {
56 const filter = { ...value, id: value.id.trim().toLowerCase() } as string;
57 this.jobsDataSource.filter = filter;
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;
67 this.ui.darkModeState.subscribe((isDark) => {
68 this.darkMode = isDark;
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);
84 this.jobsDataSource.data = data;
87 compare(a: any, b: any, isAsc: boolean) {
88 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
91 stopSort(event: any) {
92 event.stopPropagation();
95 isDataIncluding(data: string, filter: string): boolean {
96 return !filter || data.toLowerCase().includes(filter);
99 getJobTypeId(eiJob: EIJob): string {
100 if (eiJob.ei_type_identity) {
101 return eiJob.ei_type_identity;
103 return '< No type >';
106 getJobOwner(eiJob: EIJob): string {
110 return '< No owner >';
114 this.eiJobsDataSource.loadJobs();
116 this.eiJobsDataSource.eiJobsSubject().subscribe((data) => {
117 this.jobsDataSource.data = data;