annotate js/specification.js @ 3062:e441e3d5c7e7

Potential implementation of #109
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 31 Oct 2017 15:55:07 +0000
parents ae0950bc1c99
children 998e05c5769a
rev   line source
nicholas@2690 1 /* globals document, console, DOMParser */
nicholas@2224 2 function Specification() {
nicholas@2690 3 var schemaRoot;
nicholas@2690 4 var schemaString;
nicholas@2538 5 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nicholas@2538 6
nicholas@2378 7 // <setup> attributes
nicholas@2688 8 this.interface = undefined;
nicholas@2688 9 this.projectReturn = undefined;
nicholas@2688 10 this.returnURL = undefined;
nicholas@2688 11 this.randomiseOrder = undefined;
nicholas@2688 12 this.poolSize = undefined;
nicholas@2688 13 this.loudness = undefined;
nicholas@2688 14 this.sampleRate = undefined;
nicholas@2688 15 this.calibration = undefined;
nicholas@2688 16 this.crossFade = undefined;
nicholas@2688 17 this.preSilence = undefined;
nicholas@2688 18 this.postSilence = undefined;
nicholas@2688 19 this.playOne = undefined;
nicholas@2822 20 this.minNumberPlays = undefined;
nicholas@2822 21 this.maxNumberPlays = undefined;
nicholas@3062 22 this.randomiseAxisOrder = undefined;
nicholas@2538 23
nicholas@2378 24 // nodes
nicholas@2854 25 this.metrics = new metricNode();
nicholas@2857 26 this.preTest = new surveyNode(this);
nicholas@2857 27 this.postTest = new surveyNode(this);
nicholas@2866 28 this.preTest.location = "pre";
nicholas@2866 29 this.postTest.location = "post";
nicholas@2538 30 this.pages = [];
nicholas@2854 31 this.interfaces = new interfaceNode(this);
nicholas@2538 32 this.errors = [];
nicholas@2224 33 this.exitText = "Thank you.";
nicholas@2538 34
nicholas@2857 35 // Creators
nicholas@2857 36 this.createNewPage = function () {
nicholas@2857 37 var newpage = new page(this);
nicholas@2857 38 this.pages.push(newpage);
nicholas@2857 39 return newpage;
nicholas@2859 40 };
nicholas@2857 41
nicholas@2687 42 var processAttribute = function (attribute, schema) {
nicholas@2538 43 // attribute is the string returned from getAttribute on the XML
nicholas@2538 44 // schema is the <xs:attribute> node
nicholas@2687 45 if (schema.getAttribute('name') === null && schema.getAttribute('ref') !== undefined) {
nicholas@2852 46 schema = schemaRoot.querySelector("[name=" + schema.getAttribute('ref') + "]");
nicholas@2538 47 }
nicholas@2538 48 var defaultOpt = schema.getAttribute('default');
nicholas@2684 49 if (attribute === null) {
nicholas@2538 50 attribute = defaultOpt;
nicholas@2538 51 }
nicholas@2538 52 var dataType = schema.getAttribute('type');
nicholas@2538 53 if (typeof dataType == "string") {
nicholas@2538 54 dataType = dataType.substr(3);
nicholas@2538 55 } else {
nicholas@2852 56 var rest = schema.querySelectorAll("restriction,enumeration");
nicholas@2224 57 if (rest.length > 0) {
nicholas@2224 58 dataType = rest[0].getAttribute("base");
nicholas@2224 59 if (typeof dataType == "string") {
nicholas@2224 60 dataType = dataType.substr(3);
nicholas@2224 61 } else {
nicholas@2224 62 dataType = "string";
nicholas@2224 63 }
nicholas@2224 64 } else {
nicholas@2224 65 dataType = "string";
nicholas@2224 66 }
nicholas@2224 67 }
nicholas@2684 68 if (attribute === null) {
nicholas@2538 69 return attribute;
nicholas@2538 70 }
nicholas@2538 71 switch (dataType) {
nicholas@2538 72 case "boolean":
nicholas@2538 73 if (attribute == 'true') {
nicholas@2538 74 attribute = true;
nicholas@2538 75 } else {
nicholas@2538 76 attribute = false;
nicholas@2538 77 }
nicholas@2538 78 break;
nicholas@2538 79 case "negativeInteger":
nicholas@2538 80 case "positiveInteger":
nicholas@2538 81 case "nonNegativeInteger":
nicholas@2538 82 case "nonPositiveInteger":
nicholas@2538 83 case "integer":
nicholas@2538 84 case "decimal":
nicholas@2538 85 case "short":
nicholas@2538 86 attribute = Number(attribute);
nicholas@2538 87 break;
nicholas@2538 88 default:
nicholas@2538 89 attribute = String(attribute);
nicholas@2538 90 break;
nicholas@2538 91 }
nicholas@2538 92 return attribute;
nicholas@2538 93 };
nicholas@2538 94
nicholas@2687 95 this.processSchema = function (schemaXSD) {
nicholas@2687 96 if (schemaRoot === undefined) {
nicholas@2687 97 schemaString = schemaXSD;
nicholas@2687 98 var parse = new DOMParser();
nicholas@2687 99 schemaRoot = parse.parseFromString(schemaString, 'text/xml');
nicholas@2687 100 Object.defineProperties(this, {
nicholas@2687 101 'schema': {
nicholas@2687 102 'value': schemaRoot
nicholas@2687 103 },
nicholas@2687 104 'schemaString': {
nicholas@2687 105 'value': schemaString
nicholas@2687 106 }
nicholas@2687 107 });
nicholas@2687 108 }
nicholas@2690 109 };
nicholas@2687 110 this.getSchema = function () {
nicholas@2687 111 return schemaRoot;
nicholas@2690 112 };
nicholas@2687 113 this.getSchemaString = function () {
nicholas@2687 114 return schemaString;
nicholas@2690 115 };
nicholas@2687 116
nicholas@2538 117 this.decode = function (projectXML) {
nicholas@2687 118 schemaRoot = this.schema;
nicholas@2538 119 this.errors = [];
nicholas@2538 120 // projectXML - DOM Parsed document
nicholas@2538 121 this.projectXML = projectXML.childNodes[0];
nicholas@2538 122 var setupNode = projectXML.getElementsByTagName('setup')[0];
nicholas@2852 123 var schemaSetup = schemaRoot.querySelector('[name=setup]');
nicholas@2538 124 // First decode the attributes
nicholas@2852 125 var attributes = schemaSetup.querySelectorAll('attribute');
nicholas@2684 126 var i;
nicholas@2684 127 for (i = 0; i < attributes.length; i++) {
nicholas@2538 128 var attributeName = attributes[i].getAttribute('name') || attributes[i].getAttribute('ref');
nicholas@2538 129 var projectAttr = setupNode.getAttribute(attributeName);
nicholas@2692 130 projectAttr = processAttribute(projectAttr, attributes[i]);
nicholas@2687 131 if (projectAttr !== null) {
nicholas@2687 132 this[attributeName] = projectAttr;
nicholas@2538 133 }
nicholas@2538 134
nicholas@2538 135 }
nicholas@2538 136
nicholas@2224 137 var exitTextNode = setupNode.getElementsByTagName('exitText');
nicholas@2224 138 if (exitTextNode.length == 1) {
nicholas@2224 139 this.exitText = exitTextNode[0].textContent;
nicholas@2224 140 }
nicholas@2538 141
nicholas@2538 142 this.metrics.decode(this, setupNode.getElementsByTagName('metric')[0]);
nicholas@2538 143
nicholas@2538 144 // Now process the survey node options
nicholas@2538 145 var survey = setupNode.getElementsByTagName('survey');
nicholas@2684 146 for (i = 0; i < survey.length; i++) {
nicholas@2538 147 var location = survey[i].getAttribute('location');
nicholas@2538 148 switch (location) {
nicholas@2257 149 case 'pre':
nicholas@2257 150 case 'before':
nicholas@2538 151 this.preTest.decode(this, survey[i]);
nicholas@2257 152 break;
nicholas@2257 153 case 'post':
nicholas@2257 154 case 'after':
nicholas@2538 155 this.postTest.decode(this, survey[i]);
nicholas@2257 156 break;
nicholas@2257 157 }
nicholas@2538 158 }
nicholas@2538 159
nicholas@2538 160 var interfaceNode = setupNode.getElementsByTagName('interface');
nicholas@2538 161 if (interfaceNode.length > 1) {
nicholas@2538 162 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
nicholas@2538 163 }
nicholas@2854 164
nicholas@2684 165 if (interfaceNode.length !== 0) {
nicholas@2538 166 interfaceNode = interfaceNode[0];
nicholas@2852 167 this.interfaces.decode(this, interfaceNode, this.schema.querySelectorAll('[name=interface]')[1]);
nicholas@2538 168 }
nicholas@2538 169
nicholas@2538 170 // Page tags
nicholas@2538 171 var pageTags = projectXML.getElementsByTagName('page');
nicholas@2852 172 var pageSchema = this.schema.querySelector('[name=page]');
nicholas@2684 173 for (i = 0; i < pageTags.length; i++) {
nicholas@2854 174 var node = new page(this);
nicholas@2538 175 node.decode(this, pageTags[i], pageSchema);
nicholas@2538 176 this.pages.push(node);
nicholas@2941 177 for (var r = 0; r < node.repeatCount; r++) {
nicholas@2941 178 var repeat = new page(this);
nicholas@2941 179 repeat.decode(this, pageTags[i], pageSchema);
nicholas@2941 180 repeat.id += "-repeat-" + r;
nicholas@2941 181 this.pages.push(repeat);
nicholas@2941 182 }
nicholas@2538 183 }
nicholas@2538 184 };
nicholas@2538 185
nicholas@2538 186 this.encode = function () {
nicholas@2538 187 var RootDocument = document.implementation.createDocument(null, "waet");
nicholas@2538 188 var root = RootDocument.firstChild;
nicholas@2538 189 root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
nicholas@2538 190 root.setAttribute("xsi:noNamespaceSchemaLocation", "test-schema.xsd");
nicholas@2538 191 // Build setup node
nicholas@2224 192 var setup = RootDocument.createElement("setup");
nicholas@2852 193 var schemaSetup = schemaRoot.querySelector('[name=setup]');
nicholas@2224 194 // First decode the attributes
nicholas@2852 195 var attributes = schemaSetup.querySelectorAll('attribute');
nicholas@2538 196 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 197 var name = attributes[i].getAttribute("name");
nicholas@2895 198 if (name === null) {
nicholas@2224 199 name = attributes[i].getAttribute("ref");
nicholas@2224 200 }
nicholas@2685 201 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 202 setup.setAttribute(name, this[name]);
nicholas@2224 203 }
nicholas@2224 204 }
nicholas@2224 205 root.appendChild(setup);
nicholas@2224 206 // Survey node
nicholas@2684 207 if (this.exitText !== null) {
nicholas@2224 208 var exitTextNode = RootDocument.createElement('exitText');
nicholas@2224 209 exitTextNode.textContent = this.exitText;
nicholas@2224 210 setup.appendChild(exitTextNode);
nicholas@2224 211 }
nicholas@2224 212 setup.appendChild(this.preTest.encode(RootDocument));
nicholas@2224 213 setup.appendChild(this.postTest.encode(RootDocument));
nicholas@2224 214 setup.appendChild(this.metrics.encode(RootDocument));
nicholas@2224 215 setup.appendChild(this.interfaces.encode(RootDocument));
nicholas@2684 216 this.pages.forEach(function (page) {
nicholas@2224 217 root.appendChild(page.encode(RootDocument));
nicholas@2684 218 });
nicholas@2538 219 return RootDocument;
nicholas@2538 220 };
nicholas@2538 221
nicholas@2854 222 function surveyNode(specification) {
nicholas@2688 223 this.location = undefined;
nicholas@2538 224 this.options = [];
nicholas@2688 225 this.parent = undefined;
nicholas@3020 226 this.showBackButton = true;
nicholas@2224 227 this.specification = specification;
nicholas@2538 228
nicholas@2857 229 this.addOption = function () {
nicholas@2857 230 var node = new this.OptionNode(this.specification);
nicholas@2857 231 this.options.push(node);
nicholas@2857 232 return node;
nicholas@2859 233 };
nicholas@2857 234
nicholas@2538 235 this.OptionNode = function (specification) {
nicholas@2538 236 this.type = undefined;
n@2582 237 this.schema = undefined;
nicholas@2538 238 this.id = undefined;
nicholas@2224 239 this.name = undefined;
nicholas@2538 240 this.mandatory = undefined;
nicholas@2538 241 this.statement = undefined;
nicholas@2538 242 this.boxsize = undefined;
nicholas@2538 243 this.options = [];
nicholas@2538 244 this.min = undefined;
nicholas@2538 245 this.max = undefined;
nicholas@2774 246 this.minWait = undefined;
nicholas@2538 247 this.step = undefined;
nicholas@2464 248 this.conditions = [];
nicholas@2538 249
nicholas@2538 250 this.decode = function (parent, child) {
nicholas@2852 251 this.schema = schemaRoot.querySelector("[name=" + child.nodeName + "]");
nicholas@2852 252 var attributeMap = this.schema.querySelectorAll('attribute');
nicholas@2684 253 var i;
nicholas@2684 254 for (i in attributeMap) {
nicholas@2684 255 if (isNaN(Number(i)) === true) {
nicholas@2538 256 break;
nicholas@2538 257 }
nicholas@2538 258 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 259 var projectAttr = child.getAttribute(attributeName);
nicholas@2692 260 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 261 if (projectAttr !== null) {
nicholas@2687 262 this[attributeName] = projectAttr;
nicholas@2538 263 }
nicholas@2538 264 }
n@2582 265 if (child.nodeName == 'surveyentry') {
n@2582 266 console.log("NOTE - Use of <surveyelement> is now deprecated. Whilst these will still work, newer nodes and tighter error checking will not be enforced");
n@2582 267 console.log("Please use the newer, type specifc nodes");
n@2582 268 if (!this.type) {
n@2582 269 throw ("Type not specified");
n@2582 270 }
n@2582 271 } else {
n@2582 272 this.type = child.nodeName.split('survey')[1];
n@2582 273 }
nicholas@2538 274 this.statement = child.getElementsByTagName('statement')[0].textContent;
nicholas@2538 275 if (this.type == "checkbox" || this.type == "radio") {
nicholas@2538 276 var children = child.getElementsByTagName('option');
nicholas@2684 277 if (children.length === null) {
nicholas@2538 278 console.log('Malformed' + child.nodeName + 'entry');
nicholas@2538 279 this.statement = 'Malformed' + child.nodeName + 'entry';
nicholas@2538 280 this.type = 'statement';
nicholas@2538 281 } else {
nicholas@2538 282 this.options = [];
nicholas@2684 283 for (i = 0; i < children.length; i++) {
nicholas@2538 284 this.options.push({
nicholas@2538 285 name: children[i].getAttribute('name'),
nicholas@2538 286 text: children[i].textContent
nicholas@2538 287 });
nicholas@2538 288 }
nicholas@2538 289 }
n@2582 290 } else if (this.type == "slider") {
n@2582 291 this.leftText = "";
n@2582 292 this.rightText = "";
n@2582 293 var minText = child.getElementsByTagName("minText");
n@2582 294 var maxText = child.getElementsByTagName("maxText");
n@2582 295 if (minText.length > 0) {
n@2582 296 this.leftText = minText[0].textContent;
n@2582 297 }
n@2582 298 if (maxText.length > 0) {
n@2582 299 this.rightText = maxText[0].textContent;
n@2582 300 }
nicholas@2538 301 }
nicholas@2464 302 var conditionElements = child.getElementsByTagName("conditional");
nicholas@2684 303 for (i = 0; i < conditionElements.length; i++) {
nicholas@2464 304 var condition = conditionElements[i];
nicholas@2464 305 var obj = {
nicholas@2464 306 check: condition.getAttribute("check"),
nicholas@2464 307 value: condition.getAttribute("value"),
nicholas@2464 308 jumpToOnPass: condition.getAttribute("jumpToOnPass"),
nicholas@2464 309 jumpToOnFail: condition.getAttribute("jumpToOnFail")
nicholas@2684 310 };
nicholas@2464 311 this.conditions.push(obj);
nicholas@2464 312 }
nicholas@2538 313 };
nicholas@2538 314
nicholas@2538 315 this.exportXML = function (doc) {
n@2583 316 var node = doc.createElement('survey' + this.type);
nicholas@2538 317 var statement = doc.createElement('statement');
nicholas@2538 318 statement.textContent = this.statement;
nicholas@2538 319 node.appendChild(statement);
nicholas@2224 320 node.id = this.id;
nicholas@2684 321 if (this.name !== undefined) {
nicholas@2538 322 node.setAttribute("name", this.name);
nicholas@2538 323 }
nicholas@2684 324 if (this.mandatory !== undefined) {
nicholas@2538 325 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 326 }
nicholas@2224 327 node.id = this.id;
nicholas@2684 328 if (this.name !== undefined) {
nicholas@2538 329 node.setAttribute("name", this.name);
nicholas@2538 330 }
nicholas@2538 331 switch (this.type) {
nicholas@2224 332 case "checkbox":
nicholas@2684 333 if (this.min !== undefined) {
nicholas@2566 334 node.setAttribute("min", this.min);
nicholas@2566 335 } else {
nicholas@2566 336 node.setAttribute("min", "0");
nicholas@2566 337 }
nicholas@2684 338 if (this.max !== undefined) {
nicholas@2566 339 node.setAttribute("max", this.max);
nicholas@2566 340 } else {
nicholas@2573 341 node.setAttribute("max", "undefined");
nicholas@2686 342 } /* falls through */
nicholas@2224 343 case "radio":
nicholas@2538 344 for (var i = 0; i < this.options.length; i++) {
nicholas@2224 345 var option = this.options[i];
nicholas@2224 346 var optionNode = doc.createElement("option");
nicholas@2538 347 optionNode.setAttribute("name", option.name);
nicholas@2224 348 optionNode.textContent = option.text;
nicholas@2224 349 node.appendChild(optionNode);
nicholas@2224 350 }
nicholas@2562 351 break;
nicholas@2224 352 case "number":
nicholas@2684 353 if (this.min !== undefined) {
nicholas@2538 354 node.setAttribute("min", this.min);
nicholas@2538 355 }
nicholas@2684 356 if (this.max !== undefined) {
nicholas@2538 357 node.setAttribute("max", this.max);
nicholas@2538 358 }
nicholas@2562 359 break;
nicholas@2224 360 case "question":
nicholas@2684 361 if (this.boxsize !== undefined) {
nicholas@2538 362 node.setAttribute("boxsize", this.boxsize);
nicholas@2538 363 }
nicholas@2684 364 if (this.mandatory !== undefined) {
nicholas@2538 365 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 366 }
nicholas@2562 367 break;
nicholas@2562 368 case "video":
nicholas@2684 369 if (this.mandatory !== undefined) {
nicholas@2573 370 node.setAttribute("mandatory", this.mandatory);
nicholas@2686 371 } /* falls through */
nicholas@2562 372 case "youtube":
nicholas@2684 373 if (this.url !== undefined) {
nicholas@2573 374 node.setAttribute("url", this.url);
nicholas@2573 375 }
nicholas@2562 376 break;
n@2583 377 case "slider":
n@2583 378 node.setAttribute("min", this.min);
n@2583 379 node.setAttribute("max", this.max);
n@2583 380 if (this.leftText) {
n@2583 381 var minText = doc.createElement("minText");
n@2583 382 minText.textContent = this.leftText;
n@2583 383 node.appendChild(minText);
n@2583 384 }
n@2583 385 if (this.rightText) {
n@2583 386 var maxText = doc.createElement("maxText");
n@2583 387 maxText.textContent = this.rightText;
n@2583 388 node.appendChild(maxText);
n@2583 389 }
nicholas@2686 390 break;
nicholas@2224 391 default:
nicholas@2224 392 break;
nicholas@2224 393 }
nicholas@2684 394 this.conditions.forEach(function (condition) {
nicholas@2465 395 var conditionDOM = doc.createElement("conditional");
nicholas@2538 396 conditionDOM.setAttribute("check", condition.check);
nicholas@2538 397 conditionDOM.setAttribute("value", condition.value);
nicholas@2538 398 conditionDOM.setAttribute("jumpToOnPass", condition.jumpToOnPass);
nicholas@2538 399 conditionDOM.setAttribute("jumpToOnFail", condition.jumpToOnFail);
nicholas@2465 400 node.appendChild(conditionDOM);
nicholas@2684 401 });
nicholas@2538 402 return node;
nicholas@2538 403 };
nicholas@2538 404 };
nicholas@2538 405 this.decode = function (parent, xml) {
nicholas@2857 406 this.schema = schemaRoot.querySelector('[name=survey]');
nicholas@2224 407 this.parent = parent;
nicholas@2538 408 this.location = xml.getAttribute('location');
nicholas@2538 409 if (this.location == 'before') {
nicholas@2538 410 this.location = 'pre';
nicholas@2538 411 } else if (this.location == 'after') {
nicholas@2538 412 this.location = 'post';
nicholas@2538 413 }
nicholas@3020 414 this.showBackButton = xml.getAttribute("showBackButton");
nicholas@3020 415 if (this.showBackButton == "false") {
nicholas@3020 416 this.showBackButton = false;
nicholas@3020 417 } else {
nicholas@3020 418 this.showBackButton = true;
nicholas@3020 419 }
nicholas@2684 420 var child = xml.firstElementChild;
n@2582 421 while (child) {
nicholas@2538 422 var node = new this.OptionNode(this.specification);
n@2582 423 node.decode(parent, child);
nicholas@2538 424 this.options.push(node);
n@2582 425 child = child.nextElementSibling;
nicholas@2538 426 }
nicholas@2684 427 if (this.options.length === 0) {
nicholas@2257 428 console.log("Empty survey node");
nicholas@2257 429 console.log(this);
nicholas@2257 430 }
nicholas@2538 431 };
nicholas@2538 432 this.encode = function (doc) {
nicholas@2538 433 var node = doc.createElement('survey');
nicholas@2538 434 node.setAttribute('location', this.location);
nicholas@3021 435 node.setAttribute('showBackButton', this.showBackButton);
nicholas@2538 436 for (var i = 0; i < this.options.length; i++) {
nicholas@2538 437 node.appendChild(this.options[i].exportXML(doc));
nicholas@2538 438 }
nicholas@2538 439 return node;
nicholas@2538 440 };
nicholas@2859 441 }
nicholas@2538 442
nicholas@2854 443 function interfaceNode(specification) {
nicholas@2688 444 this.title = undefined;
nicholas@2688 445 this.name = undefined;
nicholas@2777 446 this.image = undefined;
nicholas@2538 447 this.options = [];
nicholas@2538 448 this.scales = [];
nicholas@2854 449 this.schema = undefined;
nicholas@2538 450
nicholas@2538 451 this.decode = function (parent, xml) {
nicholas@2854 452 this.schema = schemaRoot.querySelectorAll('[name=interface]')[1];
nicholas@2538 453 this.name = xml.getAttribute('name');
nicholas@2538 454 var titleNode = xml.getElementsByTagName('title');
nicholas@2538 455 if (titleNode.length == 1) {
nicholas@2538 456 this.title = titleNode[0].textContent;
nicholas@2538 457 }
nicholas@2538 458 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
nicholas@2538 459 // Extract interfaceoption node schema
nicholas@2852 460 var interfaceOptionNodeSchema = this.schema.querySelector('[name=interfaceoption]');
nicholas@2852 461 var attributeMap = interfaceOptionNodeSchema.querySelectorAll('attribute');
nicholas@2684 462 var i, j;
nicholas@2684 463 for (i = 0; i < interfaceOptionNodes.length; i++) {
nicholas@2538 464 var ioNode = interfaceOptionNodes[i];
nicholas@2538 465 var option = {};
nicholas@2684 466 for (j = 0; j < attributeMap.length; j++) {
nicholas@2538 467 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
nicholas@2538 468 var projectAttr = ioNode.getAttribute(attributeName);
nicholas@2692 469 projectAttr = processAttribute(projectAttr, attributeMap[j]);
nicholas@2687 470 if (projectAttr !== null) {
nicholas@2688 471 option[attributeName] = projectAttr;
nicholas@2538 472 }
nicholas@2538 473 }
n@2787 474 if (option.type == "check" && ioNode.firstElementChild) {
n@2787 475 option.errorMessage = ioNode.firstElementChild.textContent;
n@2787 476 }
nicholas@2538 477 this.options.push(option);
nicholas@2538 478 }
nicholas@2777 479 // Get the image node
nicholas@2777 480 var imageNode = xml.getElementsByTagName("image");
nicholas@2777 481 if (imageNode.length == 1) {
nicholas@2777 482 this.image = imageNode[0].getAttribute("src");
nicholas@2777 483 }
nicholas@2538 484 // Now the scales nodes
nicholas@2538 485 var scaleParent = xml.getElementsByTagName('scales');
nicholas@2538 486 if (scaleParent.length == 1) {
nicholas@2538 487 scaleParent = scaleParent[0];
nicholas@2852 488 var scalelabels = scaleParent.querySelectorAll('scalelabel');
nicholas@2684 489 for (i = 0; i < scalelabels.length; i++) {
nicholas@2538 490 this.scales.push({
nicholas@2538 491 text: scalelabels[i].textContent,
nicholas@2538 492 position: Number(scalelabels[i].getAttribute('position'))
nicholas@2538 493 });
nicholas@2538 494 }
nicholas@2538 495 }
nicholas@2538 496 };
nicholas@2538 497
nicholas@2538 498 this.encode = function (doc) {
nicholas@2538 499 var node = doc.createElement("interface");
nicholas@2684 500 if (typeof this.name == "string" && this.name.length > 0)
nicholas@2538 501 node.setAttribute("name", this.name);
nicholas@2243 502 if (typeof this.title == "string") {
nicholas@2243 503 var titleNode = doc.createElement("title");
nicholas@2243 504 titleNode.textContent = this.title;
nicholas@2243 505 node.appendChild(titleNode);
nicholas@2243 506 }
nicholas@2684 507 this.options.forEach(function (option) {
nicholas@2224 508 var child = doc.createElement("interfaceoption");
nicholas@2538 509 child.setAttribute("type", option.type);
nicholas@2538 510 child.setAttribute("name", option.name);
n@2788 511 if (option.type == "check" && option.errorMessage !== undefined) {
n@2788 512 var errorMessage = doc.createElement("errormessage");
n@2788 513 errorMessage.textContent = option.errorMessage;
n@2788 514 child.appendChild(errorMessage);
n@2788 515 }
nicholas@2224 516 node.appendChild(child);
nicholas@2684 517 });
nicholas@2780 518 if (typeof this.image == "string" && this.image.length !== 0) {
nicholas@2780 519 var imgNode = doc.createElement("image");
nicholas@2780 520 imgNode.setAttribute("src", this.image);
nicholas@2780 521 node.appendChild(imgNode);
nicholas@2780 522 }
nicholas@2684 523 if (this.scales.length !== 0) {
nicholas@2224 524 var scales = doc.createElement("scales");
nicholas@2684 525 this.scales.forEach(function (scale) {
nicholas@2224 526 var child = doc.createElement("scalelabel");
nicholas@2538 527 child.setAttribute("position", scale.position);
nicholas@2224 528 child.textContent = scale.text;
nicholas@2224 529 scales.appendChild(child);
nicholas@2684 530 });
nicholas@2224 531 node.appendChild(scales);
nicholas@2224 532 }
nicholas@2224 533 return node;
nicholas@2538 534 };
nicholas@2859 535 }
nicholas@2538 536
nicholas@2854 537 function metricNode() {
nicholas@2224 538 this.enabled = [];
nicholas@2538 539 this.decode = function (parent, xml) {
nicholas@2224 540 var children = xml.getElementsByTagName('metricenable');
nicholas@2538 541 for (var i in children) {
nicholas@2684 542 if (isNaN(Number(i)) === true) {
nicholas@2538 543 break;
nicholas@2538 544 }
nicholas@2224 545 this.enabled.push(children[i].textContent);
nicholas@2224 546 }
nicholas@2684 547 };
nicholas@2538 548 this.encode = function (doc) {
nicholas@2224 549 var node = doc.createElement('metric');
nicholas@2538 550 for (var i in this.enabled) {
nicholas@2684 551 if (isNaN(Number(i)) === true) {
nicholas@2538 552 break;
nicholas@2538 553 }
nicholas@2224 554 var child = doc.createElement('metricenable');
nicholas@2224 555 child.textContent = this.enabled[i];
nicholas@2224 556 node.appendChild(child);
nicholas@2224 557 }
nicholas@2224 558 return node;
nicholas@2684 559 };
nicholas@2859 560 }
nicholas@2538 561
nicholas@2854 562 function page(specification) {
nicholas@2538 563 this.presentedId = undefined;
nicholas@2538 564 this.id = undefined;
nicholas@2470 565 this.title = undefined;
nicholas@2538 566 this.hostURL = undefined;
nicholas@2538 567 this.randomiseOrder = undefined;
nicholas@2538 568 this.loop = undefined;
nicholas@2688 569 this.outsideReference = undefined;
nicholas@2688 570 this.loudness = undefined;
nicholas@2688 571 this.label = undefined;
nicholas@2688 572 this.labelStart = undefined;
nicholas@2857 573 this.preTest = new surveyNode(specification);
nicholas@2857 574 this.postTest = new surveyNode(specification);
nicholas@2866 575 this.preTest.location = "pre";
nicholas@2866 576 this.postTest.location = "post";
nicholas@2538 577 this.interfaces = [];
nicholas@2688 578 this.playOne = undefined;
nicholas@2688 579 this.restrictMovement = undefined;
n@2713 580 this.position = undefined;
nicholas@2538 581 this.commentBoxPrefix = "Comment on track";
nicholas@2822 582 this.minNumberPlays = undefined;
nicholas@2822 583 this.maxNumberPlays = undefined;
nicholas@3062 584 this.randomiseAxisOrder = undefined;
nicholas@2538 585 this.audioElements = [];
nicholas@2538 586 this.commentQuestions = [];
nicholas@2852 587 this.schema = schemaRoot.querySelector("[name=page]");
nicholas@2224 588 this.specification = specification;
nicholas@2688 589 this.parent = undefined;
nicholas@2538 590
nicholas@2859 591 this.addInterface = function () {
nicholas@2859 592 var node = new interfaceNode(specification);
nicholas@2859 593 this.interfaces.push(node);
nicholas@2859 594 return node;
nicholas@2859 595 };
nicholas@2859 596 this.addCommentQuestion = function () {
nicholas@2859 597 var node = new commentQuestionNode(specification);
nicholas@2859 598 this.commentQuestions.push(node);
nicholas@2859 599 return node;
nicholas@2859 600 };
nicholas@2859 601 this.addAudioElement = function () {
nicholas@2859 602 var node = new audioElementNode(specification);
nicholas@2859 603 this.audioElements.push(node);
nicholas@2859 604 return node;
nicholas@2859 605 };
nicholas@2859 606
nicholas@2538 607 this.decode = function (parent, xml) {
nicholas@2224 608 this.parent = parent;
nicholas@2852 609 var attributeMap = this.schema.querySelectorAll('attribute');
nicholas@2684 610 var i, node;
nicholas@2684 611 for (i = 0; i < attributeMap.length; i++) {
nicholas@2538 612 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 613 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 614 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 615 if (projectAttr !== null) {
nicholas@2687 616 this[attributeName] = projectAttr;
nicholas@2538 617 }
nicholas@2538 618 }
nicholas@2538 619
nicholas@2470 620 // Get the title
nicholas@2470 621 var title = xml.getElementsByTagName('title');
nicholas@2684 622 if (title.length !== 0 && title[0].parentElement == xml) {
nicholas@2470 623 this.title = title[0].textContent;
nicholas@2470 624 }
nicholas@2538 625
nicholas@2538 626 // Get the Comment Box Prefix
nicholas@2538 627 var CBP = xml.getElementsByTagName('commentboxprefix');
nicholas@2684 628 if (CBP.length !== 0 && CBP[0].parentElement == xml) {
nicholas@2538 629 this.commentBoxPrefix = CBP[0].textContent;
nicholas@2538 630 }
nicholas@2538 631
nicholas@2538 632 // Now decode the interfaces
nicholas@2854 633 var interfaceNodes = xml.getElementsByTagName('interface');
nicholas@2854 634 for (i = 0; i < interfaceNodes.length; i++) {
nicholas@2854 635 node = new interfaceNode(this.specification);
nicholas@2854 636 node.decode(this, interfaceNodes[i], parent.schema.querySelectorAll('[name=interface]')[1]);
nicholas@2538 637 this.interfaces.push(node);
nicholas@2538 638 }
nicholas@2538 639
nicholas@2538 640 // Now process the survey node options
nicholas@2538 641 var survey = xml.getElementsByTagName('survey');
nicholas@2852 642 var surveySchema = parent.schema.querySelector('[name=survey]');
nicholas@2684 643 for (i = 0; i < survey.length; i++) {
nicholas@2538 644 var location = survey[i].getAttribute('location');
nicholas@2538 645 if (location == 'pre' || location == 'before') {
nicholas@2857 646 if (this.preTest.options.length !== 0) {
nicholas@2538 647 this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
nicholas@2538 648 } else {
nicholas@2538 649 this.preTest.decode(parent, survey[i], surveySchema);
nicholas@2538 650 }
nicholas@2538 651 } else if (location == 'post' || location == 'after') {
nicholas@2857 652 if (this.postTest.options.length !== 0) {
nicholas@2538 653 this.errors.push("Already a post/after test survey defined! Ignoring second!!");
nicholas@2538 654 } else {
nicholas@2538 655 this.postTest.decode(parent, survey[i], surveySchema);
nicholas@2538 656 }
nicholas@2538 657 }
nicholas@2538 658 }
nicholas@2538 659
nicholas@2538 660 // Now process the audioelement tags
nicholas@2538 661 var audioElements = xml.getElementsByTagName('audioelement');
nicholas@2684 662 for (i = 0; i < audioElements.length; i++) {
nicholas@2854 663 var audioNode = new audioElementNode(this.specification);
nicholas@2684 664 audioNode.decode(this, audioElements[i]);
nicholas@2684 665 this.audioElements.push(audioNode);
nicholas@2538 666 }
nicholas@2538 667
nicholas@2538 668 // Now decode the commentquestions
n@2579 669 var cqNode = xml.getElementsByTagName('commentquestions');
nicholas@2684 670 if (cqNode.length !== 0) {
n@2579 671 cqNode = cqNode[0];
nicholas@2644 672 var commentQuestion = cqNode.firstElementChild;
nicholas@2644 673 while (commentQuestion) {
nicholas@2854 674 node = new commentQuestionNode(this.specification);
nicholas@2644 675 node.decode(parent, commentQuestion);
n@2579 676 this.commentQuestions.push(node);
nicholas@2644 677 commentQuestion = commentQuestion.nextElementSibling;
n@2579 678 }
nicholas@2538 679 }
nicholas@2538 680 };
nicholas@2538 681
nicholas@2538 682 this.encode = function (root) {
nicholas@2538 683 var AHNode = root.createElement("page");
nicholas@2224 684 // First decode the attributes
nicholas@2852 685 var attributes = this.schema.querySelectorAll('attribute');
nicholas@2684 686 var i;
nicholas@2684 687 for (i = 0; i < attributes.length; i++) {
nicholas@2224 688 var name = attributes[i].getAttribute("name");
nicholas@2688 689 if (name === null) {
nicholas@2224 690 name = attributes[i].getAttribute("ref");
nicholas@2224 691 }
nicholas@2685 692 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 693 AHNode.setAttribute(name, this[name]);
nicholas@2224 694 }
nicholas@2224 695 }
nicholas@2224 696 // <commentboxprefix>
nicholas@2224 697 var commentboxprefix = root.createElement("commentboxprefix");
nicholas@2224 698 commentboxprefix.textContent = this.commentBoxPrefix;
nicholas@2224 699 AHNode.appendChild(commentboxprefix);
nicholas@2538 700
nicholas@2684 701 for (i = 0; i < this.interfaces.length; i++) {
nicholas@2538 702 AHNode.appendChild(this.interfaces[i].encode(root));
nicholas@2538 703 }
nicholas@2538 704
nicholas@2684 705 for (i = 0; i < this.audioElements.length; i++) {
nicholas@2538 706 AHNode.appendChild(this.audioElements[i].encode(root));
nicholas@2538 707 }
nicholas@2538 708 // Create <CommentQuestion>
nicholas@3036 709 if (this.commentQuestions.length > 0) {
nicholas@3036 710 var node = root.createElement("commentquestions");
nicholas@3036 711 for (i = 0; i < this.commentQuestions.length; i++) {
nicholas@3036 712 node.appendChild(this.commentQuestions[i].encode(root));
nicholas@3036 713 }
nicholas@3036 714 AHNode.appendChild(node);
nicholas@2538 715 }
nicholas@2538 716
nicholas@2538 717 AHNode.appendChild(this.preTest.encode(root));
nicholas@2224 718 AHNode.appendChild(this.postTest.encode(root));
nicholas@2538 719 return AHNode;
nicholas@2538 720 };
nicholas@2538 721
nicholas@2854 722 function commentQuestionNode(specification) {
nicholas@2688 723 this.id = undefined;
nicholas@2224 724 this.name = undefined;
nicholas@2538 725 this.type = undefined;
nicholas@2538 726 this.statement = undefined;
nicholas@3034 727 this.mandatory = undefined;
nicholas@2852 728 this.schema = schemaRoot.querySelector('[name=commentquestion]');
nicholas@2538 729 this.decode = function (parent, xml) {
nicholas@2538 730 this.id = xml.id;
nicholas@2224 731 this.name = xml.getAttribute('name');
nicholas@3034 732 this.mandatory = xml.getAttribute("mandatory") == "true";
nicholas@2688 733 if (this.name === null) {
nicholas@2688 734 this.name = undefined;
nicholas@2688 735 }
n@2579 736 switch (xml.nodeName) {
n@2579 737 case "commentradio":
n@2579 738 this.type = "radio";
n@2579 739 this.options = [];
n@2579 740 break;
n@2579 741 case "commentcheckbox":
n@2579 742 this.type = "checkbox";
n@2579 743 this.options = [];
n@2579 744 break;
n@2579 745 case "commentslider":
n@2579 746 this.type = "slider";
n@2579 747 this.min = undefined;
n@2579 748 this.max = undefined;
n@2579 749 this.step = undefined;
n@2579 750 break;
n@2579 751 case "commentquestion":
n@2579 752 this.type = "question";
n@2579 753 break;
nicholas@2686 754 default:
nicholas@2686 755 throw ("Unknown comment type " + xml.nodeName);
n@2579 756 }
nicholas@2538 757 this.statement = xml.getElementsByTagName('statement')[0].textContent;
n@2579 758 if (this.type == "radio" || this.type == "checkbox") {
n@2579 759 var optNodes = xml.getElementsByTagName('option');
n@2579 760 for (var i = 0; i < optNodes.length; i++) {
n@2579 761 var optNode = optNodes[i];
n@2579 762 this.options.push({
n@2579 763 name: optNode.getAttribute('name'),
n@2579 764 text: optNode.textContent
n@2579 765 });
n@2579 766 }
n@2579 767 }
n@2579 768 if (this.type == "slider") {
n@2579 769 this.min = Number(xml.getAttribute("min"));
n@2579 770 this.max = Number(xml.getAttribute("max"));
n@2579 771 this.step = Number(xml.getAttribute("step"));
nicholas@2684 772 if (this.step === undefined) {
n@2579 773 this.step = 1;
n@2579 774 }
n@2579 775 this.value = Number(xml.getAttribute("value"));
nicholas@2684 776 if (this.value === undefined) {
nicholas@2684 777 this.value = this.min;
n@2579 778 }
n@2580 779 this.leftText = xml.getElementsByTagName("minText");
n@2580 780 if (this.leftText && this.leftText.length > 0) {
n@2580 781 this.leftText = this.leftText[0].textContent;
n@2580 782 } else {
n@2580 783 this.leftText = "";
n@2580 784 }
n@2580 785 this.rightText = xml.getElementsByTagName("maxText");
n@2580 786 if (this.rightText && this.rightText.length > 0) {
n@2580 787 this.rightText = this.rightText[0].textContent;
n@2580 788 } else {
n@2580 789 this.rightText = "";
n@2580 790 }
nicholas@2538 791 }
nicholas@2538 792 };
nicholas@2538 793
nicholas@2538 794 this.encode = function (root) {
n@2579 795 var node;
n@2579 796 switch (this.type) {
n@2579 797 case "radio":
n@2579 798 node = root.createElement("commentradio");
n@2579 799 break;
n@2579 800 case "checkbox":
n@2579 801 node = root.createElement("commentcheckbox");
n@2579 802 break;
n@2579 803 case "slider":
n@2579 804 node = root.createElement("commentslider");
n@2579 805 break;
n@2579 806 case "question":
n@2579 807 node = root.createElement("commentquestion");
n@2579 808 break;
nicholas@2686 809 default:
nicholas@2686 810 throw ("Unknown type " + this.type);
n@2579 811 }
nicholas@2224 812 node.id = this.id;
nicholas@3036 813 node.setAttribute("mandatory", this.mandatory);
nicholas@2538 814 node.setAttribute("type", this.type);
nicholas@2684 815 if (this.name !== undefined) {
nicholas@2538 816 node.setAttribute("name", this.name);
nicholas@2538 817 }
nicholas@2224 818 var statement = root.createElement("statement");
nicholas@2224 819 statement.textContent = this.statement;
nicholas@2224 820 node.appendChild(statement);
n@2579 821 if (this.type == "radio" || this.type == "checkbox") {
nicholas@2684 822 this.options.forEach(function (option) {
n@2579 823 var child = root.createElement("option");
n@2579 824 child.setAttribute("name", option.name);
n@2579 825 child.textContent = option.text;
n@2579 826 node.appendChild(child);
nicholas@2684 827 });
n@2579 828 }
n@2579 829 if (this.type == "slider") {
n@2579 830 node.setAttribute("min", this.min);
n@2579 831 node.setAttribute("max", this.max);
n@2579 832 if (this.step !== 1) {
n@2579 833 node.setAttribute("step", this.step);
n@2579 834 }
n@2579 835 if (this.value !== this.min) {
n@2579 836 node.setAttribute("value", this.value);
n@2579 837 }
n@2580 838 if (this.leftText.length > 0) {
n@2580 839 var leftText = root.createElement("minText");
n@2580 840 leftText.textContent = this.leftText;
n@2580 841 node.appendChild(leftText);
n@2580 842 }
n@2580 843 if (this.rightText.length > 0) {
n@2580 844 var rightText = root.createElement("maxText");
n@2580 845 rightText.textContent = this.rightText;
n@2580 846 node.appendChild(rightText);
n@2580 847 }
nicholas@2224 848 }
nicholas@2224 849 return node;
nicholas@2538 850 };
nicholas@2859 851 }
nicholas@2538 852
nicholas@2854 853 function audioElementNode(specification) {
nicholas@2688 854 this.url = undefined;
nicholas@2688 855 this.id = undefined;
nicholas@2688 856 this.name = undefined;
nicholas@2688 857 this.parent = undefined;
nicholas@2688 858 this.type = undefined;
nicholas@2688 859 this.marker = undefined;
nicholas@2538 860 this.enforce = false;
nicholas@2538 861 this.gain = 0.0;
nicholas@2688 862 this.label = undefined;
nicholas@2460 863 this.startTime = undefined;
nicholas@2460 864 this.stopTime = undefined;
nicholas@2614 865 this.sampleRate = undefined;
nicholas@2777 866 this.image = undefined;
nicholas@2822 867 this.minNumberPlays = undefined;
nicholas@2822 868 this.maxNumberPlays = undefined;
nicholas@2614 869 this.alternatives = [];
nicholas@2852 870 this.schema = schemaRoot.querySelector('[name=audioelement]');
nicholas@2688 871 this.parent = undefined;
nicholas@2538 872 this.decode = function (parent, xml) {
nicholas@2538 873 this.parent = parent;
nicholas@2852 874 var attributeMap = this.schema.querySelectorAll('attribute');
nicholas@2538 875 for (var i = 0; i < attributeMap.length; i++) {
nicholas@2538 876 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 877 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 878 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 879 if (projectAttr !== null) {
nicholas@2687 880 this[attributeName] = projectAttr;
nicholas@2538 881 }
nicholas@2538 882 }
nicholas@2614 883 // Get the alternative nodes
nicholas@2614 884 var child = xml.firstElementChild;
nicholas@2614 885 while (child) {
nicholas@2614 886 if (child.nodeName == "alternative") {
nicholas@2614 887 this.alternatives.push({
nicholas@2614 888 'url': child.getAttribute("url"),
nicholas@2614 889 'sampleRate': child.getAttribute("sampleRate")
nicholas@2614 890 });
nicholas@2614 891 }
nicholas@2614 892 child = child.nextElementSibling;
nicholas@2614 893 }
nicholas@2538 894
nicholas@2538 895 };
nicholas@2538 896 this.encode = function (root) {
nicholas@2538 897 var AENode = root.createElement("audioelement");
nicholas@2852 898 var attributes = this.schema.querySelectorAll('attribute');
nicholas@2538 899 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 900 var name = attributes[i].getAttribute("name");
nicholas@2688 901 if (name === null) {
nicholas@2224 902 name = attributes[i].getAttribute("ref");
nicholas@2224 903 }
nicholas@2685 904 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 905 AENode.setAttribute(name, this[name]);
nicholas@2224 906 }
nicholas@2224 907 }
nicholas@2614 908 this.alternatives.forEach(function (alt) {
nicholas@2614 909 var node = root.createElement("alternative");
nicholas@2614 910 node.setAttribute("url", alt.url);
nicholas@2614 911 node.setAttribute("sampleRate", alt.sampleRate);
nicholas@2614 912 AENode.appendChild(node);
nicholas@2614 913 });
nicholas@2538 914 return AENode;
nicholas@2538 915 };
nicholas@2859 916 }
nicholas@2859 917 }
b@2437 918 }