Skip to content

Commit

Permalink
refactor select improver to build a dom element and then add it at th…
Browse files Browse the repository at this point in the history
…e end. This might be more performant but not meaningfully so
  • Loading branch information
adelikat committed Sep 3, 2023
1 parent 12d2067 commit 73cde0d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions TASVideos/wwwroot/js/select-improver.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
}

function engageSelectImprover(multiSelectId, maxHeight = '250px') {
let initialHtmlToAdd = `
const initialHtmlToAddElem = document.createElement('div');
initialHtmlToAddElem.innerHTML = `
<div id='${multiSelectId}_div' class='d-none border bg-body rounded-2 onclick-focusinput' style='cursor: text;'>
<div id='${multiSelectId}_buttons' class='onclick-focusinput px-2 pt-2 pb-1'>
<span class='text-body-tertiary onclick-focusinput px-1'>No selection</span>
Expand All @@ -41,16 +42,18 @@ function engageSelectImprover(multiSelectId, maxHeight = '250px') {
<div id='${multiSelectId}_list' class='list-group mt-1 overflow-auto border' style='max-height: ${maxHeight};'></div>
</div>
`;
let multiSelect = document.getElementById(multiSelectId);
const multiSelect = document.getElementById(multiSelectId);
multiSelect.classList.add('d-none');

// defensively deleting the improved html, if it already exists?
let div = document.getElementById(multiSelectId + '_div');
div?.remove();
multiSelect.insertAdjacentHTML('afterend', initialHtmlToAdd);
div = document.getElementById(multiSelectId + '_div');
let list = document.getElementById(multiSelectId + '_list');
let buttons = document.getElementById(multiSelectId + '_buttons');

div = initialHtmlToAddElem.querySelector(`#${multiSelectId}_div`);
let list = initialHtmlToAddElem.querySelector(`#${multiSelectId}_list`);
let buttons = initialHtmlToAddElem.querySelector(`#${multiSelectId}_buttons`);
div.classList.remove('d-none');
let input = document.getElementById(multiSelectId + '_input');
let input = initialHtmlToAddElem.querySelector(`#${multiSelectId}_input`);
let anyNotSelected = false;
for (var option of multiSelect.options) {
let entry = document.createElement('div');
Expand Down Expand Up @@ -142,4 +145,6 @@ function engageSelectImprover(multiSelectId, maxHeight = '250px') {
input.classList.add('d-none');
}
});

multiSelect.insertAdjacentHTML('afterend', initialHtmlToAddElem.innerHTML);
}

0 comments on commit 73cde0d

Please sign in to comment.