import type {
CustomText,
HeadData,
CellValue,
Badge,
Image,
BodyData,
SortDirection,
CellsFormat,
Link,
SelectionParams,
ProgressBar,
Button,
} from '../components/datatable/datatable.types';
const customTextFormatter = (customText: CustomText): string => {
let customTextHtml = '';
customText.map(
(item) =>
(customTextHtml += `
${item.text}
`),
);
return customTextHtml;
};
const imageFormatter = (image: Image): string =>
`
`;
const badgeFormatter = (badge: Badge): string =>
`${
badge.content
}
`;
const linkFormatter = (link: Link): string =>
`${link.text}`;
const linkWithoutRedirectFormatter = (link: Link, id: string): string =>
`${link.text}`;
const progressBarFormatter = (progressBar: ProgressBar): string => {
if (progressBar.labelOnLeft) {
return `${progressBar.value}%
`;
}
return ``;
};
const progressBarFormatterBranded = (progressBar: ProgressBar): string => {
if (progressBar.labelOnLeft) {
return `${progressBar.value}%
`;
}
return ``;
};
const buttonFormatterBranded = (button: Button, id: string): string => {
let buttonText = ``;
return buttonText;
};
const getFormattedCell = (
headerFormat: string,
cellValue: CellValue,
id: string,
): string => {
switch (headerFormat) {
case 'Text':
return cellValue as string;
case 'CustomText':
return customTextFormatter(cellValue as CustomText);
case 'Image':
return imageFormatter(cellValue as Image);
case 'Badge':
return badgeFormatter(cellValue as Badge);
case 'Link':
return linkFormatter(cellValue as Link);
case 'LinkWithoutRedirect':
return linkWithoutRedirectFormatter(cellValue as Link, id);
case 'ProgressBar':
if ((cellValue as ProgressBar).branded === true) {
return progressBarFormatterBranded(cellValue as ProgressBar);
}
return progressBarFormatter(cellValue as ProgressBar);
case 'Button':
return buttonFormatterBranded(cellValue as Button, id);
default:
return cellValue as string;
}
};
export const getCellContent = (
headData: HeadData,
bodyData: BodyData,
): string => {
const cellValue = bodyData?.cells[headData.id];
return cellValue
? getFormattedCell(headData.cellsFormat, cellValue, bodyData.id)
: '';
};
export const getDataAttribute = (
attributeSuffix: string | undefined,
value: string,
): { [key: string]: string } => {
const key = `data-${attributeSuffix ? attributeSuffix : ''}`;
return attributeSuffix
? {
[key]: value,
}
: {};
};
export const areAllItemsSelected = (items: BodyData[]): boolean => {
const selectedItems = items
.filter((item) => !item.disableSelection)
.filter((item) => item.selected === true);
return (
selectedItems.length ===
items.filter((item) => !item.disableSelection).length &&
items.filter((item) => !item.disableSelection).length > 0
);
};
export const checkSubRow = (items: BodyData[]): boolean => {
let result = false;
items.forEach((item) => {
if ('subRow' in item) {
result = true;
}
});
return result;
};
export const getSortDirection = (
initialSortDirection: SortDirection,
): SortDirection => {
let sortDirection: SortDirection;
switch (initialSortDirection) {
case 'na':
case 'desc':
default:
sortDirection = 'asc';
break;
case 'asc':
sortDirection = 'desc';
break;
}
return sortDirection;
};
export const onePageSort = (
items: BodyData[],
columnId: string,
cellsFormat: CellsFormat,
sortDirection: SortDirection,
): BodyData[] => {
let compareFunction;
switch (cellsFormat) {
default:
case 'Text':
if (sortDirection === 'asc') {
compareFunction = (a: BodyData, b: BodyData) =>
a.cells[columnId] > b.cells[columnId] ? 1 : -1;
} else if (sortDirection === 'desc') {
compareFunction = (a: BodyData, b: BodyData) =>
a.cells[columnId] > b.cells[columnId] ? -1 : 1;
}
break;
case 'Badge':
if (sortDirection === 'asc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as Badge)?.content || '0') >
((b.cells[columnId] as Badge)?.content || '0')
? 1
: -1;
} else if (sortDirection === 'desc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as Badge)?.content || '0') >
((b.cells[columnId] as Badge)?.content || '0')
? -1
: 1;
}
break;
case 'Link':
if (sortDirection === 'asc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as Link)?.text || '0') >
((b.cells[columnId] as Link)?.text || '0')
? 1
: -1;
} else if (sortDirection === 'desc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as Link)?.text || '0') >
((b.cells[columnId] as Link)?.text || '0')
? -1
: 1;
}
break;
case 'CustomText':
if (sortDirection === 'asc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as CustomText)[0].text || '0') >
((b.cells[columnId] as CustomText)[0].text || '0')
? 1
: -1;
} else if (sortDirection === 'desc') {
compareFunction = (a: BodyData, b: BodyData) =>
((a.cells[columnId] as CustomText)[0].text || '0') >
((b.cells[columnId] as CustomText)[0].text || '0')
? -1
: 1;
}
break;
}
const sortedItems = items.sort(compareFunction);
return sortedItems;
};
export const getPageSelectOptionsArray = (
totalPages: number,
currentPage: number,
showAllPages: boolean,
): number[] => {
if (totalPages <= 10 || showAllPages) {
return Array.from({ length: totalPages }, (_, i) => i + 1);
} else {
const pagesArray = [1, 2, 3];
if (!pagesArray.includes(currentPage - 1) && currentPage - 1 != 0) {
pagesArray.push(currentPage - 1);
}
if (!pagesArray.includes(currentPage)) {
pagesArray.push(currentPage);
}
if (!pagesArray.includes(currentPage + 1) && currentPage != totalPages) {
pagesArray.push(currentPage + 1);
}
pagesArray.push(totalPages - 2, totalPages - 1, totalPages);
const optionsArray = [...new Set(pagesArray)];
optionsArray.sort((a: number, b: number): number => a - b);
return optionsArray;
}
};
export const handleCountItemSelection = (
item: BodyData,
selectionParams: SelectionParams | undefined,
showSelectionCount: boolean,
): SelectionParams | undefined => {
if (showSelectionCount && selectionParams != null) {
if (selectionParams.allSelected) {
if (!item.selected) {
selectionParams.excludedIdsFromSelection = [
...selectionParams.excludedIdsFromSelection.filter(
(itemDeselected) => {
return itemDeselected !== item.id;
},
),
];
} else {
selectionParams.excludedIdsFromSelection = [
...selectionParams.excludedIdsFromSelection,
item.id,
];
if (
selectionParams.excludedIdsFromSelection.length ===
selectionParams.totalItems
) {
selectionParams.excludedIdsFromSelection = [];
selectionParams.allSelected = false;
}
}
} else {
if (!item.selected) {
selectionParams.idsSelected = [...selectionParams.idsSelected, item.id];
if (selectionParams.idsSelected.length === selectionParams.totalItems) {
selectionParams.idsSelected = [];
selectionParams.allSelected = true;
}
} else {
selectionParams.idsSelected = [
...selectionParams.idsSelected.filter((idSelected) => {
return idSelected !== item.id;
}),
];
}
}
}
return selectionParams;
};
export const handleCountAllSelection = (
items: BodyData[],
selectionParams: SelectionParams | undefined,
showSelectionCount: boolean,
indeterminate: boolean,
allItems: boolean,
): SelectionParams | undefined => {
if (showSelectionCount && selectionParams != undefined) {
if (selectionParams?.allSelected) {
if (indeterminate) {
items
.filter((item) => !item.disableSelection)
.filter(
(item) => !(selectionParams?.idsSelected || []).includes(item.id),
)
.forEach((item) => {
if (selectionParams != undefined) {
selectionParams.idsSelected = [
...selectionParams.idsSelected,
item.id,
];
}
});
items
.filter((item) => !item.disableSelection)
.forEach((item) => {
if (selectionParams != undefined) {
const index = selectionParams.excludedIdsFromSelection.indexOf(
item.id,
0,
);
if (index > -1) {
selectionParams.excludedIdsFromSelection.splice(index, 1);
selectionParams.excludedIdsFromSelection = [
...selectionParams.excludedIdsFromSelection,
];
}
}
});
if (selectionParams.idsSelected.length === selectionParams.totalItems) {
selectionParams.allSelected = true;
selectionParams.idsSelected = [];
}
} else {
selectionParams.excludedIdsFromSelection = [
...selectionParams.excludedIdsFromSelection,
...items
.filter((item) => !item.disableSelection)
.map((item) => item.id),
];
if (
selectionParams.excludedIdsFromSelection.length ===
selectionParams.totalItems
) {
selectionParams.allSelected = false;
selectionParams.idsSelected = [];
selectionParams.excludedIdsFromSelection = [];
}
}
} else {
if (allItems) {
items
.filter((item) => !item.disableSelection)
.filter(
(item) => !(selectionParams?.idsSelected || []).includes(item.id),
)
.forEach((item) => {
if (selectionParams != undefined) {
selectionParams.idsSelected = [
...selectionParams.idsSelected,
item.id,
];
}
});
if (selectionParams.idsSelected.length === selectionParams.totalItems) {
selectionParams.allSelected = true;
selectionParams.idsSelected = [];
}
} else {
items
.filter((item) => !item.disableSelection)
.forEach((item) => {
if (selectionParams != undefined) {
selectionParams.idsSelected = [
...selectionParams.idsSelected.filter((idSelected) => {
return idSelected !== item.id;
}),
];
}
});
}
}
}
return selectionParams;
};
export const clearSelection = (
selectionParams: SelectionParams | undefined,
): SelectionParams | undefined => {
if (selectionParams != undefined) {
selectionParams.excludedIdsFromSelection = [];
selectionParams.idsSelected = [];
selectionParams.allSelected = false;
}
return selectionParams;
};
export const selectAll = (
selectionParams: SelectionParams | undefined,
): SelectionParams | undefined => {
if (selectionParams != undefined) {
selectionParams.excludedIdsFromSelection = [];
selectionParams.idsSelected = [];
selectionParams.allSelected = true;
}
return selectionParams;
};
export const isHeaderSelectCheckboxIndeterminate = (
items: BodyData[],
selectionParams: SelectionParams | undefined,
indeterminate: boolean,
): boolean => {
if (selectionParams != undefined && indeterminate != undefined) {
if (
(!selectionParams.allSelected &&
items
.filter((item) => !item.disableSelection)
.map((item) => {
return item.id;
})
.every((item) =>
(selectionParams?.idsSelected || []).includes(item),
)) ||
(!selectionParams.allSelected &&
selectionParams.idsSelected.length === 0) ||
(selectionParams.allSelected &&
!items
.map((item) => item.id)
.some((id) =>
(selectionParams?.excludedIdsFromSelection || []).includes(id),
)) ||
(selectionParams.allSelected &&
selectionParams.excludedIdsFromSelection.length === 0)
) {
return false;
}
return true;
}
return false;
};