annotate js/specification.js @ 2859:d432bf25889b

Finalised test_create UI
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 27 Apr 2017 12:02:28 +0100
parents 80a3b693b3f6
children 3b7b45f9c20d
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@2859 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@2859 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@2859 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@2859 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@2859 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@2859 567 this.addInterface = function () {
nicholas@2859 568 var node = new interfaceNode(specification);
nicholas@2859 569 this.interfaces.push(node);
nicholas@2859 570 return node;
nicholas@2859 571 };
nicholas@2859 572 this.addCommentQuestion = function () {
nicholas@2859 573 var node = new commentQuestionNode(specification);
nicholas@2859 574 this.commentQuestions.push(node);
nicholas@2859 575 return node;
nicholas@2859 576 };
nicholas@2859 577 this.addAudioElement = function () {
nicholas@2859 578 var node = new audioElementNode(specification);
nicholas@2859 579 this.audioElements.push(node);
nicholas@2859 580 return node;
nicholas@2859 581 };
nicholas@2859 582
nicholas@2538 583 this.decode = function (parent, xml) {
nicholas@2224 584 this.parent = parent;
nicholas@2852 585 var attributeMap = this.schema.querySelectorAll('attribute');
nicholas@2684 586 var i, node;
nicholas@2684 587 for (i = 0; i < attributeMap.length; i++) {
nicholas@2538 588 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 589 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 590 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 591 if (projectAttr !== null) {
nicholas@2687 592 this[attributeName] = projectAttr;
nicholas@2538 593 }
nicholas@2538 594 }
nicholas@2538 595
nicholas@2470 596 // Get the title
nicholas@2470 597 var title = xml.getElementsByTagName('title');
nicholas@2684 598 if (title.length !== 0 && title[0].parentElement == xml) {
nicholas@2470 599 this.title = title[0].textContent;
nicholas@2470 600 }
nicholas@2538 601
nicholas@2538 602 // Get the Comment Box Prefix
nicholas@2538 603 var CBP = xml.getElementsByTagName('commentboxprefix');
nicholas@2684 604 if (CBP.length !== 0 && CBP[0].parentElement == xml) {
nicholas@2538 605 this.commentBoxPrefix = CBP[0].textContent;
nicholas@2538 606 }
nicholas@2538 607
nicholas@2538 608 // Now decode the interfaces
nicholas@2854 609 var interfaceNodes = xml.getElementsByTagName('interface');
nicholas@2854 610 for (i = 0; i < interfaceNodes.length; i++) {
nicholas@2854 611 node = new interfaceNode(this.specification);
nicholas@2854 612 node.decode(this, interfaceNodes[i], parent.schema.querySelectorAll('[name=interface]')[1]);
nicholas@2538 613 this.interfaces.push(node);
nicholas@2538 614 }
nicholas@2538 615
nicholas@2538 616 // Now process the survey node options
nicholas@2538 617 var survey = xml.getElementsByTagName('survey');
nicholas@2852 618 var surveySchema = parent.schema.querySelector('[name=survey]');
nicholas@2684 619 for (i = 0; i < survey.length; i++) {
nicholas@2538 620 var location = survey[i].getAttribute('location');
nicholas@2538 621 if (location == 'pre' || location == 'before') {
nicholas@2857 622 if (this.preTest.options.length !== 0) {
nicholas@2538 623 this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
nicholas@2538 624 } else {
nicholas@2538 625 this.preTest.decode(parent, survey[i], surveySchema);
nicholas@2538 626 }
nicholas@2538 627 } else if (location == 'post' || location == 'after') {
nicholas@2857 628 if (this.postTest.options.length !== 0) {
nicholas@2538 629 this.errors.push("Already a post/after test survey defined! Ignoring second!!");
nicholas@2538 630 } else {
nicholas@2538 631 this.postTest.decode(parent, survey[i], surveySchema);
nicholas@2538 632 }
nicholas@2538 633 }
nicholas@2538 634 }
nicholas@2538 635
nicholas@2538 636 // Now process the audioelement tags
nicholas@2538 637 var audioElements = xml.getElementsByTagName('audioelement');
nicholas@2684 638 for (i = 0; i < audioElements.length; i++) {
nicholas@2854 639 var audioNode = new audioElementNode(this.specification);
nicholas@2684 640 audioNode.decode(this, audioElements[i]);
nicholas@2684 641 this.audioElements.push(audioNode);
nicholas@2538 642 }
nicholas@2538 643
nicholas@2538 644 // Now decode the commentquestions
n@2579 645 var cqNode = xml.getElementsByTagName('commentquestions');
nicholas@2684 646 if (cqNode.length !== 0) {
n@2579 647 cqNode = cqNode[0];
nicholas@2644 648 var commentQuestion = cqNode.firstElementChild;
nicholas@2644 649 while (commentQuestion) {
nicholas@2854 650 node = new commentQuestionNode(this.specification);
nicholas@2644 651 node.decode(parent, commentQuestion);
n@2579 652 this.commentQuestions.push(node);
nicholas@2644 653 commentQuestion = commentQuestion.nextElementSibling;
n@2579 654 }
nicholas@2538 655 }
nicholas@2538 656 };
nicholas@2538 657
nicholas@2538 658 this.encode = function (root) {
nicholas@2538 659 var AHNode = root.createElement("page");
nicholas@2224 660 // First decode the attributes
nicholas@2852 661 var attributes = this.schema.querySelectorAll('attribute');
nicholas@2684 662 var i;
nicholas@2684 663 for (i = 0; i < attributes.length; i++) {
nicholas@2224 664 var name = attributes[i].getAttribute("name");
nicholas@2688 665 if (name === null) {
nicholas@2224 666 name = attributes[i].getAttribute("ref");
nicholas@2224 667 }
nicholas@2685 668 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 669 AHNode.setAttribute(name, this[name]);
nicholas@2224 670 }
nicholas@2224 671 }
nicholas@2224 672 // <commentboxprefix>
nicholas@2224 673 var commentboxprefix = root.createElement("commentboxprefix");
nicholas@2224 674 commentboxprefix.textContent = this.commentBoxPrefix;
nicholas@2224 675 AHNode.appendChild(commentboxprefix);
nicholas@2538 676
nicholas@2684 677 for (i = 0; i < this.interfaces.length; i++) {
nicholas@2538 678 AHNode.appendChild(this.interfaces[i].encode(root));
nicholas@2538 679 }
nicholas@2538 680
nicholas@2684 681 for (i = 0; i < this.audioElements.length; i++) {
nicholas@2538 682 AHNode.appendChild(this.audioElements[i].encode(root));
nicholas@2538 683 }
nicholas@2538 684 // Create <CommentQuestion>
nicholas@2684 685 for (i = 0; i < this.commentQuestions.length; i++) {
nicholas@2538 686 AHNode.appendChild(this.commentQuestions[i].encode(root));
nicholas@2538 687 }
nicholas@2538 688
nicholas@2538 689 AHNode.appendChild(this.preTest.encode(root));
nicholas@2224 690 AHNode.appendChild(this.postTest.encode(root));
nicholas@2538 691 return AHNode;
nicholas@2538 692 };
nicholas@2538 693
nicholas@2854 694 function commentQuestionNode(specification) {
nicholas@2688 695 this.id = undefined;
nicholas@2224 696 this.name = undefined;
nicholas@2538 697 this.type = undefined;
nicholas@2538 698 this.statement = undefined;
nicholas@2852 699 this.schema = schemaRoot.querySelector('[name=commentquestion]');
nicholas@2538 700 this.decode = function (parent, xml) {
nicholas@2538 701 this.id = xml.id;
nicholas@2224 702 this.name = xml.getAttribute('name');
nicholas@2688 703 if (this.name === null) {
nicholas@2688 704 this.name = undefined;
nicholas@2688 705 }
n@2579 706 switch (xml.nodeName) {
n@2579 707 case "commentradio":
n@2579 708 this.type = "radio";
n@2579 709 this.options = [];
n@2579 710 break;
n@2579 711 case "commentcheckbox":
n@2579 712 this.type = "checkbox";
n@2579 713 this.options = [];
n@2579 714 break;
n@2579 715 case "commentslider":
n@2579 716 this.type = "slider";
n@2579 717 this.min = undefined;
n@2579 718 this.max = undefined;
n@2579 719 this.step = undefined;
n@2579 720 break;
n@2579 721 case "commentquestion":
n@2579 722 this.type = "question";
n@2579 723 break;
nicholas@2686 724 default:
nicholas@2686 725 throw ("Unknown comment type " + xml.nodeName);
n@2579 726 }
nicholas@2538 727 this.statement = xml.getElementsByTagName('statement')[0].textContent;
n@2579 728 if (this.type == "radio" || this.type == "checkbox") {
n@2579 729 var optNodes = xml.getElementsByTagName('option');
n@2579 730 for (var i = 0; i < optNodes.length; i++) {
n@2579 731 var optNode = optNodes[i];
n@2579 732 this.options.push({
n@2579 733 name: optNode.getAttribute('name'),
n@2579 734 text: optNode.textContent
n@2579 735 });
n@2579 736 }
n@2579 737 }
n@2579 738 if (this.type == "slider") {
n@2579 739 this.min = Number(xml.getAttribute("min"));
n@2579 740 this.max = Number(xml.getAttribute("max"));
n@2579 741 this.step = Number(xml.getAttribute("step"));
nicholas@2684 742 if (this.step === undefined) {
n@2579 743 this.step = 1;
n@2579 744 }
n@2579 745 this.value = Number(xml.getAttribute("value"));
nicholas@2684 746 if (this.value === undefined) {
nicholas@2684 747 this.value = this.min;
n@2579 748 }
n@2580 749 this.leftText = xml.getElementsByTagName("minText");
n@2580 750 if (this.leftText && this.leftText.length > 0) {
n@2580 751 this.leftText = this.leftText[0].textContent;
n@2580 752 } else {
n@2580 753 this.leftText = "";
n@2580 754 }
n@2580 755 this.rightText = xml.getElementsByTagName("maxText");
n@2580 756 if (this.rightText && this.rightText.length > 0) {
n@2580 757 this.rightText = this.rightText[0].textContent;
n@2580 758 } else {
n@2580 759 this.rightText = "";
n@2580 760 }
nicholas@2538 761 }
nicholas@2538 762 };
nicholas@2538 763
nicholas@2538 764 this.encode = function (root) {
n@2579 765 var node;
n@2579 766 switch (this.type) {
n@2579 767 case "radio":
n@2579 768 node = root.createElement("commentradio");
n@2579 769 break;
n@2579 770 case "checkbox":
n@2579 771 node = root.createElement("commentcheckbox");
n@2579 772 break;
n@2579 773 case "slider":
n@2579 774 node = root.createElement("commentslider");
n@2579 775 break;
n@2579 776 case "question":
n@2579 777 node = root.createElement("commentquestion");
n@2579 778 break;
nicholas@2686 779 default:
nicholas@2686 780 throw ("Unknown type " + this.type);
n@2579 781 }
nicholas@2224 782 node.id = this.id;
nicholas@2538 783 node.setAttribute("type", this.type);
nicholas@2684 784 if (this.name !== undefined) {
nicholas@2538 785 node.setAttribute("name", this.name);
nicholas@2538 786 }
nicholas@2224 787 var statement = root.createElement("statement");
nicholas@2224 788 statement.textContent = this.statement;
nicholas@2224 789 node.appendChild(statement);
n@2579 790 if (this.type == "radio" || this.type == "checkbox") {
nicholas@2684 791 this.options.forEach(function (option) {
n@2579 792 var child = root.createElement("option");
n@2579 793 child.setAttribute("name", option.name);
n@2579 794 child.textContent = option.text;
n@2579 795 node.appendChild(child);
nicholas@2684 796 });
n@2579 797 }
n@2579 798 if (this.type == "slider") {
n@2579 799 node.setAttribute("min", this.min);
n@2579 800 node.setAttribute("max", this.max);
n@2579 801 if (this.step !== 1) {
n@2579 802 node.setAttribute("step", this.step);
n@2579 803 }
n@2579 804 if (this.value !== this.min) {
n@2579 805 node.setAttribute("value", this.value);
n@2579 806 }
n@2580 807 if (this.leftText.length > 0) {
n@2580 808 var leftText = root.createElement("minText");
n@2580 809 leftText.textContent = this.leftText;
n@2580 810 node.appendChild(leftText);
n@2580 811 }
n@2580 812 if (this.rightText.length > 0) {
n@2580 813 var rightText = root.createElement("maxText");
n@2580 814 rightText.textContent = this.rightText;
n@2580 815 node.appendChild(rightText);
n@2580 816 }
nicholas@2224 817 }
nicholas@2224 818 return node;
nicholas@2538 819 };
nicholas@2859 820 }
nicholas@2538 821
nicholas@2854 822 function audioElementNode(specification) {
nicholas@2688 823 this.url = undefined;
nicholas@2688 824 this.id = undefined;
nicholas@2688 825 this.name = undefined;
nicholas@2688 826 this.parent = undefined;
nicholas@2688 827 this.type = undefined;
nicholas@2688 828 this.marker = undefined;
nicholas@2538 829 this.enforce = false;
nicholas@2538 830 this.gain = 0.0;
nicholas@2688 831 this.label = undefined;
nicholas@2460 832 this.startTime = undefined;
nicholas@2460 833 this.stopTime = undefined;
nicholas@2614 834 this.sampleRate = undefined;
nicholas@2777 835 this.image = undefined;
nicholas@2614 836 this.alternatives = [];
nicholas@2852 837 this.schema = schemaRoot.querySelector('[name=audioelement]');
nicholas@2688 838 this.parent = undefined;
nicholas@2538 839 this.decode = function (parent, xml) {
nicholas@2538 840 this.parent = parent;
nicholas@2852 841 var attributeMap = this.schema.querySelectorAll('attribute');
nicholas@2538 842 for (var i = 0; i < attributeMap.length; i++) {
nicholas@2538 843 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nicholas@2538 844 var projectAttr = xml.getAttribute(attributeName);
nicholas@2692 845 projectAttr = processAttribute(projectAttr, attributeMap[i]);
nicholas@2687 846 if (projectAttr !== null) {
nicholas@2687 847 this[attributeName] = projectAttr;
nicholas@2538 848 }
nicholas@2538 849 }
nicholas@2614 850 // Get the alternative nodes
nicholas@2614 851 var child = xml.firstElementChild;
nicholas@2614 852 while (child) {
nicholas@2614 853 if (child.nodeName == "alternative") {
nicholas@2614 854 this.alternatives.push({
nicholas@2614 855 'url': child.getAttribute("url"),
nicholas@2614 856 'sampleRate': child.getAttribute("sampleRate")
nicholas@2614 857 });
nicholas@2614 858 }
nicholas@2614 859 child = child.nextElementSibling;
nicholas@2614 860 }
nicholas@2538 861
nicholas@2538 862 };
nicholas@2538 863 this.encode = function (root) {
nicholas@2538 864 var AENode = root.createElement("audioelement");
nicholas@2852 865 var attributes = this.schema.querySelectorAll('attribute');
nicholas@2538 866 for (var i = 0; i < attributes.length; i++) {
nicholas@2224 867 var name = attributes[i].getAttribute("name");
nicholas@2688 868 if (name === null) {
nicholas@2224 869 name = attributes[i].getAttribute("ref");
nicholas@2224 870 }
nicholas@2685 871 if (this[name] !== undefined || attributes[i].getAttribute("use") == "required") {
nicholas@2685 872 AENode.setAttribute(name, this[name]);
nicholas@2224 873 }
nicholas@2224 874 }
nicholas@2614 875 this.alternatives.forEach(function (alt) {
nicholas@2614 876 var node = root.createElement("alternative");
nicholas@2614 877 node.setAttribute("url", alt.url);
nicholas@2614 878 node.setAttribute("sampleRate", alt.sampleRate);
nicholas@2614 879 AENode.appendChild(node);
nicholas@2614 880 });
nicholas@2538 881 return AENode;
nicholas@2538 882 };
nicholas@2859 883 }
nicholas@2859 884 }
b@2437 885 }