Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/lib/common/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@
/** @type {boolean} */
let loading = false;

/**
* @type {number | undefined}
*/
let timer;

onMount(() => {
initOptions();
});

$: {
innerOptions = verifySelectedOptions(innerOptions, selectedValues);
refOptions = verifySelectedOptions(innerOptions, selectedValues);
applySearchFilter();
changeDisplayText();
}

$: {
Expand All @@ -105,36 +108,39 @@
...newOptions
];

changeDisplayText();
} else {
applySearchFilter();
changeDisplayText();
}
} else {
innerOptions = verifySelectedOptions(options, selectedValues);
refOptions = verifySelectedOptions(options, selectedValues);
}
}

$: {
if (innerOptions && refOptions) {
applySearchFilter();
changeDisplayText();
}
}


function initOptions() {
innerOptions = options.map(x => {
const newInnerOptions = options.map(x => {
return {
label: x.label,
value: x.value,
checked: false
}
});

refOptions = options.map(x => {
const newRefOptions = options.map(x => {
return {
label: x.label,
value: x.value,
checked: false
}
});

innerOptions = newInnerOptions;
refOptions = newRefOptions;
}

/**
Expand Down Expand Up @@ -167,8 +173,15 @@
/** @param {any} e */
function changeSearchValue(e) {
searchValue = e.target.value || '';
applySearchFilter();
verifySelectAll();

if (timer) {
clearTimeout(timer);
}

timer = setTimeout(() => {
applySearchFilter();
verifySelectAll();
}, 500);
}

function applySearchFilter() {
Expand All @@ -186,7 +199,7 @@
* @param {any} option
*/
function checkOption(e, option) {
innerOptions = innerOptions.map(x => {
const newInnerOptions = innerOptions.map(x => {
const item = { ...x };
if (item.value == option.value) {
item.checked = e == null ? !item.checked : e.target.checked;
Expand All @@ -196,7 +209,7 @@
return item;
});

refOptions = refOptions.map(x => {
const newRefOptions = refOptions.map(x => {
const item = { ...x };
if (item.value == option.value) {
item.checked = e == null ? !item.checked : e.target.checked;
Expand All @@ -206,7 +219,9 @@
return item;
});

changeDisplayText();
innerOptions = newInnerOptions;
refOptions = newRefOptions;

sendEvent();
hideOptionList();
}
Expand All @@ -219,7 +234,6 @@
});

syncChangesToRef(selectAllChecked);
changeDisplayText();
sendEvent();
}

Expand Down Expand Up @@ -320,15 +334,17 @@
}

function clearSelection() {
innerOptions = innerOptions.map(x => {
const newInnerOptions = innerOptions.map(x => {
return { ...x, checked: false }
});

refOptions = refOptions.map(x => {
const newRefOptions = refOptions.map(x => {
return { ...x, checked: false }
});

changeDisplayText();
innerOptions = newInnerOptions;
refOptions = newRefOptions;

sendEvent();
hideOptionList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
let selectedTemplate = null;

$: {
collectAgentOptions(agents);
initAgentOptions(agents);
}

/**
* @param {import('$agentTypes').AgentModel[]} agents
*/
function collectAgentOptions(agents) {
function initAgentOptions(agents) {
agentOptions = [];
templateOptions = [];
selectedAgent = null;
selectedTemplate = null;

agentOptions = agents?.map(x => ({
label: x.name,
value: x.id
Expand Down