This commit is contained in:
zandercymatics 2024-06-11 16:06:44 -06:00
parent 6108705b99
commit b0b788ae77
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 76 additions and 0 deletions

View file

@ -137,6 +137,72 @@ function openInNewTab(el, removeAttribute = false){
prepareDjangoAdmin();
})();
/** An IIFE for pages in DjangoAdmin that use a clipboard button
*/
(function (){
let investigatorSelect = document.querySelector("#id_investigator");
let assignSelfButton = document.querySelector("#investigator__assign_self");
if (investigatorSelect && assignSelfButton) {
assignSelfButton.addEventListener('click', function() {
let currentUserId = this.getAttribute("data-user-id");
let select2Container = investigatorSelect.nextElementSibling.querySelector(".select2-selection");
// Log the Select2 container to verify it's the correct element
console.log(select2Container);
// Check if the Select2 container exists and trigger a click
if (select2Container) {
// Create and dispatch a mouse event to mimic user interaction
var event = new MouseEvent('mousedown', {
'view': window,
'bubbles': true,
'cancelable': true
});
select2Container.dispatchEvent(event);
}
// Wait for the dropdown to open and the search field to be visible
let searchField = document.querySelector(".select2-search__field");
if (searchField) {
searchField.value = "Zander Adkinson"; // Set the value you want to search for
// Trigger input event to filter results based on the entered value
var inputEvent = new Event('input', {
'bubbles': true,
'cancelable': true
});
searchField.dispatchEvent(inputEvent);
}
let observer = new MutationObserver(function(mutations) {
mutations.forEach(mutation => {
if (mutation.addedNodes.length) {
let options = document.querySelectorAll("#select2-id_investigator-results .select2-results__option");
options.forEach(option => {
if (option.innerText.trim() === "Zander Adkinson zander.adkinson@ecstech.com") {
option.dispatchEvent(new MouseEvent('mouseup', { 'bubbles': true, 'cancelable': true }));
console.log("Option with the desired text has been selected.");
observer.disconnect(); // Stop observing after the desired action
}
});
}
});
});
let resultsContainer = document.querySelector("#select2-id_investigator-results");
if (resultsContainer) {
observer.observe(resultsContainer, {
childList: true,
subtree: true
});
}
// options are here: select2-results__options
});
}
})();
/** An IIFE for pages in DjangoAdmin that use a clipboard button
*/
(function (){

View file

@ -174,5 +174,15 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% endif %}
</span>
</div>
{% elif field.field.name == "investigator" %}
<div class="flex-container">
<label aria-label="Assign yourself as the investigator"></label>
<button id="investigator__assign_self" data-user-id="{{ request.user.id }}" type="button" class="usa-button usa-button--unstyled margin-top-2 margin-bottom-1 margin-left-1 usa-button__small-text text-no-underline">
<svg class="usa-icon" aria-hidden="true" focusable="false" role="img" width="24" height="24">
<use xlink:href="/public/img/sprite.svg#person"></use>
</svg>
<span>Assign to me</span>
</button>
</div>
{% endif %}
{% endblock after_help_text %}