annotate test_create/test_core.js @ 2861:79258b2a8245

Completed file upload for test_create
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 27 Apr 2017 12:57:31 +0100
parents 65b8d9ad75cc
children b8795a6452f8
rev   line source
nicholas@2861 1 /* globals document, angular, window, Promise, XMLHttpRequest, Specification, XMLSerializer, Blob, DOMParser*/
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@2860 67 $s.exportXML = function () {
nicholas@2860 68 var s = new XMLSerializer();
nicholas@2860 69 var doc = specification.encode();
nicholas@2860 70 var bb = new Blob([s.serializeToString(doc)], {
nicholas@2860 71 type: 'application/xml'
nicholas@2860 72 });
nicholas@2860 73 var dnlk = window.URL.createObjectURL(bb);
nicholas@2860 74 window.location.href = dnlk;
nicholas@2861 75 };
nicholas@2851 76 }]);
nickjillings@1370 77
nicholas@2851 78 AngularInterface.controller("introduction", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 79 $s.state = 0;
nicholas@2851 80 $s.next = function () {
nicholas@2851 81 $s.state++;
nicholas@2861 82 if ($s.state > 1 || $s.file) {
nicholas@2851 83 $s.hidePopup();
nicholas@2851 84 }
nicholas@2851 85 };
nicholas@2851 86 $s.back = function () {
nicholas@2851 87 $s.state--;
nicholas@2851 88 };
nicholas@2851 89 $s.mouseover = function (name) {
nicholas@2851 90 var obj = $s.interfaces.find(function (i) {
nicholas@2851 91 return i.name == name;
nicholas@2851 92 });
nicholas@2851 93 if (obj) {
nicholas@2851 94 $s.description = obj.description.en;
nicholas@2851 95 }
nicholas@2851 96 };
nicholas@2851 97 $s.initialise = function (name) {
nicholas@2851 98 var obj = $s.interfaces.find(function (i) {
nicholas@2851 99 return i.name == name;
nicholas@2851 100 });
nicholas@2851 101 specification.interface = obj.interface;
nicholas@2851 102 };
nicholas@2851 103 // Get the test interface specifications
nicholas@2851 104 $s.interfaces = {};
nicholas@2861 105 $s.file = undefined;
nicholas@2851 106 $s.description = "";
nicholas@2851 107 var interfaceCollection = new Promise(function (resolve, reject) {
nicholas@2851 108 var xml = new XMLHttpRequest();
nicholas@2851 109 xml.open("GET", "test_create/interfaces/specifications.json");
nicholas@2851 110 xml.onload = function () {
nicholas@2851 111 if (xml.status === 200) {
nicholas@2851 112 resolve(xml.responseText);
nicholas@2851 113 return;
nicholas@2851 114 }
nicholas@2851 115 reject(xml.status);
nicholas@2851 116 };
nicholas@2851 117 xml.onerror = function () {
nicholas@2851 118 reject(new Error("Network Error"));
nicholas@2851 119 };
nicholas@2851 120 xml.send();
nicholas@2851 121 }).then(JSON.parse).then(function (data) {
nicholas@2851 122 $s.interfaces = data.interfaces;
nicholas@2851 123 $s.$apply();
nicholas@2851 124 });
nicholas@2861 125
nicholas@2861 126 $s.handleFiles = function ($event) {
nicholas@2861 127 $s.file = $event.currentTarget.files[0];
nicholas@2861 128 var r = new FileReader();
nicholas@2861 129 r.onload = function () {
nicholas@2861 130 var p = new DOMParser();
nicholas@2861 131 specification.decode(p.parseFromString(r.result, "text/xml"));
nicholas@2861 132 $s.$apply();
nicholas@2861 133 }
nicholas@2861 134 r.readAsText($s.file);
nicholas@2861 135 };
nicholas@2851 136 }]);
nickjillings@1370 137
nicholas@2851 138 AngularInterface.controller("setup", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 139 function initialise() {
nicholas@2851 140 if ($s.globalSchema) {
nicholas@2851 141 $s.schema = $s.globalSchema.querySelector("[name=setup]");
nickjillings@1385 142 }
nickjillings@1370 143 }
nicholas@2851 144 $s.schema = undefined;
nicholas@2851 145 $s.attributes = [];
nickjillings@1370 146
nicholas@2851 147 $s.$watch("globalSchema", initialise);
nicholas@2853 148 $s.$watch("specification.metrics.enabled.length", function () {
nicholas@2853 149 var metricsNode = document.getElementById("metricsNode");
nicholas@2853 150 if (!$s.specification.metrics) {
nicholas@2853 151 return;
nicholas@2853 152 }
nicholas@2853 153 metricsNode.querySelectorAll("input").forEach(function (DOM) {
nicholas@2853 154 DOM.checked = false;
nicholas@2853 155 });
nicholas@2853 156 $s.specification.metrics.enabled.forEach(function (metric) {
nicholas@2853 157 var DOM = metricsNode.querySelector("[value=" + metric + "]");
nicholas@2853 158 if (DOM) {
nicholas@2853 159 DOM.checked = true;
nicholas@2853 160 }
nicholas@2853 161 });
nicholas@2855 162 });
nicholas@2853 163
nicholas@2853 164 $s.enableMetric = function ($event) {
nicholas@2853 165 var metric = $event.currentTarget.value;
nicholas@2853 166 var index = specification.metrics.enabled.findIndex(function (a) {
nicholas@2853 167 return a == metric;
nicholas@2853 168 });
nicholas@2853 169 if ($event.currentTarget.checked) {
nicholas@2853 170 if (index == -1) {
nicholas@2853 171 specification.metrics.enabled.push(metric);
nicholas@2853 172 }
nicholas@2853 173 } else {
nicholas@2853 174 if (index >= 0) {
nicholas@2853 175 specification.metrics.enabled.splice(index, 1);
nicholas@2853 176 }
nicholas@2853 177 }
nicholas@2855 178 };
nicholas@2851 179 }]);
nicholas@2853 180
nicholas@2858 181 AngularInterface.controller("survey", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2858 182 $s.addSurveyEntry = function () {
nicholas@2858 183 $s.survey.addOption();
nicholas@2858 184 };
nicholas@2858 185 $s.removeSurveyEntry = function (entry) {
nicholas@2858 186 var index = $s.survey.options.findIndex(function (a) {
nicholas@2858 187 return a == entry;
nicholas@2858 188 });
nicholas@2858 189 if (index === -1) {
nicholas@2858 190 throw ("Invalid Entry");
nicholas@2858 191 }
nicholas@2858 192 $s.survey.options.splice(index, 1);
nicholas@2858 193 };
nicholas@2858 194 }]);
nicholas@2858 195
nicholas@2853 196 AngularInterface.controller("surveyOption", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2853 197
nicholas@2853 198 $s.removeOption = function (option) {
nicholas@2853 199 var index = $s.opt.options.findIndex(function (a) {
nicholas@2853 200 return a == option;
nicholas@2853 201 });
nicholas@2853 202 if (index === -1) {
nicholas@2853 203 throw ("Invalid option");
nicholas@2853 204 }
nicholas@2853 205 $s.opt.options.splice(index, 1);
nicholas@2853 206 };
nicholas@2853 207 $s.addOption = function () {
nicholas@2853 208 $s.opt.options.push({
nicholas@2853 209 name: "",
nicholas@2853 210 text: ""
nicholas@2853 211 });
nicholas@2855 212 };
nicholas@2857 213
nicholas@2857 214 $s.addCondition = function () {
nicholas@2857 215 $s.opt.conditions.push({
nicholas@2857 216 check: "equals",
nicholas@2857 217 value: "",
nicholas@2857 218 jumpToOnPass: undefined,
nicholas@2857 219 jumpToOnFail: undefined
nicholas@2857 220 });
nicholas@2857 221 };
nicholas@2857 222
nicholas@2857 223 $s.removeCondition = function (condition) {
nicholas@2857 224 var index = $s.opt.conditions.findIndex(function (c) {
nicholas@2857 225 return c == condition;
nicholas@2857 226 });
nicholas@2857 227 if (index === -1) {
nicholas@2857 228 throw ("Invalid Condition");
nicholas@2857 229 }
nicholas@2857 230 $s.opt.conditions.splice(index, 1);
nicholas@2857 231 };
nicholas@2855 232 }]);
nicholas@2855 233
nicholas@2855 234 AngularInterface.controller("interfaceNode", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2855 235 $s.$watch("interface.options.length", function () {
nicholas@2855 236 if (!$s.interface || !$s.interface.options) {
nicholas@2855 237 return;
nicholas@2855 238 }
nicholas@2855 239 var options = $e[0].querySelector(".interfaceOptions").querySelectorAll(".attribute");
nicholas@2855 240 options.forEach(function (option) {
nicholas@2855 241 var name = option.getAttribute("name");
nicholas@2855 242 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 243 return io.name == name;
nicholas@2855 244 });
nicholas@2855 245 option.querySelector("input").checked = (index >= 0);
nicholas@2855 246 if (name == "scalerange" && index >= 0) {
nicholas@2855 247 option.querySelector("[name=min]").value = $s.interface.options[index].min;
nicholas@2855 248 option.querySelector("[name=max]").value = $s.interface.options[index].max;
nicholas@2855 249 }
nicholas@2855 250 });
nicholas@2855 251 });
nicholas@2855 252 $s.enableInterfaceOption = function ($event) {
nicholas@2855 253 var name = $event.currentTarget.parentElement.getAttribute("name");
nicholas@2855 254 var type = $event.currentTarget.parentElement.getAttribute("type");
nicholas@2855 255 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 256 return io.name == name;
nicholas@2855 257 });
nicholas@2855 258 if (index == -1 && $event.currentTarget.checked) {
nicholas@2855 259 var obj = $s.interface.options.push({
nicholas@2855 260 name: name,
nicholas@2855 261 type: type
nicholas@2855 262 });
nicholas@2855 263 if (name == "scalerange") {
nicholas@2855 264 obj.min = $event.currentTarget.parentElement.querySelector("[name=min]").value;
nicholas@2855 265 obj.max = $event.currentTarget.parentElement.querySelector("[name=max]").value;
nicholas@2855 266 }
nicholas@2855 267 } else if (index >= 0 && !$event.currentTarget.checked) {
nicholas@2855 268 $s.interface.options.splice(index, 1);
nicholas@2855 269 }
nicholas@2857 270 };
nicholas@2857 271 $s.removeScale = function (scale) {
nicholas@2857 272 var index = $s.interface.scales.findIndex(function (s) {
nicholas@2857 273 return s == scale;
nicholas@2857 274 });
nicholas@2857 275 if (index >= 0) {
nicholas@2857 276 $s.interface.scales.splice(index, 1);
nicholas@2857 277 }
nicholas@2857 278 };
nicholas@2857 279 $s.addScale = function () {
nicholas@2857 280 $s.interface.scales.push({
nicholas@2857 281 position: undefined,
nicholas@2857 282 text: undefined
nicholas@2857 283 });
nicholas@2857 284 };
nicholas@2853 285 }]);
nicholas@2859 286 AngularInterface.controller("page", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2859 287 $s.addInterface = function () {
nicholas@2859 288 $s.page.addInterface();
nicholas@2859 289 };
nicholas@2859 290 $s.removeInterface = function (node) {
nicholas@2859 291 var index = $s.page.interfaces.findIndex(function (a) {
nicholas@2859 292 return a == node;
nicholas@2859 293 });
nicholas@2859 294 if (index === -1) {
nicholas@2859 295 throw ("Invalid node");
nicholas@2859 296 }
nicholas@2859 297 $s.page.interfaces.splice(index, 1);
nicholas@2859 298 };
nicholas@2859 299
nicholas@2859 300 $s.addCommentQuestion = function () {
nicholas@2859 301 $s.page.addCommentQuestion();
nicholas@2859 302 };
nicholas@2859 303 $s.removeCommentQuestion = function (node) {
nicholas@2859 304 var index = $s.page.commentQuestions.findIndex(function (a) {
nicholas@2859 305 return a == node;
nicholas@2859 306 });
nicholas@2859 307 if (index === -1) {
nicholas@2859 308 throw ("Invalid node");
nicholas@2859 309 }
nicholas@2859 310 $s.page.commentQuestions.splice(index, 1);
nicholas@2859 311 };
nicholas@2859 312 $s.addAudioElement = function () {
nicholas@2859 313 $s.page.addAudioElement();
nicholas@2859 314 };
nicholas@2859 315 $s.removeAudioElement = function (element) {
nicholas@2859 316 var index = $s.page.audioElements.findIndex(function (a) {
nicholas@2859 317 return a == element;
nicholas@2859 318 });
nicholas@2859 319 if (index === -1) {
nicholas@2859 320 throw ("Invalid node");
nicholas@2859 321 }
nicholas@2859 322 $s.page.audioElements.splice(index, 1);
nicholas@2859 323 };
nicholas@2859 324 }]);