Don't show on select

This commit is contained in:
zandercymatics 2024-06-12 12:11:45 -06:00
parent 23ee19ba28
commit 2a9d8ce6ef
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 15 additions and 4 deletions

View file

@ -144,13 +144,13 @@ function openInNewTab(el, removeAttribute = false){
let investigatorSelect = document.querySelector("#id_investigator");
let assignSelfButton = document.querySelector("#investigator__assign_self");
if (investigatorSelect && assignSelfButton) {
let selector = django.jQuery(investigatorSelect)
assignSelfButton.addEventListener('click', function() {
let currentUserId = this.getAttribute("data-user-id");
let currentUserName = this.getAttribute("data-user-name");
if (currentUserId && currentUserName){
if (selector && currentUserId && currentUserName){
// Logic borrowed from here:
// https://select2.org/programmatic-control/add-select-clear-items#create-if-not-exists
let selector = django.jQuery(investigatorSelect)
// Set the value, creating a new option if necessary
if (selector.find(`option[value='${currentUserId}']`).length) {
selector.val(currentUserId).trigger("change");
@ -159,11 +159,22 @@ function openInNewTab(el, removeAttribute = false){
let currentUser = new Option(currentUserName, currentUserId, true, true);
// Append it to the select
selector.append(currentUser).trigger("change");
}
}
}else {
console.error("Could not assign current user.")
}
});
selector.on('change', function() {
let selectedValue = this.value;
if (selectedValue === "" || selectedValue === null) {
// If no investigator is selected, show the 'Assign to Self' button
assignSelfButton.parentElement.style.display = 'flex';
} else {
// If an investigator is selected, hide the 'Assign to Self' button
assignSelfButton.parentElement.style.display = 'none';
}
});
}
})();
/** An IIFE for pages in DjangoAdmin that use a clipboard button

View file

@ -174,7 +174,7 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% endif %}
</span>
</div>
{% elif field.field.name == "investigator" %}
{% elif field.field.name == "investigator" and not original_object.investigator %}
<div class="flex-container">
<label aria-label="Assign yourself as the investigator"></label>
<button id="investigator__assign_self"