Skip to content

Commit da72692

Browse files
committed
Add the ability to sort the tables
1 parent 9803e04 commit da72692

9 files changed

+148
-44
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
2+
3+
4+
export class BaseListComponent {
5+
orderBy: string;
6+
orderDirection: string;
7+
currentPage: number;
8+
loading: boolean = false;
9+
exhausted: boolean = false;
10+
11+
processRouteParams(params: ParamMap) {
12+
// TODO: Add validation here
13+
if (params.get('orderBy')) {
14+
this.orderBy = params.get('orderBy');
15+
} else {
16+
this.orderBy = 'id';
17+
}
18+
if (params.get('orderDirection')) {
19+
this.orderDirection = params.get('orderDirection');
20+
} else {
21+
this.orderDirection = 'desc';
22+
}
23+
}
24+
25+
getOrderDirection(header): string {
26+
if (header.toLowerCase().replace(' ', '_') == this.orderBy && this.orderDirection == 'desc') {
27+
return 'asc';
28+
}
29+
return 'desc';
30+
}
31+
32+
getArrowClass(header: string): string {
33+
if (header.toLowerCase().replace(' ', '_') == this.orderBy) {
34+
if (this.orderDirection == 'asc') {
35+
return 'orderAsc';
36+
} else {
37+
return 'orderDesc';
38+
}
39+
}
40+
return '';
41+
}
42+
}

src/app/mbs/module-components/module-components.component.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
<h2 class="title">Components</h2>
44

5-
<table *ngIf="components.length || exhausted" class="table table-responsive-sm table-hover table-bordered" infinite-scroll (scrolled)="onScrollDown()">
5+
<table class="table table-responsive-sm table-hover table-bordered mbs-list-table" infinite-scroll (scrolled)="onScrollDown()">
66
<thead>
7-
<td scope="col">Module Build</td>
8-
<td scope="col">Package</td>
9-
<td scope="col">Task ID</td>
10-
<td scope="col">State</td>
7+
<th *ngFor="let header of ['ID', 'Module Build', 'Package', 'Task ID', 'State']" scope="col">
8+
<a *ngIf="header != 'Module Build'" [routerLink]="['/components', {'orderBy': header.toLowerCase().replace(' ', '_'), 'orderDirection': getOrderDirection(header)}]"
9+
[ngClass]="getArrowClass(header)">
10+
{{ header }}
11+
</a>
12+
<span *ngIf="header == 'Module Build'">{{ header }}</span>
13+
</th>
1114
</thead>
1215
<tbody>
1316
<tr *ngFor="let component of components">
17+
<td scope="row">{{ component.id }}</td>
1418
<td scope="row"><a [routerLink]="['/module', component.module_build]">{{ component.module_build }}</a></td>
1519
<td scope="row">{{ component.package }}</td>
1620
<td scope="row">

src/app/mbs/module-components/module-components.component.scss

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
table thead td {
2-
width: 20%;
1+
table thead {
2+
th {
3+
width: 15%;
4+
white-space: nowrap;
5+
}
6+
th:nth-child(3) {
7+
width: 25%;
8+
}
39
}
4-
5-
table thead td:nth-child(2) {
6-
width: 40%;
7-
}

src/app/mbs/module-components/module-components.component.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
23
import { NgProgress } from 'ngx-progressbar';
34

5+
import { BaseListComponent } from 'mbs/base-components/base-list.component';
46
import { ModuleService } from 'mbs/services/module.service';
57
import { MbsComponent } from 'mbs/types/mbs.type';
68

@@ -10,25 +12,30 @@ import { MbsComponent } from 'mbs/types/mbs.type';
1012
templateUrl: './module-components.component.html',
1113
styleUrls: ['./module-components.component.scss']
1214
})
13-
export class ModuleComponentsComponent implements OnInit {
15+
export class ModuleComponentsComponent extends BaseListComponent implements OnInit {
1416

1517
readonly koji_url: string = 'https://koji.fedoraproject.org/koji/';
1618
components: Array<MbsComponent> = [];
17-
currentPage: number = 1;
18-
loading: boolean = false;
19-
exhausted: boolean = false;
2019

21-
constructor(public ngProgress: NgProgress,
22-
private moduleService: ModuleService) { }
20+
constructor(private router: Router,
21+
private route: ActivatedRoute,
22+
public ngProgress: NgProgress,
23+
private moduleService: ModuleService) {super();}
2324

2425
ngOnInit() {
25-
this.getComponents();
26+
this.route.paramMap.subscribe((params: ParamMap) => {
27+
this.processRouteParams(params);
28+
this.components = [];
29+
this.exhausted = false;
30+
this.currentPage = 1;
31+
this.getComponents();
32+
})
2633
}
2734

2835
getComponents(): void {
2936
if (!this.exhausted && !this.loading) {
3037
this.loading = true;
31-
this.moduleService.getComponents(this.currentPage).subscribe(
38+
this.moduleService.getComponents(this.currentPage, this.orderBy, this.orderDirection).subscribe(
3239
data => {
3340
if (data.items.length) {
3441
this.components = this.components.concat(data.items);

src/app/mbs/modules/modules.component.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
<h2 class="title">Modules</h2>
44

5-
<table *ngIf="modules.length || exhausted" class="table table-responsive-sm table-hover table-bordered" infinite-scroll (scrolled)="onScrollDown()">
5+
<table class="table table-responsive-sm table-hover table-bordered mbs-list-table" infinite-scroll (scrolled)="onScrollDown()">
66
<thead>
7-
<td scope="col">ID</td>
8-
<td scope="col">Name</td>
9-
<td scope="col">Stream</td>
10-
<td scope="col">Version</td>
11-
<td scope="col">State</td>
7+
<th *ngFor="let header of ['ID', 'Name', 'Stream', 'Version', 'State']" scope="col">
8+
<a [routerLink]="['/modules', {'orderBy': header.toLowerCase(), 'orderDirection': getOrderDirection(header)}]"
9+
[ngClass]="getArrowClass(header)">
10+
{{ header }}
11+
</a>
12+
</th>
1213
</thead>
1314
<tbody>
1415
<tr *ngFor="let module of modules">
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
table thead {
2+
th {
3+
width: 20%;
4+
white-space: nowrap;
5+
}
6+
th:nth-child(2) {
7+
width:40%;
8+
}
9+
}

src/app/mbs/modules/modules.component.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
23
import { NgProgress } from 'ngx-progressbar';
34

5+
import { BaseListComponent } from 'mbs/base-components/base-list.component';
46
import { ModuleService } from 'mbs/services/module.service';
57
import { MbsModuleShort } from 'mbs/types/mbs.type';
68

@@ -10,24 +12,29 @@ import { MbsModuleShort } from 'mbs/types/mbs.type';
1012
templateUrl: './modules.component.html',
1113
styleUrls: ['./modules.component.scss'],
1214
})
13-
export class ModulesComponent implements OnInit {
15+
export class ModulesComponent extends BaseListComponent implements OnInit {
1416

15-
modules: Array<MbsModuleShort> = [];
16-
currentPage: number = 1;
17-
loading: boolean = false;
18-
exhausted: boolean = false;
17+
modules: Array<MbsModuleShort>;
1918

20-
constructor(public ngProgress: NgProgress,
21-
private moduleService: ModuleService) { }
19+
constructor(private router: Router,
20+
private route: ActivatedRoute,
21+
public ngProgress: NgProgress,
22+
private moduleService: ModuleService) {super();}
2223

2324
ngOnInit() {
24-
this.getModules();
25+
this.route.paramMap.subscribe((params: ParamMap) => {
26+
this.processRouteParams(params);
27+
this.modules = [];
28+
this.exhausted = false;
29+
this.currentPage = 1;
30+
this.getModules();
31+
})
2532
}
2633

2734
getModules(): void {
2835
if (!this.exhausted && !this.loading) {
2936
this.loading = true;
30-
this.moduleService.getModules(this.currentPage).subscribe(
37+
this.moduleService.getModules(this.currentPage, this.orderBy, this.orderDirection).subscribe(
3138
data => {
3239
if (data.items.length) {
3340
this.modules = this.modules.concat(data.items);

src/app/mbs/services/module.service.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,31 @@ export class ModuleService {
1111
private readonly mbsUrl: string = 'https://mbs.fedoraproject.org/module-build-service/1/'
1212
constructor(private http: HttpClient) { }
1313

14-
getModules (page: number = 1): Observable<MbsModulesShortApi> {
15-
return this.http.get<MbsModulesShortApi>(this.mbsUrl + 'module-builds/?short=true&per_page=40&page=' + page);
14+
private getOrderKey(orderDirection: string): string {
15+
if (orderDirection == 'desc') {
16+
return 'order_desc_by';
17+
} else {
18+
return 'order_by';
19+
}
1620
}
1721

18-
getModule (id: number): Observable<MbsModule> {
22+
getModules(page: number = 1, orderBy: string = 'id', orderDirection: string = 'desc'): Observable<MbsModulesShortApi> {
23+
let orderKey = this.getOrderKey(orderDirection);
24+
let url = this.mbsUrl + 'module-builds/?short=true&per_page=40&page=' + page + '&' + orderKey + '=' + orderBy;
25+
return this.http.get<MbsModulesShortApi>(url);
26+
}
27+
28+
getModule(id: number): Observable<MbsModule> {
1929
return this.http.get<MbsModule>(this.mbsUrl + 'module-builds/' + id);
2030
}
2131

22-
getComponents (page: number = 1): Observable<MbsComponentsApi> {
23-
return this.http.get<MbsComponentsApi>(this.mbsUrl + 'component-builds/?per_page=40&page=' + page);
32+
getComponents(page: number = 1, orderBy: string = 'id', orderDirection: string = 'desc'): Observable<MbsComponentsApi> {
33+
let orderKey = this.getOrderKey(orderDirection);
34+
let url = this.mbsUrl + 'component-builds/?per_page=40&page=' + page + '&' + orderKey + '=' + orderBy;
35+
return this.http.get<MbsComponentsApi>(url);
2436
}
2537

26-
getComponent (id: number): Observable<MbsComponent> {
38+
getComponent(id: number): Observable<MbsComponent> {
2739
return this.http.get<MbsComponent>(this.mbsUrl + 'component-builds/' + id);
2840
}
2941
}

src/styles.scss

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,30 @@ $fa-font-path: "../node_modules/font-awesome/fonts";
1313
}
1414
}
1515

16-
table {
17-
thead {
18-
td {
19-
font-weight: bold;
16+
table.mbs-list-table thead {
17+
th {
18+
a {
19+
text-decoration: none;
20+
color: #212529;
21+
display:inline-block;
22+
}
23+
24+
a:hover {
25+
text-decoration: underline;
26+
}
27+
28+
a.orderAsc:after {
29+
font-family: FontAwesome;
30+
content: '\f077';
31+
font-weight: lighter;
32+
display:inline-block;
33+
}
34+
35+
a.orderDesc:after {
36+
font-family: FontAwesome;
37+
content: '\f078';
38+
font-weight: lighter;
39+
display:inline-block;
2040
}
2141
}
2242
}

0 commit comments

Comments
 (0)