mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ACS-7430] Make name optional in SortableByCategoryItem interface (#9735)
This commit is contained in:
@@ -18,86 +18,84 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
export interface SortableByCategoryItem {
|
export interface SortableByCategoryItem {
|
||||||
name: string;
|
name?: string;
|
||||||
category?: string;
|
category?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ItemsByCategory<T> {
|
export interface ItemsByCategory<T> {
|
||||||
category: string;
|
category: string;
|
||||||
items: T[];
|
items: T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class SortByCategoryMapperService<T extends SortableByCategoryItem = SortableByCategoryItem> {
|
export class SortByCategoryMapperService<T extends SortableByCategoryItem = SortableByCategoryItem> {
|
||||||
|
private defaultCategories: string[] = [];
|
||||||
|
|
||||||
private defaultCategories: string[] = [];
|
mapItems(items: T[], defaultCategories: string[]): ItemsByCategory<T>[] {
|
||||||
|
this.defaultCategories = defaultCategories;
|
||||||
|
|
||||||
mapItems(items: T[], defaultCategories: string[]): ItemsByCategory<T>[] {
|
const sortedItems = this.sortItems(items);
|
||||||
this.defaultCategories = defaultCategories;
|
const itemsByCategory = this.mapItemsByCategory(sortedItems);
|
||||||
|
const itemsSortedByCategory = this.sortCategories(itemsByCategory);
|
||||||
|
|
||||||
const sortedItems = this.sortItems(items);
|
return itemsSortedByCategory;
|
||||||
const itemsByCategory = this.mapItemsByCategory(sortedItems);
|
|
||||||
const itemsSortedByCategory = this.sortCategories(itemsByCategory);
|
|
||||||
|
|
||||||
return itemsSortedByCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
private mapItemsByCategory(items: T[]): ItemsByCategory<T>[] {
|
|
||||||
const itemsByCategoryObject: { [category: string]: T[] } = {};
|
|
||||||
|
|
||||||
items.forEach((item) => {
|
|
||||||
const category = this.mapItemDefaultCategory(item);
|
|
||||||
if (!itemsByCategoryObject[category]) {
|
|
||||||
itemsByCategoryObject[category] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
itemsByCategoryObject[category].push(item);
|
|
||||||
});
|
|
||||||
|
|
||||||
const itemsByCategory: ItemsByCategory<T>[] = Object.keys(itemsByCategoryObject).map((key) => {
|
|
||||||
const category = key;
|
|
||||||
return { category, items: itemsByCategoryObject[category] };
|
|
||||||
});
|
|
||||||
|
|
||||||
return itemsByCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
private sortItems(items: T[]): T[] {
|
|
||||||
return items.sort((itemA, itemB) => itemA.name.localeCompare(itemB.name));
|
|
||||||
}
|
|
||||||
|
|
||||||
private sortCategories(itemsByCategory: ItemsByCategory<T>[]): ItemsByCategory<T>[] {
|
|
||||||
return itemsByCategory.sort((itemA, itemB) => {
|
|
||||||
if (itemB.category === '' && itemA.category === '') {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (itemA.category === '') {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (itemB.category === '') {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemA.category.localeCompare(itemB.category);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private mapItemDefaultCategory(listItem: SortableByCategoryItem): string {
|
|
||||||
const itemCategory = listItem.category;
|
|
||||||
|
|
||||||
if (!this.isDefaultCategory(itemCategory)) {
|
|
||||||
return (itemCategory ?? '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
private mapItemsByCategory(items: T[]): ItemsByCategory<T>[] {
|
||||||
}
|
const itemsByCategoryObject: { [category: string]: T[] } = {};
|
||||||
|
|
||||||
private isDefaultCategory(category?: string): boolean {
|
items.forEach((item) => {
|
||||||
return category ? this.defaultCategories.includes(category) : false;
|
const category = this.mapItemDefaultCategory(item);
|
||||||
}
|
if (!itemsByCategoryObject[category]) {
|
||||||
|
itemsByCategoryObject[category] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
itemsByCategoryObject[category].push(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
const itemsByCategory: ItemsByCategory<T>[] = Object.keys(itemsByCategoryObject).map((key) => {
|
||||||
|
const category = key;
|
||||||
|
return { category, items: itemsByCategoryObject[category] };
|
||||||
|
});
|
||||||
|
|
||||||
|
return itemsByCategory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private sortItems(items: T[]): T[] {
|
||||||
|
return items.sort((itemA, itemB) => itemA.name.localeCompare(itemB.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
private sortCategories(itemsByCategory: ItemsByCategory<T>[]): ItemsByCategory<T>[] {
|
||||||
|
return itemsByCategory.sort((itemA, itemB) => {
|
||||||
|
if (itemB.category === '' && itemA.category === '') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (itemA.category === '') {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (itemB.category === '') {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemA.category.localeCompare(itemB.category);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private mapItemDefaultCategory(listItem: SortableByCategoryItem): string {
|
||||||
|
const itemCategory = listItem.category;
|
||||||
|
|
||||||
|
if (!this.isDefaultCategory(itemCategory)) {
|
||||||
|
return itemCategory ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
private isDefaultCategory(category?: string): boolean {
|
||||||
|
return category ? this.defaultCategories.includes(category) : false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user