2017-12-15 21:39:18 +00:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import { get, computed } from '@ember/object';
|
2018-04-12 16:18:33 +00:00
|
|
|
import { reads } from '@ember/object/computed';
|
2018-07-06 20:38:57 +00:00
|
|
|
import Fuse from 'fuse.js';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Searchable mixin
|
|
|
|
|
|
|
|
Simple search filtering behavior for a list of objects.
|
|
|
|
|
|
|
|
Properties to override:
|
|
|
|
- searchTerm: the string to use as a query
|
|
|
|
- searchProps: the props on each object to search
|
2018-04-23 23:18:06 +00:00
|
|
|
-- exactMatchSearchProps: the props for exact search when props are different per search type
|
|
|
|
-- regexSearchProps: the props for regex search when props are different per search type
|
|
|
|
-- fuzzySearchProps: the props for fuzzy search when props are different per search type
|
|
|
|
- exactMatchEnabled: (true) disable to not use the exact match search type
|
|
|
|
- fuzzySearchEnabled: (false) enable to use the fuzzy search type
|
|
|
|
- regexEnabled: (true) disable to disable the regex search type
|
2017-09-19 14:47:10 +00:00
|
|
|
- listToSearch: the list of objects to search
|
|
|
|
|
|
|
|
Properties provided:
|
2017-10-18 19:52:24 +00:00
|
|
|
- listSearched: a subset of listToSearch of items that meet the search criteria
|
2017-09-19 14:47:10 +00:00
|
|
|
*/
|
2020-06-09 21:03:28 +00:00
|
|
|
// eslint-disable-next-line ember/no-new-mixins
|
2017-09-19 14:47:10 +00:00
|
|
|
export default Mixin.create({
|
|
|
|
searchTerm: '',
|
2020-06-09 21:03:28 +00:00
|
|
|
listToSearch: computed(function() {
|
|
|
|
return [];
|
|
|
|
}),
|
2018-04-12 16:18:33 +00:00
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
searchProps: null,
|
2018-04-12 16:18:33 +00:00
|
|
|
exactMatchSearchProps: reads('searchProps'),
|
|
|
|
regexSearchProps: reads('searchProps'),
|
2018-04-23 23:18:06 +00:00
|
|
|
fuzzySearchProps: reads('searchProps'),
|
2018-04-12 16:18:33 +00:00
|
|
|
|
|
|
|
// Three search modes
|
|
|
|
exactMatchEnabled: true,
|
|
|
|
fuzzySearchEnabled: false,
|
2020-06-25 13:51:52 +00:00
|
|
|
includeFuzzySearchMatches: false,
|
2018-04-12 16:18:33 +00:00
|
|
|
regexEnabled: true,
|
|
|
|
|
2018-10-30 17:50:50 +00:00
|
|
|
// Search should reset pagination. Not every instance of
|
|
|
|
// search will be paired with pagination, but it's still
|
|
|
|
// preferable to generalize this rather than risking it being
|
|
|
|
// forgotten on a single page.
|
|
|
|
resetPagination() {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.currentPage != null) {
|
2018-10-30 17:50:50 +00:00
|
|
|
this.set('currentPage', 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
fuse: computed('fuzzySearchProps.[]', 'includeFuzzySearchMatches', 'listToSearch.[]', function() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return new Fuse(this.listToSearch, {
|
2018-04-12 16:18:33 +00:00
|
|
|
shouldSort: true,
|
2018-04-16 22:09:47 +00:00
|
|
|
threshold: 0.4,
|
2018-04-12 16:18:33 +00:00
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
2018-04-16 22:09:47 +00:00
|
|
|
tokenize: true,
|
|
|
|
matchAllTokens: true,
|
2018-04-12 16:18:33 +00:00
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
2020-06-25 13:51:52 +00:00
|
|
|
includeMatches: this.includeFuzzySearchMatches,
|
2019-03-26 07:46:44 +00:00
|
|
|
keys: this.fuzzySearchProps || [],
|
2018-04-12 16:18:33 +00:00
|
|
|
getFn(item, key) {
|
|
|
|
return get(item, key);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-04-23 23:18:06 +00:00
|
|
|
listSearched: computed(
|
|
|
|
'exactMatchEnabled',
|
|
|
|
'exactMatchSearchProps.[]',
|
2021-02-17 21:01:44 +00:00
|
|
|
'fuse',
|
|
|
|
'fuzzySearchEnabled',
|
2018-04-23 23:18:06 +00:00
|
|
|
'fuzzySearchProps.[]',
|
2021-02-17 21:01:44 +00:00
|
|
|
'includeFuzzySearchMatches',
|
|
|
|
'listToSearch.[]',
|
|
|
|
'regexEnabled',
|
2018-04-23 23:18:06 +00:00
|
|
|
'regexSearchProps.[]',
|
2021-02-17 21:01:44 +00:00
|
|
|
'searchTerm',
|
2018-04-23 23:18:06 +00:00
|
|
|
function() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const searchTerm = this.searchTerm.trim();
|
2018-05-05 03:38:55 +00:00
|
|
|
|
|
|
|
if (!searchTerm || !searchTerm.length) {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.listToSearch;
|
2018-04-12 16:18:33 +00:00
|
|
|
}
|
2018-05-05 03:38:55 +00:00
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.exactMatchEnabled) {
|
2018-05-05 03:38:55 +00:00
|
|
|
results.push(
|
2020-06-09 21:03:28 +00:00
|
|
|
...exactMatchSearch(searchTerm, this.listToSearch, this.exactMatchSearchProps)
|
2018-05-05 03:38:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.fuzzySearchEnabled) {
|
2020-06-25 13:51:52 +00:00
|
|
|
let fuseSearchResults = this.fuse.search(searchTerm);
|
|
|
|
|
|
|
|
if (this.includeFuzzySearchMatches) {
|
|
|
|
fuseSearchResults = fuseSearchResults.map(result => {
|
|
|
|
const item = result.item;
|
|
|
|
item.set('fuzzySearchMatches', result.matches);
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(...fuseSearchResults);
|
2018-05-05 03:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.regexEnabled) {
|
2020-06-09 21:03:28 +00:00
|
|
|
results.push(...regexSearch(searchTerm, this.listToSearch, this.regexSearchProps));
|
2018-05-05 03:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results.uniq();
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|
2018-04-23 23:18:06 +00:00
|
|
|
),
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
|
2018-04-12 16:18:33 +00:00
|
|
|
function exactMatchSearch(term, list, keys) {
|
|
|
|
if (term.length) {
|
|
|
|
return list.filter(item => keys.some(key => get(item, key) === term));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:45:12 +00:00
|
|
|
function regexSearch(term, list, keys) {
|
2017-10-18 19:52:24 +00:00
|
|
|
if (term.length) {
|
2017-09-19 14:47:10 +00:00
|
|
|
try {
|
2017-10-18 19:52:24 +00:00
|
|
|
const regex = new RegExp(term, 'i');
|
2017-09-19 14:47:10 +00:00
|
|
|
// Test the value of each key for each object against the regex
|
|
|
|
// All that match are returned.
|
|
|
|
return list.filter(item => keys.some(key => regex.test(get(item, key))));
|
|
|
|
} catch (e) {
|
|
|
|
// Swallow the error; most likely due to an eager search of an incomplete regex
|
|
|
|
}
|
2018-04-12 16:18:33 +00:00
|
|
|
return [];
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|
|
|
|
}
|