-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
sort.service.ts
258 lines (229 loc) · 9.53 KB
/
sort.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { SortDirectionNumber } from './../models/sortDirectionNumber.enum';
import { castToPromise } from './utilities';
import {
CellArgs,
Column,
ColumnSort,
CurrentSorter,
FieldType,
GridOption,
SlickEvent,
SortDirection,
SortDirectionString
} from './../models/index';
import { sortByFieldType } from '../sorters/sorterUtilities';
import { Sorters } from './../sorters';
import { Subject } from 'rxjs/Subject';
// using external non-typed js libraries
declare var Slick: any;
export class SortService {
private _currentLocalSorters: CurrentSorter[] = [];
private _eventHandler: any = new Slick.EventHandler();
private _dataView: any;
private _grid: any;
private _slickSubscriber: SlickEvent;
private _isBackendGrid = false;
onSortChanged = new Subject<CurrentSorter[]>();
/** Getter for the Grid Options pulled through the Grid Object */
private get _gridOptions(): GridOption {
return (this._grid && this._grid.getOptions) ? this._grid.getOptions() : {};
}
/** Getter for the Column Definitions pulled through the Grid Object */
private get _columnDefinitions(): Column[] {
return (this._grid && this._grid.getColumns) ? this._grid.getColumns() : [];
}
/**
* Attach a backend sort (single/multi) hook to the grid
* @param grid SlickGrid Grid object
* @param dataView SlickGrid DataView object
*/
attachBackendOnSort(grid: any, dataView: any) {
this._isBackendGrid = true;
this._grid = grid;
this._dataView = dataView;
this._slickSubscriber = grid.onSort;
// subscribe to the SlickGrid event and call the backend execution
this._slickSubscriber.subscribe(this.onBackendSortChanged.bind(this));
}
async onBackendSortChanged(event: Event, args: any) {
if (!args || !args.grid) {
throw new Error('Something went wrong when trying to attach the "onBackendSortChanged(event, args)" function, it seems that "args" is not populated correctly');
}
const gridOptions: GridOption = args.grid.getOptions() || {};
const backendApi = gridOptions.backendServiceApi;
if (!backendApi || !backendApi.process || !backendApi.service) {
throw new Error(`BackendServiceApi requires at least a "process" function and a "service" defined`);
}
if (backendApi.preProcess) {
backendApi.preProcess();
}
const query = backendApi.service.processOnSortChanged(event, args);
this.emitSortChanged('remote');
// the process could be an Observable (like HttpClient) or a Promise
// in any case, we need to have a Promise so that we can await on it (if an Observable, convert it to Promise)
const observableOrPromise = backendApi.process(query);
const processResult = await castToPromise(observableOrPromise);
// from the result, call our internal post process to update the Dataset and Pagination info
if (processResult && backendApi.internalPostProcess) {
backendApi.internalPostProcess(processResult);
}
// send the response process to the postProcess callback
if (backendApi.postProcess) {
backendApi.postProcess(processResult);
}
}
/**
* Attach a local sort (single/multi) hook to the grid
* @param grid SlickGrid Grid object
* @param gridOptions Grid Options object
* @param dataView
*/
attachLocalOnSort(grid: any, dataView: any) {
this._isBackendGrid = false;
this._grid = grid;
this._dataView = dataView;
this._slickSubscriber = grid.onSort;
this._slickSubscriber.subscribe((e: any, args: any) => {
// multiSort and singleSort are not exactly the same, but we want to structure it the same for the (for loop) after
// also to avoid having to rewrite the for loop in the sort, we will make the singleSort an array of 1 object
const sortColumns = (args.multiColumnSort) ? args.sortCols : new Array({sortAsc: args.sortAsc, sortCol: args.sortCol});
// keep current sorters
this._currentLocalSorters = []; // reset current local sorters
if (Array.isArray(sortColumns)) {
sortColumns.forEach((sortColumn) => {
if (sortColumn.sortCol) {
this._currentLocalSorters.push({
columnId: sortColumn.sortCol.id,
direction: sortColumn.sortAsc ? SortDirection.ASC : SortDirection.DESC
});
}
});
}
this.onLocalSortChanged(grid, dataView, sortColumns);
this.emitSortChanged('local');
});
if (dataView && dataView.onRowCountChanged) {
this._eventHandler.subscribe(dataView.onRowCountChanged, (e: Event, args: any) => {
// load any presets if there are any
if (args.current > 0) {
this.loadLocalPresets(grid, dataView);
}
});
}
}
clearSorting() {
if (this._grid && this._gridOptions && this._dataView) {
// remove any sort icons (this setSortColumns function call really does only that)
this._grid.setSortColumns([]);
// we also need to trigger a sort change
// for a backend grid, we will trigger a backend sort changed with an empty sort columns array
// however for a local grid, we need to pass a sort column and so we will sort by the 1st column
if (this._isBackendGrid) {
this.onBackendSortChanged(undefined, { grid: this._grid, sortCols: [] });
} else {
if (this._columnDefinitions && Array.isArray(this._columnDefinitions)) {
this.onLocalSortChanged(this._grid, this._dataView, new Array({sortAsc: true, sortCol: this._columnDefinitions[0] }));
}
}
}
}
getCurrentLocalSorters(): CurrentSorter[] {
return this._currentLocalSorters;
}
/**
* Get column sorts,
* If a column is passed as an argument, we won't add this column to our output array since it is already in the array
* We want to know the sort prior to calling the next sorting command
*/
getPreviousColumnSorts(columnId?: string) {
// getSortColumns() only returns sortAsc & columnId, we want the entire column definition
const oldSortColumns = this._grid.getSortColumns();
// get the column definition but only keep column which are not equal to our current column
const sortedCols = oldSortColumns.reduce((cols, col) => {
if (!columnId || col.columnId !== columnId) {
cols.push({ sortCol: this._columnDefinitions[this._grid.getColumnIndex(col.columnId)], sortAsc: col.sortAsc });
}
return cols;
}, []);
return sortedCols;
}
/**
* load any presets if there are any
* @param grid
* @param dataView
*/
loadLocalPresets(grid: any, dataView: any) {
const sortCols: ColumnSort[] = [];
this._currentLocalSorters = []; // reset current local sorters
if (this._gridOptions && this._gridOptions.presets && this._gridOptions.presets.sorters) {
const sorters = this._gridOptions.presets.sorters;
this._columnDefinitions.forEach((columnDef: Column) => {
const columnPreset = sorters.find((currentSorter: CurrentSorter) => {
return currentSorter.columnId === columnDef.id;
});
if (columnPreset) {
sortCols.push({
columnId: columnDef.id,
sortAsc: ((columnPreset.direction.toUpperCase() === SortDirection.ASC) ? true : false),
sortCol: columnDef
});
// keep current sorters
this._currentLocalSorters.push({
columnId: columnDef.id + '',
direction: columnPreset.direction.toUpperCase() as SortDirectionString
});
}
});
if (sortCols.length > 0) {
this.onLocalSortChanged(grid, dataView, sortCols);
grid.setSortColumns(sortCols); // add sort icon in UI
}
}
}
onLocalSortChanged(grid: any, dataView: any, sortColumns: ColumnSort[]) {
dataView.sort((dataRow1: any, dataRow2: any) => {
for (let i = 0, l = sortColumns.length; i < l; i++) {
const columnSortObj = sortColumns[i];
if (columnSortObj && columnSortObj.sortCol) {
const sortDirection = columnSortObj.sortAsc ? SortDirectionNumber.asc : SortDirectionNumber.desc;
const sortField = columnSortObj.sortCol.queryField || columnSortObj.sortCol.queryFieldFilter || columnSortObj.sortCol.field;
const fieldType = columnSortObj.sortCol.type || FieldType.string;
const value1 = dataRow1[sortField];
const value2 = dataRow2[sortField];
const sortResult = sortByFieldType(value1, value2, fieldType, sortDirection);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
}
}
}
return 0;
});
grid.invalidate();
grid.render();
}
dispose() {
// unsubscribe local event
if (this._slickSubscriber && typeof this._slickSubscriber.unsubscribe === 'function') {
this._slickSubscriber.unsubscribe();
}
// unsubscribe all SlickGrid events
this._eventHandler.unsubscribeAll();
}
/**
* A simple function that is attached to the subscriber and emit a change when the sort is called.
* Other services, like Pagination, can then subscribe to it.
* @param sender
*/
emitSortChanged(sender: 'local' | 'remote') {
if (sender === 'remote' && this._gridOptions && this._gridOptions.backendServiceApi) {
let currentSorters: CurrentSorter[] = [];
const backendService = this._gridOptions.backendServiceApi.service;
if (backendService && backendService.getCurrentSorters) {
currentSorters = backendService.getCurrentSorters() as CurrentSorter[];
}
this.onSortChanged.next(currentSorters);
} else if (sender === 'local') {
this.onSortChanged.next(this.getCurrentLocalSorters());
}
}
}