annotate js/specification.js @ 2684:f9cba2241c35

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