2022-08-22 21:24:32 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-10-21 18:18:00 +03:00
|
|
|
import {A} from '@ember/array';
|
|
|
|
import {action, get} from '@ember/object';
|
|
|
|
import {
|
|
|
|
defaultMatcher,
|
|
|
|
filterOptions
|
|
|
|
} from 'ember-power-select/utils/group-utils';
|
2022-08-22 21:24:32 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class GhResourceSelect extends Component {
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
@tracked _options = [];
|
|
|
|
|
|
|
|
get renderInPlace() {
|
|
|
|
return this.args.renderInPlace === undefined ? false : this.args.renderInPlace;
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:18:00 +03:00
|
|
|
get searchField() {
|
|
|
|
return this.args.searchField === undefined ? 'name' : this.args.searchField;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
searchAndSuggest(term, select) {
|
|
|
|
return this.searchAndSuggestTask.perform(term, select);
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*searchAndSuggestTask(term) {
|
|
|
|
let newOptions = this.flatOptions.toArray();
|
|
|
|
|
|
|
|
if (term.length === 0) {
|
|
|
|
return newOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: we can do actual filtering on posts here (allow searching when we have lots and lots of posts)
|
|
|
|
yield undefined;
|
|
|
|
|
|
|
|
newOptions = this._filter(A(newOptions), term);
|
|
|
|
|
|
|
|
return newOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
get matcher() {
|
|
|
|
return this.args.matcher || defaultMatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
_filter(options, searchText) {
|
|
|
|
let matcher;
|
|
|
|
if (this.searchField) {
|
|
|
|
matcher = (option, text) => this.matcher(get(option, this.searchField), text);
|
|
|
|
} else {
|
|
|
|
matcher = (option, text) => this.matcher(option, text);
|
|
|
|
}
|
|
|
|
return filterOptions(options || [], searchText, matcher);
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:24:32 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.fetchOptionsTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
get options() {
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
get flatOptions() {
|
|
|
|
const options = [];
|
|
|
|
|
|
|
|
function getOptions(option) {
|
|
|
|
if (option.options) {
|
|
|
|
return option.options.forEach(getOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.push(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._options.forEach(getOptions);
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:18:00 +03:00
|
|
|
get selectedOption() {
|
|
|
|
if (this.args.resource.title) {
|
|
|
|
return this.args.resource;
|
|
|
|
}
|
|
|
|
const resource = this.args.resource ?? {};
|
|
|
|
return this.flatOptions.find(option => resource.id === option.id);
|
2022-08-22 21:24:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onChange(options) {
|
|
|
|
this.args.onChange(options);
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:14:32 +03:00
|
|
|
get placeholderText() {
|
|
|
|
if (this.args.type === 'email') {
|
|
|
|
return 'Select an email';
|
|
|
|
}
|
|
|
|
return 'Select a page/post';
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:18:00 +03:00
|
|
|
get searchPlaceholderText() {
|
|
|
|
if (this.args.type === 'email') {
|
|
|
|
return 'Search emails';
|
|
|
|
}
|
|
|
|
return 'Search posts/pages';
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:24:32 +03:00
|
|
|
@task
|
|
|
|
*fetchOptionsTask() {
|
|
|
|
const options = yield [];
|
2022-09-28 18:14:32 +03:00
|
|
|
|
|
|
|
if (this.args.type === 'email') {
|
|
|
|
const posts = yield this.store.query('post', {filter: '(status:published,status:sent)+newsletter_id:-null', limit: 'all'});
|
2022-10-21 18:18:00 +03:00
|
|
|
options.push(...posts.map(mapResource));
|
2022-09-28 18:14:32 +03:00
|
|
|
this._options = options;
|
|
|
|
return;
|
|
|
|
}
|
2022-08-22 21:24:32 +03:00
|
|
|
|
|
|
|
const posts = yield this.store.query('post', {filter: 'status:published', limit: 'all'});
|
|
|
|
const pages = yield this.store.query('page', {filter: 'status:published', limit: 'all'});
|
|
|
|
|
|
|
|
function mapResource(resource) {
|
|
|
|
return {
|
2022-10-21 18:18:00 +03:00
|
|
|
id: resource.id,
|
|
|
|
title: resource.title
|
2022-08-22 21:24:32 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (posts.length > 0) {
|
|
|
|
options.push({
|
|
|
|
groupName: 'Posts',
|
|
|
|
options: posts.map(mapResource)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pages.length > 0) {
|
|
|
|
options.push({
|
|
|
|
groupName: 'Pages',
|
|
|
|
options: pages.map(mapResource)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
}
|