2020-12-01 15:45:09 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
2021-01-25 18:13:54 +00:00
|
|
|
import { computed, get, action } from '@ember/object';
|
2020-12-18 10:38:15 +00:00
|
|
|
import { alias } from '@ember/object/computed';
|
2020-12-09 19:12:17 +00:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
2020-12-01 15:45:09 +00:00
|
|
|
import { sort } from '@ember/object/computed';
|
|
|
|
import { defineProperty } from '@ember/object';
|
|
|
|
|
|
|
|
export default class DataCollectionComponent extends Component {
|
|
|
|
@service('filter') filter;
|
|
|
|
@service('sort') sort;
|
2020-12-09 19:12:17 +00:00
|
|
|
@service('search') searchService;
|
|
|
|
|
|
|
|
@tracked term = '';
|
2020-12-01 15:45:09 +00:00
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
@alias('searchService.searchables') searchableMap;
|
|
|
|
|
2020-12-01 15:45:09 +00:00
|
|
|
get type() {
|
|
|
|
return this.args.type;
|
|
|
|
}
|
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
get searchMethod() {
|
|
|
|
return this.args.searchable || 'exact';
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchProperties() {
|
|
|
|
return this.args.filters.searchproperties;
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:12:17 +00:00
|
|
|
@computed('term', 'args.search')
|
|
|
|
get searchTerm() {
|
|
|
|
return this.term || this.args.search || '';
|
|
|
|
}
|
|
|
|
|
2021-01-25 18:13:54 +00:00
|
|
|
@computed('type', 'searchMethod', 'filtered', 'args.filters')
|
2020-12-18 10:38:15 +00:00
|
|
|
get searchable() {
|
2021-01-25 18:13:54 +00:00
|
|
|
const searchproperties =
|
|
|
|
get(this, 'args.filters.searchproperty.value') || get(this, 'args.filters.searchproperty');
|
2020-12-18 10:38:15 +00:00
|
|
|
const Searchable =
|
|
|
|
typeof this.searchMethod === 'string'
|
|
|
|
? this.searchableMap[this.searchMethod]
|
|
|
|
: this.args.searchable;
|
2021-01-25 18:13:54 +00:00
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
return new Searchable(this.filtered, {
|
|
|
|
finders: Object.fromEntries(
|
|
|
|
Object.entries(this.searchService.predicate(this.type)).filter(([key, value]) => {
|
2021-01-25 18:13:54 +00:00
|
|
|
return typeof searchproperties === 'undefined' ? true : searchproperties.includes(key);
|
2020-12-18 10:38:15 +00:00
|
|
|
})
|
|
|
|
),
|
|
|
|
});
|
2020-12-09 19:12:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
@computed('type', 'args.sort')
|
|
|
|
get comparator() {
|
|
|
|
if (typeof this.args.sort === 'undefined') {
|
|
|
|
return [];
|
2020-12-07 09:14:30 +00:00
|
|
|
}
|
2020-12-18 10:38:15 +00:00
|
|
|
return this.sort.comparator(this.type)(this.args.sort);
|
2020-12-07 09:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('comparator', 'searched')
|
2020-12-01 15:45:09 +00:00
|
|
|
get items() {
|
|
|
|
// the ember sort computed accepts either:
|
|
|
|
// 1. The name of a property (as a string) returning an array properties to sort by
|
|
|
|
// 2. A function to use for sorting
|
|
|
|
let comparator = 'comparator';
|
|
|
|
if (typeof this.comparator === 'function') {
|
|
|
|
comparator = this.comparator;
|
|
|
|
}
|
|
|
|
defineProperty(this, 'sorted', sort('searched', comparator));
|
|
|
|
return this.sorted;
|
|
|
|
}
|
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
@computed('searchTerm', 'searchable', 'filtered')
|
2020-12-01 15:45:09 +00:00
|
|
|
get searched() {
|
2020-12-14 15:28:35 +00:00
|
|
|
if (this.searchTerm === '') {
|
2020-12-01 15:45:09 +00:00
|
|
|
return this.filtered;
|
|
|
|
}
|
2020-12-18 10:38:15 +00:00
|
|
|
return this.searchable.search(this.searchTerm);
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 09:14:30 +00:00
|
|
|
@computed('type', 'content', 'args.filters')
|
2020-12-01 15:45:09 +00:00
|
|
|
get filtered() {
|
2020-12-18 10:38:15 +00:00
|
|
|
// if we don't filter, return a copy of the content so we end up with what
|
|
|
|
// filter will return when filtering ED recordsets
|
2020-12-01 15:45:09 +00:00
|
|
|
if (typeof this.args.filters === 'undefined') {
|
2020-12-18 10:38:15 +00:00
|
|
|
return this.content.slice();
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
const predicate = this.filter.predicate(this.type);
|
|
|
|
if (typeof predicate === 'undefined') {
|
2020-12-18 10:38:15 +00:00
|
|
|
return this.content.slice();
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
2021-01-25 18:13:54 +00:00
|
|
|
const filters = Object.entries(this.args.filters)
|
|
|
|
.filter(([key, value]) => Boolean(value))
|
|
|
|
.map(([key, value]) => {
|
|
|
|
const val = typeof value !== 'string' ? value.value : value;
|
|
|
|
return [key, val];
|
|
|
|
});
|
|
|
|
return this.content.filter(predicate(Object.fromEntries(filters)));
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
@computed('args.{items.[],items.content.[]}')
|
|
|
|
get content() {
|
|
|
|
// TODO: Temporary little hack to ensure we detect DataSource proxy
|
|
|
|
// objects but not any other special Ember Proxy object like ember-data
|
|
|
|
// things. Remove this once we no longer need the Proxies
|
2021-01-29 15:53:28 +00:00
|
|
|
const items = this.args.items || [];
|
|
|
|
if (typeof items.dispatchEvent === 'function') {
|
|
|
|
return items.content;
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
2021-01-29 15:53:28 +00:00
|
|
|
return items;
|
2020-12-18 10:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
search(term) {
|
|
|
|
this.term = term;
|
|
|
|
return this.items;
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
}
|