Skip to content

Commit

Permalink
testing fix: esaude/mental-health#25, also, in setDependentDisabledSt…
Browse files Browse the repository at this point in the history
…ate(), move on to next target selector if a provided selector provides no nodes in the DOM (typos, etc), avoid crashes so other functionality is still available
  • Loading branch information
kml27 committed Sep 17, 2018
1 parent 9f562f7 commit c589262
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function initializeDefaultSrc(control) {
//emulate a HTMLInputElement, type "checkbox", "radio"
"checked": control.defaultChecked,
//emulate a HTMLInputElement type "date", "number", "tel", "text", "textarea"
"value": control.defaultValue,
"value": control.defaultValue == undefined ? "" : control.defaultValue,
//emulate a HTMLSelectElement
0: { "value": ""},
selectedIndex: 0,
Expand Down Expand Up @@ -91,6 +91,11 @@ function setDependentDisabledState(clickedElement, specificRadio, specificTarget

for(target of specificTarget) {
var targetEl = document.querySelector(target);

if(targetEl==null){
continue;
}

targetEl.disabled = targetState;

if($(`fieldset[id=${targetEl.id}] input[type=radio]`).length){
Expand Down Expand Up @@ -292,7 +297,7 @@ function setAllDisabledStates(){
}
}

function confirmReset(evt){
function confirmReset(evt, reinitialize=true){
var result = confirm('Are you sure you want to reset *ALL* patient inputs to defaults?');

if(result==false)
Expand All @@ -302,7 +307,11 @@ function confirmReset(evt){

}else{
//go through clearing and re-initilization of localdatastore without re-attaching inputs' event listeners
initializeInputValuePersistence(reset=true);
if(reinitialize){
initializeInputValuePersistence(reset=true);
} else {
initializeLocalStore(clear=true, initializeEmptyStore=false);
}

//the default behavior of form input type="reset" will be applied after this handler returns
}
Expand Down Expand Up @@ -393,13 +402,13 @@ function setMaxDateToToday(){

const dataStoreProto = JSON.stringify({"storageVersion": "0.1"})

function initializeLocalStore(clear){
function initializeLocalStore(clear, initializeEmptyStore=true){

if(clear){
delete localStorage[dataStoreNS];
}

if(!localStorage[dataStoreNS]){
if(initializeEmptyStore && !localStorage[dataStoreNS]){
localStorage[dataStoreNS] = dataStoreProto;
}
}
Expand Down Expand Up @@ -1143,7 +1152,8 @@ $(document).ready(
//the submit succeeded and we got a redirect
if (window.location.href != xhr.responseURL) {

initializeInputValuePersistence(reset=true);
//initializeInputValuePersistence(reset=true);
initializeLocalStore(clear=true, initializeEmptyStore=false);

//update the window location url
window.location.href = xhr.responseURL;
Expand All @@ -1168,9 +1178,10 @@ $(document).ready(
};

var discardLink = $("[id=discardLinkSpan] a[class=html-form-entry-discard-changes]");
if (discardLink.text() === 'Discard changes') {
discardLink.on("click", function(event) {confirmReset(event);} )
}
discardLink.on("click", function(event) {confirmReset(event, reinitialize=false);} )

var editLink = $("a[href*='mode=EDIT']");
editLink.on("click", function(event) {initializeLocalStore(clear=true, initializeEmptyStore=false);} )

//addTypeAheadToDrugInput();
addOptionsToSelect("primary-dx-list", dxsList);
Expand Down

0 comments on commit c589262

Please sign in to comment.