annotate test_create/test_core.js @ 2857:80a3b693b3f6

More test_create work
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 27 Apr 2017 11:24:16 +0100
parents 5591d01adf1c
children cffde1e75b2d
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@2853 153 AngularInterface.controller("surveyOption", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2853 154
nicholas@2853 155 $s.removeOption = function (option) {
nicholas@2853 156 var index = $s.opt.options.findIndex(function (a) {
nicholas@2853 157 return a == option;
nicholas@2853 158 });
nicholas@2853 159 if (index === -1) {
nicholas@2853 160 throw ("Invalid option");
nicholas@2853 161 }
nicholas@2853 162 $s.opt.options.splice(index, 1);
nicholas@2853 163 };
nicholas@2853 164 $s.addOption = function () {
nicholas@2853 165 $s.opt.options.push({
nicholas@2853 166 name: "",
nicholas@2853 167 text: ""
nicholas@2853 168 });
nicholas@2855 169 };
nicholas@2857 170
nicholas@2857 171 $s.addCondition = function () {
nicholas@2857 172 $s.opt.conditions.push({
nicholas@2857 173 check: "equals",
nicholas@2857 174 value: "",
nicholas@2857 175 jumpToOnPass: undefined,
nicholas@2857 176 jumpToOnFail: undefined
nicholas@2857 177 });
nicholas@2857 178 };
nicholas@2857 179
nicholas@2857 180 $s.removeCondition = function (condition) {
nicholas@2857 181 var index = $s.opt.conditions.findIndex(function (c) {
nicholas@2857 182 return c == condition;
nicholas@2857 183 });
nicholas@2857 184 if (index === -1) {
nicholas@2857 185 throw ("Invalid Condition");
nicholas@2857 186 }
nicholas@2857 187 $s.opt.conditions.splice(index, 1);
nicholas@2857 188 };
nicholas@2855 189 }]);
nicholas@2855 190
nicholas@2855 191 AngularInterface.controller("interfaceNode", ['$scope', '$element', '$window', function ($s, $e, $w) {
nicholas@2855 192 $s.$watch("interface.options.length", function () {
nicholas@2855 193 if (!$s.interface || !$s.interface.options) {
nicholas@2855 194 return;
nicholas@2855 195 }
nicholas@2855 196 var options = $e[0].querySelector(".interfaceOptions").querySelectorAll(".attribute");
nicholas@2855 197 options.forEach(function (option) {
nicholas@2855 198 var name = option.getAttribute("name");
nicholas@2855 199 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 200 return io.name == name;
nicholas@2855 201 });
nicholas@2855 202 option.querySelector("input").checked = (index >= 0);
nicholas@2855 203 if (name == "scalerange" && index >= 0) {
nicholas@2855 204 option.querySelector("[name=min]").value = $s.interface.options[index].min;
nicholas@2855 205 option.querySelector("[name=max]").value = $s.interface.options[index].max;
nicholas@2855 206 }
nicholas@2855 207 });
nicholas@2855 208 });
nicholas@2855 209 $s.enableInterfaceOption = function ($event) {
nicholas@2855 210 var name = $event.currentTarget.parentElement.getAttribute("name");
nicholas@2855 211 var type = $event.currentTarget.parentElement.getAttribute("type");
nicholas@2855 212 var index = $s.interface.options.findIndex(function (io) {
nicholas@2855 213 return io.name == name;
nicholas@2855 214 });
nicholas@2855 215 if (index == -1 && $event.currentTarget.checked) {
nicholas@2855 216 var obj = $s.interface.options.push({
nicholas@2855 217 name: name,
nicholas@2855 218 type: type
nicholas@2855 219 });
nicholas@2855 220 if (name == "scalerange") {
nicholas@2855 221 obj.min = $event.currentTarget.parentElement.querySelector("[name=min]").value;
nicholas@2855 222 obj.max = $event.currentTarget.parentElement.querySelector("[name=max]").value;
nicholas@2855 223 }
nicholas@2855 224 } else if (index >= 0 && !$event.currentTarget.checked) {
nicholas@2855 225 $s.interface.options.splice(index, 1);
nicholas@2855 226 }
nicholas@2857 227 };
nicholas@2857 228 $s.removeScale = function (scale) {
nicholas@2857 229 var index = $s.interface.scales.findIndex(function (s) {
nicholas@2857 230 return s == scale;
nicholas@2857 231 });
nicholas@2857 232 if (index >= 0) {
nicholas@2857 233 $s.interface.scales.splice(index, 1);
nicholas@2857 234 }
nicholas@2857 235 };
nicholas@2857 236 $s.addScale = function () {
nicholas@2857 237 $s.interface.scales.push({
nicholas@2857 238 position: undefined,
nicholas@2857 239 text: undefined
nicholas@2857 240 });
nicholas@2857 241 };
nicholas@2853 242 }]);
nicholas@2856 243 AngularInterface.controller("page", ['$scope', '$element', '$window', function ($s, $e, $w) {}]);