Skip to content

Commit

Permalink
auspice: Update serum id creation
Browse files Browse the repository at this point in the history
Replace any characters in the serum name that are not alphanumeric,
dashes or underscores with an underscore to create a serum id that can
used as a query selector. I know that replacing with just an underscore
is not the most robust way to create unique IDs, but this is just a
quick fix to get the private flu site working again.

Also refactored the three separate serum id creations into a single
function so that I don't have to change the code in three places.
  • Loading branch information
joverlee521 committed Aug 5, 2022
1 parent 2682944 commit 2915be8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions auspice/js/auspice/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ function resetFocusNode() {
}
}

function createSerumId(serum) {
// Regex for replacing characters in the serum name that are not alphanumeric or underscores
const REPLACE_SERUM_ID_REGEX = new RegExp(/[^a-zA-Z0-9-\_]/g);
return "s" + serum.split("/").join("").replace(REPLACE_SERUM_ID_REGEX, "_");
}

function newFocus(){
if (typeof(focusNode)=="undefined"){
resetFocusNode();
Expand All @@ -448,18 +454,18 @@ function newFocus(){

for (var i=0; i<allSera.length; i++){
var serum = allSera[i];
var serumID = "s" + serum.split("/").join("").replace(/[;,\s\'\+\*\.\(\)]/g, "_");
var serumID = createSerumId(serum);
htmlStr+='<input type="checkbox" id="' + serumID + '" name="' + serum + '" checked="checked"> ' + serum +"<br>";
activeSera[serum]=true;
}
seraDiv.innerHTML = htmlStr;
for (var serum in titer_subs_model["potency"][focusNode.strain]){
var serumID = "s" + serum.split("/").join("").replace(/[;,\s\'\+\*\.\(\)]/g, "_");
var serumID = createSerumId(serum);
d3.select("#"+serumID)
.on("change", function(elem){
for (var j=0; j<allSera.length; j++){
var tmpserum = allSera[j];
var tmpserumID = "s" + tmpserum.split("/").join("").replace(/[;,\s\'\+\*\.\(\)]/g, "_");
var tmpserumID = createSerumId(tmpserum);
activeSera[tmpserum]=document.getElementById(tmpserumID).checked;
}
colorByHIDistance()});
Expand Down

0 comments on commit 2915be8

Please sign in to comment.