annotate test_create/test_core.js @ 2858:cffde1e75b2d

Test create: Completed setup node
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 27 Apr 2017 11:43:08 +0100
parents 80a3b693b3f6
children d432bf25889b
rev   line source
nicholas@2855 1 /* globals document, angular, window, Promise, XMLHttpRequest, Specification */
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@2851 41 AngularInterface.controller("view", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 42 $s.popupVisible = true;
nickjillings@1370 43
nicholas@2851 44 $s.showPopup = function () {
nicholas@2851 45 $s.popupVisible = true;
nicholas@2851 46 };
nicholas@2851 47 $s.hidePopup = function () {
nicholas@2851 48 $s.popupVisible = false;
nicholas@2851 49 };
nicholas@2851 50 $s.globalSchema = undefined;
nicholas@2851 51 get("xml/test-schema.xsd").then(function (text) {
nicholas@2851 52 specification.processSchema(text);
nicholas@2851 53 $s.globalSchema = specification.getSchema();
nicholas@2851 54 });
nicholas@2851 55 $s.specification = specification;
nicholas@2857 56
nicholas@2857 57 $s.addPage = function () {
nicholas@2857 58 $s.specification.createNewPage();
nicholas@2857 59 }
nicholas@2851 60 }]);
nickjillings@1370 61
nicholas@2851 62 AngularInterface.controller("introduction", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 63 $s.state = 0;
nicholas@2851 64 $s.next = function () {
nicholas@2851 65 $s.state++;
nicholas@2851 66 if ($s.state > 1) {
nicholas@2851 67 $s.hidePopup();
nicholas@2851 68 }
nicholas@2851 69 };
nicholas@2851 70 $s.back = function () {
nicholas@2851 71 $s.state--;
nicholas@2851 72 };
nicholas@2851 73 $s.mouseover = function (name) {
nicholas@2851 74 var obj = $s.interfaces.find(function (i) {
nicholas@2851 75 return i.name == name;
nicholas@2851 76 });
nicholas@2851 77 if (obj) {
nicholas@2851 78 $s.description = obj.description.en;
nicholas@2851 79 }
nicholas@2851 80 };
nicholas@2851 81 $s.initialise = function (name) {
nicholas@2851 82 var obj = $s.interfaces.find(function (i) {
nicholas@2851 83 return i.name == name;
nicholas@2851 84 });
nicholas@2851 85 specification.interface = obj.interface;
nicholas@2851 86 };
nicholas@2851 87 // Get the test interface specifications
nicholas@2851 88 $s.interfaces = {};
nicholas@2851 89 $s.description = "";
nicholas@2851 90 var interfaceCollection = new Promise(function (resolve, reject) {
nicholas@2851 91 var xml = new XMLHttpRequest();
nicholas@2851 92 xml.open("GET", "test_create/interfaces/specifications.json");
nicholas@2851 93 xml.onload = function () {
nicholas@2851 94 if (xml.status === 200) {
nicholas@2851 95 resolve(xml.responseText);
nicholas@2851 96 return;
nicholas@2851 97 }
nicholas@2851 98 reject(xml.status);
nicholas@2851 99 };
nicholas@2851 100 xml.onerror = function () {
nicholas@2851 101 reject(new Error("Network Error"));
nicholas@2851 102 };
nicholas@2851 103 xml.send();
nicholas@2851 104 }).then(JSON.parse).then(function (data) {
nicholas@2851 105 $s.interfaces = data.interfaces;
nicholas@2851 106 $s.$apply();
nicholas@2851 107 });
nicholas@2851 108 }]);
nickjillings@1370 109
nicholas@2851 110 AngularInterface.controller("setup", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2851 111 function initialise() {
nicholas@2851 112 if ($s.globalSchema) {
nicholas@2851 113 $s.schema = $s.globalSchema.querySelector("[name=setup]");
nickjillings@1385 114 }
nickjillings@1370 115 }
nicholas@2851 116 $s.schema = undefined;
nicholas@2851 117 $s.attributes = [];
nickjillings@1370 118
nicholas@2851 119 $s.$watch("globalSchema", initialise);
nicholas@2853 120 $s.$watch("specification.metrics.enabled.length", function () {
nicholas@2853 121 var metricsNode = document.getElementById("metricsNode");
nicholas@2853 122 if (!$s.specification.metrics) {
nicholas@2853 123 return;
nicholas@2853 124 }
nicholas@2853 125 metricsNode.querySelectorAll("input").forEach(function (DOM) {
nicholas@2853 126 DOM.checked = false;
nicholas@2853 127 });
nicholas@2853 128 $s.specification.metrics.enabled.forEach(function (metric) {
nicholas@2853 129 var DOM = metricsNode.querySelector("[value=" + metric + "]");
nicholas@2853 130 if (DOM) {
nicholas@2853 131 DOM.checked = true;
nicholas@2853 132 }
nicholas@2853 133 });
nicholas@2855 134 });
nicholas@2853 135
nicholas@2853 136 $s.enableMetric = function ($event) {
nicholas@2853 137 var metric = $event.currentTarget.value;
nicholas@2853 138 var index = specification.metrics.enabled.findIndex(function (a) {
nicholas@2853 139 return a == metric;
nicholas@2853 140 });
nicholas@2853 141 if ($event.currentTarget.checked) {
nicholas@2853 142 if (index == -1) {
nicholas@2853 143 specification.metrics.enabled.push(metric);
nicholas@2853 144 }
nicholas@2853 145 } else {
nicholas@2853 146 if (index >= 0) {
nicholas@2853 147 specification.metrics.enabled.splice(index, 1);
nicholas@2853 148 }
nicholas@2853 149 }
nicholas@2855 150 };
nicholas@2851 151 }]);
nicholas@2853 152
nicholas@2858 153 AngularInterface.controller("survey", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2858 154 $s.addSurveyEntry = function () {
nicholas@2858 155 $s.survey.addOption();
nicholas@2858 156 };
nicholas@2858 157 $s.removeSurveyEntry = function (entry) {
nicholas@2858 158 var index = $s.survey.options.findIndex(function (a) {
nicholas@2858 159 return a == entry;
nicholas@2858 160 });
nicholas@2858 161 if (index === -1) {
nicholas@2858 162 throw ("Invalid Entry");
nicholas@2858 163 }
nicholas@2858 164 $s.survey.options.splice(index, 1);
nicholas@2858 165 };
nicholas@2858 166 }]);
nicholas@2858 167
nicholas@2853 168 AngularInterface.controller("surveyOption", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2853 169
nicholas@2853 170 $s.removeOption = function (option) {
nicholas@2853 171 var index = $s.opt.options.findIndex(function (a) {
nicholas@2853 172 return a == option;
nicholas@2853 173 });
nicholas@2853 174 if (index === -1) {
nicholas@2853 175 throw ("Invalid option");
nicholas@2853 176 }
nicholas@2853 177 $s.opt.options.splice(index, 1);
nicholas@2853 178 };
nicholas@2853 179 $s.addOption = function () {
nicholas@2853 180 $s.opt.options.push({
nicholas@2853 181 name: "",
nicholas@2853 182 text: ""
nicholas@2853 183 });
nicholas@2855 184 };
nicholas@2857 185
nicholas@2857 186 $s.addCondition = function () {
nicholas@2857 187 $s.opt.conditions.push({
nicholas@2857 188 check: "equals",
nicholas@2857 189 value: "",
nicholas@2857 190 jumpToOnPass: undefined,
nicholas@2857 191 jumpToOnFail: undefined
nicholas@2857 192 });
nicholas@2857 193 };
nicholas@2857 194
nicholas@2857 195 $s.removeCondition = function (condition) {
nicholas@2857 196 var index = $s.opt.conditions.findIndex(function (c) {
nicholas@2857 197 return c == condition;
nicholas@2857 198 });
nicholas@2857 199 if (index === -1) {
nicholas@2857 200 throw ("Invalid Condition");
nicholas@2857 201 }
nicholas@2857 202 $s.opt.conditions.splice(index, 1);
nicholas@2857 203 };
nicholas@2855 204 }]);
nicholas@2855 205
nicholas@2855 206 AngularInterface.controller("interfaceNode", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2855 207 $s.$watch("interface.options.length", function () {
nicholas@2855 208 if (!$s.interface || !$s.interface.options) {
nicholas@2855 209 return;
nicholas@2855 210 }
nicholas@2855 211 var options = $e[0].querySelector(".interfaceOptions").querySelectorAll(".attribute");
nicholas@2855 212 options.forEach(function (option) {
nicholas@2855 213 var name = option.getAttribute("name");
nicholas@2855 214 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 215 return io.name == name;
nicholas@2855 216 });
nicholas@2855 217 option.querySelector("input").checked = (index >= 0);
nicholas@2855 218 if (name == "scalerange" && index >= 0) {
nicholas@2855 219 option.querySelector("[name=min]").value = $s.interface.options[index].min;
nicholas@2855 220 option.querySelector("[name=max]").value = $s.interface.options[index].max;
nicholas@2855 221 }
nicholas@2855 222 });
nicholas@2855 223 });
nicholas@2855 224 $s.enableInterfaceOption = function ($event) {
nicholas@2855 225 var name = $event.currentTarget.parentElement.getAttribute("name");
nicholas@2855 226 var type = $event.currentTarget.parentElement.getAttribute("type");
nicholas@2855 227 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 228 return io.name == name;
nicholas@2855 229 });
nicholas@2855 230 if (index == -1 && $event.currentTarget.checked) {
nicholas@2855 231 var obj = $s.interface.options.push({
nicholas@2855 232 name: name,
nicholas@2855 233 type: type
nicholas@2855 234 });
nicholas@2855 235 if (name == "scalerange") {
nicholas@2855 236 obj.min = $event.currentTarget.parentElement.querySelector("[name=min]").value;
nicholas@2855 237 obj.max = $event.currentTarget.parentElement.querySelector("[name=max]").value;
nicholas@2855 238 }
nicholas@2855 239 } else if (index >= 0 && !$event.currentTarget.checked) {
nicholas@2855 240 $s.interface.options.splice(index, 1);
nicholas@2855 241 }
nicholas@2857 242 };
nicholas@2857 243 $s.removeScale = function (scale) {
nicholas@2857 244 var index = $s.interface.scales.findIndex(function (s) {
nicholas@2857 245 return s == scale;
nicholas@2857 246 });
nicholas@2857 247 if (index >= 0) {
nicholas@2857 248 $s.interface.scales.splice(index, 1);
nicholas@2857 249 }
nicholas@2857 250 };
nicholas@2857 251 $s.addScale = function () {
nicholas@2857 252 $s.interface.scales.push({
nicholas@2857 253 position: undefined,
nicholas@2857 254 text: undefined
nicholas@2857 255 });
nicholas@2857 256 };
nicholas@2853 257 }]);
nicholas@2856 258 AngularInterface.controller("page", ['$scope', '$element', '$window', function ($s, $e, $w) {}]);