annotate js/specification.js @ 3034:49eca76782d2

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