From 9b92a541f842ebb144c2208ae4999b01916757a0 Mon Sep 17 00:00:00 2001 From: Thomas Scherz Date: Wed, 17 Jul 2024 15:30:54 -0400 Subject: [PATCH] Refactors javascript assets. --- app/assets/javascripts/filtermanagement.js | 17 +++++ app/assets/javascripts/inputsanitization.js | 4 ++ .../{custom.js => multivalueinputs.js} | 71 +++++-------------- app/assets/javascripts/navigation.js | 11 +++ app/views/layouts/application.html.erb | 5 +- app/views/layouts/software_records.html.erb | 5 +- config/initializers/assets.rb | 5 +- 7 files changed, 60 insertions(+), 58 deletions(-) create mode 100644 app/assets/javascripts/filtermanagement.js create mode 100644 app/assets/javascripts/inputsanitization.js rename app/assets/javascripts/{custom.js => multivalueinputs.js} (60%) create mode 100644 app/assets/javascripts/navigation.js diff --git a/app/assets/javascripts/filtermanagement.js b/app/assets/javascripts/filtermanagement.js new file mode 100644 index 0000000..fa499f0 --- /dev/null +++ b/app/assets/javascripts/filtermanagement.js @@ -0,0 +1,17 @@ +// Specific to software_records.html.erb +function clearFilters() { + document.getElementById("vendor-record-filter").style.display = "none"; + document.getElementById("software-type-filter").style.display = "none"; + var loaded = window.location.host; + window.location = "software_records"; +} + +function handleRadio(myRadio) { + if(myRadio.value === "vendor_records") { + document.getElementById("vendor-record-filter").style.display = "block"; + document.getElementById("software-type-filter").style.display = "none"; + } else { + document.getElementById("vendor-record-filter").style.display = "none"; + document.getElementById("software-type-filter").style.display = "block"; + } +} diff --git a/app/assets/javascripts/inputsanitization.js b/app/assets/javascripts/inputsanitization.js new file mode 100644 index 0000000..3cb7bc4 --- /dev/null +++ b/app/assets/javascripts/inputsanitization.js @@ -0,0 +1,4 @@ +var createdbyfield = document.getElementsByClassName('regex-createdby')[0]; +createdbyfield.onkeyup = function() { + createdbyfield.value = createdbyfield.value.replace(/[^a-zA-Z0-9 ]/, ''); +} diff --git a/app/assets/javascripts/custom.js b/app/assets/javascripts/multivalueinputs.js similarity index 60% rename from app/assets/javascripts/custom.js rename to app/assets/javascripts/multivalueinputs.js index 60a7a86..7a671b2 100644 --- a/app/assets/javascripts/custom.js +++ b/app/assets/javascripts/multivalueinputs.js @@ -1,25 +1,12 @@ -function openNav() { - document.getElementById("mySidenav").style.visibility = "visible"; - document.getElementById("mySidenav").style.width = "250px"; - document.getElementById("main").style.marginLeft = "250px"; -} - -function closeNav() { - document.getElementById("mySidenav").style.visibility = "hidden"; - document.getElementById("mySidenav").style.width = "0"; - document.getElementById("main").style.marginLeft = "0"; -} - - window.onload = function() { window.counts = { - + developers: window.counts?.developers || 1, tech_leads: window.counts?.tech_leads || 1, departments: window.counts?.departments || 1, product_owners: window.counts?.product_owners || 1, admin_users: window.counts?.admin_users || 1 - + }; }; @@ -27,11 +14,11 @@ function add(name, value) { var count = window.counts[name]++; var elementId = name + count; var inputId = "software_record_" + name + "_" + count; - + var element = document.createElement("div"); element.className = "input-group mt-2"; element.id = elementId; - + var inputElement = document.createElement("input"); inputElement.type = "text"; inputElement.required = true; @@ -42,76 +29,50 @@ function add(name, value) { inputElement.value = value; } element.appendChild(inputElement); - + var inputGroupAppend = document.createElement("div"); inputGroupAppend.className = "input-group-append btnRemove"; element.appendChild(inputGroupAppend); - + var spanElement = document.createElement("span"); spanElement.className = "input-group-text"; - inputGroupAppend.appendChild(spanElement); - + inputGroupAppend.appendChild(spanElement); + var removeButton = document.createElement("i"); removeButton.className = "fas fa-minus remove"; removeButton.innerHTML = " Delete"; spanElement.appendChild(removeButton); - + // Update the onclick handler to directly remove the parent element inputGroupAppend.onclick = function() { element.remove(); }; - + var valued = "multiple_" + name; var multiValued = document.getElementById(valued); multiValued.appendChild(element); - - console.log("Added element with ID: " + element.id + " and input ID: " + inputId); } - + document.getElementById("btnAddProductOwners").onclick = function() { add("product_owners", ""); }; - + document.getElementById("btnAddAdminUsers").onclick = function() { add("admin_users", ""); }; - + document.getElementById("btnAddDepartments").onclick = function() { add("departments", ""); }; - + document.getElementById("btnAddDevelopers").onclick = function() { add("developers", ""); }; - + document.getElementById("btnAddTechLeads").onclick = function() { add("tech_leads", ""); }; - + function remove(id) { document.getElementById(id).remove(); } - -var createdbyfield = document.getElementsByClassName('regex-createdby')[0]; -createdbyfield.onkeyup = function() { - createdbyfield.value = createdbyfield.value.replace(/[^a-zA-Z0-9 ]/, ''); -} - -// Specific to software_records.html.erb -function clearFilters() { - document.getElementById("vendor-record-filter").style.display = "none"; - document.getElementById("software-type-filter").style.display = "none"; - var loaded = window.location.host; - window.location = "software_records"; -} - -function handleRadio(myRadio) { - if(myRadio.value === "vendor_records") { - document.getElementById("vendor-record-filter").style.display = "block"; - document.getElementById("software-type-filter").style.display = "none"; - } else { - document.getElementById("vendor-record-filter").style.display = "none"; - document.getElementById("software-type-filter").style.display = "block"; - } -} - diff --git a/app/assets/javascripts/navigation.js b/app/assets/javascripts/navigation.js new file mode 100644 index 0000000..777f8df --- /dev/null +++ b/app/assets/javascripts/navigation.js @@ -0,0 +1,11 @@ +function openNav() { + document.getElementById("mySidenav").style.visibility = "visible"; + document.getElementById("mySidenav").style.width = "250px"; + document.getElementById("main").style.marginLeft = "250px"; +} + +function closeNav() { + document.getElementById("mySidenav").style.visibility = "hidden"; + document.getElementById("mySidenav").style.width = "0"; + document.getElementById("main").style.marginLeft = "0"; +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index db4bc2c..dbc2b4c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,7 +29,10 @@ crossorigin="anonymous"> <%= render 'shared/application_footer' %> <% end %> - <%= javascript_include_tag 'custom' %> + <%= javascript_include_tag 'navigation' %> + <%= javascript_include_tag 'filtermanagement' %> + <%= javascript_include_tag 'inputsanitization' %> + <%= javascript_include_tag 'multivalueinputs' %> diff --git a/app/views/layouts/software_records.html.erb b/app/views/layouts/software_records.html.erb index 4369ba1..4b33899 100644 --- a/app/views/layouts/software_records.html.erb +++ b/app/views/layouts/software_records.html.erb @@ -25,7 +25,10 @@ crossorigin="anonymous"> <%= alerts %> <%= render 'shared/dashboard_footer' %> - <%= javascript_include_tag 'custom' %> + <%= javascript_include_tag 'navigation' %> + <%= javascript_include_tag 'filtermanagement' %> + <%= javascript_include_tag 'inputsanitization' %> + <%= javascript_include_tag 'multivalueinputs' %> diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 881741d..ad2ab73 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -14,4 +14,7 @@ # application.js, application.css, and all non-JS/CSS in the app/assets # folder are already added. # Rails.application.config.assets.precompile += %w( admin.js admin.css ) -Rails.application.config.assets.precompile += %w[custom.js] +Rails.application.config.assets.precompile += %w( navigation.js ) +Rails.application.config.assets.precompile += %w( filtermanagement.js ) +Rails.application.config.assets.precompile += %w( inputsanitization.js ) +Rails.application.config.assets.precompile += %w( multivalueinputs.js )