Files
we_bstly-web/src/app/ui/quotas/quotas.component.ts
T
2021-12-15 13:30:12 +01:00

51 lines
1.5 KiB
TypeScript

import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Sort } from '@angular/material/sort';
import { I18nService } from './../../services/i18n.service';
@Component({
selector: 'app-quotas',
templateUrl: './quotas.component.html',
styleUrls: [ './quotas.component.scss' ]
})
export class QuotasComponent implements OnInit, OnChanges {
@Input() quotas;
quotaColumns = [ "name", "value", "fixed_value", "quotaUnit" ];
constructor(private i18n: I18nService) { }
ngOnInit(): void {
this.sortData({ "active": "name", "direction": "asc" });
}
ngOnChanges(changes: SimpleChanges): void {
this.sortData({ "active": "name", "direction": "asc" });
}
sortData(sort: Sort) {
const data = this.quotas.slice();
if (!sort.active || sort.direction === '') {
this.quotas = data;
return;
}
this.quotas = data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'name': return this.compare(this.i18n.get('services.' + a.name + '.title', []), this.i18n.get('services.' + b.name + '.title', []), isAsc);
case 'value': return this.compare(a.value, b.value, isAsc);
default: return 0;
}
});
}
compare(a: number | string, b: number | string, isAsc: boolean) {
if (typeof a === 'string' && typeof b === 'string') {
return a.localeCompare(b, undefined, { sensitivity: 'accent' }) * (isAsc ? 1 : -1);
}
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
}