annotate js/specification.js @ 2644:9c84c368f4a8

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