//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2020-2021 Dawson Dean // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// // // predictorJS.cpp // // This is the top-level module. It initiates the entire UI and also does all // the layout of HTML elements in the UI. This layout is done initially when the // UI opens, but also may change the UI elements dynamically in response to user // actions. // ///////////////////////////////////////////////////////////////////////////// var DD_DEBUG = true; ///////////////////////// // HTML Elements // These are the main windows that we show/hide to expose different subgroups of funtions and outputs. var g_CoumadinCPanelDivElement = null; var g_VancomycinCPanelDivElement = null; var g_TacrolimusCPanelDivElement = null; var g_VoriconazoleCPanelDivElement = null; var g_TobramycinCPanelDivElement = null; var g_MethotrexateCPanelDivElement = null; var g_CoumadinCPanelTable = null; var g_VancomycinCPanelTable = null; var g_TacrolimusCPanelTable = null; var g_VoriconazoleCPanelTable = null; var g_TobramycinCPanelTable = null; var g_MethotrexateCPanelTable = null; var g_MedSelectElement = null; //////////////////////////////////////////////////////////////////////////////// // // [StartPredictorClient] // // This is called by the browser to initialize the entire UI. //////////////////////////////////////////////////////////////////////////////// function StartPredictorClient() { //LogEvent("StartPredictorClient"); g_CoumadinCPanelDivElement = document.getElementById("CoumadinCPanel"); g_VancomycinCPanelDivElement = document.getElementById("VancomycinCPanel"); g_TacrolimusCPanelDivElement = document.getElementById("TacrolimusCPanel"); g_VoriconazoleCPanelDivElement = document.getElementById("VoriconazoleCPanel"); g_TobramycinCPanelDivElement = document.getElementById("TobramycinCPanel"); g_MethotrexateCPanelDivElement = document.getElementById("MethotrexateCPanel"); g_CoumadinCPanelTable = document.getElementById("CoumadinCPanelTable"); g_VancomycinCPanelTable = document.getElementById("VancomycinCPanelTable"); g_TacrolimusCPanelTable = document.getElementById("TacrolimusCPanelTable"); g_VoriconazoleCPanelTable = document.getElementById("VoriconazoleCPanelTable"); g_TobramycinCPanelTable = document.getElementById("TobramycinCPanelTable"); g_MethotrexateCPanelTable = document.getElementById("MethotrexateCPanelTable"); g_MedSelectElement = document.getElementById("Predictor_MedSelect"); if (g_MedSelectElement) { g_MedSelectElement.selectedIndex = 0; } ShowSection("Warfarin_INR"); //LogEvent("StartPredictorClient finished"); } // End StartPredictorClient //////////////////////////////////////////////////////////////////////////////// // // [OnMedPredictSelect] // //////////////////////////////////////////////////////////////////////////////// function OnMedPredictSelect(menuControl) { //LogEvent("Called OnMedPredictSelect"); //LogEvent("menu=" + menuControl); var selectedItem = menuControl.options[menuControl.selectedIndex].value; //LogEvent("selectedItem=" + selectedItem); ShowSection(selectedItem) document.getElementById("select").selectedIndex = 0; } // OnMedPredictSelect //////////////////////////////////////////////////////////////////////////////// // // [ShowSection] // //////////////////////////////////////////////////////////////////////////////// function ShowSection(groupName) { //LogEvent("ShowSection"); //LogEvent("button.id = " + button.id); //////////////////////////////////// // First, turn everythng off. // We will selectively turn fields on depending on what was selected. if (g_CoumadinCPanelDivElement) { g_CoumadinCPanelDivElement.style.display = "none"; } if (g_VancomycinCPanelDivElement) { g_VancomycinCPanelDivElement.style.display = "none"; } if (g_TacrolimusCPanelDivElement) { g_TacrolimusCPanelDivElement.style.display = "none"; } if (g_VoriconazoleCPanelDivElement) { g_VoriconazoleCPanelDivElement.style.display = "None"; } if (g_TobramycinCPanelDivElement) { g_TobramycinCPanelDivElement.style.display = "None"; } if (g_MethotrexateCPanelDivElement) { g_MethotrexateCPanelDivElement.style.display = "None"; } if (g_CoumadinCPanelTable) { g_CoumadinCPanelTable.style.display = "none"; } if (g_TacrolimusCPanelTable) { g_TacrolimusCPanelTable.style.display = "none"; } if (g_VancomycinCPanelTable) { g_VancomycinCPanelTable.style.display = "none"; } if (g_VoriconazoleCPanelTable) { g_VoriconazoleCPanelTable.style.display = "None"; } if (g_TobramycinCPanelTable) { g_TobramycinCPanelTable.style.display = "None"; } if (g_MethotrexateCPanelTable) { g_MethotrexateCPanelTable.style.display = "None"; } //////////////////////////////////// if (groupName == "Warfarin_INR") { if (g_CoumadinCPanelDivElement) { g_CoumadinCPanelDivElement.style.display = "inline"; } if (g_CoumadinCPanelTable) { g_CoumadinCPanelTable.style.display = "inline"; } } if (groupName == "Vancomycin_Level") { if (g_VancomycinCPanelDivElement) { g_VancomycinCPanelDivElement.style.display = "inline"; } if (g_VancomycinCPanelTable) { g_VancomycinCPanelTable.style.display = "inline"; } } if (groupName == "Tacrolimus_Level") { if (g_TacrolimusCPanelDivElement) { g_TacrolimusCPanelDivElement.style.display = "inline"; } if (g_TacrolimusCPanelTable) { g_TacrolimusCPanelTable.style.display = "inline"; } } if (groupName == "Voriconazole_Level") { if (g_VoriconazoleCPanelDivElement) { g_VoriconazoleCPanelDivElement.style.display = "inline"; } if (g_VoriconazoleCPanelTable) { g_VoriconazoleCPanelTable.style.display = "inline"; } } if (groupName == "Tobramycin_Level") { if (g_TobramycinCPanelDivElement) { g_TobramycinCPanelDivElement.style.display = "inline"; } if (g_TobramycinCPanelTable) { g_TobramycinCPanelTable.style.display = "inline"; } } if (groupName == "Methotrexate_Level") { if (g_MethotrexateCPanelDivElement) { g_MethotrexateCPanelDivElement.style.display = "inline"; } if (g_MethotrexateCPanelTable) { g_MethotrexateCPanelTable.style.display = "inline"; } } } // ShowSection //////////////////////////////////////////////////////////////////////////////// // // [GetPredictionFromServer] // //////////////////////////////////////////////////////////////////////////////// function GetPredictionFromServer(formElementID, groupName, opName) { LogEvent("GetPredictionFromServer. formElementID=" + formElementID); LogEvent("GetPredictionFromServer. groupName=" + groupName); LogEvent("GetPredictionFromServer. opName=" + opName); //////////////////////////////////// // If we are doing a demo, then generate some demo values and then // turn the operation into a predict level if (opName == 'Demo') { if (groupName == "Warfarin_INR") { Util_SetInputStr("WarfarinDose[-3]", "3"); Util_SetInputStr("INR[-3]", "1.0"); Util_SetInputStr("WarfarinDose[-1]", "3"); Util_SetInputStr("INR[-1]", "1.0"); Util_SetInputStr("WarfarinDose", "5"); Util_SetInputStr("INR", "1.1"); } if (groupName == "Vancomycin_Level") { Util_SetInputStr("VancDose[-3]", "750"); Util_SetInputStr("VancLvl[-3]", "10"); Util_SetInputStr("VancDose[-1]", "750"); Util_SetInputStr("VancLvl[-1]", "11"); Util_SetInputStr("VancDose", "1000"); Util_SetInputStr("VancLvl", "15"); } if (groupName == "Tacrolimus_Level") { Util_SetInputStr("TacroDoseAM[-3]", "3"); Util_SetInputStr("TacroDosePM[-3]", "3"); Util_SetInputStr("TacLvl[-3]", "1.1"); Util_SetInputStr("TacroDoseAM[-1]", "3"); Util_SetInputStr("TacroDosePM[-1]", "3"); Util_SetInputStr("TacLvl[-1]", "1.1"); Util_SetInputStr("TacroDoseAM", "5"); Util_SetInputStr("TacroDosePM", "5"); Util_SetInputStr("TacLvl", "1.2"); } if (groupName == "Voriconazole_Level") { Util_SetInputStr("VoriDose[-3]", "3"); Util_SetInputStr("VoriLvl[-3]", "10"); Util_SetInputStr("VoriDose[-1]", "3"); Util_SetInputStr("VoriLvl[-1]", "10"); Util_SetInputStr("VoriDose", "5"); Util_SetInputStr("VoriLvl", "11"); } if (groupName == "Tobramycin_Level") { Util_SetInputStr("TobraDose[-3]", "3"); Util_SetInputStr("TobLvl[-3]", "10"); Util_SetInputStr("TobraDose[-1]", "3"); Util_SetInputStr("TobLvl[-1]", "10"); Util_SetInputStr("TobraDose", "5"); Util_SetInputStr("TobLvl", "11"); } if (groupName == "Methotrexate_Level") { Util_SetInputStr("MTXDose[-3]", "3"); Util_SetInputStr("MTXLvl[-3]", "10"); Util_SetInputStr("MTXDose[-1]", "3"); Util_SetInputStr("MTXLvl[-1]", "10"); Util_SetInputStr("MTXDose", "5"); Util_SetInputStr("MTXLvl", "10"); } opName = 'Predict_Value'; } // if (opName == 'Demo') // Now, create the HTTP request and send it to the server. //LogEvent("GetPredictionFromServer"); var request = new XMLHttpRequest(); //LogEvent("GetPredictionFromServer. Made a request") formElement = document.getElementById(formElementID); if (null == formElement) { LogEvent("GetPredictionFromServer. Failed to get the form"); return; } //LogEvent("GetPredictionFromServer. Got the form element"); formData = new FormData(formElement); //LogEvent("GetPredictionFromServer. Got the form"); formData.set("Group", groupName); formData.set("OpName", opName); //LogEvent("GetPredictionFromServer. Initialized the form"); // Define what happens on successful data return and on error request.addEventListener("load", function(event) {OnGetPredictionResult(event, formElementID, groupName, opName, request);}); request.addEventListener("error", function(event) {OnGetPredictionError(event, formElementID, groupName, opName, request);}); // Set up our request. The URL cannot contain query suffix, because that conflicts with // sending all of the data as a form. request.open("POST", "/cgi-bin/predictor.py"); //LogEvent("GetPredictionFromServer. Opened"); //request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); //formStr = ""; //"Group=" + groupName + "&OpName=" + opName; //LogEvent("GetPredictionFromServer. formStr=" + formStr); request.send(formData); //LogEvent("GetPredictionFromServer. Called send()"); } // GetPredictionFromServer //////////////////////////////////////////////////////////////////////////////// // // [OnGetPredictionError] // //////////////////////////////////////////////////////////////////////////////// function OnGetPredictionError(event, formElementID, groupName, opName, request) { //alert("Called OnGetPredictionError: " + event.target.responseText); LogEvent("Called OnGetPredictionError"); } // OnGetPredictionError //////////////////////////////////////////////////////////////////////////////// // // [OnGetPredictionResult] // // This ic called when we get a successful response from the server. // // // // nnn // xxxxxx // VancDose[-3]=111,VancLvl[-3]=222,VancDose[-1]=333,VancLvl[-1]=444,VancDose=555,VancLvl=666 // xxxxxx // // .... // // //////////////////////////////////////////////////////////////////////////////// function OnGetPredictionResult(event, formElementID, groupName, opName, request) { LogEvent("OnGetPredictionResult. Response = " + event.target.responseText) //LogEvent("Called OnGetPredictionResult. groupName=" + groupName); //LogEvent("Called OnGetPredictionResult. opName=" + opName); // Get the response as an XML object. xmlDoc = Util_ParseStringIntoXML(event.target.responseText); if (!xmlDoc) { LogEvent("LoadServerResponse. No xmlDoc"); return; } rootElementList = xmlDoc.getElementsByTagName("MLResponse"); // SCHEMATICODE_RESPONSE_ROOT_ELEMENT_NAME); if ((!rootElementList) || (rootElementList.length <= 0)) { LogEvent("LoadServerResponse. No responseNode"); return; } responseNode = rootElementList[0]; if (!responseNode) { LogEvent("LoadServerResponse. No responseNode"); return; } var predictionNode = Util_GetChildNode(responseNode, "Predict"); if (null == predictionNode) { LogEvent("LoadServerResponse. No predictionNode"); return; } var groupName = predictionNode.getAttribute('FunctionGroup'); //LogEvent("OnGetPredictionResult. groupName = " + groupName); if (null == groupName) { LogEvent("LoadServerResponse. No groupName"); return; } var errIDStr = Util_GetChildNodeText(predictionNode, "ErrID"); var errMsg = Util_GetChildNodeText(predictionNode, "ErrMsg"); var predictValStr = Util_GetChildNodeText(predictionNode, "PredVal"); var resultElementID = "" var menuIndex = 0; if (groupName == "Warfarin_INR") { resultElementID = "PredictedINR" menuIndex = 0; } if (groupName == "Vancomycin_Level") { resultElementID = "PredictedVancTrough" menuIndex = 1; } if (groupName == "Tacrolimus_Level") { resultElementID = "PredictedTacLvl" menuIndex = 2; } if (groupName == "Voriconazole_Level") { resultElementID = "PredictedVoriconazoleLevel" menuIndex = 3; } if (groupName == "Tobramycin_Level") { resultElementID = "PredictedTobramycinLevel" groupName = 4; } if (groupName == "Methotrexate_Level") { resultElementID = "PredictedMethotrexateLevel" prefixStr = "Predicted Methotrexate Level = "; menuIndex = 5; } var resultElement = document.getElementById(resultElementID); if (resultElement) { resultElement.value = predictValStr } //if (g_MedSelectElement) { // g_MedSelectElement.selectedIndex = menuIndex; //} //ShowSection(groupName); } // OnGetPredictionResult