Skip to content

Commit

Permalink
AG-515 Allow configuration of accented sort
Browse files Browse the repository at this point in the history
  • Loading branch information
makinggoodsoftware committed Jun 12, 2017
1 parent 8e6415a commit e5f9327
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ts/components/componentUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ComponentUtil {
'enableGroupEdit', 'embedFullWidthRows', 'suppressTabbing', 'suppressPaginationPanel', 'floatingFilter',
'groupHideOpenParents', 'groupMultiAutoColumn', 'pagination', 'stopEditingWhenGridLosesFocus',
'paginationAutoPageSize', 'suppressScrollOnNewData', 'purgeClosedRowNodes', 'cacheQuickFilter',
'deltaRowDataMode', 'enforceRowDomOrder'
'deltaRowDataMode', 'enforceRowDomOrder', 'accentedSort'
];

public static FUNCTION_PROPERTIES = ['headerCellRenderer', 'localeTextFunc', 'groupRowInnerRenderer', 'groupRowInnerRendererFramework',
Expand Down
1 change: 1 addition & 0 deletions src/ts/entities/gridOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface GridOptions {
suppressRowHoverClass?: boolean;
sortingOrder?: string[];
suppressMultiSort?: boolean;
accentedSort?: boolean;
suppressHorizontalScroll?: boolean;
suppressTabbing?: boolean;
unSortIcon?: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/ts/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {RowNode} from "./entities/rowNode";
import {Utils as _} from './utils';

export function defaultGroupComparator(valueA: any, valueB: any, nodeA: RowNode, nodeB: RowNode): number {
export function defaultGroupComparator(valueA: any, valueB: any, nodeA: RowNode, nodeB: RowNode, accentedCompare: boolean = false): number {

let nodeAIsGroup = _.exists(nodeA) && nodeA.group;
let nodeBIsGroup = _.exists(nodeB) && nodeB.group;
Expand All @@ -10,9 +10,9 @@ export function defaultGroupComparator(valueA: any, valueB: any, nodeA: RowNode,
let bothAreNormal = !nodeAIsGroup && !nodeBIsGroup;

if (bothAreGroups) {
return _.defaultComparator(nodeA.key, nodeB.key);
return _.defaultComparator(nodeA.key, nodeB.key, accentedCompare);
} else if (bothAreNormal) {
return _.defaultComparator(valueA, valueB);
return _.defaultComparator(valueA, valueB, accentedCompare);
} else if (nodeAIsGroup) {
return 1;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/ts/gridOptionsWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class GridOptionsWrapper {
public getViewportDatasource(): IViewportDatasource { return this.gridOptions.viewportDatasource; }
public getEnterpriseDatasource(): IEnterpriseDatasource { return this.gridOptions.enterpriseDatasource; }
public isEnableSorting() { return isTrue(this.gridOptions.enableSorting) || isTrue(this.gridOptions.enableServerSideSorting); }
public isAccentedSort() { return isTrue(this.gridOptions.accentedSort) }
public isEnableCellExpressions() { return isTrue(this.gridOptions.enableCellExpressions); }
public isEnableGroupEdit() { return isTrue(this.gridOptions.enableGroupEdit); }
public isSuppressMiddleClickScrolls() { return isTrue(this.gridOptions.suppressMiddleClickScrolls); }
Expand Down
4 changes: 3 additions & 1 deletion src/ts/rowNodes/sortService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Autowired, Bean} from "../context/context";
import {SortController} from "../sortController";
import {_} from "../utils";
import {ValueService} from "../valueService";
import {GridOptionsWrapper} from "../gridOptionsWrapper";

export interface SortOption {
inverter:number,
Expand All @@ -19,6 +20,7 @@ export interface SortedRowNode {
export class SortService {
@Autowired('sortController') private sortController: SortController;
@Autowired('valueService') private valueService: ValueService;
@Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;

sortAccordingToColumnsState (rowNode: RowNode){
let sortOptions: SortOption[] = this.sortController.getSortForRowController();
Expand Down Expand Up @@ -69,7 +71,7 @@ export class SortService {
comparatorResult = sortOption.column.getColDef().comparator(valueA, valueB, nodeA, nodeB, isInverted);
} else {
//otherwise do our own comparison
comparatorResult = _.defaultComparator(valueA, valueB);
comparatorResult = _.defaultComparator(valueA, valueB, this.gridOptionsWrapper.isAccentedSort());
}

if (comparatorResult !== 0) {
Expand Down
29 changes: 20 additions & 9 deletions src/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export class Utils {
});
}

static defaultComparator(valueA: any, valueB: any): number {
static defaultComparator(valueA: any, valueB: any, accentedCompare: boolean = false): number {
let valueAMissing = valueA === null || valueA === undefined;
let valueBMissing = valueB === null || valueB === undefined;
if (valueAMissing && valueBMissing) {
Expand All @@ -615,13 +615,19 @@ export class Utils {
}

if (typeof valueA === "string") {
try {
// using local compare also allows chinese comparisons
return valueA.localeCompare(valueB);
} catch (e) {
// if something wrong with localeCompare, eg not supported
// by browser, then just continue without using it
if (! accentedCompare) {
return doQuickCompare(valueA, valueB);
} else {
try {
// using local compare also allows chinese comparisons
return valueA.localeCompare(valueB);
} catch (e) {
// if something wrong with localeCompare, eg not supported
// by browser, then just continue with the quick one
return doQuickCompare(valueA, valueB);
}
}

}

if (valueA < valueB) {
Expand All @@ -631,6 +637,11 @@ export class Utils {
} else {
return 0;
}


function doQuickCompare (a:string, b:string): number{
return (a > b ? 1 : (a < b ? -1 : 0));
}
}

static compareArrays(array1: any[], array2: any[]): boolean {
Expand Down Expand Up @@ -1150,8 +1161,8 @@ export class Utils {
};
};

static referenceCompare (left:any, right:any):boolean{
if (left == null && right==null) return true;
static referenceCompare(left: any, right: any): boolean {
if (left == null && right == null) return true;
if (left == null && right) return false;
if (left && right == null) return false;
return left === right;
Expand Down

0 comments on commit e5f9327

Please sign in to comment.