2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-10-08 11:33:55 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
|
|
|
|
export default class SearchProvider extends Component {
|
2022-10-10 07:50:00 +00:00
|
|
|
// custom base route / router abstraction is doing weird things
|
|
|
|
get _search() {
|
|
|
|
return this.args.search || '';
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:33:55 +00:00
|
|
|
get items() {
|
2022-10-10 07:50:00 +00:00
|
|
|
const { items, searchProperties } = this.args;
|
|
|
|
const { _search: search } = this;
|
2022-10-08 11:33:55 +00:00
|
|
|
|
2022-10-09 11:00:22 +00:00
|
|
|
if (search.length > 0) {
|
|
|
|
return items.filter((item) => {
|
|
|
|
const matchesInSearchProperties = searchProperties.reduce((acc, searchProperty) => {
|
2022-10-11 11:04:33 +00:00
|
|
|
const match = item[searchProperty].indexOf(search) !== -1;
|
2022-10-09 11:00:22 +00:00
|
|
|
if (match) {
|
|
|
|
return [...acc, match];
|
|
|
|
} else {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
return matchesInSearchProperties.length > 0;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return items;
|
|
|
|
}
|
2022-10-08 11:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
const { items } = this;
|
|
|
|
return {
|
|
|
|
items,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|