annotate test_create/test_core.js @ 2862:b8795a6452f8

#94
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 27 Apr 2017 13:06:29 +0100
parents 79258b2a8245
children df1823dbfb93
rev   line source
nicholas@2862 1 /* globals document, angular, window, Promise, XMLHttpRequest, Specification, XMLSerializer, Blob, DOMParser, FileReader*/
nicholas@2851 2 function get(url) {
nicholas@2851 3 // Return a new promise.
nicholas@2851 4 return new Promise(function (resolve, reject) {
nicholas@2851 5 // Do the usual XHR stuff
nicholas@2851 6 var req = new XMLHttpRequest();
nicholas@2851 7 req.open('GET', url);
nickjillings@1370 8
nicholas@2851 9 req.onload = function () {
nicholas@2851 10 // This is called even on 404 etc
nicholas@2851 11 // so check the status
nicholas@2851 12 if (req.status == 200) {
nicholas@2851 13 // Resolve the promise with the response text
nicholas@2851 14 resolve(req.response);
nicholas@2851 15 } else {
nicholas@2851 16 // Otherwise reject with the status text
nicholas@2851 17 // which will hopefully be a meaningful error
nicholas@2851 18 reject(Error(req.statusText));
nicholas@2851 19 }
nicholas@2851 20 };
nicholas@2851 21
nicholas@2851 22 // Handle network errors
nicholas@2851 23 req.onerror = function () {
nicholas@2851 24 reject(Error("Network Error"));
nicholas@2851 25 };
nicholas@2851 26
nicholas@2851 27 // Make the request
nicholas@2851 28 req.send();
nicholas@2851 29 });
nickjillings@1370 30 }
nickjillings@1370 31
nicholas@2851 32 var AngularInterface = angular.module("creator", []);
nickjillings@1370 33
nicholas@2851 34 var specification = new Specification();
nickjillings@1370 35
nicholas@2535 36 window.onload = function () {
nicholas@2851 37 // Get the test interface specifications
nicholas@2535 38
nicholas@2851 39 };
nicholas@2535 40
nicholas@2861 41 function handleFiles(event) {
nicholas@2861 42 var s = angular.element(event.currentTarget).scope();
nicholas@2861 43 s.handleFiles(event);
nicholas@2861 44 s.$apply();
nicholas@2861 45 }
nicholas@2861 46
nicholas@2851 47 AngularInterface.controller("view", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 48 $s.popupVisible = true;
nickjillings@1370 49
nicholas@2851 50 $s.showPopup = function () {
nicholas@2851 51 $s.popupVisible = true;
nicholas@2851 52 };
nicholas@2851 53 $s.hidePopup = function () {
nicholas@2851 54 $s.popupVisible = false;
nicholas@2851 55 };
nicholas@2851 56 $s.globalSchema = undefined;
nicholas@2851 57 get("xml/test-schema.xsd").then(function (text) {
nicholas@2851 58 specification.processSchema(text);
nicholas@2851 59 $s.globalSchema = specification.getSchema();
nicholas@2851 60 });
nicholas@2851 61 $s.specification = specification;
nicholas@2857 62
nicholas@2857 63 $s.addPage = function () {
nicholas@2857 64 $s.specification.createNewPage();
nicholas@2859 65 };
nicholas@2860 66
nicholas@2862 67 $s.removePage = function (page) {
nicholas@2862 68 var index = $s.specification.pages.findIndex(function (a) {
nicholas@2862 69 return a == page;
nicholas@2862 70 });
nicholas@2862 71 if (index === -1) {
nicholas@2862 72 throw ("Invalid Page");
nicholas@2862 73 }
nicholas@2862 74 $s.specification.pages.splice(index, 1);
nicholas@2862 75 };
nicholas@2862 76
nicholas@2860 77 $s.exportXML = function () {
nicholas@2860 78 var s = new XMLSerializer();
nicholas@2860 79 var doc = specification.encode();
nicholas@2860 80 var bb = new Blob([s.serializeToString(doc)], {
nicholas@2860 81 type: 'application/xml'
nicholas@2860 82 });
nicholas@2860 83 var dnlk = window.URL.createObjectURL(bb);
nicholas@2860 84 window.location.href = dnlk;
nicholas@2861 85 };
nicholas@2851 86 }]);
nickjillings@1370 87
nicholas@2851 88 AngularInterface.controller("introduction", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 89 $s.state = 0;
nicholas@2851 90 $s.next = function () {
nicholas@2851 91 $s.state++;
nicholas@2861 92 if ($s.state > 1 || $s.file) {
nicholas@2851 93 $s.hidePopup();
nicholas@2851 94 }
nicholas@2851 95 };
nicholas@2851 96 $s.back = function () {
nicholas@2851 97 $s.state--;
nicholas@2851 98 };
nicholas@2851 99 $s.mouseover = function (name) {
nicholas@2851 100 var obj = $s.interfaces.find(function (i) {
nicholas@2851 101 return i.name == name;
nicholas@2851 102 });
nicholas@2851 103 if (obj) {
nicholas@2851 104 $s.description = obj.description.en;
nicholas@2851 105 }
nicholas@2851 106 };
nicholas@2851 107 $s.initialise = function (name) {
nicholas@2851 108 var obj = $s.interfaces.find(function (i) {
nicholas@2851 109 return i.name == name;
nicholas@2851 110 });
nicholas@2851 111 specification.interface = obj.interface;
nicholas@2851 112 };
nicholas@2851 113 // Get the test interface specifications
nicholas@2851 114 $s.interfaces = {};
nicholas@2861 115 $s.file = undefined;
nicholas@2851 116 $s.description = "";
nicholas@2851 117 var interfaceCollection = new Promise(function (resolve, reject) {
nicholas@2851 118 var xml = new XMLHttpRequest();
nicholas@2851 119 xml.open("GET", "test_create/interfaces/specifications.json");
nicholas@2851 120 xml.onload = function () {
nicholas@2851 121 if (xml.status === 200) {
nicholas@2851 122 resolve(xml.responseText);
nicholas@2851 123 return;
nicholas@2851 124 }
nicholas@2851 125 reject(xml.status);
nicholas@2851 126 };
nicholas@2851 127 xml.onerror = function () {
nicholas@2851 128 reject(new Error("Network Error"));
nicholas@2851 129 };
nicholas@2851 130 xml.send();
nicholas@2851 131 }).then(JSON.parse).then(function (data) {
nicholas@2851 132 $s.interfaces = data.interfaces;
nicholas@2851 133 $s.$apply();
nicholas@2851 134 });
nicholas@2861 135
nicholas@2861 136 $s.handleFiles = function ($event) {
nicholas@2861 137 $s.file = $event.currentTarget.files[0];
nicholas@2861 138 var r = new FileReader();
nicholas@2861 139 r.onload = function () {
nicholas@2861 140 var p = new DOMParser();
nicholas@2861 141 specification.decode(p.parseFromString(r.result, "text/xml"));
nicholas@2861 142 $s.$apply();
nicholas@2862 143 };
nicholas@2861 144 r.readAsText($s.file);
nicholas@2861 145 };
nicholas@2851 146 }]);
nickjillings@1370 147
nicholas@2851 148 AngularInterface.controller("setup", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 149 function initialise() {
nicholas@2851 150 if ($s.globalSchema) {
nicholas@2851 151 $s.schema = $s.globalSchema.querySelector("[name=setup]");
nickjillings@1385 152 }
nickjillings@1370 153 }
nicholas@2851 154 $s.schema = undefined;
nicholas@2851 155 $s.attributes = [];
nickjillings@1370 156
nicholas@2851 157 $s.$watch("globalSchema", initialise);
nicholas@2853 158 $s.$watch("specification.metrics.enabled.length", function () {
nicholas@2853 159 var metricsNode = document.getElementById("metricsNode");
nicholas@2853 160 if (!$s.specification.metrics) {
nicholas@2853 161 return;
nicholas@2853 162 }
nicholas@2853 163 metricsNode.querySelectorAll("input").forEach(function (DOM) {
nicholas@2853 164 DOM.checked = false;
nicholas@2853 165 });
nicholas@2853 166 $s.specification.metrics.enabled.forEach(function (metric) {
nicholas@2853 167 var DOM = metricsNode.querySelector("[value=" + metric + "]");
nicholas@2853 168 if (DOM) {
nicholas@2853 169 DOM.checked = true;
nicholas@2853 170 }
nicholas@2853 171 });
nicholas@2855 172 });
nicholas@2853 173
nicholas@2853 174 $s.enableMetric = function ($event) {
nicholas@2853 175 var metric = $event.currentTarget.value;
nicholas@2853 176 var index = specification.metrics.enabled.findIndex(function (a) {
nicholas@2853 177 return a == metric;
nicholas@2853 178 });
nicholas@2853 179 if ($event.currentTarget.checked) {
nicholas@2853 180 if (index == -1) {
nicholas@2853 181 specification.metrics.enabled.push(metric);
nicholas@2853 182 }
nicholas@2853 183 } else {
nicholas@2853 184 if (index >= 0) {
nicholas@2853 185 specification.metrics.enabled.splice(index, 1);
nicholas@2853 186 }
nicholas@2853 187 }
nicholas@2855 188 };
nicholas@2851 189 }]);
nicholas@2853 190
nicholas@2858 191 AngularInterface.controller("survey", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2858 192 $s.addSurveyEntry = function () {
nicholas@2858 193 $s.survey.addOption();
nicholas@2858 194 };
nicholas@2858 195 $s.removeSurveyEntry = function (entry) {
nicholas@2858 196 var index = $s.survey.options.findIndex(function (a) {
nicholas@2858 197 return a == entry;
nicholas@2858 198 });
nicholas@2858 199 if (index === -1) {
nicholas@2858 200 throw ("Invalid Entry");
nicholas@2858 201 }
nicholas@2858 202 $s.survey.options.splice(index, 1);
nicholas@2858 203 };
nicholas@2858 204 }]);
nicholas@2858 205
nicholas@2853 206 AngularInterface.controller("surveyOption", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2853 207
nicholas@2853 208 $s.removeOption = function (option) {
nicholas@2853 209 var index = $s.opt.options.findIndex(function (a) {
nicholas@2853 210 return a == option;
nicholas@2853 211 });
nicholas@2853 212 if (index === -1) {
nicholas@2853 213 throw ("Invalid option");
nicholas@2853 214 }
nicholas@2853 215 $s.opt.options.splice(index, 1);
nicholas@2853 216 };
nicholas@2853 217 $s.addOption = function () {
nicholas@2853 218 $s.opt.options.push({
nicholas@2853 219 name: "",
nicholas@2853 220 text: ""
nicholas@2853 221 });
nicholas@2855 222 };
nicholas@2857 223
nicholas@2857 224 $s.addCondition = function () {
nicholas@2857 225 $s.opt.conditions.push({
nicholas@2857 226 check: "equals",
nicholas@2857 227 value: "",
nicholas@2857 228 jumpToOnPass: undefined,
nicholas@2857 229 jumpToOnFail: undefined
nicholas@2857 230 });
nicholas@2857 231 };
nicholas@2857 232
nicholas@2857 233 $s.removeCondition = function (condition) {
nicholas@2857 234 var index = $s.opt.conditions.findIndex(function (c) {
nicholas@2857 235 return c == condition;
nicholas@2857 236 });
nicholas@2857 237 if (index === -1) {
nicholas@2857 238 throw ("Invalid Condition");
nicholas@2857 239 }
nicholas@2857 240 $s.opt.conditions.splice(index, 1);
nicholas@2857 241 };
nicholas@2855 242 }]);
nicholas@2855 243
nicholas@2855 244 AngularInterface.controller("interfaceNode", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2855 245 $s.$watch("interface.options.length", function () {
nicholas@2855 246 if (!$s.interface || !$s.interface.options) {
nicholas@2855 247 return;
nicholas@2855 248 }
nicholas@2855 249 var options = $e[0].querySelector(".interfaceOptions").querySelectorAll(".attribute");
nicholas@2855 250 options.forEach(function (option) {
nicholas@2855 251 var name = option.getAttribute("name");
nicholas@2855 252 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 253 return io.name == name;
nicholas@2855 254 });
nicholas@2855 255 option.querySelector("input").checked = (index >= 0);
nicholas@2855 256 if (name == "scalerange" && index >= 0) {
nicholas@2855 257 option.querySelector("[name=min]").value = $s.interface.options[index].min;
nicholas@2855 258 option.querySelector("[name=max]").value = $s.interface.options[index].max;
nicholas@2855 259 }
nicholas@2855 260 });
nicholas@2855 261 });
nicholas@2855 262 $s.enableInterfaceOption = function ($event) {
nicholas@2855 263 var name = $event.currentTarget.parentElement.getAttribute("name");
nicholas@2855 264 var type = $event.currentTarget.parentElement.getAttribute("type");
nicholas@2855 265 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 266 return io.name == name;
nicholas@2855 267 });
nicholas@2855 268 if (index == -1 && $event.currentTarget.checked) {
nicholas@2855 269 var obj = $s.interface.options.push({
nicholas@2855 270 name: name,
nicholas@2855 271 type: type
nicholas@2855 272 });
nicholas@2855 273 if (name == "scalerange") {
nicholas@2855 274 obj.min = $event.currentTarget.parentElement.querySelector("[name=min]").value;
nicholas@2855 275 obj.max = $event.currentTarget.parentElement.querySelector("[name=max]").value;
nicholas@2855 276 }
nicholas@2855 277 } else if (index >= 0 && !$event.currentTarget.checked) {
nicholas@2855 278 $s.interface.options.splice(index, 1);
nicholas@2855 279 }
nicholas@2857 280 };
nicholas@2857 281 $s.removeScale = function (scale) {
nicholas@2857 282 var index = $s.interface.scales.findIndex(function (s) {
nicholas@2857 283 return s == scale;
nicholas@2857 284 });
nicholas@2857 285 if (index >= 0) {
nicholas@2857 286 $s.interface.scales.splice(index, 1);
nicholas@2857 287 }
nicholas@2857 288 };
nicholas@2857 289 $s.addScale = function () {
nicholas@2857 290 $s.interface.scales.push({
nicholas@2857 291 position: undefined,
nicholas@2857 292 text: undefined
nicholas@2857 293 });
nicholas@2857 294 };
nicholas@2853 295 }]);
nicholas@2859 296 AngularInterface.controller("page", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2859 297 $s.addInterface = function () {
nicholas@2859 298 $s.page.addInterface();
nicholas@2859 299 };
nicholas@2859 300 $s.removeInterface = function (node) {
nicholas@2859 301 var index = $s.page.interfaces.findIndex(function (a) {
nicholas@2859 302 return a == node;
nicholas@2859 303 });
nicholas@2859 304 if (index === -1) {
nicholas@2859 305 throw ("Invalid node");
nicholas@2859 306 }
nicholas@2859 307 $s.page.interfaces.splice(index, 1);
nicholas@2859 308 };
nicholas@2859 309
nicholas@2859 310 $s.addCommentQuestion = function () {
nicholas@2859 311 $s.page.addCommentQuestion();
nicholas@2859 312 };
nicholas@2859 313 $s.removeCommentQuestion = function (node) {
nicholas@2859 314 var index = $s.page.commentQuestions.findIndex(function (a) {
nicholas@2859 315 return a == node;
nicholas@2859 316 });
nicholas@2859 317 if (index === -1) {
nicholas@2859 318 throw ("Invalid node");
nicholas@2859 319 }
nicholas@2859 320 $s.page.commentQuestions.splice(index, 1);
nicholas@2859 321 };
nicholas@2859 322 $s.addAudioElement = function () {
nicholas@2859 323 $s.page.addAudioElement();
nicholas@2859 324 };
nicholas@2859 325 $s.removeAudioElement = function (element) {
nicholas@2859 326 var index = $s.page.audioElements.findIndex(function (a) {
nicholas@2859 327 return a == element;
nicholas@2859 328 });
nicholas@2859 329 if (index === -1) {
nicholas@2859 330 throw ("Invalid node");
nicholas@2859 331 }
nicholas@2859 332 $s.page.audioElements.splice(index, 1);
nicholas@2859 333 };
nicholas@2859 334 }]);