Skip to content

Commit

Permalink
Refactors javascript assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Scherz committed Jul 23, 2024
1 parent 88dfcbe commit e36075b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 58 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/filtermanagement.js
Original file line number Diff line number Diff line change
@@ -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";
}
}
4 changes: 4 additions & 0 deletions app/assets/javascripts/inputsanitization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var createdbyfield = document.getElementsByClassName('regex-createdby')[0];
createdbyfield.onkeyup = function() {
createdbyfield.value = createdbyfield.value.replace(/[^a-zA-Z0-9 ]/, '');
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
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

};
};

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;
Expand All @@ -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";
}
}

11 changes: 11 additions & 0 deletions app/assets/javascripts/navigation.js
Original file line number Diff line number Diff line change
@@ -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";
}
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ crossorigin="anonymous">
<%= render 'shared/application_footer' %>
<% end %>
</div>
<%= javascript_include_tag 'custom' %>
<%= javascript_include_tag 'navigation' %>
<%= javascript_include_tag 'filtermanagement' %>
<%= javascript_include_tag 'inputsanitization' %>
<%= javascript_include_tag 'multivalueinputs' %>
</body>
</html>

5 changes: 4 additions & 1 deletion app/views/layouts/software_records.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ crossorigin="anonymous">
<%= alerts %>
<%= render 'shared/dashboard_footer' %>
</div>
<%= javascript_include_tag 'custom' %>
<%= javascript_include_tag 'navigation' %>
<%= javascript_include_tag 'filtermanagement' %>
<%= javascript_include_tag 'inputsanitization' %>
<%= javascript_include_tag 'multivalueinputs' %>
</body>
</html>

5 changes: 4 additions & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

0 comments on commit e36075b

Please sign in to comment.