|
11 | 11 | import { removeDuplicatedItems } from '$lib/common/component_utilities'; |
12 | 12 | import StandardDismissableAlert from '../../common/StandardDismissableAlert.svelte'; |
13 | 13 | import TimestampCell from '../../jobs/TimestampCell.svelte'; |
14 | | - import { setSlimSelect, setSlimSelectOptions } from '$lib/common/slim_select'; |
| 14 | + import SlimSelect from 'slim-select'; |
15 | 15 |
|
16 | 16 | /** @type {() => Promise<import('$lib/types').ApplyWorkflow[]>} */ |
17 | 17 | export let jobUpdater; |
|
44 | 44 | let rows = tableHandler.getRows(); |
45 | 45 |
|
46 | 46 | // Selectors |
47 | | - /** @type {import('slim-select').default|undefined} */ |
| 47 | + /** @type {SlimSelect|undefined} */ |
48 | 48 | let statusSelect; |
49 | | - /** @type {import('slim-select').default|undefined} */ |
| 49 | + /** @type {SlimSelect|undefined} */ |
50 | 50 | let projectSelect; |
51 | | - /** @type {import('slim-select').default|undefined} */ |
| 51 | + /** @type {SlimSelect|undefined} */ |
52 | 52 | let workflowSelect; |
53 | | - /** @type {import('slim-select').default|undefined} */ |
| 53 | + /** @type {SlimSelect|undefined} */ |
54 | 54 | let inputDatasetSelect; |
55 | | - /** @type {import('slim-select').default|undefined} */ |
| 55 | + /** @type {SlimSelect|undefined} */ |
56 | 56 | let outputDatasetSelect; |
57 | 57 |
|
58 | 58 | // Filters |
|
206 | 206 | ); |
207 | 207 | }); |
208 | 208 |
|
| 209 | + /** |
| 210 | + * Initializes slim-select dropdown on a given HTML element. |
| 211 | + * @param {string} id id of HTML element where slim-select has to be configured |
| 212 | + * @param {Array<{ name: string, id: number|string }>} values |
| 213 | + * @param {(value: string) => void} setter function executed when a dropdown value is selected |
| 214 | + * @param {boolean} showSearch |
| 215 | + * @returns the SlimSelect instance |
| 216 | + */ |
| 217 | + function setSlimSelect(id, values, setter, showSearch = true) { |
| 218 | + if (!values) { |
| 219 | + return; |
| 220 | + } |
| 221 | + const selectElement = document.getElementById(id); |
| 222 | + if (!selectElement) { |
| 223 | + return; |
| 224 | + } |
| 225 | + selectElement.classList.remove('invisible'); |
| 226 | + const select = new SlimSelect({ |
| 227 | + select: `#${id}`, |
| 228 | + settings: { |
| 229 | + showSearch, |
| 230 | + allowDeselect: true |
| 231 | + }, |
| 232 | + events: { |
| 233 | + afterChange: (selection) => { |
| 234 | + const selectedOption = selection[0]; |
| 235 | + if (!selectedOption || selectedOption.placeholder) { |
| 236 | + setter(''); |
| 237 | + } else { |
| 238 | + setter(selectedOption.value); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + }); |
| 243 | + setSlimSelectOptions(select, values); |
| 244 | + return select; |
| 245 | + } |
| 246 | +
|
209 | 247 | /** |
210 | 248 | * Rebuilds valid slim-select options according to the visible rows. |
211 | 249 | * Example: if a project filter is selected the user can select only the workflows belonging to that project. |
|
229 | 267 | /** |
230 | 268 | * Updates the available options only when needed, to avoid unsetting the selected value when the desired list of |
231 | 269 | * options is already set (e.g. if we change the selected dataset we don't want to change the selected workflow). |
232 | | - * @param {import('slim-select').default|undefined} select |
| 270 | + * @param {SlimSelect|undefined} select |
233 | 271 | * @param {{id: number, name: string}[]} validValues |
234 | 272 | */ |
235 | 273 | function setValidSlimSelectOptions(select, validValues) { |
|
242 | 280 | } |
243 | 281 | } |
244 | 282 |
|
| 283 | + /** |
| 284 | + * Updates SlimSelect options. This rebuilds the HTML elements and unset the selected value. |
| 285 | + * @param {SlimSelect|undefined} select |
| 286 | + * @param {Array<{ name: string, id: number|string }>} values |
| 287 | + */ |
| 288 | + function setSlimSelectOptions(select, values) { |
| 289 | + if (!select) { |
| 290 | + return; |
| 291 | + } |
| 292 | + const options = values.map((p) => ({ text: p.name, value: p.id.toString() })); |
| 293 | + select.setData([{ text: 'All', placeholder: true }, ...options]); |
| 294 | + } |
| 295 | +
|
245 | 296 | onDestroy(() => { |
246 | 297 | clearTimeout(updateJobsTimeout); |
247 | 298 | }); |
|
0 commit comments