-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfetch-select.js
More file actions
258 lines (216 loc) · 6.85 KB
/
fetch-select.js
File metadata and controls
258 lines (216 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { isEmpty } from '@ember/utils';
import { action, set } from '@ember/object';
import { isArray } from '@ember/array';
import { assert } from '@ember/debug';
import { timeout, restartableTask } from 'ember-concurrency';
/**
* FetchSelectComponent is a Glimmer component responsible for rendering a
* select input and fetching options asynchronously based on user input.
*
* @class FetchSelectComponent
* @extends Component
* @memberof FleetbaseComponents
*
* @property {Service} fetch - The fetch service injected into the component.
* @property {Array} options - The list of selectable options.
* @property {Object} selected - The currently selected option.
* @property {number} debounceDuration - The duration to debounce the search input, in milliseconds.
*/
export default class FetchSelectComponent extends Component {
/**
* The fetch service is used to make network requests to fetch the options for the select input.
* @type {Service}
*/
@service fetch;
/**
* The list of selectable options.
* @type {Array}
*/
@tracked options = [];
/**
* The currently selected option.
* @type {Object}
*/
@tracked selected;
/**
* The power select API.
* @type {Object}
*/
@tracked api;
/**
* The duration to debounce the search input, in milliseconds.
* @type {number}
*/
@tracked debounceDuration = 250;
/**
* The constructor ensures that the endpoint argument is specified, and
* initializes the component's properties based on the arguments passed to it.
*/
constructor() {
super(...arguments);
assert('<FetchSelect /> requires a valid `endpoint`.', !isEmpty(this.args.endpoint));
this.endpoint = this.args.endpoint;
this.selected = this.setSelectedOption(this.args.selected);
// this.debounceDuration = this.args.debounceDuration || this.debounceDuration;
}
/**
* Searches for options based on the term provided. Debounces the search
* if it's not the initial load.
*
* @param {string} term - The search term.
* @param {Object} [options={}] - Additional options for the search.
* @param {boolean} [initialLoad=false] - Whether this is the initial load.
* @task
*/
@restartableTask() searchOptions = function* (term, options = {}, initialLoad = false) {
if (!initialLoad) {
yield timeout(this.debounceDuration);
}
yield this.fetchOptions.perform(term, options);
};
/**
* Fetches options based on the term provided.
*
* @param {string} term - The search term.
* @param {Object} [options={}] - Additional options for the fetch.
* @task
*/
@restartableTask() fetchOptions = function* (term, options = {}) {
// query might be an EmptyObject/{{hash}}, make it a normal Object
const query = Object.assign({}, this.args.query);
if (term) {
set(query, 'query', term);
}
let _options = yield this.fetch.get(this.endpoint, query, options);
// if options returns is an object and not array
if (this.isFetchResponseObject(_options)) {
_options = this.convertOptionsObjectToArray(_options);
}
// set options
this.options = _options;
return _options;
};
convertOptionsObjectToArray(_options) {
const objectKeys = Object.keys(_options);
const _optionsFromObject = [];
objectKeys.forEach((key) => {
_optionsFromObject.pushObject({
key,
value: _options[key],
});
});
return _optionsFromObject;
}
isFetchResponseObject(_options) {
return !isArray(_options) && typeof _options === 'object' && Object.keys(_options).length;
}
/**
* Set the selected option.
*
* @param {*} selected
* @memberof FetchSelectComponent
*/
setSelectedOption(selected) {
const { optionValue } = this.args;
if (optionValue) {
this.fetchOptions.perform().then((options) => {
let foundSelected = null;
if (isArray(options)) {
foundSelected = options.find((option) => option[optionValue] === selected);
}
if (foundSelected) {
this.select(foundSelected);
} else {
this.select(selected);
}
});
} else {
this.select(selected);
}
}
select(option) {
this.selected = option;
// set via api
if (this.api && this.api.actions && typeof this.api.actions.select === 'function') {
this.api.actions.select(option);
}
}
/**
* Loads the default set of options.
*/
loadDefaultOptions() {
const { loadDefaultOptions } = this.args;
if (loadDefaultOptions === undefined || loadDefaultOptions) {
this.fetchOptions.perform(null, {}, true);
}
}
/**
* Called when the select input is opened.
* @action
*/
@action onOpen() {
const { onOpen } = this.args;
this.loadDefaultOptions();
if (typeof onOpen === 'function') {
onOpen(...arguments);
}
}
/**
* Called when the user inputs a search term.
*
* @param {string} term - The search term.
* @action
*/
@action onInput(term) {
const { onInput } = this.args;
if (isEmpty(term)) {
this.loadDefaultOptions();
}
if (typeof onInput === 'function') {
onInput(...arguments);
}
}
/**
* Called when an option is selected.
*
* @param {Object} option - The selected option.
* @action
*/
@action onChange(option, ...rest) {
const { onChange, optionValue } = this.args;
// set selected
this.selected = option;
// if option value supplied
if (optionValue && typeof option === 'object') {
option = option[optionValue];
}
if (typeof onChange === 'function') {
onChange(option, ...rest);
}
}
/**
* Called when the select input is closed.
* @action
*/
@action onClose() {
const { onClose } = this.args;
this.fetchOptions.cancelAll();
if (typeof onClose === 'function') {
onClose(...arguments);
}
}
/**
* Register the power select API
*
* @memberof FetchSelectComponent
*/
@action registerAPI(api) {
this.api = api;
if (typeof this.args.registerAPI === 'function') {
this.args.registerAPI(...arguments);
}
}
}