UNPKG

@revolist/revogrid

Version:

Virtual reactive data grid spreadsheet component - RevoGrid.

82 lines (81 loc) 3.24 kB
/*! * Built by Revolist OU ❤️ */ import { isGrouping } from "../groupingRow/grouping.service"; import { getCellRaw } from "../../utils/column.utils"; export function sortIndexByItems(indexes, source, sortingFunc = {}) { // if no sorting - return unsorted indexes if (Object.entries(sortingFunc).length === 0) { // Unsorted indexes return [...Array(indexes.length).keys()]; } // /** * go through all indexes and align in new order * performs a multi-level sorting by applying multiple comparison functions to determine the order of the items based on different properties. */ return indexes.sort((a, b) => { const itemA = source[a]; const itemB = source[b]; for (const [prop, cmp] of Object.entries(sortingFunc)) { if (isGrouping(itemA)) { if (itemA['__rvgr-prop'] !== prop) { return 0; } } if (isGrouping(itemB)) { if (itemB['__rvgr-prop'] !== prop) { return 0; } } /** * If the comparison function returns a non-zero value (sorted), it means that the items should be sorted based on the given property. In such a case, the function immediately returns the sorted value, indicating the order in which the items should be arranged. * If none of the comparison functions result in a non-zero value, indicating that the items are equal or should remain in the same order, the function eventually returns 0. */ const sorted = cmp === null || cmp === void 0 ? void 0 : cmp(prop, itemA, itemB); if (sorted) { return sorted; } } return 0; }); } export function defaultCellCompare(prop, a, b) { const aRaw = this.column ? getCellRaw(a, this.column) : a === null || a === void 0 ? void 0 : a[prop]; const bRaw = this.column ? getCellRaw(b, this.column) : b === null || b === void 0 ? void 0 : b[prop]; const av = typeof aRaw === 'number' ? aRaw : aRaw === null || aRaw === void 0 ? void 0 : aRaw.toString().toLowerCase(); const bv = typeof bRaw === 'number' ? bRaw : bRaw === null || bRaw === void 0 ? void 0 : bRaw.toString().toLowerCase(); if (av === bv) { return 0; } if (av > bv) { return 1; } return -1; } export function descCellCompare(cmp) { return (prop, a, b) => { return -1 * cmp(prop, a, b); }; } export function getNextOrder(currentOrder) { switch (currentOrder) { case undefined: return 'asc'; case 'asc': return 'desc'; case 'desc': return undefined; } } export function getComparer(column, order) { var _a; const cellCmp = ((_a = column === null || column === void 0 ? void 0 : column.cellCompare) === null || _a === void 0 ? void 0 : _a.bind({ order })) || (defaultCellCompare === null || defaultCellCompare === void 0 ? void 0 : defaultCellCompare.bind({ column, order })); if (order == 'asc') { return cellCmp; } if (order == 'desc') { return descCellCompare(cellCmp); } return undefined; }