From 3bef766524e40400629e40e8428c07a9fd5b7833 Mon Sep 17 00:00:00 2001 From: "Mark M. Florida" <markflorida@wustl.edu> Date: Wed, 31 Aug 2016 12:33:22 -0500 Subject: [PATCH] XNAT-4483: The REAL fix for "false" values showing up empty. --- src/main/webapp/scripts/globals.js | 16 +++++++++------- src/main/webapp/scripts/xnat/xhr.js | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/webapp/scripts/globals.js b/src/main/webapp/scripts/globals.js index dbaef7f0..4ac56bc8 100644 --- a/src/main/webapp/scripts/globals.js +++ b/src/main/webapp/scripts/globals.js @@ -312,7 +312,7 @@ function setExtendedObject(obj, str, val){ // loops over object string using quasi-dot notation (with colons): // we use colons because property names can contain periods (which is dumb) -// var myVal = lookupObjectValue(XNAT, 'data:siteConfig:siteId'); +// var myVal = lookupObjectValue(XNAT, ':data:siteConfig:siteId'); // --> myVal == 'myXnatSiteId' function lookupObjectValue(root, objStr, prop){ @@ -321,7 +321,7 @@ function lookupObjectValue(root, objStr, prop){ brackets = /[\]\[]/, hasBrackets = false, parts = [], - undefined; + undef; if (!objStr) { objStr = root+''; @@ -339,8 +339,8 @@ function lookupObjectValue(root, objStr, prop){ //objStr = objStr.replace(/^\[|]$/g, '') hasBrackets = true; } - // if 'objStr' contains colons, use those as the path delimiter - else if (/:/.test(objStr)) { + // if 'objStr' STARTS WITH a colon, use those as the path delimiter + else if (/^:/.test(objStr)) { delim = ':'; } // otherwise we're probably using dot notation @@ -360,17 +360,17 @@ function lookupObjectValue(root, objStr, prop){ parts.forEach(function(part, i){ // start at the root object if (i === 0) { - val = root[part] || ''; + val = (root[part] !== undef) ? root[part] : ''; } else { - if (val === undefined) return false; + if (val === undef) return false; val = val[part]; } }); // explicitly set a final property name to look for if (prop) { - val = val[prop] || val; + val = (val[prop] !== undef) ? val[prop] : val; } return val; @@ -658,6 +658,8 @@ autoID = randomID; // set 'forceLower' === true (or omit argument) // to ensure output is lowercase function toDashed(str){ + var undefined; + str = str !== undefined ? str+'' : ''; return str.replace(/[A-Z]/g, function(u) { return '-' + u; }).replace(/[A-Z]-/g, function(c){ diff --git a/src/main/webapp/scripts/xnat/xhr.js b/src/main/webapp/scripts/xnat/xhr.js index 7a70b78e..9ea911b3 100755 --- a/src/main/webapp/scripts/xnat/xhr.js +++ b/src/main/webapp/scripts/xnat/xhr.js @@ -451,7 +451,7 @@ var XNAT = getObject(XNAT||{}), } // apply values to each input - $inputs.each(function(){ + $inputs.not('button').each(function(){ var $this = $(this); var val = lookupObjectValue(dataObj, this.name||this.title); @@ -467,7 +467,12 @@ var XNAT = getObject(XNAT||{}), //if (val === "") return; if (/checkbox/i.test(this.type)) { - this.checked = realValue(val); + val = realValue(val); + // allow values other than 'true' or 'false' + this.checked = (this.value && val && isEqual(this.value, val)) ? true : val; + if (this.value === '') { + this.value = val; + } } else if (/radio/i.test(this.type)) { this.checked = isEqual(this.value, val); @@ -479,7 +484,13 @@ var XNAT = getObject(XNAT||{}), changeValue($this, val); } - $this.removeClass('dirty').dataAttr('value', val); + if (!/textarea/i.test(this.tagName)) { + $this.dataAttr('value', val); + } + + $this.removeClass('dirty').on('change', function(){ + $(this).addClass('dirty'); + }); }); @@ -530,7 +541,7 @@ var XNAT = getObject(XNAT||{}), if (/POST|PUT/i.test(opts.method)) { if ($form.hasClass('json') || /json/i.test(opts.contentType||'')){ // opts.data = formToJSON($form, true); - opts.data = JSON.stringify(form2js(inputs, ':', false)); + opts.data = JSON.stringify(form2js(inputs, opts.delimiter||opts.delim||':', false)); opts.processData = false; opts.contentType = 'application/json'; } -- GitLab