import {Component, OnInit, Input} from '@angular/core'; import {Sort} from '@angular/material/sort'; import {I18nService} from './../../services/i18n.service'; @Component({ selector: 'app-permissions', templateUrl: './permissions.component.html', styleUrls: ['./permissions.component.scss'] }) export class PermissionsComponent implements OnInit { datetimeformat: String; @Input() permissions; permissionColumns = ["name", "starts", "expires"]; constructor(private i18n: I18nService) {} ngOnInit(): void { this.datetimeformat = this.i18n.get('format.datetime', []); this.sortData({ "active": "name", "direction": "asc" }); } sortData(sort: Sort) { const data = this.permissions.slice(); if(!sort.active || sort.direction === '') { this.permissions = data; return; } this.permissions = 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 'starts': return this.compare(a.starts, b.starts, isAsc); case 'expires': return this.compare(a.expires, b.expires, 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); } }