view js/min/specification.min.js @ 3141:335bc77627e0 tip

fixing discrete interface to allow labels to display
author Dave Moffat <me@davemoffat.com>
date Mon, 26 Jul 2021 12:15:24 +0100
parents 90c7b42264d4
children
line wrap: on
line source
function Specification(){function t(t){this.location=void 0,this.options=[],this.parent=void 0,this.showBackButton=!0,this.specification=t,this.addOption=function(){var t=new this.OptionNode(this.specification);return this.options.push(t),t},this.OptionNode=function(t){this.type=void 0,this.schema=void 0,this.id=void 0,this.name=void 0,this.mandatory=void 0,this.statement=void 0,this.boxsize=void 0,this.options=[],this.min=void 0,this.max=void 0,this.minWait=void 0,this.step=void 0,this.conditions=[],this.decode=function(t,e){this.schema=s.querySelector("[name="+e.nodeName+"]");var i,n=this.schema.querySelectorAll("attribute");for(i in n){if(!0===isNaN(Number(i)))break;var r=n[i].getAttribute("name")||n[i].getAttribute("ref"),o=e.getAttribute(r);null!==(o=a(o,n[i]))&&(this[r]=o)}if("surveyentry"==e.nodeName){if(console.log("NOTE - Use of <surveyelement> is now deprecated. Whilst these will still work, newer nodes and tighter error checking will not be enforced"),console.log("Please use the newer, type specifc nodes"),!this.type)throw"Type not specified"}else this.type=e.nodeName.split("survey")[1];if(this.statement=e.getElementsByTagName("statement")[0].textContent,"checkbox"==this.type||"radio"==this.type){var h=e.getElementsByTagName("option");if(null===h.length)console.log("Malformed"+e.nodeName+"entry"),this.statement="Malformed"+e.nodeName+"entry",this.type="statement";else for(this.options=[],i=0;i<h.length;i++)this.options.push({name:h[i].getAttribute("name"),text:h[i].textContent})}else if("slider"==this.type){this.leftText="",this.rightText="";var l=e.getElementsByTagName("minText"),m=e.getElementsByTagName("maxText");l.length>0&&(this.leftText=l[0].textContent),m.length>0&&(this.rightText=m[0].textContent)}var c=e.getElementsByTagName("conditional");for(i=0;i<c.length;i++){var d=c[i],u={check:d.getAttribute("check"),value:d.getAttribute("value"),jumpToOnPass:d.getAttribute("jumpToOnPass"),jumpToOnFail:d.getAttribute("jumpToOnFail")};this.conditions.push(u)}},this.exportXML=function(t){var e=t.createElement("survey"+this.type),i=t.createElement("statement");switch(i.textContent=this.statement,e.appendChild(i),e.id=this.id,void 0!==this.name&&e.setAttribute("name",this.name),void 0!==this.mandatory&&e.setAttribute("mandatory",this.mandatory),e.id=this.id,void 0!==this.name&&e.setAttribute("name",this.name),this.type){case"checkbox":void 0!==this.min?e.setAttribute("min",this.min):e.setAttribute("min","0"),void 0!==this.max?e.setAttribute("max",this.max):e.setAttribute("max","undefined");case"radio":for(var s=0;s<this.options.length;s++){var n=this.options[s],a=t.createElement("option");a.setAttribute("name",n.name),a.textContent=n.text,e.appendChild(a)}break;case"number":void 0!==this.min&&e.setAttribute("min",this.min),void 0!==this.max&&e.setAttribute("max",this.max);break;case"question":void 0!==this.boxsize&&e.setAttribute("boxsize",this.boxsize),void 0!==this.mandatory&&e.setAttribute("mandatory",this.mandatory);break;case"video":void 0!==this.mandatory&&e.setAttribute("mandatory",this.mandatory);case"youtube":void 0!==this.url&&e.setAttribute("url",this.url);break;case"slider":if(e.setAttribute("min",this.min),e.setAttribute("max",this.max),this.leftText){var r=t.createElement("minText");r.textContent=this.leftText,e.appendChild(r)}if(this.rightText){var o=t.createElement("maxText");o.textContent=this.rightText,e.appendChild(o)}}return this.conditions.forEach(function(i){var s=t.createElement("conditional");s.setAttribute("check",i.check),s.setAttribute("value",i.value),s.setAttribute("jumpToOnPass",i.jumpToOnPass),s.setAttribute("jumpToOnFail",i.jumpToOnFail),e.appendChild(s)}),e}},this.decode=function(t,e){this.schema=s.querySelector("[name=survey]"),this.parent=t,this.location=e.getAttribute("location"),"before"==this.location?this.location="pre":"after"==this.location&&(this.location="post"),this.showBackButton=e.getAttribute("showBackButton"),"false"==this.showBackButton?this.showBackButton=!1:this.showBackButton=!0;for(var i=e.firstElementChild;i;){var n=new this.OptionNode(this.specification);n.decode(t,i),this.options.push(n),i=i.nextElementSibling}0===this.options.length&&(console.log("Empty survey node"),console.log(this))},this.encode=function(t){var e=t.createElement("survey");e.setAttribute("location",this.location),e.setAttribute("showBackButton",this.showBackButton);for(var i=0;i<this.options.length;i++)e.appendChild(this.options[i].exportXML(t));return e}}function e(t){this.title=void 0,this.name=void 0,this.image=void 0,this.options=[],this.scales=[],this.schema=void 0,this.decode=function(t,e){this.schema=s.querySelectorAll("[name=interface]")[1],this.name=e.getAttribute("name");var i=e.getElementsByTagName("title");1==i.length&&(this.title=i[0].textContent);var n,r,o=e.getElementsByTagName("interfaceoption"),h=this.schema.querySelector("[name=interfaceoption]").querySelectorAll("attribute");for(n=0;n<o.length;n++){var l=o[n],m={};for(r=0;r<h.length;r++){var c=h[r].getAttribute("name")||h[r].getAttribute("ref"),d=l.getAttribute(c);null!==(d=a(d,h[r]))&&(m[c]=d)}"check"==m.type&&l.firstElementChild&&(m.errorMessage=l.firstElementChild.textContent),this.options.push(m)}var u=e.getElementsByTagName("image");1==u.length&&(this.image=u[0].getAttribute("src"));var p=e.getElementsByTagName("scales");if(1==p.length){var g=(p=p[0]).querySelectorAll("scalelabel");for(n=0;n<g.length;n++)this.scales.push({text:g[n].textContent,position:Number(g[n].getAttribute("position"))})}},this.encode=function(t){var e=t.createElement("interface");if("string"==typeof this.name&&this.name.length>0&&e.setAttribute("name",this.name),"string"==typeof this.title){var i=t.createElement("title");i.textContent=this.title,e.appendChild(i)}if(this.options.forEach(function(i){var s=t.createElement("interfaceoption");if(s.setAttribute("type",i.type),s.setAttribute("name",i.name),"check"==i.type&&void 0!==i.errorMessage){var n=t.createElement("errormessage");n.textContent=i.errorMessage,s.appendChild(n)}e.appendChild(s)}),"string"==typeof this.image&&0!==this.image.length){var s=t.createElement("image");s.setAttribute("src",this.image),e.appendChild(s)}if(0!==this.scales.length){var n=t.createElement("scales");this.scales.forEach(function(e){var i=t.createElement("scalelabel");i.setAttribute("position",e.position),i.textContent=e.text,n.appendChild(i)}),e.appendChild(n)}return e}}function i(i){function n(t){this.id=void 0,this.name=void 0,this.type=void 0,this.statement=void 0,this.mandatory=void 0,this.schema=s.querySelector("[name=commentquestion]"),this.decode=function(t,e){switch(this.id=e.id,this.name=e.getAttribute("name"),this.mandatory="true"==e.getAttribute("mandatory"),null===this.name&&(this.name=void 0),e.nodeName){case"commentradio":this.type="radio",this.options=[];break;case"commentcheckbox":this.type="checkbox",this.options=[];break;case"commentslider":this.type="slider",this.min=void 0,this.max=void 0,this.step=void 0;break;case"commentquestion":this.type="question";break;default:throw"Unknown comment type "+e.nodeName}if(this.statement=e.getElementsByTagName("statement")[0].textContent,"radio"==this.type||"checkbox"==this.type)for(var i=e.getElementsByTagName("option"),s=0;s<i.length;s++){var n=i[s];this.options.push({name:n.getAttribute("name"),text:n.textContent})}"slider"==this.type&&(this.min=Number(e.getAttribute("min")),this.max=Number(e.getAttribute("max")),this.step=Number(e.getAttribute("step")),void 0===this.step&&(this.step=1),this.value=Number(e.getAttribute("value")),void 0===this.value&&(this.value=this.min),this.leftText=e.getElementsByTagName("minText"),this.leftText&&this.leftText.length>0?this.leftText=this.leftText[0].textContent:this.leftText="",this.rightText=e.getElementsByTagName("maxText"),this.rightText&&this.rightText.length>0?this.rightText=this.rightText[0].textContent:this.rightText="")},this.encode=function(t){var e;switch(this.type){case"radio":e=t.createElement("commentradio");break;case"checkbox":e=t.createElement("commentcheckbox");break;case"slider":e=t.createElement("commentslider");break;case"question":e=t.createElement("commentquestion");break;default:throw"Unknown type "+this.type}e.id=this.id,e.setAttribute("mandatory",this.mandatory),e.setAttribute("type",this.type),void 0!==this.name&&e.setAttribute("name",this.name);var i=t.createElement("statement");if(i.textContent=this.statement,e.appendChild(i),"radio"!=this.type&&"checkbox"!=this.type||this.options.forEach(function(i){var s=t.createElement("option");s.setAttribute("name",i.name),s.textContent=i.text,e.appendChild(s)}),"slider"==this.type){if(e.setAttribute("min",this.min),e.setAttribute("max",this.max),1!==this.step&&e.setAttribute("step",this.step),this.value!==this.min&&e.setAttribute("value",this.value),this.leftText.length>0){var s=t.createElement("minText");s.textContent=this.leftText,e.appendChild(s)}if(this.rightText.length>0){var n=t.createElement("maxText");n.textContent=this.rightText,e.appendChild(n)}}return e}}function r(t){this.url=void 0,this.id=void 0,this.name=void 0,this.parent=void 0,this.type=void 0,this.marker=void 0,this.enforce=!1,this.gain=0,this.label=void 0,this.startTime=void 0,this.stopTime=void 0,this.sampleRate=void 0,this.image=void 0,this.minNumberPlays=void 0,this.maxNumberPlays=void 0,this.alternatives=[],this.schema=s.querySelector("[name=audioelement]"),this.parent=void 0,this.decode=function(t,e){this.parent=t;for(var i=this.schema.querySelectorAll("attribute"),s=0;s<i.length;s++){var n=i[s].getAttribute("name")||i[s].getAttribute("ref"),r=e.getAttribute(n);null!==(r=a(r,i[s]))&&(this[n]=r)}for(var o=e.firstElementChild;o;)"alternative"==o.nodeName&&this.alternatives.push({url:o.getAttribute("url"),sampleRate:o.getAttribute("sampleRate")}),o=o.nextElementSibling},this.encode=function(t){for(var e=t.createElement("audioelement"),i=this.schema.querySelectorAll("attribute"),s=0;s<i.length;s++){var n=i[s].getAttribute("name");null===n&&(n=i[s].getAttribute("ref")),void 0===this[n]&&"required"!=i[s].getAttribute("use")||e.setAttribute(n,this[n])}return this.alternatives.forEach(function(i){var s=t.createElement("alternative");s.setAttribute("url",i.url),s.setAttribute("sampleRate",i.sampleRate),e.appendChild(s)}),e}}this.presentedId=void 0,this.id=void 0,this.title=void 0,this.hostURL=void 0,this.randomiseOrder=void 0,this.loop=void 0,this.outsideReference=void 0,this.loudness=void 0,this.label=void 0,this.labelStart=void 0,this.preTest=new t(i),this.postTest=new t(i),this.preTest.location="pre",this.postTest.location="post",this.interfaces=[],this.playOne=void 0,this.restrictMovement=void 0,this.position=void 0,this.commentBoxPrefix="Comment on track",this.minNumberPlays=void 0,this.maxNumberPlays=void 0,this.randomiseAxisOrder=void 0,this.audioElements=[],this.commentQuestions=[],this.schema=s.querySelector("[name=page]"),this.specification=i,this.parent=void 0,this.addInterface=function(){var t=new e(i);return this.interfaces.push(t),t},this.addCommentQuestion=function(){var t=new n(i);return this.commentQuestions.push(t),t},this.addAudioElement=function(){var t=new r(i);return this.audioElements.push(t),t},this.decode=function(t,i){this.parent=t;var s,o,h=this.schema.querySelectorAll("attribute");for(s=0;s<h.length;s++){var l=h[s].getAttribute("name")||h[s].getAttribute("ref"),m=i.getAttribute(l);null!==(m=a(m,h[s]))&&(this[l]=m)}var c=i.getElementsByTagName("title");0!==c.length&&c[0].parentElement==i&&(this.title=c[0].textContent);var d=i.getElementsByTagName("commentboxprefix");0!==d.length&&d[0].parentElement==i&&(this.commentBoxPrefix=d[0].textContent);var u=i.getElementsByTagName("interface");for(s=0;s<u.length;s++)(o=new e(this.specification)).decode(this,u[s],t.schema.querySelectorAll("[name=interface]")[1]),this.interfaces.push(o);var p=i.getElementsByTagName("survey"),g=t.schema.querySelector("[name=survey]");for(s=0;s<p.length;s++){var v=p[s].getAttribute("location");"pre"==v||"before"==v?0!==this.preTest.options.length?this.errors.push("Already a pre/before test survey defined! Ignoring second!!"):this.preTest.decode(t,p[s],g):"post"!=v&&"after"!=v||(0!==this.postTest.options.length?this.errors.push("Already a post/after test survey defined! Ignoring second!!"):this.postTest.decode(t,p[s],g))}var f=i.getElementsByTagName("audioelement");for(s=0;s<f.length;s++){var b=new r(this.specification);b.decode(this,f[s]),this.audioElements.push(b)}var x=i.getElementsByTagName("commentquestions");if(0!==x.length)for(var y=(x=x[0]).firstElementChild;y;)(o=new n(this.specification)).decode(t,y),this.commentQuestions.push(o),y=y.nextElementSibling},this.encode=function(t){var e,i=t.createElement("page"),s=this.schema.querySelectorAll("attribute");for(e=0;e<s.length;e++){var n=s[e].getAttribute("name");null===n&&(n=s[e].getAttribute("ref")),void 0===this[n]&&"required"!=s[e].getAttribute("use")||i.setAttribute(n,this[n])}var a=t.createElement("commentboxprefix");for(a.textContent=this.commentBoxPrefix,i.appendChild(a),e=0;e<this.interfaces.length;e++)i.appendChild(this.interfaces[e].encode(t));for(e=0;e<this.audioElements.length;e++)i.appendChild(this.audioElements[e].encode(t));if(this.commentQuestions.length>0){var r=t.createElement("commentquestions");for(e=0;e<this.commentQuestions.length;e++)r.appendChild(this.commentQuestions[e].encode(t));i.appendChild(r)}return i.appendChild(this.preTest.encode(t)),i.appendChild(this.postTest.encode(t)),i}}var s,n;this.interface=void 0,this.projectReturn=void 0,this.returnURL=void 0,this.randomiseOrder=void 0,this.poolSize=void 0,this.loudness=void 0,this.sampleRate=void 0,this.calibration=void 0,this.crossFade=void 0,this.preSilence=void 0,this.postSilence=void 0,this.playOne=void 0,this.minNumberPlays=void 0,this.maxNumberPlays=void 0,this.randomiseAxisOrder=void 0,this.metrics=new function(){this.enabled=[],this.decode=function(t,e){var i=e.getElementsByTagName("metricenable");for(var s in i){if(!0===isNaN(Number(s)))break;this.enabled.push(i[s].textContent)}},this.encode=function(t){var e=t.createElement("metric");for(var i in this.enabled){if(!0===isNaN(Number(i)))break;var s=t.createElement("metricenable");s.textContent=this.enabled[i],e.appendChild(s)}return e}},this.preTest=new t(this),this.postTest=new t(this),this.preTest.location="pre",this.postTest.location="post",this.pages=[],this.interfaces=new e(this),this.errors=[],this.exitText="Thank you.",this.createNewPage=function(){var t=new i(this);return this.pages.push(t),t};var a=function(t,e){null===e.getAttribute("name")&&void 0!==e.getAttribute("ref")&&(e=s.querySelector("[name="+e.getAttribute("ref")+"]"));var i=e.getAttribute("default");null===t&&(t=i);var n=e.getAttribute("type");if("string"==typeof n)n=n.substr(3);else{var a=e.querySelectorAll("restriction,enumeration");n=a.length>0&&"string"==typeof(n=a[0].getAttribute("base"))?n.substr(3):"string"}if(null===t)return t;switch(n){case"boolean":t="true"==t;break;case"negativeInteger":case"positiveInteger":case"nonNegativeInteger":case"nonPositiveInteger":case"integer":case"decimal":case"short":t=Number(t);break;default:t=String(t)}return t};this.processSchema=function(t){if(void 0===s){n=t;var e=new DOMParser;s=e.parseFromString(n,"text/xml"),Object.defineProperties(this,{schema:{value:s},schemaString:{value:n}})}},this.getSchema=function(){return s},this.getSchemaString=function(){return n},this.decode=function(t){s=this.schema,this.errors=[],this.projectXML=t.childNodes[0];var e,n=t.getElementsByTagName("setup")[0],r=s.querySelector("[name=setup]").querySelectorAll("attribute");for(e=0;e<r.length;e++){var o=r[e].getAttribute("name")||r[e].getAttribute("ref"),h=n.getAttribute(o);null!==(h=a(h,r[e]))&&(this[o]=h)}var l=n.getElementsByTagName("exitText");1==l.length&&(this.exitText=l[0].textContent),this.metrics.decode(this,n.getElementsByTagName("metric")[0]);var m=n.getElementsByTagName("survey");for(e=0;e<m.length;e++){switch(m[e].getAttribute("location")){case"pre":case"before":this.preTest.decode(this,m[e]);break;case"post":case"after":this.postTest.decode(this,m[e])}}var c=n.getElementsByTagName("interface");c.length>1&&this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!"),0!==c.length&&(c=c[0],this.interfaces.decode(this,c,this.schema.querySelectorAll("[name=interface]")[1]));var d=t.getElementsByTagName("page"),u=this.schema.querySelector("[name=page]");for(e=0;e<d.length;e++){var p=new i(this);p.decode(this,d[e],u),this.pages.push(p);for(var g=0;g<p.repeatCount;g++){var v=new i(this);v.decode(this,d[e],u),v.id+="-repeat-"+g,this.pages.push(v)}}},this.encode=function(){var t=document.implementation.createDocument(null,"waet"),e=t.firstChild;e.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"),e.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");for(var i=t.createElement("setup"),n=s.querySelector("[name=setup]").querySelectorAll("attribute"),a=0;a<n.length;a++){var r=n[a].getAttribute("name");null===r&&(r=n[a].getAttribute("ref")),void 0===this[r]&&"required"!=n[a].getAttribute("use")||i.setAttribute(r,this[r])}if(e.appendChild(i),null!==this.exitText){var o=t.createElement("exitText");o.textContent=this.exitText,i.appendChild(o)}return i.appendChild(this.preTest.encode(t)),i.appendChild(this.postTest.encode(t)),i.appendChild(this.metrics.encode(t)),i.appendChild(this.interfaces.encode(t)),this.pages.forEach(function(i){e.appendChild(i.encode(t))}),t}}