UNPKG

7.6 kBPlain TextView Raw
1import {Column} from "./entities/column";
2import {Autowired} from "./context/context";
3import {GridOptionsWrapper} from "./gridOptionsWrapper";
4import {ColumnApi} from "./columnController/columnApi";
5import {ColumnController} from "./columnController/columnController";
6import {EventService} from "./eventService";
7import {ColumnEventType, Events, SortChangedEvent} from "./events";
8import {Bean} from "./context/context";
9import {Utils as _} from './utils';
10import {GridApi} from "./gridApi";
11
12@Bean('sortController')
13export class SortController {
14
15 private static DEFAULT_SORTING_ORDER = [Column.SORT_ASC, Column.SORT_DESC, null];
16
17 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
18 @Autowired('columnController') private columnController: ColumnController;
19 @Autowired('eventService') private eventService: EventService;
20 @Autowired('columnApi') private columnApi: ColumnApi;
21 @Autowired('gridApi') private gridApi: GridApi;
22
23 public progressSort(column: Column, multiSort: boolean, source: ColumnEventType = "api"): void {
24 let nextDirection = this.getNextSortDirection(column);
25 this.setSortForColumn(column, nextDirection, multiSort, source);
26 }
27
28 public setSortForColumn(column: Column, sort: string, multiSort: boolean, source: ColumnEventType = "api"): void {
29
30 // auto correct - if sort not legal value, then set it to 'no sort' (which is null)
31 if (sort!==Column.SORT_ASC && sort!==Column.SORT_DESC) { sort = null; }
32
33 // update sort on current col
34 column.setSort(sort, source);
35
36 // sortedAt used for knowing order of cols when multi-col sort
37 if (column.getSort()) {
38 let sortedAt = Number(new Date().valueOf());
39 column.setSortedAt(sortedAt);
40 } else {
41 column.setSortedAt(null);
42 }
43
44 let doingMultiSort = multiSort && !this.gridOptionsWrapper.isSuppressMultiSort();
45
46 // clear sort on all columns except this one, and update the icons
47 if (!doingMultiSort) {
48 this.clearSortBarThisColumn(column, source);
49 }
50
51 this.dispatchSortChangedEvents();
52 }
53
54 // gets called by API, so if data changes, use can call this, which will end up
55 // working out the sort order again of the rows.
56 public onSortChanged(): void {
57 this.dispatchSortChangedEvents();
58 }
59
60 private dispatchSortChangedEvents(): void {
61 let event: SortChangedEvent = {
62 type: Events.EVENT_SORT_CHANGED,
63 api: this.gridApi,
64 columnApi: this.columnApi
65 };
66 this.eventService.dispatchEvent(event);
67 }
68
69 private clearSortBarThisColumn(columnToSkip: Column, source: ColumnEventType): void {
70 this.columnController.getPrimaryAndSecondaryAndAutoColumns().forEach( (columnToClear: Column)=> {
71 // Do not clear if either holding shift, or if column in question was clicked
72 if (!(columnToClear === columnToSkip)) {
73 // setting to 'undefined' as null means 'none' rather than cleared, otherwise issue will arise
74 // if sort order is: ['desc', null , 'asc'], as it will start at null rather than 'desc'.
75 columnToClear.setSort(undefined, source);
76 }
77 });
78 }
79
80 private getNextSortDirection(column: Column): string {
81
82 let sortingOrder: string[];
83 if (column.getColDef().sortingOrder) {
84 sortingOrder = column.getColDef().sortingOrder;
85 } else if (this.gridOptionsWrapper.getSortingOrder()) {
86 sortingOrder = this.gridOptionsWrapper.getSortingOrder();
87 } else {
88 sortingOrder = SortController.DEFAULT_SORTING_ORDER;
89 }
90
91 if ( !Array.isArray(sortingOrder) || sortingOrder.length <= 0) {
92 console.warn('ag-grid: sortingOrder must be an array with at least one element, currently it\'s ' + sortingOrder);
93 return;
94 }
95
96 let currentIndex = sortingOrder.indexOf(column.getSort());
97 let notInArray = currentIndex < 0;
98 let lastItemInArray = currentIndex == sortingOrder.length - 1;
99 let result: string;
100 if (notInArray || lastItemInArray) {
101 result = sortingOrder[0];
102 } else {
103 result = sortingOrder[currentIndex + 1];
104 }
105
106 // verify the sort type exists, as the user could provide the sortingOrder, need to make sure it's valid
107 if (SortController.DEFAULT_SORTING_ORDER.indexOf(result) < 0) {
108 console.warn('ag-grid: invalid sort type ' + result);
109 return null;
110 }
111
112 return result;
113 }
114
115 // used by the public api, for saving the sort model
116 public getSortModel() {
117 let columnsWithSorting = this.getColumnsWithSortingOrdered();
118
119 return _.map(columnsWithSorting, (column: Column) => {
120 return {
121 colId: column.getColId(),
122 sort: column.getSort()
123 }
124 });
125 }
126
127 public setSortModel(sortModel: any, source: ColumnEventType = "api") {
128 if (!this.gridOptionsWrapper.isEnableSorting()) {
129 console.warn('ag-grid: You are setting the sort model on a grid that does not have sorting enabled');
130 return;
131 }
132 // first up, clear any previous sort
133 let sortModelProvided = sortModel && sortModel.length > 0;
134
135 let allColumnsIncludingAuto = this.columnController.getPrimaryAndSecondaryAndAutoColumns();
136 allColumnsIncludingAuto.forEach( (column: Column)=> {
137 let sortForCol: any = null;
138 let sortedAt = -1;
139 if (sortModelProvided && !column.getColDef().suppressSorting) {
140 for (let j = 0; j < sortModel.length; j++) {
141 let sortModelEntry = sortModel[j];
142 if (typeof sortModelEntry.colId === 'string'
143 && typeof column.getColId() === 'string'
144 && this.compareColIds(sortModelEntry, column)) {
145 sortForCol = sortModelEntry.sort;
146 sortedAt = j;
147 }
148 }
149 }
150
151 if (sortForCol) {
152 column.setSort(sortForCol, source);
153 column.setSortedAt(sortedAt);
154 } else {
155 column.setSort(null, source);
156 column.setSortedAt(null);
157 }
158 });
159
160 this.dispatchSortChangedEvents();
161 }
162
163 private compareColIds(sortModelEntry: any, column: Column) {
164 return sortModelEntry.colId === column.getColId();
165 }
166
167 public getColumnsWithSortingOrdered(): Column[] {
168 // pull out all the columns that have sorting set
169 let allColumnsIncludingAuto = this.columnController.getPrimaryAndSecondaryAndAutoColumns();
170 let columnsWithSorting = <Column[]> _.filter(allColumnsIncludingAuto, (column:Column) => { return !!column.getSort();} );
171
172 // put the columns in order of which one got sorted first
173 columnsWithSorting.sort( (a: any, b: any) => { return a.sortedAt - b.sortedAt} );
174
175 return columnsWithSorting;
176 }
177
178 // used by row controller, when doing the sorting
179 public getSortForRowController(): any[] {
180 let columnsWithSorting = this.getColumnsWithSortingOrdered();
181
182 return _.map(columnsWithSorting, (column: Column) => {
183 let ascending = column.getSort() === Column.SORT_ASC;
184 return {
185 inverter: ascending ? 1 : -1,
186 column: column
187 }
188 });
189 }
190}
\No newline at end of file