forked from angular-ui/ui-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrowSearcher.js
380 lines (311 loc) · 12.5 KB
/
rowSearcher.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(function() {
var module = angular.module('ui.grid');
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
/**
* @ngdoc service
* @name ui.grid.service:rowSearcher
*
* @description Service for searching/filtering rows based on column value conditions.
*/
module.service('rowSearcher', ['gridUtil', 'uiGridConstants', function (gridUtil, uiGridConstants) {
var defaultCondition = uiGridConstants.filter.STARTS_WITH;
var rowSearcher = {};
/**
* @ngdoc function
* @name getTerm
* @methodOf ui.grid.service:rowSearcher
* @description Get the term from a filter
* Trims leading and trailing whitespace
* @param {object} filter object to use
* @returns {object} Parsed term
*/
rowSearcher.getTerm = function getTerm(filter) {
if (typeof(filter.term) === 'undefined') { return filter.term; }
var term = filter.term;
// Strip leading and trailing whitespace if the term is a string
if (typeof(term) === 'string') {
term = term.trim();
}
return term;
};
/**
* @ngdoc function
* @name stripTerm
* @methodOf ui.grid.service:rowSearcher
* @description Remove leading and trailing asterisk (*) from the filter's term
* @param {object} filter object to use
* @returns {uiGridConstants.filter<int>} Value representing the condition constant value
*/
rowSearcher.stripTerm = function stripTerm(filter) {
var term = rowSearcher.getTerm(filter);
if (typeof(term) === 'string') {
return escapeRegExp(term.replace(/(^\*|\*$)/g, ''));
}
else {
return term;
}
};
/**
* @ngdoc function
* @name guessCondition
* @methodOf ui.grid.service:rowSearcher
* @description Guess the condition for a filter based on its term
* <br>
* Defaults to STARTS_WITH. Uses CONTAINS for strings beginning and ending with *s (*bob*).
* Uses STARTS_WITH for strings ending with * (bo*). Uses ENDS_WITH for strings starting with * (*ob).
* @param {object} filter object to use
* @returns {uiGridConstants.filter<int>} Value representing the condition constant value
*/
rowSearcher.guessCondition = function guessCondition(filter) {
if (typeof(filter.term) === 'undefined' || !filter.term) {
return defaultCondition;
}
var term = rowSearcher.getTerm(filter);
// A searchOption value could be an object so it is better to user angular.equals instead of a regex.
if (angular.isObject(term) && term.value && !angular.isDefined(filter.condition)) {
return function(searchTerm, cellValue) {
return angular.equals(searchTerm, cellValue);
};
}
if (/\*/.test(term)) {
var regexpFlags = '';
if (!filter.flags || !filter.flags.caseSensitive) {
regexpFlags += 'i';
}
var reText = term.replace(/(\\)?\*/g, function ($0, $1) { return $1 ? $0 : '[\\s\\S]*?'; });
return new RegExp('^' + reText + '$', regexpFlags);
}
// Otherwise default to default condition
else {
return defaultCondition;
}
};
/**
* @ngdoc function
* @name setupFilters
* @methodOf ui.grid.service:rowSearcher
* @description For a given columns filters (either col.filters, or [col.filter] can be passed in),
* do all the parsing and pre-processing and store that data into a new filters object. The object
* has the condition, the flags, the stripped term, and a parsed reg exp if there was one.
*
* We could use a forEach in here, since it's much less performance sensitive, but since we're using
* for loops everywhere else in this module...
*
* @param {array} filters the filters from the column (col.filters or [col.filter])
* @returns {array} An array of parsed/preprocessed filters
*/
rowSearcher.setupFilters = function setupFilters( filters ){
var newFilters = [];
var filtersLength = filters.length;
for ( var i = 0; i < filtersLength; i++ ){
var filter = filters[i];
if ( filter.noTerm || !gridUtil.isNullOrUndefined(filter.term) ){
var newFilter = {};
var regexpFlags = '';
if (!filter.flags || !filter.flags.caseSensitive) {
regexpFlags += 'i';
}
if ( !gridUtil.isNullOrUndefined(filter.term) ){
// it is possible to have noTerm. We don't need to copy that across, it was just a flag to avoid
// getting the filter ignored if the filter was a function that didn't use a term
newFilter.term = rowSearcher.stripTerm(filter);
}
if ( filter.condition ){
newFilter.condition = filter.condition;
} else {
newFilter.condition = rowSearcher.guessCondition(filter);
}
newFilter.flags = angular.extend( { caseSensitive: false, date: false }, filter.flags );
if (newFilter.condition === uiGridConstants.filter.STARTS_WITH) {
newFilter.startswithRE = new RegExp('^' + newFilter.term, regexpFlags);
}
if (newFilter.condition === uiGridConstants.filter.ENDS_WITH) {
newFilter.endswithRE = new RegExp(newFilter.term + '$', regexpFlags);
}
if (newFilter.condition === uiGridConstants.filter.CONTAINS) {
newFilter.containsRE = new RegExp(newFilter.term, regexpFlags);
}
if (newFilter.condition === uiGridConstants.filter.EXACT) {
newFilter.exactRE = new RegExp('^' + newFilter.term + '$', regexpFlags);
}
newFilters.push(newFilter);
}
}
return newFilters;
};
/**
* @ngdoc function
* @name runColumnFilter
* @methodOf ui.grid.service:rowSearcher
* @description Runs a single pre-parsed filter against a cell, returning true
* if the cell matches that one filter.
*
* @param {Grid} grid the grid we're working against
* @param {GridRow} row the row we're matching against
* @param {GridCol} column the column that we're working against
* @param {object} filter the specific, preparsed, filter that we want to test
* @returns {boolean} true if we match (row stays visible)
*/
rowSearcher.runColumnFilter = function runColumnFilter(grid, row, column, filter) {
// Cache typeof condition
var conditionType = typeof(filter.condition);
// Term to search for.
// Exctract the term.value for selectOptions since ng-options track by returns the entire object.
var term = angular.isObject(filter.term) && filter.term.value ? filter.term.value : filter.term;
// Get the column value for this row
var value = grid.getCellValue(row, column);
// If the filter's condition is a RegExp, then use it
if (filter.condition instanceof RegExp) {
return filter.condition.test(value);
}
// If the filter's condition is a function, run it
if (conditionType === 'function') {
return filter.condition(term, value, row, column);
}
if (filter.startswithRE) {
return filter.startswithRE.test(value);
}
if (filter.endswithRE) {
return filter.endswithRE.test(value);
}
if (filter.containsRE) {
return filter.containsRE.test(value);
}
if (filter.exactRE) {
return filter.exactRE.test(value);
}
if (filter.condition === uiGridConstants.filter.NOT_EQUAL) {
var regex = new RegExp('^' + term + '$');
return !regex.exec(value);
}
if (typeof(value) === 'number'){
// if the term has a decimal in it, it comes through as '9\.4', we need to take out the \
// the same for negative numbers
// TODO: I suspect the right answer is to look at escapeRegExp at the top of this code file, maybe it's not needed?
var tempFloat = parseFloat(term.replace(/\\\./,'.').replace(/\\\-/,'-'));
if (!isNaN(tempFloat)) {
term = tempFloat;
}
}
if (filter.flags.date === true) {
value = new Date(value);
// If the term has a dash in it, it comes through as '\-' -- we need to take out the '\'.
term = new Date(term.replace(/\\/g, ''));
}
if (filter.condition === uiGridConstants.filter.GREATER_THAN) {
return (value > term);
}
if (filter.condition === uiGridConstants.filter.GREATER_THAN_OR_EQUAL) {
return (value >= term);
}
if (filter.condition === uiGridConstants.filter.LESS_THAN) {
return (value < term);
}
if (filter.condition === uiGridConstants.filter.LESS_THAN_OR_EQUAL) {
return (value <= term);
}
return true;
};
/**
* @ngdoc boolean
* @name useExternalFiltering
* @propertyOf ui.grid.class:GridOptions
* @description False by default. When enabled, this setting suppresses the internal filtering.
* All UI logic will still operate, allowing filter conditions to be set and modified.
*
* The external filter logic can listen for the `filterChange` event, which fires whenever
* a filter has been adjusted.
*/
/**
* @ngdoc function
* @name searchColumn
* @methodOf ui.grid.service:rowSearcher
* @description Process provided filters on provided column against a given row. If the row meets
* the conditions on all the filters, return true.
* @param {Grid} grid Grid to search in
* @param {GridRow} row Row to search on
* @param {GridCol} column Column with the filters to use
* @param {array} filters array of pre-parsed/preprocessed filters to apply
* @returns {boolean} Whether the column matches or not.
*/
rowSearcher.searchColumn = function searchColumn(grid, row, column, filters) {
if (grid.options.useExternalFiltering) {
return true;
}
var filtersLength = filters.length;
for (var i = 0; i < filtersLength; i++) {
var filter = filters[i];
var ret = rowSearcher.runColumnFilter(grid, row, column, filter);
if (!ret) {
return false;
}
}
return true;
};
/**
* @ngdoc function
* @name search
* @methodOf ui.grid.service:rowSearcher
* @description Run a search across the given rows and columns, marking any rows that don't
* match the stored col.filters or col.filter as invisible.
* @param {Grid} grid Grid instance to search inside
* @param {Array[GridRow]} rows GridRows to filter
* @param {Array[GridColumn]} columns GridColumns with filters to process
*/
rowSearcher.search = function search(grid, rows, columns) {
/*
* Added performance optimisations into this code base, as this logic creates deeply nested
* loops and is therefore very performance sensitive. In particular, avoiding forEach as
* this impacts some browser optimisers (particularly Chrome), using iterators instead
*/
// Don't do anything if we weren't passed any rows
if (!rows) {
return;
}
// don't filter if filtering currently disabled
if (!grid.options.enableFiltering){
return rows;
}
// Build list of filters to apply
var filterData = [];
var colsLength = columns.length;
for (var i = 0; i < colsLength; i++) {
var col = columns[i];
if (typeof(col.filters) !== 'undefined' && ( col.filters.length > 1 || col.filters.length === 1 && ( !gridUtil.isNullOrUndefined(col.filters[0].term) || col.filters[0].noTerm ) ) ) {
filterData.push( { col: col, filters: rowSearcher.setupFilters(col.filters) } );
}
else if (typeof(col.filter) !== 'undefined' && col.filter && ( !gridUtil.isNullOrUndefined(col.filters[0].term) || col.filter.noTerm ) ) {
filterData.push( { col: col, filters: rowSearcher.setupFilters([col.filter]) } );
}
}
if (filterData.length > 0) {
// define functions outside the loop, performance optimisation
var foreachRow = function(grid, row, col, filters){
if ( row.visible && !rowSearcher.searchColumn(grid, row, col, filters) ) {
row.visible = false;
}
};
var foreachFilterCol = function(grid, filterData){
var rowsLength = rows.length;
for ( var i = 0; i < rowsLength; i++){
foreachRow(grid, rows[i], filterData.col, filterData.filters);
}
};
// nested loop itself - foreachFilterCol, which in turn calls foreachRow
var filterDataLength = filterData.length;
for ( var j = 0; j < filterDataLength; j++){
foreachFilterCol( grid, filterData[j] );
}
if (grid.api.core.raise.rowsVisibleChanged) {
grid.api.core.raise.rowsVisibleChanged();
}
// drop any invisible rows
rows = rows.filter(function(row){ return row.visible; });
}
return rows;
};
return rowSearcher;
}]);
})();