2020-12-01 15:45:09 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
2020-12-09 19:12:17 +00:00
|
|
|
import { computed, get, action } from '@ember/object';
|
|
|
|
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
|
|
|
|
|
|
|
get type() {
|
|
|
|
return this.args.type;
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:12:17 +00:00
|
|
|
@computed('term', 'args.search')
|
|
|
|
get searchTerm() {
|
|
|
|
return this.term || this.args.search || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
search(term) {
|
|
|
|
this.term = term;
|
|
|
|
return this.items;
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:14:30 +00:00
|
|
|
@computed('args.items', 'args.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
|
|
|
|
if (this.args.items.dispatchEvent === 'function') {
|
|
|
|
return this.args.items.content;
|
|
|
|
}
|
|
|
|
return this.args.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-09 19:12:17 +00:00
|
|
|
@computed('type', 'filtered', 'args.filters.searchproperties', 'searchTerm')
|
2020-12-01 15:45:09 +00:00
|
|
|
get searched() {
|
2020-12-09 19:12:17 +00:00
|
|
|
if (typeof this.searchTerm === '') {
|
2020-12-01 15:45:09 +00:00
|
|
|
return this.filtered;
|
|
|
|
}
|
2020-12-09 19:12:17 +00:00
|
|
|
const predicate = this.searchService.predicate(this.type);
|
2020-12-01 15:45:09 +00:00
|
|
|
const options = {};
|
2020-12-09 19:12:17 +00:00
|
|
|
if (typeof get(this, 'args.filters.searchproperties') !== 'undefined') {
|
2020-12-01 15:45:09 +00:00
|
|
|
options.properties = this.args.filters.searchproperties;
|
|
|
|
}
|
2020-12-09 19:12:17 +00:00
|
|
|
return this.filtered.filter(predicate(this.searchTerm, options));
|
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() {
|
|
|
|
if (typeof this.args.filters === 'undefined') {
|
2020-12-07 09:14:30 +00:00
|
|
|
return this.content;
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
const predicate = this.filter.predicate(this.type);
|
|
|
|
if (typeof predicate === 'undefined') {
|
2020-12-07 09:14:30 +00:00
|
|
|
return this.content;
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
2020-12-07 09:14:30 +00:00
|
|
|
return this.content.filter(predicate(this.args.filters));
|
2020-12-01 15:45:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 09:14:30 +00:00
|
|
|
@computed('type', 'args.sort')
|
2020-12-01 15:45:09 +00:00
|
|
|
get comparator() {
|
|
|
|
if (typeof this.args.sort === 'undefined') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return this.sort.comparator(this.type)(this.args.sort);
|
|
|
|
}
|
|
|
|
}
|