mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-13 21:19:42 +02:00
Simplify js and add comments
This commit is contained in:
parent
32c5a5a325
commit
1fe971c7ff
1 changed files with 33 additions and 34 deletions
|
@ -138,44 +138,43 @@ function openInNewTab(el, removeAttribute = false){
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
/** An IIFE for pages in DjangoAdmin that use a clipboard button
|
/** An IIFE for the "Assign to me" button under the investigator field in DomainRequests.
|
||||||
|
** This field uses the "select2" selector, rather than the default.
|
||||||
|
** To perform data operations on this - we need to use jQuery rather than vanilla js.
|
||||||
*/
|
*/
|
||||||
(function (){
|
(function (){
|
||||||
let investigatorSelect = document.querySelector("#id_investigator");
|
let selector = django.jQuery("#id_investigator")
|
||||||
let assignSelfButton = document.querySelector("#investigator__assign_self");
|
let assignSelfButton = document.querySelector("#investigator__assign_self");
|
||||||
if (investigatorSelect && assignSelfButton) {
|
if (!selector || !assignSelfButton) {
|
||||||
let selector = django.jQuery(investigatorSelect)
|
return;
|
||||||
assignSelfButton.addEventListener('click', function() {
|
}
|
||||||
let currentUserId = this.getAttribute("data-user-id");
|
|
||||||
let currentUserName = this.getAttribute("data-user-name");
|
let currentUserId = assignSelfButton.getAttribute("data-user-id");
|
||||||
if (selector && currentUserId && currentUserName){
|
let currentUserName = assignSelfButton.getAttribute("data-user-name");
|
||||||
// Logic borrowed from here:
|
if (!currentUserId || !currentUserName){
|
||||||
// https://select2.org/programmatic-control/add-select-clear-items#create-if-not-exists
|
console.error("Could not assign current user: no values found.")
|
||||||
// Set the value, creating a new option if necessary
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hook a click listener to the "Assign to me" button.
|
||||||
|
// Logic borrowed from here: https://select2.org/programmatic-control/add-select-clear-items#create-if-not-exists
|
||||||
|
assignSelfButton.addEventListener("click", function() {
|
||||||
if (selector.find(`option[value='${currentUserId}']`).length) {
|
if (selector.find(`option[value='${currentUserId}']`).length) {
|
||||||
|
// Select the value that is associated with the current user.
|
||||||
selector.val(currentUserId).trigger("change");
|
selector.val(currentUserId).trigger("change");
|
||||||
} else {
|
} else {
|
||||||
// Create a DOM Option and pre-select by default
|
// Create a DOM Option that matches the desired user. Then append it and select it.
|
||||||
let currentUser = new Option(currentUserName, currentUserId, true, true);
|
let userOption = new Option(currentUserName, currentUserId, true, true);
|
||||||
// Append it to the select
|
selector.append(userOption).trigger("change");
|
||||||
selector.append(currentUser).trigger("change");
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
console.error("Could not assign current user.")
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Listen to any change events, and hide the parent container if investigator has a value.
|
||||||
selector.on('change', function() {
|
selector.on('change', function() {
|
||||||
let selectedValue = this.value;
|
// The parent container has display type flex.
|
||||||
if (selectedValue === "" || selectedValue === null) {
|
assignSelfButton.parentElement.style.display = this.value ? "none" : "flex";
|
||||||
// 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
|
/** An IIFE for pages in DjangoAdmin that use a clipboard button
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue