annotate js/specification.js @ 2857:80a3b693b3f6

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