Skip to content

Commit

Permalink
Merge pull request #637 from revolist/minor-refactor
Browse files Browse the repository at this point in the history
Sorting extracted outside
  • Loading branch information
revolist authored Nov 19, 2024
2 parents 7a2c6fe + 977d6f1 commit 1845829
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 49 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/angular
2 changes: 1 addition & 1 deletion packages/react
Submodule react updated 2 files
+1 −1 demo/package.json
+2 −2 package.json
2 changes: 1 addition & 1 deletion packages/svelte
Submodule svelte updated 1 files
+2 −2 package.json
2 changes: 1 addition & 1 deletion packages/vue2
Submodule vue2 updated 2 files
+1 −1 demo/package.json
+2 −2 package.json
2 changes: 1 addition & 1 deletion packages/vue3
Submodule vue3 updated 2 files
+2 −2 demo/package.json
+2 −2 package.json
5 changes: 5 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ declare global {
"aftergridinit": any;
"additionaldatachanged": any;
"afterthemechanged": Theme;
"created": any;
}
/**
* Revogrid - High-performance, customizable grid library for managing large datasets.
Expand Down Expand Up @@ -1497,6 +1498,10 @@ declare namespace LocalJSX {
* New content size has been applied. The size excludes the header. Currently, the event responsible for applying the new content size does not provide the actual size. To retrieve the actual content size, you can utilize the `getContentSize` function after the event has been triggered.
*/
"onContentsizechanged"?: (event: RevoGridCustomEvent<MultiDimensionType>) => void;
/**
* Emmited after grid created
*/
"onCreated"?: (event: RevoGridCustomEvent<any>) => void;
/**
* Emitted when the filter configuration is changed
*/
Expand Down
9 changes: 8 additions & 1 deletion src/components/revoGrid/revo-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,13 @@ export class RevoGridComponent {
/**
* Emmited after the theme is changed
*/
@Event() afterthemechanged: EventEmitter<Theme>
@Event() afterthemechanged: EventEmitter<Theme>;


/**
* Emmited after grid created
*/
@Event() created: EventEmitter;

// #endregion

Expand Down Expand Up @@ -1409,6 +1415,7 @@ export class RevoGridComponent {
if (this.isInited) {
this.setPlugins();
}
this.created.emit();
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/plugins/sorting/sorting.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { DataType } from '@type';
import type { SortingOrderFunction } from './sorting.types';

export function sortIndexByItems(
indexes: number[],
source: DataType[],
sortingFunc: SortingOrderFunction = {},
): number[] {
// 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) => {
for (const [prop, cmp] of Object.entries(sortingFunc)) {
const itemA = source[a];
const itemB = source[b];

/**
* 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?.(prop, itemA, itemB);
if (sorted) {
return sorted;
}
}
return 0;
});
}
43 changes: 3 additions & 40 deletions src/plugins/sorting/sorting.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import type {
DimensionRows,
PluginProviders,
} from '@type';
import type { SortingOrder, SortingOrderFunction, ColumnSetEvent } from './sorting.types';
import { getCellRaw, getColumnByProp } from '../../utils/column.utils';
import { rowTypes } from '@store';

export type SortingOrder = Record<ColumnProp, Order>;
type SortingOrderFunction = Record<ColumnProp, CellCompareFunc | undefined>;
type ColumnSetEvent = {
order: SortingOrder;
};
import { sortIndexByItems } from './sorting.func';

/**
* Lifecycle
Expand Down Expand Up @@ -239,7 +235,7 @@ export class SortingPlugin extends BasePlugin {
// row indexes
const proxyItems = storeService.store.get('proxyItems');

const newItemsOrder = this.sortIndexByItems(
const newItemsOrder = sortIndexByItems(
[...proxyItems],
source,
sortingFunc,
Expand Down Expand Up @@ -274,39 +270,6 @@ export class SortingPlugin extends BasePlugin {
};
}

sortIndexByItems(
indexes: number[],
source: DataType[],
sortingFunc: SortingOrderFunction = {},
): number[] {
// 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) => {
for (const [prop, cmp] of Object.entries(sortingFunc)) {
const itemA = source[a];
const itemB = source[b];

/**
* 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?.(prop, itemA, itemB);
if (sorted) {
return sorted;
}
}
return 0;
});
}

getNextOrder(currentOrder: Order): Order {
switch (currentOrder) {
case undefined:
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/sorting/sorting.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { CellCompareFunc, ColumnProp, Order } from '@type';

export type SortingOrder = Record<ColumnProp, Order>;
export type SortingOrderFunction = Record<
ColumnProp,
CellCompareFunc | undefined
>;
export type ColumnSetEvent = {
order: SortingOrder;
};
2 changes: 2 additions & 0 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type RevogridEvents = 'contentsizechanged'|
'aftergridinit'|
'additionaldatachanged'|
'afterthemechanged'|
'created'|
'beforepaste'|
'beforepasteapply'|
'pasteregion'|
Expand Down Expand Up @@ -140,6 +141,7 @@ export const REVOGRID_EVENTS = new Map<RevogridEvents, RevogridEvents>([
['aftergridinit', 'aftergridinit'],
['additionaldatachanged', 'additionaldatachanged'],
['afterthemechanged', 'afterthemechanged'],
['created', 'created'],
['beforepaste', 'beforepaste'],
['beforepasteapply', 'beforepasteapply'],
['pasteregion', 'pasteregion'],
Expand Down

0 comments on commit 1845829

Please sign in to comment.