annotate js/specification.js @ 2686:a005429f575b

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