annotate js/specification.js @ 3021:a5a6c44bab6e

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