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';
|
2024-01-17 19:06:06 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2022-08-22 21:24:32 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2024-01-17 19:06:06 +03:00
|
|
|
const DEBOUNCE_MS = 200;
|
|
|
|
|
|
|
|
function mapResource(resource) {
|
|
|
|
return {
|
|
|
|
id: resource.id,
|
|
|
|
title: resource.title
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:24:32 +03:00
|
|
|
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() {
|
2024-01-17 19:06:06 +03:00
|
|
|
return this.args.searchField === undefined ? 'title' : this.args.searchField;
|
2022-10-21 18:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2024-01-17 19:06:06 +03:00
|
|
|
if (newOptions.length === 0) {
|
|
|
|
// Do a query lookup
|
|
|
|
newOptions = yield this.fetchOptionsForSearchTask.perform(term);
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:18:00 +03:00
|
|
|
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 [];
|
2024-01-17 19:06:06 +03:00
|
|
|
|
2022-09-28 18:14:32 +03:00
|
|
|
if (this.args.type === 'email') {
|
2024-01-17 19:06:06 +03:00
|
|
|
const posts = yield this.store.query('post', {filter: '(status:published,status:sent)+newsletter_id:-null', limit: '25', fields: 'id,title'});
|
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
|
|
|
|
2024-01-17 19:06:06 +03:00
|
|
|
const posts = yield this.store.query('post', {filter: 'status:published', limit: '25', fields: 'id,title'});
|
|
|
|
const pages = yield this.store.query('page', {filter: 'status:published', limit: '25', fields: 'id,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;
|
|
|
|
}
|
2024-01-17 19:06:06 +03:00
|
|
|
|
|
|
|
@task({restartable: true})
|
|
|
|
*fetchOptionsForSearchTask(searchTerm) {
|
|
|
|
// Debounce
|
|
|
|
yield timeout(DEBOUNCE_MS);
|
|
|
|
|
|
|
|
const options = yield [];
|
|
|
|
|
|
|
|
if (this.args.type === 'email') {
|
|
|
|
const posts = yield this.store.query('post', {filter: '(status:published,status:sent)+newsletter_id:-null+title:~\'' + searchTerm.replace('\'', '\\\'') + '\'', limit: '10', fields: 'id,title'});
|
|
|
|
options.push(...posts.map(mapResource));
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
const posts = yield this.store.query('post', {filter: 'status:published+title:~\'' + searchTerm.replace('\'', '\\\'') + '\'', limit: '10', fields: 'id,title'});
|
|
|
|
const pages = yield this.store.query('page', {filter: 'status:published+title:~\'' + searchTerm.replace('\'', '\\\'') + '\'', limit: '10', fields: 'id,title'});
|
|
|
|
|
|
|
|
if (posts.length > 0) {
|
|
|
|
options.push(...posts.map(mapResource));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pages.length > 0) {
|
|
|
|
options.push(...pages.map(mapResource));
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
2022-08-22 21:24:32 +03:00
|
|
|
}
|