annotate js/specification.js @ 2822:394b5e20d78f

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