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@2851
|
56 }]);
|
nickjillings@1370
|
57
|
nicholas@2851
|
58 AngularInterface.controller("introduction", ['$scope', '$element', '$window', function ($s, $e, $w) {
|
nicholas@2851
|
59 $s.state = 0;
|
nicholas@2851
|
60 $s.next = function () {
|
nicholas@2851
|
61 $s.state++;
|
nicholas@2851
|
62 if ($s.state > 1) {
|
nicholas@2851
|
63 $s.hidePopup();
|
nicholas@2851
|
64 }
|
nicholas@2851
|
65 };
|
nicholas@2851
|
66 $s.back = function () {
|
nicholas@2851
|
67 $s.state--;
|
nicholas@2851
|
68 };
|
nicholas@2851
|
69 $s.mouseover = function (name) {
|
nicholas@2851
|
70 var obj = $s.interfaces.find(function (i) {
|
nicholas@2851
|
71 return i.name == name;
|
nicholas@2851
|
72 });
|
nicholas@2851
|
73 if (obj) {
|
nicholas@2851
|
74 $s.description = obj.description.en;
|
nicholas@2851
|
75 }
|
nicholas@2851
|
76 };
|
nicholas@2851
|
77 $s.initialise = function (name) {
|
nicholas@2851
|
78 var obj = $s.interfaces.find(function (i) {
|
nicholas@2851
|
79 return i.name == name;
|
nicholas@2851
|
80 });
|
nicholas@2851
|
81 specification.interface = obj.interface;
|
nicholas@2851
|
82 };
|
nicholas@2851
|
83 // Get the test interface specifications
|
nicholas@2851
|
84 $s.interfaces = {};
|
nicholas@2851
|
85 $s.description = "";
|
nicholas@2851
|
86 var interfaceCollection = new Promise(function (resolve, reject) {
|
nicholas@2851
|
87 var xml = new XMLHttpRequest();
|
nicholas@2851
|
88 xml.open("GET", "test_create/interfaces/specifications.json");
|
nicholas@2851
|
89 xml.onload = function () {
|
nicholas@2851
|
90 if (xml.status === 200) {
|
nicholas@2851
|
91 resolve(xml.responseText);
|
nicholas@2851
|
92 return;
|
nicholas@2851
|
93 }
|
nicholas@2851
|
94 reject(xml.status);
|
nicholas@2851
|
95 };
|
nicholas@2851
|
96 xml.onerror = function () {
|
nicholas@2851
|
97 reject(new Error("Network Error"));
|
nicholas@2851
|
98 };
|
nicholas@2851
|
99 xml.send();
|
nicholas@2851
|
100 }).then(JSON.parse).then(function (data) {
|
nicholas@2851
|
101 $s.interfaces = data.interfaces;
|
nicholas@2851
|
102 $s.$apply();
|
nicholas@2851
|
103 });
|
nicholas@2851
|
104 }]);
|
nickjillings@1370
|
105
|
nicholas@2851
|
106 AngularInterface.controller("setup", ['$scope', '$element', '$window', function ($s, $e, $w) {
|
nicholas@2851
|
107 function initialise() {
|
nicholas@2851
|
108 if ($s.globalSchema) {
|
nicholas@2851
|
109 $s.schema = $s.globalSchema.querySelector("[name=setup]");
|
nickjillings@1385
|
110 }
|
nickjillings@1370
|
111 }
|
nicholas@2851
|
112 $s.schema = undefined;
|
nicholas@2851
|
113 $s.attributes = [];
|
nickjillings@1370
|
114
|
nicholas@2851
|
115 $s.$watch("globalSchema", initialise);
|
nicholas@2853
|
116 $s.$watch("specification.metrics.enabled.length", function () {
|
nicholas@2853
|
117 var metricsNode = document.getElementById("metricsNode");
|
nicholas@2853
|
118 if (!$s.specification.metrics) {
|
nicholas@2853
|
119 return;
|
nicholas@2853
|
120 }
|
nicholas@2853
|
121 metricsNode.querySelectorAll("input").forEach(function (DOM) {
|
nicholas@2853
|
122 DOM.checked = false;
|
nicholas@2853
|
123 });
|
nicholas@2853
|
124 $s.specification.metrics.enabled.forEach(function (metric) {
|
nicholas@2853
|
125 var DOM = metricsNode.querySelector("[value=" + metric + "]");
|
nicholas@2853
|
126 if (DOM) {
|
nicholas@2853
|
127 DOM.checked = true;
|
nicholas@2853
|
128 }
|
nicholas@2853
|
129 });
|
nicholas@2855
|
130 });
|
nicholas@2853
|
131
|
nicholas@2853
|
132 $s.enableMetric = function ($event) {
|
nicholas@2853
|
133 var metric = $event.currentTarget.value;
|
nicholas@2853
|
134 var index = specification.metrics.enabled.findIndex(function (a) {
|
nicholas@2853
|
135 return a == metric;
|
nicholas@2853
|
136 });
|
nicholas@2853
|
137 if ($event.currentTarget.checked) {
|
nicholas@2853
|
138 if (index == -1) {
|
nicholas@2853
|
139 specification.metrics.enabled.push(metric);
|
nicholas@2853
|
140 }
|
nicholas@2853
|
141 } else {
|
nicholas@2853
|
142 if (index >= 0) {
|
nicholas@2853
|
143 specification.metrics.enabled.splice(index, 1);
|
nicholas@2853
|
144 }
|
nicholas@2853
|
145 }
|
nicholas@2855
|
146 };
|
nicholas@2851
|
147 }]);
|
nicholas@2853
|
148
|
nicholas@2853
|
149 AngularInterface.controller("surveyOption", ['$scope', '$element', '$window', function ($s, $e, $w) {
|
nicholas@2853
|
150
|
nicholas@2853
|
151 $s.removeOption = function (option) {
|
nicholas@2853
|
152 var index = $s.opt.options.findIndex(function (a) {
|
nicholas@2853
|
153 return a == option;
|
nicholas@2853
|
154 });
|
nicholas@2853
|
155 if (index === -1) {
|
nicholas@2853
|
156 throw ("Invalid option");
|
nicholas@2853
|
157 }
|
nicholas@2853
|
158 $s.opt.options.splice(index, 1);
|
nicholas@2853
|
159 };
|
nicholas@2853
|
160 $s.addOption = function () {
|
nicholas@2853
|
161 $s.opt.options.push({
|
nicholas@2853
|
162 name: "",
|
nicholas@2853
|
163 text: ""
|
nicholas@2853
|
164 });
|
nicholas@2855
|
165 };
|
nicholas@2855
|
166 }]);
|
nicholas@2855
|
167
|
nicholas@2855
|
168 AngularInterface.controller("interfaceNode", ['$scope', '$element', '$window', function ($s, $e, $w) {
|
nicholas@2855
|
169 $s.$watch("interface.options.length", function () {
|
nicholas@2855
|
170 if (!$s.interface || !$s.interface.options) {
|
nicholas@2855
|
171 return;
|
nicholas@2855
|
172 }
|
nicholas@2855
|
173 var options = $e[0].querySelector(".interfaceOptions").querySelectorAll(".attribute");
|
nicholas@2855
|
174 options.forEach(function (option) {
|
nicholas@2855
|
175 var name = option.getAttribute("name");
|
nicholas@2855
|
176 var index = $s.interface.options.findIndex(function (io) {
|
nicholas@2855
|
177 return io.name == name;
|
nicholas@2855
|
178 });
|
nicholas@2855
|
179 option.querySelector("input").checked = (index >= 0);
|
nicholas@2855
|
180 if (name == "scalerange" && index >= 0) {
|
nicholas@2855
|
181 option.querySelector("[name=min]").value = $s.interface.options[index].min;
|
nicholas@2855
|
182 option.querySelector("[name=max]").value = $s.interface.options[index].max;
|
nicholas@2855
|
183 }
|
nicholas@2855
|
184 });
|
nicholas@2855
|
185 });
|
nicholas@2855
|
186 $s.enableInterfaceOption = function ($event) {
|
nicholas@2855
|
187 var name = $event.currentTarget.parentElement.getAttribute("name");
|
nicholas@2855
|
188 var type = $event.currentTarget.parentElement.getAttribute("type");
|
nicholas@2855
|
189 var index = $s.interface.options.findIndex(function (io) {
|
nicholas@2855
|
190 return io.name == name;
|
nicholas@2855
|
191 });
|
nicholas@2855
|
192 if (index == -1 && $event.currentTarget.checked) {
|
nicholas@2855
|
193 var obj = $s.interface.options.push({
|
nicholas@2855
|
194 name: name,
|
nicholas@2855
|
195 type: type
|
nicholas@2855
|
196 });
|
nicholas@2855
|
197 if (name == "scalerange") {
|
nicholas@2855
|
198 obj.min = $event.currentTarget.parentElement.querySelector("[name=min]").value;
|
nicholas@2855
|
199 obj.max = $event.currentTarget.parentElement.querySelector("[name=max]").value;
|
nicholas@2855
|
200 }
|
nicholas@2855
|
201 } else if (index >= 0 && !$event.currentTarget.checked) {
|
nicholas@2855
|
202 $s.interface.options.splice(index, 1);
|
nicholas@2855
|
203 }
|
nicholas@2853
|
204 }
|
nicholas@2853
|
205 }]);
|
nicholas@2856
|
206 AngularInterface.controller("page", ['$scope', '$element', '$window', function ($s, $e, $w) {}]);
|