nickjillings@1453
|
1 /**
|
nickjillings@1453
|
2 * core.js
|
nickjillings@1453
|
3 *
|
nickjillings@1453
|
4 * Main script to run, calls all other core functions and manages loading/store to backend.
|
nickjillings@1453
|
5 * Also contains all global variables.
|
nickjillings@1453
|
6 */
|
nickjillings@1453
|
7
|
nickjillings@1453
|
8 /* create the web audio API context and store in audioContext*/
|
nickjillings@1453
|
9 var audioContext; // Hold the browser web audio API
|
nickjillings@1453
|
10 var projectXML; // Hold the parsed setup XML
|
nickjillings@1453
|
11 var specification;
|
nickjillings@1453
|
12 var interfaceContext;
|
nickjillings@1453
|
13 var popup; // Hold the interfacePopup object
|
nickjillings@1453
|
14 var testState;
|
nickjillings@1453
|
15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
|
nickjillings@1453
|
16 var audioEngineContext; // The custome AudioEngine object
|
nickjillings@1453
|
17 var projectReturn; // Hold the URL for the return
|
nickjillings@1453
|
18
|
nickjillings@1453
|
19
|
nickjillings@1453
|
20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
|
nickjillings@1453
|
21 AudioBufferSourceNode.prototype.owner = undefined;
|
nickjillings@1453
|
22
|
nickjillings@1453
|
23 window.onload = function() {
|
nickjillings@1453
|
24 // Function called once the browser has loaded all files.
|
nickjillings@1453
|
25 // This should perform any initial commands such as structure / loading documents
|
nickjillings@1453
|
26
|
nickjillings@1453
|
27 // Create a web audio API context
|
nickjillings@1453
|
28 // Fixed for cross-browser support
|
nickjillings@1453
|
29 var AudioContext = window.AudioContext || window.webkitAudioContext;
|
nickjillings@1453
|
30 audioContext = new AudioContext;
|
nickjillings@1453
|
31
|
nickjillings@1453
|
32 // Create test state
|
nickjillings@1453
|
33 testState = new stateMachine();
|
nickjillings@1453
|
34
|
nickjillings@1453
|
35 // Create the audio engine object
|
nickjillings@1453
|
36 audioEngineContext = new AudioEngine();
|
nickjillings@1453
|
37
|
nickjillings@1453
|
38 // Create the popup interface object
|
nickjillings@1453
|
39 popup = new interfacePopup();
|
nickjillings@1453
|
40
|
nickjillings@1453
|
41 // Create the specification object
|
nickjillings@1453
|
42 specification = new Specification();
|
nickjillings@1453
|
43
|
nickjillings@1453
|
44 // Create the interface object
|
nickjillings@1453
|
45 interfaceContext = new Interface(specification);
|
nickjillings@1453
|
46 };
|
nickjillings@1453
|
47
|
nickjillings@1453
|
48 function interfacePopup() {
|
nickjillings@1453
|
49 // Creates an object to manage the popup
|
nickjillings@1453
|
50 this.popup = null;
|
nickjillings@1453
|
51 this.popupContent = null;
|
nickjillings@1453
|
52 this.popupTitle = null;
|
nickjillings@1453
|
53 this.popupResponse = null;
|
nickjillings@1453
|
54 this.buttonProceed = null;
|
nickjillings@1453
|
55 this.buttonPrevious = null;
|
nickjillings@1453
|
56 this.popupOptions = null;
|
nickjillings@1453
|
57 this.currentIndex = null;
|
nickjillings@1453
|
58 this.responses = null;
|
nickjillings@1453
|
59
|
nickjillings@1453
|
60 this.createPopup = function(){
|
nickjillings@1453
|
61 // Create popup window interface
|
nickjillings@1453
|
62 var insertPoint = document.getElementById("topLevelBody");
|
nickjillings@1453
|
63 var blank = document.createElement('div');
|
nickjillings@1453
|
64 blank.className = 'testHalt';
|
nickjillings@1453
|
65
|
nickjillings@1453
|
66 this.popup = document.createElement('div');
|
nickjillings@1453
|
67 this.popup.id = 'popupHolder';
|
nickjillings@1453
|
68 this.popup.className = 'popupHolder';
|
nickjillings@1453
|
69 this.popup.style.position = 'absolute';
|
nickjillings@1453
|
70 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
|
nickjillings@1453
|
71 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
|
nickjillings@1453
|
72
|
nickjillings@1453
|
73 this.popupContent = document.createElement('div');
|
nickjillings@1453
|
74 this.popupContent.id = 'popupContent';
|
nickjillings@1453
|
75 this.popupContent.style.marginTop = '20px';
|
nickjillings@1453
|
76 this.popupContent.align = 'center';
|
nickjillings@1453
|
77 this.popup.appendChild(this.popupContent);
|
nickjillings@1453
|
78
|
nickjillings@1453
|
79 var titleHolder = document.createElement('div');
|
nickjillings@1453
|
80 titleHolder.id = 'popupTitleHolder';
|
nickjillings@1453
|
81 titleHolder.style.width = 'inherit';
|
nickjillings@1453
|
82 titleHolder.style.height = '25px';
|
nickjillings@1453
|
83 titleHolder.style.marginBottom = '5px';
|
nickjillings@1453
|
84
|
nickjillings@1453
|
85 this.popupTitle = document.createElement('span');
|
nickjillings@1453
|
86 this.popupTitle.id = 'popupTitle';
|
nickjillings@1453
|
87 titleHolder.appendChild(this.popupTitle);
|
nickjillings@1453
|
88 this.popupContent.appendChild(titleHolder);
|
nickjillings@1453
|
89
|
nickjillings@1453
|
90 this.popupResponse = document.createElement('div');
|
nickjillings@1453
|
91 this.popupResponse.id = 'popupResponse';
|
nickjillings@1453
|
92 this.popupResponse.style.width = 'inherit';
|
nickjillings@1453
|
93 this.popupResponse.style.minHeight = '170px';
|
nickjillings@1453
|
94 this.popupResponse.style.maxHeight = '320px';
|
nickjillings@1453
|
95 this.popupResponse.style.overflow = 'auto';
|
nickjillings@1453
|
96 this.popupContent.appendChild(this.popupResponse);
|
nickjillings@1453
|
97
|
nickjillings@1453
|
98 var buttonHolder = document.createElement('div');
|
nickjillings@1453
|
99 buttonHolder.id='buttonHolder';
|
nickjillings@1453
|
100 buttonHolder.width = 'inherit';
|
nickjillings@1453
|
101 buttonHolder.style.height= '30px';
|
nickjillings@1453
|
102 buttonHolder.align = 'left';
|
nickjillings@1453
|
103 this.popupContent.appendChild(buttonHolder);
|
nickjillings@1453
|
104
|
nickjillings@1453
|
105 this.buttonProceed = document.createElement('button');
|
nickjillings@1453
|
106 this.buttonProceed.className = 'popupButton';
|
nickjillings@1453
|
107 this.buttonProceed.style.left = '390px';
|
nickjillings@1453
|
108 this.buttonProceed.style.top = '2px';
|
nickjillings@1453
|
109 this.buttonProceed.innerHTML = 'Next';
|
nickjillings@1453
|
110 this.buttonProceed.onclick = function(){popup.proceedClicked();};
|
nickjillings@1453
|
111
|
nickjillings@1453
|
112 this.buttonPrevious = document.createElement('button');
|
nickjillings@1453
|
113 this.buttonPrevious.className = 'popupButton';
|
nickjillings@1453
|
114 this.buttonPrevious.style.left = '10px';
|
nickjillings@1453
|
115 this.buttonPrevious.style.top = '2px';
|
nickjillings@1453
|
116 this.buttonPrevious.innerHTML = 'Back';
|
nickjillings@1453
|
117 this.buttonPrevious.onclick = function(){popup.previousClick();};
|
nickjillings@1453
|
118
|
nickjillings@1453
|
119 buttonHolder.appendChild(this.buttonPrevious);
|
nickjillings@1453
|
120 buttonHolder.appendChild(this.buttonProceed);
|
nickjillings@1453
|
121
|
nickjillings@1453
|
122 this.popup.style.zIndex = -1;
|
nickjillings@1453
|
123 this.popup.style.visibility = 'hidden';
|
nickjillings@1453
|
124 blank.style.zIndex = -2;
|
nickjillings@1453
|
125 blank.style.visibility = 'hidden';
|
nickjillings@1453
|
126 insertPoint.appendChild(this.popup);
|
nickjillings@1453
|
127 insertPoint.appendChild(blank);
|
nickjillings@1453
|
128 };
|
nickjillings@1453
|
129
|
nickjillings@1453
|
130 this.showPopup = function(){
|
nickjillings@1453
|
131 if (this.popup == null) {
|
nickjillings@1453
|
132 this.createPopup();
|
nickjillings@1453
|
133 }
|
nickjillings@1453
|
134 this.popup.style.zIndex = 3;
|
nickjillings@1453
|
135 this.popup.style.visibility = 'visible';
|
nickjillings@1453
|
136 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1453
|
137 blank.style.zIndex = 2;
|
nickjillings@1453
|
138 blank.style.visibility = 'visible';
|
nickjillings@1453
|
139 $(window).keypress(function(e){
|
nickjillings@1453
|
140 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible')
|
nickjillings@1453
|
141 {
|
nickjillings@1453
|
142 // Enter key pressed
|
nickjillings@1453
|
143 var textarea = $(popup.popupContent).find('textarea');
|
nickjillings@1453
|
144 if (textarea.length != 0)
|
nickjillings@1453
|
145 {
|
nickjillings@1453
|
146 if (textarea[0] == document.activeElement)
|
nickjillings@1453
|
147 {return;}
|
nickjillings@1453
|
148 }
|
nickjillings@1453
|
149 popup.buttonProceed.onclick();
|
nickjillings@1453
|
150 }
|
nickjillings@1453
|
151 });
|
nickjillings@1453
|
152 };
|
nickjillings@1453
|
153
|
nickjillings@1453
|
154 this.hidePopup = function(){
|
nickjillings@1453
|
155 this.popup.style.zIndex = -1;
|
nickjillings@1453
|
156 this.popup.style.visibility = 'hidden';
|
nickjillings@1453
|
157 var blank = document.getElementsByClassName('testHalt')[0];
|
nickjillings@1453
|
158 blank.style.zIndex = -2;
|
nickjillings@1453
|
159 blank.style.visibility = 'hidden';
|
nickjillings@1453
|
160 this.buttonPrevious.style.visibility = 'inherit';
|
nickjillings@1453
|
161 };
|
nickjillings@1453
|
162
|
nickjillings@1453
|
163 this.postNode = function() {
|
nickjillings@1453
|
164 // This will take the node from the popupOptions and display it
|
nickjillings@1453
|
165 var node = this.popupOptions[this.currentIndex];
|
nickjillings@1453
|
166 this.popupResponse.innerHTML = null;
|
nickjillings@1453
|
167 if (node.type == 'statement') {
|
nickjillings@1453
|
168 this.popupTitle.textContent = null;
|
nickjillings@1453
|
169 var statement = document.createElement('span');
|
nickjillings@1453
|
170 statement.textContent = node.statement;
|
nickjillings@1453
|
171 this.popupResponse.appendChild(statement);
|
nickjillings@1453
|
172 } else if (node.type == 'question') {
|
nickjillings@1453
|
173 this.popupTitle.textContent = node.question;
|
nickjillings@1453
|
174 var textArea = document.createElement('textarea');
|
nickjillings@1453
|
175 switch (node.boxsize) {
|
nickjillings@1453
|
176 case 'small':
|
nickjillings@1453
|
177 textArea.cols = "20";
|
nickjillings@1453
|
178 textArea.rows = "1";
|
nickjillings@1453
|
179 break;
|
nickjillings@1453
|
180 case 'normal':
|
nickjillings@1453
|
181 textArea.cols = "30";
|
nickjillings@1453
|
182 textArea.rows = "2";
|
nickjillings@1453
|
183 break;
|
nickjillings@1453
|
184 case 'large':
|
nickjillings@1453
|
185 textArea.cols = "40";
|
nickjillings@1453
|
186 textArea.rows = "5";
|
nickjillings@1453
|
187 break;
|
nickjillings@1453
|
188 case 'huge':
|
nickjillings@1453
|
189 textArea.cols = "50";
|
nickjillings@1453
|
190 textArea.rows = "10";
|
nickjillings@1453
|
191 break;
|
nickjillings@1453
|
192 }
|
nickjillings@1457
|
193 document.onkeydown=function(){
|
nickjillings@1457
|
194 if(window.event.keyCode=='13'){ // when you hit enter
|
nickjillings@1457
|
195 window.event.preventDefault(); // don't make newline
|
nickjillings@1457
|
196 popup.proceedClicked(); // go to the next window (or start the test or submit)
|
nickjillings@1457
|
197 }
|
nickjillings@1457
|
198 }
|
nickjillings@1453
|
199 this.popupResponse.appendChild(textArea);
|
nickjillings@1453
|
200 textArea.focus();
|
nickjillings@1453
|
201 } else if (node.type == 'checkbox') {
|
nickjillings@1453
|
202 this.popupTitle.textContent = node.statement;
|
nickjillings@1453
|
203 var optHold = this.popupResponse;
|
nickjillings@1453
|
204 for (var i=0; i<node.options.length; i++) {
|
nickjillings@1453
|
205 var option = node.options[i];
|
nickjillings@1453
|
206 var input = document.createElement('input');
|
nickjillings@1453
|
207 input.id = option.id;
|
nickjillings@1453
|
208 input.type = 'checkbox';
|
nickjillings@1453
|
209 var span = document.createElement('span');
|
nickjillings@1453
|
210 span.textContent = option.text;
|
nickjillings@1453
|
211 var hold = document.createElement('div');
|
nickjillings@1453
|
212 hold.setAttribute('name','option');
|
nickjillings@1453
|
213 hold.style.padding = '4px';
|
nickjillings@1453
|
214 hold.appendChild(input);
|
nickjillings@1453
|
215 hold.appendChild(span);
|
nickjillings@1453
|
216 optHold.appendChild(hold);
|
nickjillings@1453
|
217 }
|
nickjillings@1453
|
218 } else if (node.type == 'radio') {
|
nickjillings@1453
|
219 this.popupTitle.textContent = node.statement;
|
nickjillings@1453
|
220 var optHold = this.popupResponse;
|
nickjillings@1453
|
221 for (var i=0; i<node.options.length; i++) {
|
nickjillings@1453
|
222 var option = node.options[i];
|
nickjillings@1453
|
223 var input = document.createElement('input');
|
nickjillings@1453
|
224 input.id = option.name;
|
nickjillings@1453
|
225 input.type = 'radio';
|
nickjillings@1453
|
226 input.name = node.id;
|
nickjillings@1453
|
227 var span = document.createElement('span');
|
nickjillings@1453
|
228 span.textContent = option.text;
|
nickjillings@1453
|
229 var hold = document.createElement('div');
|
nickjillings@1453
|
230 hold.setAttribute('name','option');
|
nickjillings@1453
|
231 hold.style.padding = '4px';
|
nickjillings@1453
|
232 hold.appendChild(input);
|
nickjillings@1453
|
233 hold.appendChild(span);
|
nickjillings@1453
|
234 optHold.appendChild(hold);
|
nickjillings@1453
|
235 }
|
nickjillings@1453
|
236 } else if (node.type == 'number') {
|
nickjillings@1453
|
237 this.popupTitle.textContent = node.statement;
|
nickjillings@1453
|
238 var input = document.createElement('input');
|
nickjillings@1453
|
239 input.type = 'textarea';
|
nickjillings@1453
|
240 if (node.min != null) {input.min = node.min;}
|
nickjillings@1453
|
241 if (node.max != null) {input.max = node.max;}
|
nickjillings@1453
|
242 if (node.step != null) {input.step = node.step;}
|
nickjillings@1453
|
243 this.popupResponse.appendChild(input);
|
nickjillings@1453
|
244 }
|
nickjillings@1453
|
245 if(this.currentIndex+1 == this.popupOptions.length) {
|
nickjillings@1453
|
246 if (this.responses.nodeName == "PRETEST") {
|
nickjillings@1453
|
247 this.buttonProceed.textContent = 'Start';
|
nickjillings@1453
|
248 } else {
|
nickjillings@1453
|
249 this.buttonProceed.textContent = 'Submit';
|
nickjillings@1453
|
250 }
|
nickjillings@1453
|
251 } else {
|
nickjillings@1453
|
252 this.buttonProceed.textContent = 'Next';
|
nickjillings@1453
|
253 }
|
nickjillings@1453
|
254 if(this.currentIndex > 0)
|
nickjillings@1453
|
255 this.buttonPrevious.style.visibility = 'visible';
|
nickjillings@1453
|
256 else
|
nickjillings@1453
|
257 this.buttonPrevious.style.visibility = 'hidden';
|
nickjillings@1453
|
258 };
|
nickjillings@1453
|
259
|
nickjillings@1453
|
260 this.initState = function(node) {
|
nickjillings@1453
|
261 //Call this with your preTest and postTest nodes when needed to
|
nickjillings@1453
|
262 // initialise the popup procedure.
|
nickjillings@1453
|
263 this.popupOptions = node.options;
|
nickjillings@1453
|
264 if (this.popupOptions.length > 0) {
|
nickjillings@1453
|
265 if (node.type == 'pretest') {
|
nickjillings@1453
|
266 this.responses = document.createElement('PreTest');
|
nickjillings@1453
|
267 } else if (node.type == 'posttest') {
|
nickjillings@1453
|
268 this.responses = document.createElement('PostTest');
|
nickjillings@1453
|
269 } else {
|
nickjillings@1453
|
270 console.log ('WARNING - popup node neither pre or post!');
|
nickjillings@1453
|
271 this.responses = document.createElement('responses');
|
nickjillings@1453
|
272 }
|
nickjillings@1453
|
273 this.currentIndex = 0;
|
nickjillings@1453
|
274 this.showPopup();
|
nickjillings@1453
|
275 this.postNode();
|
nickjillings@1453
|
276 } else {
|
nickjillings@1453
|
277 advanceState();
|
nickjillings@1453
|
278 }
|
nickjillings@1453
|
279 };
|
nickjillings@1453
|
280
|
nickjillings@1453
|
281 this.proceedClicked = function() {
|
nickjillings@1453
|
282 // Each time the popup button is clicked!
|
nickjillings@1453
|
283 var node = this.popupOptions[this.currentIndex];
|
nickjillings@1453
|
284 if (node.type == 'question') {
|
nickjillings@1453
|
285 // Must extract the question data
|
nickjillings@1453
|
286 var textArea = $(popup.popupContent).find('textarea')[0];
|
nickjillings@1453
|
287 if (node.mandatory == true && textArea.value.length == 0) {
|
nickjillings@1453
|
288 alert('This question is mandatory');
|
nickjillings@1453
|
289 return;
|
nickjillings@1453
|
290 } else {
|
nickjillings@1453
|
291 // Save the text content
|
nickjillings@1453
|
292 var hold = document.createElement('comment');
|
nickjillings@1453
|
293 hold.id = node.id;
|
nickjillings@1453
|
294 hold.innerHTML = textArea.value;
|
nickjillings@1453
|
295 console.log("Question: "+ node.question);
|
nickjillings@1453
|
296 console.log("Question Response: "+ textArea.value);
|
nickjillings@1453
|
297 this.responses.appendChild(hold);
|
nickjillings@1453
|
298 }
|
nickjillings@1453
|
299 } else if (node.type == 'checkbox') {
|
nickjillings@1453
|
300 // Must extract checkbox data
|
nickjillings@1453
|
301 var optHold = this.popupResponse;
|
nickjillings@1453
|
302 var hold = document.createElement('checkbox');
|
nickjillings@1453
|
303 console.log("Checkbox: "+ node.statement);
|
nickjillings@1453
|
304 hold.id = node.id;
|
nickjillings@1453
|
305 for (var i=0; i<optHold.childElementCount; i++) {
|
nickjillings@1453
|
306 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
|
nickjillings@1453
|
307 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
|
nickjillings@1453
|
308 var response = document.createElement('option');
|
nickjillings@1453
|
309 response.setAttribute('name',input.id);
|
nickjillings@1453
|
310 response.textContent = input.checked;
|
nickjillings@1453
|
311 hold.appendChild(response);
|
nickjillings@1453
|
312 console.log(input.id +': '+ input.checked);
|
nickjillings@1453
|
313 }
|
nickjillings@1453
|
314 this.responses.appendChild(hold);
|
nickjillings@1453
|
315 } else if (node.type == "radio") {
|
nickjillings@1453
|
316 var optHold = this.popupResponse;
|
nickjillings@1453
|
317 var hold = document.createElement('radio');
|
nickjillings@1453
|
318 var responseID = null;
|
nickjillings@1453
|
319 var i=0;
|
nickjillings@1453
|
320 while(responseID == null) {
|
nickjillings@1453
|
321 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
|
nickjillings@1453
|
322 if (input.checked == true) {
|
nickjillings@1453
|
323 responseID = i;
|
nickjillings@1453
|
324 }
|
nickjillings@1453
|
325 i++;
|
nickjillings@1453
|
326 }
|
nickjillings@1453
|
327 hold.id = node.id;
|
nickjillings@1453
|
328 hold.setAttribute('name',node.options[responseID].name);
|
nickjillings@1453
|
329 hold.textContent = node.options[responseID].text;
|
nickjillings@1453
|
330 this.responses.appendChild(hold);
|
nickjillings@1453
|
331 } else if (node.type == "number") {
|
nickjillings@1453
|
332 var input = this.popupContent.getElementsByTagName('input')[0];
|
nickjillings@1453
|
333 if (node.mandatory == true && input.value.length == 0) {
|
nickjillings@1453
|
334 alert('This question is mandatory. Please enter a number');
|
nickjillings@1453
|
335 return;
|
nickjillings@1453
|
336 }
|
nickjillings@1453
|
337 var enteredNumber = Number(input.value);
|
nickjillings@1453
|
338 if (isNaN(enteredNumber)) {
|
nickjillings@1453
|
339 alert('Please enter a valid number');
|
nickjillings@1453
|
340 return;
|
nickjillings@1453
|
341 }
|
nickjillings@1453
|
342 if (enteredNumber < node.min && node.min != null) {
|
nickjillings@1453
|
343 alert('Number is below the minimum value of '+node.min);
|
nickjillings@1453
|
344 return;
|
nickjillings@1453
|
345 }
|
nickjillings@1453
|
346 if (enteredNumber > node.max && node.max != null) {
|
nickjillings@1453
|
347 alert('Number is above the maximum value of '+node.max);
|
nickjillings@1453
|
348 return;
|
nickjillings@1453
|
349 }
|
nickjillings@1453
|
350 var hold = document.createElement('number');
|
nickjillings@1453
|
351 hold.id = node.id;
|
nickjillings@1453
|
352 hold.textContent = input.value;
|
nickjillings@1453
|
353 this.responses.appendChild(hold);
|
nickjillings@1453
|
354 }
|
nickjillings@1453
|
355 this.currentIndex++;
|
nickjillings@1453
|
356 if (this.currentIndex < this.popupOptions.length) {
|
nickjillings@1453
|
357 this.postNode();
|
nickjillings@1453
|
358 } else {
|
nickjillings@1453
|
359 // Reached the end of the popupOptions
|
nickjillings@1453
|
360 this.hidePopup();
|
nickjillings@1453
|
361 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
|
nickjillings@1453
|
362 testState.stateResults[testState.stateIndex] = this.responses;
|
nickjillings@1453
|
363 } else {
|
nickjillings@1453
|
364 testState.stateResults[testState.stateIndex].appendChild(this.responses);
|
nickjillings@1453
|
365 }
|
nickjillings@1453
|
366 advanceState();
|
nickjillings@1453
|
367 }
|
nickjillings@1453
|
368 };
|
nickjillings@1453
|
369
|
nickjillings@1453
|
370 this.previousClick = function() {
|
nickjillings@1453
|
371 // Triggered when the 'Back' button is clicked in the survey
|
nickjillings@1453
|
372 if (this.currentIndex > 0) {
|
nickjillings@1453
|
373 this.currentIndex--;
|
nickjillings@1453
|
374 var node = this.popupOptions[this.currentIndex];
|
nickjillings@1453
|
375 if (node.type != 'statement') {
|
nickjillings@1453
|
376 var prevResp = this.responses.childNodes[this.responses.childElementCount-1];
|
nickjillings@1453
|
377 this.responses.removeChild(prevResp);
|
nickjillings@1453
|
378 }
|
nickjillings@1453
|
379 this.postNode();
|
nickjillings@1453
|
380 if (node.type == 'question') {
|
nickjillings@1453
|
381 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent;
|
nickjillings@1453
|
382 } else if (node.type == 'checkbox') {
|
nickjillings@1453
|
383 var options = this.popupContent.getElementsByTagName('input');
|
nickjillings@1453
|
384 var savedOptions = prevResp.getElementsByTagName('option');
|
nickjillings@1453
|
385 for (var i=0; i<options.length; i++) {
|
nickjillings@1453
|
386 var id = options[i].id;
|
nickjillings@1453
|
387 for (var j=0; j<savedOptions.length; j++) {
|
nickjillings@1453
|
388 if (savedOptions[j].getAttribute('name') == id) {
|
nickjillings@1453
|
389 if (savedOptions[j].textContent == 'true') {options[i].checked = true;}
|
nickjillings@1453
|
390 else {options[i].checked = false;}
|
nickjillings@1453
|
391 break;
|
nickjillings@1453
|
392 }
|
nickjillings@1453
|
393 }
|
nickjillings@1453
|
394 }
|
nickjillings@1453
|
395 } else if (node.type == 'number') {
|
nickjillings@1453
|
396 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent;
|
nickjillings@1453
|
397 } else if (node.type == 'radio') {
|
nickjillings@1453
|
398 var options = this.popupContent.getElementsByTagName('input');
|
nickjillings@1453
|
399 var name = prevResp.getAttribute('name');
|
nickjillings@1453
|
400 for (var i=0; i<options.length; i++) {
|
nickjillings@1453
|
401 if (options[i].id == name) {
|
nickjillings@1453
|
402 options[i].checked = true;
|
nickjillings@1453
|
403 break;
|
nickjillings@1453
|
404 }
|
nickjillings@1453
|
405 }
|
nickjillings@1453
|
406 }
|
nickjillings@1453
|
407 }
|
nickjillings@1453
|
408 };
|
nickjillings@1453
|
409 }
|
nickjillings@1453
|
410
|
nickjillings@1453
|
411 function advanceState()
|
nickjillings@1453
|
412 {
|
nickjillings@1453
|
413 // Just for complete clarity
|
nickjillings@1453
|
414 testState.advanceState();
|
nickjillings@1453
|
415 }
|
nickjillings@1453
|
416
|
nickjillings@1453
|
417 function stateMachine()
|
nickjillings@1453
|
418 {
|
nickjillings@1453
|
419 // Object prototype for tracking and managing the test state
|
nickjillings@1453
|
420 this.stateMap = [];
|
nickjillings@1453
|
421 this.stateIndex = null;
|
nickjillings@1453
|
422 this.currentStateMap = [];
|
nickjillings@1453
|
423 this.currentIndex = null;
|
nickjillings@1453
|
424 this.currentTestId = 0;
|
nickjillings@1453
|
425 this.stateResults = [];
|
nickjillings@1453
|
426 this.timerCallBackHolders = null;
|
nickjillings@1453
|
427 this.initialise = function(){
|
nickjillings@1453
|
428 if (this.stateMap.length > 0) {
|
nickjillings@1453
|
429 if(this.stateIndex != null) {
|
nickjillings@1453
|
430 console.log('NOTE - State already initialise');
|
nickjillings@1453
|
431 }
|
nickjillings@1453
|
432 this.stateIndex = -1;
|
nickjillings@1453
|
433 var that = this;
|
nickjillings@1453
|
434 var aH_pId = 0;
|
nickjillings@1453
|
435 for (var id=0; id<this.stateMap.length; id++){
|
nickjillings@1453
|
436 var name = this.stateMap[id].type;
|
nickjillings@1453
|
437 var obj = document.createElement(name);
|
nickjillings@1453
|
438 if (name == 'audioHolder') {
|
nickjillings@1453
|
439 obj.id = this.stateMap[id].id;
|
nickjillings@1453
|
440 obj.setAttribute('presentedid',aH_pId);
|
nickjillings@1453
|
441 aH_pId+=1;
|
nickjillings@1453
|
442 }
|
nickjillings@1453
|
443 this.stateResults.push(obj);
|
nickjillings@1453
|
444 }
|
nickjillings@1453
|
445 } else {
|
nickjillings@1453
|
446 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
|
nickjillings@1453
|
447 }
|
nickjillings@1453
|
448 };
|
nickjillings@1453
|
449 this.advanceState = function(){
|
nickjillings@1453
|
450 if (this.stateIndex == null) {
|
nickjillings@1453
|
451 this.initialise();
|
nickjillings@1453
|
452 }
|
nickjillings@1453
|
453 if (this.stateIndex == -1) {
|
nickjillings@1453
|
454 console.log('Starting test...');
|
nickjillings@1453
|
455 }
|
nickjillings@1453
|
456 if (this.currentIndex == null){
|
nickjillings@1453
|
457 if (this.currentStateMap.type == "audioHolder") {
|
nickjillings@1453
|
458 // Save current page
|
nickjillings@1453
|
459 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
|
nickjillings@1453
|
460 this.currentTestId++;
|
nickjillings@1453
|
461 }
|
nickjillings@1453
|
462 this.stateIndex++;
|
nickjillings@1453
|
463 if (this.stateIndex >= this.stateMap.length) {
|
nickjillings@1453
|
464 console.log('Test Completed');
|
nickjillings@1453
|
465 createProjectSave(specification.projectReturn);
|
nickjillings@1453
|
466 } else {
|
nickjillings@1453
|
467 this.currentStateMap = this.stateMap[this.stateIndex];
|
nickjillings@1453
|
468 if (this.currentStateMap.type == "audioHolder") {
|
nickjillings@1453
|
469 console.log('Loading test page');
|
nickjillings@1453
|
470 loadTest(this.currentStateMap);
|
nickjillings@1453
|
471 this.initialiseInnerState(this.currentStateMap);
|
nickjillings@1453
|
472 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
|
nickjillings@1453
|
473 if (this.currentStateMap.options.length >= 1) {
|
nickjillings@1453
|
474 popup.initState(this.currentStateMap);
|
nickjillings@1453
|
475 } else {
|
nickjillings@1453
|
476 this.advanceState();
|
nickjillings@1453
|
477 }
|
nickjillings@1453
|
478 } else {
|
nickjillings@1453
|
479 this.advanceState();
|
nickjillings@1453
|
480 }
|
nickjillings@1453
|
481 }
|
nickjillings@1453
|
482 } else {
|
nickjillings@1453
|
483 this.advanceInnerState();
|
nickjillings@1453
|
484 }
|
nickjillings@1453
|
485 };
|
nickjillings@1453
|
486
|
nickjillings@1453
|
487 this.testPageCompleted = function(store, testXML, testId) {
|
nickjillings@1453
|
488 // Function called each time a test page has been completed
|
nickjillings@1453
|
489 // Can be used to over-rule default behaviour
|
nickjillings@1453
|
490
|
nickjillings@1453
|
491 pageXMLSave(store, testXML);
|
nickjillings@1453
|
492 };
|
nickjillings@1453
|
493
|
nickjillings@1453
|
494 this.initialiseInnerState = function(node) {
|
nickjillings@1453
|
495 // Parses the received testXML for pre and post test options
|
nickjillings@1453
|
496 this.currentStateMap = [];
|
nickjillings@1453
|
497 var preTest = node.preTest;
|
nickjillings@1453
|
498 var postTest = node.postTest;
|
nickjillings@1453
|
499 if (preTest == undefined) {preTest = document.createElement("preTest");}
|
nickjillings@1453
|
500 if (postTest == undefined){postTest= document.createElement("postTest");}
|
nickjillings@1453
|
501 this.currentStateMap.push(preTest);
|
nickjillings@1453
|
502 this.currentStateMap.push(node);
|
nickjillings@1453
|
503 this.currentStateMap.push(postTest);
|
nickjillings@1453
|
504 this.currentIndex = -1;
|
nickjillings@1453
|
505 this.advanceInnerState();
|
nickjillings@1453
|
506 };
|
nickjillings@1453
|
507
|
nickjillings@1453
|
508 this.advanceInnerState = function() {
|
nickjillings@1453
|
509 this.currentIndex++;
|
nickjillings@1453
|
510 if (this.currentIndex >= this.currentStateMap.length) {
|
nickjillings@1453
|
511 this.currentIndex = null;
|
nickjillings@1453
|
512 this.currentStateMap = this.stateMap[this.stateIndex];
|
nickjillings@1453
|
513 this.advanceState();
|
nickjillings@1453
|
514 } else {
|
nickjillings@1453
|
515 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
|
nickjillings@1453
|
516 console.log("Loading test page"+this.currentTestId);
|
nickjillings@1453
|
517 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
|
nickjillings@1453
|
518 popup.initState(this.currentStateMap[this.currentIndex]);
|
nickjillings@1453
|
519 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
|
nickjillings@1453
|
520 popup.initState(this.currentStateMap[this.currentIndex]);
|
nickjillings@1453
|
521 } else {
|
nickjillings@1453
|
522 this.advanceInnerState();
|
nickjillings@1453
|
523 }
|
nickjillings@1453
|
524 }
|
nickjillings@1453
|
525 };
|
nickjillings@1453
|
526
|
nickjillings@1453
|
527 this.previousState = function(){};
|
nickjillings@1453
|
528 }
|
nickjillings@1453
|
529
|
nickjillings@1453
|
530 function testEnded(testId)
|
nickjillings@1453
|
531 {
|
nickjillings@1453
|
532 pageXMLSave(testId);
|
nickjillings@1453
|
533 if (testXMLSetups.length-1 > testId)
|
nickjillings@1453
|
534 {
|
nickjillings@1453
|
535 // Yes we have another test to perform
|
nickjillings@1453
|
536 testId = (Number(testId)+1);
|
nickjillings@1453
|
537 currentState = 'testRun-'+testId;
|
nickjillings@1453
|
538 loadTest(testId);
|
nickjillings@1453
|
539 } else {
|
nickjillings@1453
|
540 console.log('Testing Completed!');
|
nickjillings@1453
|
541 currentState = 'postTest';
|
nickjillings@1453
|
542 // Check for any post tests
|
nickjillings@1453
|
543 var xmlSetup = projectXML.find('setup');
|
nickjillings@1453
|
544 var postTest = xmlSetup.find('PostTest')[0];
|
nickjillings@1453
|
545 popup.initState(postTest);
|
nickjillings@1453
|
546 }
|
nickjillings@1453
|
547 }
|
nickjillings@1453
|
548
|
nickjillings@1453
|
549 function loadProjectSpec(url) {
|
nickjillings@1453
|
550 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
|
nickjillings@1453
|
551 // If url is null, request client to upload project XML document
|
nickjillings@1453
|
552 var r = new XMLHttpRequest();
|
nickjillings@1453
|
553 r.open('GET',url,true);
|
nickjillings@1453
|
554 r.onload = function() {
|
nickjillings@1453
|
555 loadProjectSpecCallback(r.response);
|
nickjillings@1453
|
556 };
|
nickjillings@1453
|
557 r.send();
|
nickjillings@1453
|
558 };
|
nickjillings@1453
|
559
|
nickjillings@1453
|
560 function loadProjectSpecCallback(response) {
|
nickjillings@1453
|
561 // Function called after asynchronous download of XML project specification
|
nickjillings@1453
|
562 //var decode = $.parseXML(response);
|
nickjillings@1453
|
563 //projectXML = $(decode);
|
nickjillings@1453
|
564
|
nickjillings@1453
|
565 var parse = new DOMParser();
|
nickjillings@1453
|
566 projectXML = parse.parseFromString(response,'text/xml');
|
nickjillings@1453
|
567
|
nickjillings@1453
|
568 // Build the specification
|
nickjillings@1453
|
569 specification.decode();
|
nickjillings@1453
|
570
|
nickjillings@1453
|
571 testState.stateMap.push(specification.preTest);
|
nickjillings@1453
|
572
|
nickjillings@1453
|
573 $(specification.audioHolders).each(function(index,elem){
|
nickjillings@1453
|
574 testState.stateMap.push(elem);
|
nickjillings@1453
|
575 });
|
nickjillings@1453
|
576
|
nickjillings@1453
|
577 testState.stateMap.push(specification.postTest);
|
nickjillings@1453
|
578
|
nickjillings@1453
|
579 // Obtain the metrics enabled
|
nickjillings@1453
|
580 $(specification.metrics).each(function(index,node){
|
nickjillings@1453
|
581 var enabled = node.textContent;
|
nickjillings@1453
|
582 switch(node.enabled)
|
nickjillings@1453
|
583 {
|
nickjillings@1453
|
584 case 'testTimer':
|
nickjillings@1453
|
585 sessionMetrics.prototype.enableTestTimer = true;
|
nickjillings@1453
|
586 break;
|
nickjillings@1453
|
587 case 'elementTimer':
|
nickjillings@1453
|
588 sessionMetrics.prototype.enableElementTimer = true;
|
nickjillings@1453
|
589 break;
|
nickjillings@1453
|
590 case 'elementTracker':
|
nickjillings@1453
|
591 sessionMetrics.prototype.enableElementTracker = true;
|
nickjillings@1453
|
592 break;
|
nickjillings@1453
|
593 case 'elementListenTracker':
|
nickjillings@1453
|
594 sessionMetrics.prototype.enableElementListenTracker = true;
|
nickjillings@1453
|
595 break;
|
nickjillings@1453
|
596 case 'elementInitialPosition':
|
nickjillings@1453
|
597 sessionMetrics.prototype.enableElementInitialPosition = true;
|
nickjillings@1453
|
598 break;
|
nickjillings@1453
|
599 case 'elementFlagListenedTo':
|
nickjillings@1453
|
600 sessionMetrics.prototype.enableFlagListenedTo = true;
|
nickjillings@1453
|
601 break;
|
nickjillings@1453
|
602 case 'elementFlagMoved':
|
nickjillings@1453
|
603 sessionMetrics.prototype.enableFlagMoved = true;
|
nickjillings@1453
|
604 break;
|
nickjillings@1453
|
605 case 'elementFlagComments':
|
nickjillings@1453
|
606 sessionMetrics.prototype.enableFlagComments = true;
|
nickjillings@1453
|
607 break;
|
nickjillings@1453
|
608 }
|
nickjillings@1453
|
609 });
|
nickjillings@1453
|
610
|
nickjillings@1453
|
611
|
nickjillings@1453
|
612
|
nickjillings@1453
|
613 // Detect the interface to use and load the relevant javascripts.
|
nickjillings@1453
|
614 var interfaceJS = document.createElement('script');
|
nickjillings@1453
|
615 interfaceJS.setAttribute("type","text/javascript");
|
nickjillings@1453
|
616 if (specification.interfaceType == 'APE') {
|
nickjillings@1453
|
617 interfaceJS.setAttribute("src","ape.js");
|
nickjillings@1453
|
618
|
nickjillings@1453
|
619 // APE comes with a css file
|
nickjillings@1453
|
620 var css = document.createElement('link');
|
nickjillings@1453
|
621 css.rel = 'stylesheet';
|
nickjillings@1453
|
622 css.type = 'text/css';
|
nickjillings@1453
|
623 css.href = 'ape.css';
|
nickjillings@1453
|
624
|
nickjillings@1453
|
625 document.getElementsByTagName("head")[0].appendChild(css);
|
nickjillings@1453
|
626 } else if (specification.interfaceType == "MUSHRA")
|
nickjillings@1453
|
627 {
|
nickjillings@1453
|
628 interfaceJS.setAttribute("src","mushra.js");
|
nickjillings@1453
|
629
|
nickjillings@1453
|
630 // MUSHRA comes with a css file
|
nickjillings@1453
|
631 var css = document.createElement('link');
|
nickjillings@1453
|
632 css.rel = 'stylesheet';
|
nickjillings@1453
|
633 css.type = 'text/css';
|
nickjillings@1453
|
634 css.href = 'mushra.css';
|
nickjillings@1453
|
635
|
nickjillings@1453
|
636 document.getElementsByTagName("head")[0].appendChild(css);
|
nickjillings@1453
|
637 }
|
nickjillings@1453
|
638 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
|
nickjillings@1453
|
639
|
nickjillings@1453
|
640 // Define window callbacks for interface
|
nickjillings@1453
|
641 window.onresize = function(event){interfaceContext.resizeWindow(event);};
|
nickjillings@1453
|
642 }
|
nickjillings@1453
|
643
|
nickjillings@1453
|
644 function createProjectSave(destURL) {
|
nickjillings@1453
|
645 // Save the data from interface into XML and send to destURL
|
nickjillings@1453
|
646 // If destURL is null then download XML in client
|
nickjillings@1453
|
647 // Now time to render file locally
|
nickjillings@1453
|
648 var xmlDoc = interfaceXMLSave();
|
nickjillings@1453
|
649 var parent = document.createElement("div");
|
nickjillings@1453
|
650 parent.appendChild(xmlDoc);
|
nickjillings@1453
|
651 var file = [parent.innerHTML];
|
nickjillings@1453
|
652 if (destURL == "null" || destURL == undefined) {
|
nickjillings@1453
|
653 var bb = new Blob(file,{type : 'application/xml'});
|
nickjillings@1453
|
654 var dnlk = window.URL.createObjectURL(bb);
|
nickjillings@1453
|
655 var a = document.createElement("a");
|
nickjillings@1453
|
656 a.hidden = '';
|
nickjillings@1453
|
657 a.href = dnlk;
|
nickjillings@1453
|
658 a.download = "save.xml";
|
nickjillings@1453
|
659 a.textContent = "Save File";
|
nickjillings@1453
|
660
|
nickjillings@1453
|
661 popup.showPopup();
|
nickjillings@1453
|
662 popup.popupContent.innerHTML = null;
|
nickjillings@1453
|
663 popup.popupContent.appendChild(a);
|
nickjillings@1453
|
664 } else {
|
nickjillings@1453
|
665 var xmlhttp = new XMLHttpRequest;
|
nickjillings@1453
|
666 xmlhttp.open("POST",destURL,true);
|
nickjillings@1453
|
667 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
|
nickjillings@1453
|
668 xmlhttp.onerror = function(){
|
nickjillings@1453
|
669 console.log('Error saving file to server! Presenting download locally');
|
nickjillings@1453
|
670 createProjectSave(null);
|
nickjillings@1453
|
671 };
|
nickjillings@1453
|
672 xmlhttp.onreadystatechange = function() {
|
nickjillings@1453
|
673 console.log(xmlhttp.status);
|
nickjillings@1453
|
674 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
|
nickjillings@1453
|
675 createProjectSave(null);
|
nickjillings@1453
|
676 } else {
|
nickjillings@1457
|
677 if (xmlhttp.responseXML == null)
|
nickjillings@1457
|
678 {
|
nickjillings@1457
|
679 return createProjectSave(null);
|
nickjillings@1457
|
680 }
|
nickjillings@1457
|
681 var response = xmlhttp.responseXML.childNodes[0];
|
nickjillings@1457
|
682 if (response.getAttribute('state') == "OK")
|
nickjillings@1457
|
683 {
|
nickjillings@1457
|
684 var file = response.getElementsByTagName('file')[0];
|
nickjillings@1457
|
685 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B');
|
nickjillings@1457
|
686 popup.showPopup();
|
nickjillings@1457
|
687 popup.popupContent.innerHTML = null;
|
nickjillings@1457
|
688 popup.popupContent.textContent = "Thank you!";
|
nickjillings@1457
|
689 } else {
|
nickjillings@1457
|
690 var message = response.getElementsByTagName('message')[0];
|
nickjillings@1457
|
691 errorSessionDump(message.textContent);
|
nickjillings@1457
|
692 }
|
nickjillings@1453
|
693 }
|
nickjillings@1453
|
694 };
|
nickjillings@1453
|
695 xmlhttp.send(file);
|
nickjillings@1453
|
696 }
|
nickjillings@1453
|
697 }
|
nickjillings@1453
|
698
|
nickjillings@1453
|
699 function errorSessionDump(msg){
|
nickjillings@1453
|
700 // Create the partial interface XML save
|
nickjillings@1453
|
701 // Include error node with message on why the dump occured
|
nickjillings@1453
|
702 var xmlDoc = interfaceXMLSave();
|
nickjillings@1453
|
703 var err = document.createElement('error');
|
nickjillings@1453
|
704 err.textContent = msg;
|
nickjillings@1453
|
705 xmlDoc.appendChild(err);
|
nickjillings@1453
|
706 var parent = document.createElement("div");
|
nickjillings@1453
|
707 parent.appendChild(xmlDoc);
|
nickjillings@1453
|
708 var file = [parent.innerHTML];
|
nickjillings@1453
|
709 var bb = new Blob(file,{type : 'application/xml'});
|
nickjillings@1453
|
710 var dnlk = window.URL.createObjectURL(bb);
|
nickjillings@1453
|
711 var a = document.createElement("a");
|
nickjillings@1453
|
712 a.hidden = '';
|
nickjillings@1453
|
713 a.href = dnlk;
|
nickjillings@1453
|
714 a.download = "save.xml";
|
nickjillings@1453
|
715 a.textContent = "Save File";
|
nickjillings@1453
|
716
|
nickjillings@1453
|
717 popup.showPopup();
|
nickjillings@1453
|
718 popup.popupContent.innerHTML = "ERROR : "+msg;
|
nickjillings@1453
|
719 popup.popupContent.appendChild(a);
|
nickjillings@1453
|
720 }
|
nickjillings@1453
|
721
|
nickjillings@1453
|
722 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
|
nickjillings@1453
|
723 function interfaceXMLSave(){
|
nickjillings@1453
|
724 // Create the XML string to be exported with results
|
nickjillings@1453
|
725 var xmlDoc = document.createElement("BrowserEvaluationResult");
|
nickjillings@1453
|
726 var projectDocument = specification.projectXML;
|
nickjillings@1453
|
727 projectDocument.setAttribute('file-name',url);
|
nickjillings@1453
|
728 xmlDoc.appendChild(projectDocument);
|
nickjillings@1453
|
729 xmlDoc.appendChild(returnDateNode());
|
nickjillings@1453
|
730 for (var i=0; i<testState.stateResults.length; i++)
|
nickjillings@1453
|
731 {
|
nickjillings@1453
|
732 xmlDoc.appendChild(testState.stateResults[i]);
|
nickjillings@1453
|
733 }
|
nickjillings@1453
|
734
|
nickjillings@1453
|
735 return xmlDoc;
|
nickjillings@1453
|
736 }
|
nickjillings@1453
|
737
|
nickjillings@1453
|
738 function AudioEngine() {
|
nickjillings@1453
|
739
|
nickjillings@1453
|
740 // Create two output paths, the main outputGain and fooGain.
|
nickjillings@1453
|
741 // Output gain is default to 1 and any items for playback route here
|
nickjillings@1453
|
742 // Foo gain is used for analysis to ensure paths get processed, but are not heard
|
nickjillings@1453
|
743 // because web audio will optimise and any route which does not go to the destination gets ignored.
|
nickjillings@1453
|
744 this.outputGain = audioContext.createGain();
|
nickjillings@1453
|
745 this.fooGain = audioContext.createGain();
|
nickjillings@1453
|
746 this.fooGain.gain = 0;
|
nickjillings@1453
|
747
|
nickjillings@1453
|
748 // Use this to detect playback state: 0 - stopped, 1 - playing
|
nickjillings@1453
|
749 this.status = 0;
|
nickjillings@1453
|
750
|
nickjillings@1453
|
751 // Connect both gains to output
|
nickjillings@1453
|
752 this.outputGain.connect(audioContext.destination);
|
nickjillings@1453
|
753 this.fooGain.connect(audioContext.destination);
|
nickjillings@1453
|
754
|
nickjillings@1453
|
755 // Create the timer Object
|
nickjillings@1453
|
756 this.timer = new timer();
|
nickjillings@1453
|
757 // Create session metrics
|
nickjillings@1453
|
758 this.metric = new sessionMetrics(this);
|
nickjillings@1453
|
759
|
nickjillings@1453
|
760 this.loopPlayback = false;
|
nickjillings@1453
|
761
|
nickjillings@1453
|
762 // Create store for new audioObjects
|
nickjillings@1453
|
763 this.audioObjects = [];
|
nickjillings@1453
|
764
|
nickjillings@1453
|
765 this.play = function(id) {
|
nickjillings@1453
|
766 // Start the timer and set the audioEngine state to playing (1)
|
nickjillings@1453
|
767 if (this.status == 0 && this.loopPlayback) {
|
nickjillings@1453
|
768 // Check if all audioObjects are ready
|
nickjillings@1453
|
769 if(this.checkAllReady())
|
nickjillings@1453
|
770 {
|
nickjillings@1453
|
771 this.status = 1;
|
nickjillings@1453
|
772 this.setSynchronousLoop();
|
nickjillings@1453
|
773 }
|
nickjillings@1453
|
774 }
|
nickjillings@1453
|
775 else
|
nickjillings@1453
|
776 {
|
nickjillings@1453
|
777 this.status = 1;
|
nickjillings@1453
|
778 }
|
nickjillings@1453
|
779 if (this.status== 1) {
|
nickjillings@1453
|
780 this.timer.startTest();
|
nickjillings@1453
|
781 if (id == undefined) {
|
nickjillings@1453
|
782 id = -1;
|
nickjillings@1453
|
783 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
|
nickjillings@1453
|
784 return;
|
nickjillings@1453
|
785 } else {
|
nickjillings@1453
|
786 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
|
nickjillings@1453
|
787 }
|
nickjillings@1453
|
788 if (this.loopPlayback) {
|
nickjillings@1453
|
789 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1453
|
790 {
|
nickjillings@1453
|
791 this.audioObjects[i].play(this.timer.getTestTime()+1);
|
nickjillings@1453
|
792 if (id == i) {
|
nickjillings@1453
|
793 this.audioObjects[i].loopStart();
|
nickjillings@1453
|
794 } else {
|
nickjillings@1453
|
795 this.audioObjects[i].loopStop();
|
nickjillings@1453
|
796 }
|
nickjillings@1453
|
797 }
|
nickjillings@1453
|
798 } else {
|
nickjillings@1453
|
799 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1453
|
800 {
|
nickjillings@1453
|
801 if (i != id) {
|
nickjillings@1453
|
802 this.audioObjects[i].outputGain.gain.value = 0.0;
|
nickjillings@1453
|
803 this.audioObjects[i].stop();
|
nickjillings@1453
|
804 } else if (i == id) {
|
nickjillings@1453
|
805 this.audioObjects[id].outputGain.gain.value = 1.0;
|
nickjillings@1453
|
806 this.audioObjects[id].play(audioContext.currentTime+0.01);
|
nickjillings@1453
|
807 }
|
nickjillings@1453
|
808 }
|
nickjillings@1453
|
809 }
|
nickjillings@1453
|
810 interfaceContext.playhead.start();
|
nickjillings@1453
|
811 }
|
nickjillings@1453
|
812 };
|
nickjillings@1453
|
813
|
nickjillings@1453
|
814 this.stop = function() {
|
nickjillings@1453
|
815 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
|
nickjillings@1453
|
816 if (this.status == 1) {
|
nickjillings@1453
|
817 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1453
|
818 {
|
nickjillings@1453
|
819 this.audioObjects[i].stop();
|
nickjillings@1453
|
820 }
|
nickjillings@1453
|
821 interfaceContext.playhead.stop();
|
nickjillings@1453
|
822 this.status = 0;
|
nickjillings@1453
|
823 }
|
nickjillings@1453
|
824 };
|
nickjillings@1453
|
825
|
nickjillings@1453
|
826 this.newTrack = function(element) {
|
nickjillings@1453
|
827 // Pull data from given URL into new audio buffer
|
nickjillings@1453
|
828 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
|
nickjillings@1453
|
829
|
nickjillings@1453
|
830 // Create the audioObject with ID of the new track length;
|
nickjillings@1453
|
831 audioObjectId = this.audioObjects.length;
|
nickjillings@1453
|
832 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
|
nickjillings@1453
|
833
|
nickjillings@1453
|
834 // AudioObject will get track itself.
|
nickjillings@1453
|
835 this.audioObjects[audioObjectId].specification = element;
|
nickjillings@1453
|
836 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
|
nickjillings@1453
|
837 return this.audioObjects[audioObjectId];
|
nickjillings@1453
|
838 };
|
nickjillings@1453
|
839
|
nickjillings@1453
|
840 this.newTestPage = function() {
|
nickjillings@1453
|
841 this.state = 0;
|
nickjillings@1453
|
842 this.audioObjectsReady = false;
|
nickjillings@1453
|
843 this.metric.reset();
|
nickjillings@1453
|
844 this.audioObjects = [];
|
nickjillings@1453
|
845 };
|
nickjillings@1453
|
846
|
nickjillings@1453
|
847 this.checkAllPlayed = function() {
|
nickjillings@1453
|
848 arr = [];
|
nickjillings@1453
|
849 for (var id=0; id<this.audioObjects.length; id++) {
|
nickjillings@1453
|
850 if (this.audioObjects[id].metric.wasListenedTo == false) {
|
nickjillings@1453
|
851 arr.push(this.audioObjects[id].id);
|
nickjillings@1453
|
852 }
|
nickjillings@1453
|
853 }
|
nickjillings@1453
|
854 return arr;
|
nickjillings@1453
|
855 };
|
nickjillings@1453
|
856
|
nickjillings@1453
|
857 this.checkAllReady = function() {
|
nickjillings@1453
|
858 var ready = true;
|
nickjillings@1453
|
859 for (var i=0; i<this.audioObjects.length; i++) {
|
nickjillings@1453
|
860 if (this.audioObjects[i].state == 0) {
|
nickjillings@1453
|
861 // Track not ready
|
nickjillings@1453
|
862 console.log('WAIT -- audioObject '+i+' not ready yet!');
|
nickjillings@1453
|
863 ready = false;
|
nickjillings@1453
|
864 };
|
nickjillings@1453
|
865 }
|
nickjillings@1453
|
866 return ready;
|
nickjillings@1453
|
867 };
|
nickjillings@1453
|
868
|
nickjillings@1453
|
869 this.setSynchronousLoop = function() {
|
nickjillings@1453
|
870 // Pads the signals so they are all exactly the same length
|
nickjillings@1453
|
871 var length = 0;
|
nickjillings@1453
|
872 var lens = [];
|
nickjillings@1453
|
873 var maxId;
|
nickjillings@1453
|
874 for (var i=0; i<this.audioObjects.length; i++)
|
nickjillings@1453
|
875 {
|
nickjillings@1453
|
876 lens.push(this.audioObjects[i].buffer.length);
|
nickjillings@1453
|
877 if (length < this.audioObjects[i].buffer.length)
|
nickjillings@1453
|
878 {
|
nickjillings@1453
|
879 length = this.audioObjects[i].buffer.length;
|
nickjillings@1453
|
880 maxId = i;
|
nickjillings@1453
|
881 }
|
nickjillings@1453
|
882 }
|
nickjillings@1453
|
883 // Perform difference
|
nickjillings@1453
|
884 for (var i=0; i<lens.length; i++)
|
nickjillings@1453
|
885 {
|
nickjillings@1453
|
886 lens[i] = length - lens[i];
|
nickjillings@1453
|
887 }
|
nickjillings@1453
|
888 // Extract the audio and zero-pad
|
nickjillings@1453
|
889 for (var i=0; i<lens.length; i++)
|
nickjillings@1453
|
890 {
|
nickjillings@1453
|
891 var orig = this.audioObjects[i].buffer;
|
nickjillings@1453
|
892 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
|
nickjillings@1453
|
893 for (var c=0; c<orig.numberOfChannels; c++)
|
nickjillings@1453
|
894 {
|
nickjillings@1453
|
895 var inData = hold.getChannelData(c);
|
nickjillings@1453
|
896 var outData = orig.getChannelData(c);
|
nickjillings@1453
|
897 for (var n=0; n<orig.length; n++)
|
nickjillings@1453
|
898 {inData[n] = outData[n];}
|
nickjillings@1453
|
899 }
|
nickjillings@1453
|
900 this.audioObjects[i].buffer = hold;
|
nickjillings@1453
|
901 delete orig;
|
nickjillings@1453
|
902 }
|
nickjillings@1453
|
903 };
|
nickjillings@1453
|
904
|
nickjillings@1453
|
905 }
|
nickjillings@1453
|
906
|
nickjillings@1453
|
907 function audioObject(id) {
|
nickjillings@1453
|
908 // The main buffer object with common control nodes to the AudioEngine
|
nickjillings@1453
|
909
|
nickjillings@1453
|
910 this.specification;
|
nickjillings@1453
|
911 this.id = id;
|
nickjillings@1453
|
912 this.state = 0; // 0 - no data, 1 - ready
|
nickjillings@1453
|
913 this.url = null; // Hold the URL given for the output back to the results.
|
nickjillings@1453
|
914 this.metric = new metricTracker(this);
|
nickjillings@1453
|
915
|
nickjillings@1453
|
916 // Bindings for GUI
|
nickjillings@1453
|
917 this.interfaceDOM = null;
|
nickjillings@1453
|
918 this.commentDOM = null;
|
nickjillings@1453
|
919
|
nickjillings@1453
|
920 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
|
nickjillings@1453
|
921 this.bufferNode = undefined;
|
nickjillings@1453
|
922 this.outputGain = audioContext.createGain();
|
nickjillings@1453
|
923
|
nickjillings@1453
|
924 // Default output gain to be zero
|
nickjillings@1453
|
925 this.outputGain.gain.value = 0.0;
|
nickjillings@1453
|
926
|
nickjillings@1453
|
927 // Connect buffer to the audio graph
|
nickjillings@1453
|
928 this.outputGain.connect(audioEngineContext.outputGain);
|
nickjillings@1453
|
929
|
nickjillings@1453
|
930 // the audiobuffer is not designed for multi-start playback
|
nickjillings@1453
|
931 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
|
nickjillings@1453
|
932 this.buffer;
|
nickjillings@1453
|
933
|
nickjillings@1453
|
934 this.loopStart = function() {
|
nickjillings@1453
|
935 this.outputGain.gain.value = 1.0;
|
nickjillings@1453
|
936 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1453
|
937 };
|
nickjillings@1453
|
938
|
nickjillings@1453
|
939 this.loopStop = function() {
|
nickjillings@1453
|
940 if (this.outputGain.gain.value != 0.0) {
|
nickjillings@1453
|
941 this.outputGain.gain.value = 0.0;
|
nickjillings@1453
|
942 this.metric.stopListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1453
|
943 }
|
nickjillings@1453
|
944 };
|
nickjillings@1453
|
945
|
nickjillings@1453
|
946 this.play = function(startTime) {
|
nickjillings@1453
|
947 if (this.bufferNode == undefined) {
|
nickjillings@1453
|
948 this.bufferNode = audioContext.createBufferSource();
|
nickjillings@1453
|
949 this.bufferNode.owner = this;
|
nickjillings@1453
|
950 this.bufferNode.connect(this.outputGain);
|
nickjillings@1453
|
951 this.bufferNode.buffer = this.buffer;
|
nickjillings@1453
|
952 this.bufferNode.loop = audioEngineContext.loopPlayback;
|
nickjillings@1453
|
953 this.bufferNode.onended = function(event) {
|
nickjillings@1453
|
954 // Safari does not like using 'this' to reference the calling object!
|
nickjillings@1453
|
955 event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
|
nickjillings@1453
|
956 };
|
nickjillings@1453
|
957 if (this.bufferNode.loop == false) {
|
nickjillings@1453
|
958 this.metric.startListening(audioEngineContext.timer.getTestTime());
|
nickjillings@1453
|
959 }
|
nickjillings@1453
|
960 this.bufferNode.start(startTime);
|
nickjillings@1453
|
961 }
|
nickjillings@1453
|
962 };
|
nickjillings@1453
|
963
|
nickjillings@1453
|
964 this.stop = function() {
|
nickjillings@1453
|
965 if (this.bufferNode != undefined)
|
nickjillings@1453
|
966 {
|
nickjillings@1453
|
967 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
|
nickjillings@1453
|
968 this.bufferNode.stop(0);
|
nickjillings@1453
|
969 this.bufferNode = undefined;
|
nickjillings@1453
|
970 }
|
nickjillings@1453
|
971 };
|
nickjillings@1453
|
972
|
nickjillings@1453
|
973 this.getCurrentPosition = function() {
|
nickjillings@1453
|
974 var time = audioEngineContext.timer.getTestTime();
|
nickjillings@1453
|
975 if (this.bufferNode != undefined) {
|
nickjillings@1453
|
976 if (this.bufferNode.loop == true) {
|
nickjillings@1453
|
977 if (audioEngineContext.status == 1) {
|
nickjillings@1453
|
978 return time%this.buffer.duration;
|
nickjillings@1453
|
979 } else {
|
nickjillings@1453
|
980 return 0;
|
nickjillings@1453
|
981 }
|
nickjillings@1453
|
982 } else {
|
nickjillings@1453
|
983 if (this.metric.listenHold) {
|
nickjillings@1453
|
984 return time - this.metric.listenStart;
|
nickjillings@1453
|
985 } else {
|
nickjillings@1453
|
986 return 0;
|
nickjillings@1453
|
987 }
|
nickjillings@1453
|
988 }
|
nickjillings@1453
|
989 } else {
|
nickjillings@1453
|
990 return 0;
|
nickjillings@1453
|
991 }
|
nickjillings@1453
|
992 };
|
nickjillings@1453
|
993
|
nickjillings@1453
|
994 this.constructTrack = function(url) {
|
nickjillings@1453
|
995 var request = new XMLHttpRequest();
|
nickjillings@1453
|
996 this.url = url;
|
nickjillings@1453
|
997 request.open('GET',url,true);
|
nickjillings@1453
|
998 request.responseType = 'arraybuffer';
|
nickjillings@1453
|
999
|
nickjillings@1453
|
1000 var audioObj = this;
|
nickjillings@1453
|
1001
|
nickjillings@1453
|
1002 // Create callback to decode the data asynchronously
|
nickjillings@1453
|
1003 request.onloadend = function() {
|
nickjillings@1453
|
1004 audioContext.decodeAudioData(request.response, function(decodedData) {
|
nickjillings@1453
|
1005 audioObj.buffer = decodedData;
|
nickjillings@1453
|
1006 audioObj.state = 1;
|
nickjillings@1453
|
1007 if (audioObj.specification.type != 'outsidereference')
|
nickjillings@1453
|
1008 {audioObj.interfaceDOM.enable();}
|
nickjillings@1453
|
1009 }, function(){
|
nickjillings@1453
|
1010 // Should only be called if there was an error, but sometimes gets called continuously
|
nickjillings@1453
|
1011 // Check here if the error is genuine
|
nickjillings@1453
|
1012 if (audioObj.state == 0 || audioObj.buffer == undefined) {
|
nickjillings@1453
|
1013 // Genuine error
|
nickjillings@1453
|
1014 console.log('FATAL - Error loading buffer on '+audioObj.id);
|
nickjillings@1453
|
1015 if (request.status == 404)
|
nickjillings@1453
|
1016 {
|
nickjillings@1453
|
1017 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
|
nickjillings@1453
|
1018 console.log('URL: '+audioObj.url);
|
nickjillings@1453
|
1019 errorSessionDump('Fragment '+audioObj.id+' 404 error');
|
nickjillings@1453
|
1020 }
|
nickjillings@1453
|
1021 }
|
nickjillings@1453
|
1022 });
|
nickjillings@1453
|
1023 };
|
nickjillings@1453
|
1024 request.send();
|
nickjillings@1453
|
1025 };
|
nickjillings@1453
|
1026
|
nickjillings@1453
|
1027 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1028 var root = document.createElement('audioElement');
|
nickjillings@1453
|
1029 root.id = this.specification.id;
|
nickjillings@1453
|
1030 root.setAttribute('url',this.url);
|
nickjillings@1453
|
1031 var file = document.createElement('file');
|
nickjillings@1453
|
1032 file.setAttribute('sampleRate',this.buffer.sampleRate);
|
nickjillings@1453
|
1033 file.setAttribute('channels',this.buffer.numberOfChannels);
|
nickjillings@1453
|
1034 file.setAttribute('sampleCount',this.buffer.length);
|
nickjillings@1453
|
1035 file.setAttribute('duration',this.buffer.duration);
|
nickjillings@1453
|
1036 root.appendChild(file);
|
nickjillings@1453
|
1037 if (this.specification.type != 'outsidereference') {
|
nickjillings@1453
|
1038 root.appendChild(this.interfaceDOM.exportXMLDOM(this));
|
nickjillings@1453
|
1039 root.appendChild(this.commentDOM.exportXMLDOM(this));
|
nickjillings@1453
|
1040 if(this.specification.type == 'anchor') {
|
nickjillings@1453
|
1041 root.setAttribute('anchor',true);
|
nickjillings@1453
|
1042 } else if(this.specification.type == 'reference') {
|
nickjillings@1453
|
1043 root.setAttribute('reference',true);
|
nickjillings@1453
|
1044 }
|
nickjillings@1453
|
1045 }
|
nickjillings@1453
|
1046 root.appendChild(this.metric.exportXMLDOM());
|
nickjillings@1453
|
1047 return root;
|
nickjillings@1453
|
1048 };
|
nickjillings@1453
|
1049 }
|
nickjillings@1453
|
1050
|
nickjillings@1453
|
1051 function timer()
|
nickjillings@1453
|
1052 {
|
nickjillings@1453
|
1053 /* Timer object used in audioEngine to keep track of session timings
|
nickjillings@1453
|
1054 * Uses the timer of the web audio API, so sample resolution
|
nickjillings@1453
|
1055 */
|
nickjillings@1453
|
1056 this.testStarted = false;
|
nickjillings@1453
|
1057 this.testStartTime = 0;
|
nickjillings@1453
|
1058 this.testDuration = 0;
|
nickjillings@1453
|
1059 this.minimumTestTime = 0; // No minimum test time
|
nickjillings@1453
|
1060 this.startTest = function()
|
nickjillings@1453
|
1061 {
|
nickjillings@1453
|
1062 if (this.testStarted == false)
|
nickjillings@1453
|
1063 {
|
nickjillings@1453
|
1064 this.testStartTime = audioContext.currentTime;
|
nickjillings@1453
|
1065 this.testStarted = true;
|
nickjillings@1453
|
1066 this.updateTestTime();
|
nickjillings@1453
|
1067 audioEngineContext.metric.initialiseTest();
|
nickjillings@1453
|
1068 }
|
nickjillings@1453
|
1069 };
|
nickjillings@1453
|
1070 this.stopTest = function()
|
nickjillings@1453
|
1071 {
|
nickjillings@1453
|
1072 if (this.testStarted)
|
nickjillings@1453
|
1073 {
|
nickjillings@1453
|
1074 this.testDuration = this.getTestTime();
|
nickjillings@1453
|
1075 this.testStarted = false;
|
nickjillings@1453
|
1076 } else {
|
nickjillings@1453
|
1077 console.log('ERR: Test tried to end before beginning');
|
nickjillings@1453
|
1078 }
|
nickjillings@1453
|
1079 };
|
nickjillings@1453
|
1080 this.updateTestTime = function()
|
nickjillings@1453
|
1081 {
|
nickjillings@1453
|
1082 if (this.testStarted)
|
nickjillings@1453
|
1083 {
|
nickjillings@1453
|
1084 this.testDuration = audioContext.currentTime - this.testStartTime;
|
nickjillings@1453
|
1085 }
|
nickjillings@1453
|
1086 };
|
nickjillings@1453
|
1087 this.getTestTime = function()
|
nickjillings@1453
|
1088 {
|
nickjillings@1453
|
1089 this.updateTestTime();
|
nickjillings@1453
|
1090 return this.testDuration;
|
nickjillings@1453
|
1091 };
|
nickjillings@1453
|
1092 }
|
nickjillings@1453
|
1093
|
nickjillings@1453
|
1094 function sessionMetrics(engine)
|
nickjillings@1453
|
1095 {
|
nickjillings@1453
|
1096 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
|
nickjillings@1453
|
1097 */
|
nickjillings@1453
|
1098 this.engine = engine;
|
nickjillings@1453
|
1099 this.lastClicked = -1;
|
nickjillings@1453
|
1100 this.data = -1;
|
nickjillings@1453
|
1101 this.reset = function() {
|
nickjillings@1453
|
1102 this.lastClicked = -1;
|
nickjillings@1453
|
1103 this.data = -1;
|
nickjillings@1453
|
1104 };
|
nickjillings@1453
|
1105 this.initialiseTest = function(){};
|
nickjillings@1453
|
1106 }
|
nickjillings@1453
|
1107
|
nickjillings@1453
|
1108 function metricTracker(caller)
|
nickjillings@1453
|
1109 {
|
nickjillings@1453
|
1110 /* Custom object to track and collect metric data
|
nickjillings@1453
|
1111 * Used only inside the audioObjects object.
|
nickjillings@1453
|
1112 */
|
nickjillings@1453
|
1113
|
nickjillings@1453
|
1114 this.listenedTimer = 0;
|
nickjillings@1453
|
1115 this.listenStart = 0;
|
nickjillings@1453
|
1116 this.listenHold = false;
|
nickjillings@1453
|
1117 this.initialPosition = -1;
|
nickjillings@1453
|
1118 this.movementTracker = [];
|
nickjillings@1453
|
1119 this.listenTracker =[];
|
nickjillings@1453
|
1120 this.wasListenedTo = false;
|
nickjillings@1453
|
1121 this.wasMoved = false;
|
nickjillings@1453
|
1122 this.hasComments = false;
|
nickjillings@1453
|
1123 this.parent = caller;
|
nickjillings@1453
|
1124
|
nickjillings@1453
|
1125 this.initialised = function(position)
|
nickjillings@1453
|
1126 {
|
nickjillings@1453
|
1127 if (this.initialPosition == -1) {
|
nickjillings@1453
|
1128 this.initialPosition = position;
|
nickjillings@1453
|
1129 }
|
nickjillings@1453
|
1130 };
|
nickjillings@1453
|
1131
|
nickjillings@1453
|
1132 this.moved = function(time,position)
|
nickjillings@1453
|
1133 {
|
nickjillings@1453
|
1134 this.wasMoved = true;
|
nickjillings@1453
|
1135 this.movementTracker[this.movementTracker.length] = [time, position];
|
nickjillings@1453
|
1136 };
|
nickjillings@1453
|
1137
|
nickjillings@1453
|
1138 this.startListening = function(time)
|
nickjillings@1453
|
1139 {
|
nickjillings@1453
|
1140 if (this.listenHold == false)
|
nickjillings@1453
|
1141 {
|
nickjillings@1453
|
1142 this.wasListenedTo = true;
|
nickjillings@1453
|
1143 this.listenStart = time;
|
nickjillings@1453
|
1144 this.listenHold = true;
|
nickjillings@1453
|
1145
|
nickjillings@1453
|
1146 var evnt = document.createElement('event');
|
nickjillings@1453
|
1147 var testTime = document.createElement('testTime');
|
nickjillings@1453
|
1148 testTime.setAttribute('start',time);
|
nickjillings@1453
|
1149 var bufferTime = document.createElement('bufferTime');
|
nickjillings@1453
|
1150 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
|
nickjillings@1453
|
1151 evnt.appendChild(testTime);
|
nickjillings@1453
|
1152 evnt.appendChild(bufferTime);
|
nickjillings@1453
|
1153 this.listenTracker.push(evnt);
|
nickjillings@1453
|
1154
|
nickjillings@1453
|
1155 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
|
nickjillings@1453
|
1156 }
|
nickjillings@1453
|
1157 };
|
nickjillings@1453
|
1158
|
nickjillings@1453
|
1159 this.stopListening = function(time,bufferStopTime)
|
nickjillings@1453
|
1160 {
|
nickjillings@1453
|
1161 if (this.listenHold == true)
|
nickjillings@1453
|
1162 {
|
nickjillings@1453
|
1163 var diff = time - this.listenStart;
|
nickjillings@1453
|
1164 this.listenedTimer += (diff);
|
nickjillings@1453
|
1165 this.listenStart = 0;
|
nickjillings@1453
|
1166 this.listenHold = false;
|
nickjillings@1453
|
1167
|
nickjillings@1453
|
1168 var evnt = this.listenTracker[this.listenTracker.length-1];
|
nickjillings@1453
|
1169 var testTime = evnt.getElementsByTagName('testTime')[0];
|
nickjillings@1453
|
1170 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
|
nickjillings@1453
|
1171 testTime.setAttribute('stop',time);
|
nickjillings@1453
|
1172 if (bufferStopTime == undefined) {
|
nickjillings@1453
|
1173 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
|
nickjillings@1453
|
1174 } else {
|
nickjillings@1453
|
1175 bufferTime.setAttribute('stop',bufferStopTime);
|
nickjillings@1453
|
1176 }
|
nickjillings@1453
|
1177 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
|
nickjillings@1453
|
1178 }
|
nickjillings@1453
|
1179 };
|
nickjillings@1453
|
1180
|
nickjillings@1453
|
1181 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1182 var root = document.createElement('metric');
|
nickjillings@1453
|
1183 if (audioEngineContext.metric.enableElementTimer) {
|
nickjillings@1453
|
1184 var mElementTimer = document.createElement('metricresult');
|
nickjillings@1453
|
1185 mElementTimer.setAttribute('name','enableElementTimer');
|
nickjillings@1453
|
1186 mElementTimer.textContent = this.listenedTimer;
|
nickjillings@1453
|
1187 root.appendChild(mElementTimer);
|
nickjillings@1453
|
1188 }
|
nickjillings@1453
|
1189 if (audioEngineContext.metric.enableElementTracker) {
|
nickjillings@1453
|
1190 var elementTrackerFull = document.createElement('metricResult');
|
nickjillings@1453
|
1191 elementTrackerFull.setAttribute('name','elementTrackerFull');
|
nickjillings@1453
|
1192 for (var k=0; k<this.movementTracker.length; k++)
|
nickjillings@1453
|
1193 {
|
nickjillings@1453
|
1194 var timePos = document.createElement('timePos');
|
nickjillings@1453
|
1195 timePos.id = k;
|
nickjillings@1453
|
1196 var time = document.createElement('time');
|
nickjillings@1453
|
1197 time.textContent = this.movementTracker[k][0];
|
nickjillings@1453
|
1198 var position = document.createElement('position');
|
nickjillings@1453
|
1199 position.textContent = this.movementTracker[k][1];
|
nickjillings@1453
|
1200 timePos.appendChild(time);
|
nickjillings@1453
|
1201 timePos.appendChild(position);
|
nickjillings@1453
|
1202 elementTrackerFull.appendChild(timePos);
|
nickjillings@1453
|
1203 }
|
nickjillings@1453
|
1204 root.appendChild(elementTrackerFull);
|
nickjillings@1453
|
1205 }
|
nickjillings@1453
|
1206 if (audioEngineContext.metric.enableElementListenTracker) {
|
nickjillings@1453
|
1207 var elementListenTracker = document.createElement('metricResult');
|
nickjillings@1453
|
1208 elementListenTracker.setAttribute('name','elementListenTracker');
|
nickjillings@1453
|
1209 for (var k=0; k<this.listenTracker.length; k++) {
|
nickjillings@1453
|
1210 elementListenTracker.appendChild(this.listenTracker[k]);
|
nickjillings@1453
|
1211 }
|
nickjillings@1453
|
1212 root.appendChild(elementListenTracker);
|
nickjillings@1453
|
1213 }
|
nickjillings@1453
|
1214 if (audioEngineContext.metric.enableElementInitialPosition) {
|
nickjillings@1453
|
1215 var elementInitial = document.createElement('metricResult');
|
nickjillings@1453
|
1216 elementInitial.setAttribute('name','elementInitialPosition');
|
nickjillings@1453
|
1217 elementInitial.textContent = this.initialPosition;
|
nickjillings@1453
|
1218 root.appendChild(elementInitial);
|
nickjillings@1453
|
1219 }
|
nickjillings@1453
|
1220 if (audioEngineContext.metric.enableFlagListenedTo) {
|
nickjillings@1453
|
1221 var flagListenedTo = document.createElement('metricResult');
|
nickjillings@1453
|
1222 flagListenedTo.setAttribute('name','elementFlagListenedTo');
|
nickjillings@1453
|
1223 flagListenedTo.textContent = this.wasListenedTo;
|
nickjillings@1453
|
1224 root.appendChild(flagListenedTo);
|
nickjillings@1453
|
1225 }
|
nickjillings@1453
|
1226 if (audioEngineContext.metric.enableFlagMoved) {
|
nickjillings@1453
|
1227 var flagMoved = document.createElement('metricResult');
|
nickjillings@1453
|
1228 flagMoved.setAttribute('name','elementFlagMoved');
|
nickjillings@1453
|
1229 flagMoved.textContent = this.wasMoved;
|
nickjillings@1453
|
1230 root.appendChild(flagMoved);
|
nickjillings@1453
|
1231 }
|
nickjillings@1453
|
1232 if (audioEngineContext.metric.enableFlagComments) {
|
nickjillings@1453
|
1233 var flagComments = document.createElement('metricResult');
|
nickjillings@1453
|
1234 flagComments.setAttribute('name','elementFlagComments');
|
nickjillings@1453
|
1235 if (this.parent.commentDOM == null)
|
nickjillings@1453
|
1236 {flag.textContent = 'false';}
|
nickjillings@1453
|
1237 else if (this.parent.commentDOM.textContent.length == 0)
|
nickjillings@1453
|
1238 {flag.textContent = 'false';}
|
nickjillings@1453
|
1239 else
|
nickjillings@1453
|
1240 {flag.textContet = 'true';}
|
nickjillings@1453
|
1241 root.appendChild(flagComments);
|
nickjillings@1453
|
1242 }
|
nickjillings@1453
|
1243
|
nickjillings@1453
|
1244 return root;
|
nickjillings@1453
|
1245 };
|
nickjillings@1453
|
1246 }
|
nickjillings@1453
|
1247
|
nickjillings@1453
|
1248 function randomiseOrder(input)
|
nickjillings@1453
|
1249 {
|
nickjillings@1453
|
1250 // This takes an array of information and randomises the order
|
nickjillings@1453
|
1251 var N = input.length;
|
nickjillings@1453
|
1252
|
nickjillings@1453
|
1253 var inputSequence = []; // For safety purposes: keep track of randomisation
|
nickjillings@1453
|
1254 for (var counter = 0; counter < N; ++counter)
|
nickjillings@1453
|
1255 inputSequence.push(counter) // Fill array
|
nickjillings@1453
|
1256 var inputSequenceClone = inputSequence.slice(0);
|
nickjillings@1453
|
1257
|
nickjillings@1453
|
1258 var holdArr = [];
|
nickjillings@1453
|
1259 var outputSequence = [];
|
nickjillings@1453
|
1260 for (var n=0; n<N; n++)
|
nickjillings@1453
|
1261 {
|
nickjillings@1453
|
1262 // First pick a random number
|
nickjillings@1453
|
1263 var r = Math.random();
|
nickjillings@1453
|
1264 // Multiply and floor by the number of elements left
|
nickjillings@1453
|
1265 r = Math.floor(r*input.length);
|
nickjillings@1453
|
1266 // Pick out that element and delete from the array
|
nickjillings@1453
|
1267 holdArr.push(input.splice(r,1)[0]);
|
nickjillings@1453
|
1268 // Do the same with sequence
|
nickjillings@1453
|
1269 outputSequence.push(inputSequence.splice(r,1)[0]);
|
nickjillings@1453
|
1270 }
|
nickjillings@1453
|
1271 console.log(inputSequenceClone.toString()); // print original array to console
|
nickjillings@1453
|
1272 console.log(outputSequence.toString()); // print randomised array to console
|
nickjillings@1453
|
1273 return holdArr;
|
nickjillings@1453
|
1274 }
|
nickjillings@1453
|
1275
|
nickjillings@1453
|
1276 function returnDateNode()
|
nickjillings@1453
|
1277 {
|
nickjillings@1453
|
1278 // Create an XML Node for the Date and Time a test was conducted
|
nickjillings@1453
|
1279 // Structure is
|
nickjillings@1453
|
1280 // <datetime>
|
nickjillings@1453
|
1281 // <date year="##" month="##" day="##">DD/MM/YY</date>
|
nickjillings@1453
|
1282 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
|
nickjillings@1453
|
1283 // </datetime>
|
nickjillings@1453
|
1284 var dateTime = new Date();
|
nickjillings@1453
|
1285 var year = document.createAttribute('year');
|
nickjillings@1453
|
1286 var month = document.createAttribute('month');
|
nickjillings@1453
|
1287 var day = document.createAttribute('day');
|
nickjillings@1453
|
1288 var hour = document.createAttribute('hour');
|
nickjillings@1453
|
1289 var minute = document.createAttribute('minute');
|
nickjillings@1453
|
1290 var secs = document.createAttribute('secs');
|
nickjillings@1453
|
1291
|
nickjillings@1453
|
1292 year.nodeValue = dateTime.getFullYear();
|
nickjillings@1453
|
1293 month.nodeValue = dateTime.getMonth()+1;
|
nickjillings@1453
|
1294 day.nodeValue = dateTime.getDate();
|
nickjillings@1453
|
1295 hour.nodeValue = dateTime.getHours();
|
nickjillings@1453
|
1296 minute.nodeValue = dateTime.getMinutes();
|
nickjillings@1453
|
1297 secs.nodeValue = dateTime.getSeconds();
|
nickjillings@1453
|
1298
|
nickjillings@1453
|
1299 var hold = document.createElement("datetime");
|
nickjillings@1453
|
1300 var date = document.createElement("date");
|
nickjillings@1453
|
1301 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
|
nickjillings@1453
|
1302 var time = document.createElement("time");
|
nickjillings@1453
|
1303 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
|
nickjillings@1453
|
1304
|
nickjillings@1453
|
1305 date.setAttributeNode(year);
|
nickjillings@1453
|
1306 date.setAttributeNode(month);
|
nickjillings@1453
|
1307 date.setAttributeNode(day);
|
nickjillings@1453
|
1308 time.setAttributeNode(hour);
|
nickjillings@1453
|
1309 time.setAttributeNode(minute);
|
nickjillings@1453
|
1310 time.setAttributeNode(secs);
|
nickjillings@1453
|
1311
|
nickjillings@1453
|
1312 hold.appendChild(date);
|
nickjillings@1453
|
1313 hold.appendChild(time);
|
nickjillings@1453
|
1314 return hold
|
nickjillings@1453
|
1315
|
nickjillings@1453
|
1316 }
|
nickjillings@1453
|
1317
|
nickjillings@1453
|
1318 function Specification() {
|
nickjillings@1453
|
1319 // Handles the decoding of the project specification XML into a simple JavaScript Object.
|
nickjillings@1453
|
1320
|
nickjillings@1453
|
1321 this.interfaceType = null;
|
nickjillings@1453
|
1322 this.commonInterface = null;
|
nickjillings@1453
|
1323 this.projectReturn = null;
|
nickjillings@1453
|
1324 this.randomiseOrder = null;
|
nickjillings@1453
|
1325 this.collectMetrics = null;
|
nickjillings@1453
|
1326 this.testPages = null;
|
nickjillings@1453
|
1327 this.preTest = null;
|
nickjillings@1453
|
1328 this.postTest = null;
|
nickjillings@1453
|
1329 this.metrics =[];
|
nickjillings@1453
|
1330
|
nickjillings@1453
|
1331 this.audioHolders = [];
|
nickjillings@1453
|
1332
|
nickjillings@1453
|
1333 this.decode = function() {
|
nickjillings@1453
|
1334 // projectXML - DOM Parsed document
|
nickjillings@1453
|
1335 this.projectXML = projectXML.childNodes[0];
|
nickjillings@1453
|
1336 var setupNode = projectXML.getElementsByTagName('setup')[0];
|
nickjillings@1453
|
1337 this.interfaceType = setupNode.getAttribute('interface');
|
nickjillings@1453
|
1338 this.projectReturn = setupNode.getAttribute('projectReturn');
|
nickjillings@1453
|
1339 this.testPages = setupNode.getAttribute('testPages');
|
nickjillings@1453
|
1340 if (setupNode.getAttribute('randomiseOrder') == "true") {
|
nickjillings@1453
|
1341 this.randomiseOrder = true;
|
nickjillings@1453
|
1342 } else {this.randomiseOrder = false;}
|
nickjillings@1453
|
1343 if (setupNode.getAttribute('collectMetrics') == "true") {
|
nickjillings@1453
|
1344 this.collectMetrics = true;
|
nickjillings@1453
|
1345 } else {this.collectMetrics = false;}
|
nickjillings@1453
|
1346 if (isNaN(Number(this.testPages)) || this.testPages == undefined)
|
nickjillings@1453
|
1347 {
|
nickjillings@1453
|
1348 this.testPages = null;
|
nickjillings@1453
|
1349 } else {
|
nickjillings@1453
|
1350 this.testPages = Number(this.testPages);
|
nickjillings@1453
|
1351 if (this.testPages == 0) {this.testPages = null;}
|
nickjillings@1453
|
1352 }
|
nickjillings@1453
|
1353 var metricCollection = setupNode.getElementsByTagName('Metric');
|
nickjillings@1453
|
1354
|
nickjillings@1453
|
1355 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
|
nickjillings@1453
|
1356 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
|
nickjillings@1453
|
1357
|
nickjillings@1453
|
1358 if (metricCollection.length > 0) {
|
nickjillings@1453
|
1359 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
|
nickjillings@1453
|
1360 for (var i=0; i<metricCollection.length; i++) {
|
nickjillings@1453
|
1361 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
|
nickjillings@1453
|
1362 }
|
nickjillings@1453
|
1363 }
|
nickjillings@1453
|
1364
|
nickjillings@1453
|
1365 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
|
nickjillings@1453
|
1366 if (commonInterfaceNode.length > 0) {
|
nickjillings@1453
|
1367 commonInterfaceNode = commonInterfaceNode[0];
|
nickjillings@1453
|
1368 } else {
|
nickjillings@1453
|
1369 commonInterfaceNode = undefined;
|
nickjillings@1453
|
1370 }
|
nickjillings@1453
|
1371
|
nickjillings@1453
|
1372 this.commonInterface = new function() {
|
nickjillings@1453
|
1373 this.OptionNode = function(child) {
|
nickjillings@1453
|
1374 this.type = child.nodeName;
|
nickjillings@1453
|
1375 if (this.type == 'option')
|
nickjillings@1453
|
1376 {
|
nickjillings@1453
|
1377 this.name = child.getAttribute('name');
|
nickjillings@1453
|
1378 }
|
nickjillings@1453
|
1379 else if (this.type == 'check') {
|
nickjillings@1453
|
1380 this.check = child.getAttribute('name');
|
nickjillings@1453
|
1381 if (this.check == 'scalerange') {
|
nickjillings@1453
|
1382 this.min = child.getAttribute('min');
|
nickjillings@1453
|
1383 this.max = child.getAttribute('max');
|
nickjillings@1453
|
1384 if (this.min == null) {this.min = 1;}
|
nickjillings@1453
|
1385 else if (Number(this.min) > 1 && this.min != null) {
|
nickjillings@1453
|
1386 this.min = Number(this.min)/100;
|
nickjillings@1453
|
1387 } else {
|
nickjillings@1453
|
1388 this.min = Number(this.min);
|
nickjillings@1453
|
1389 }
|
nickjillings@1453
|
1390 if (this.max == null) {this.max = 0;}
|
nickjillings@1453
|
1391 else if (Number(this.max) > 1 && this.max != null) {
|
nickjillings@1453
|
1392 this.max = Number(this.max)/100;
|
nickjillings@1453
|
1393 } else {
|
nickjillings@1453
|
1394 this.max = Number(this.max);
|
nickjillings@1453
|
1395 }
|
nickjillings@1453
|
1396 }
|
nickjillings@1453
|
1397 } else if (this.type == 'anchor' || this.type == 'reference') {
|
nickjillings@1455
|
1398 Console.log("WARNING: Anchor and Reference tags in the <interface> node are depricated");
|
nickjillings@1453
|
1399 }
|
nickjillings@1453
|
1400 };
|
nickjillings@1453
|
1401 this.options = [];
|
nickjillings@1453
|
1402 if (commonInterfaceNode != undefined) {
|
nickjillings@1453
|
1403 var child = commonInterfaceNode.firstElementChild;
|
nickjillings@1453
|
1404 while (child != undefined) {
|
nickjillings@1453
|
1405 this.options.push(new this.OptionNode(child));
|
nickjillings@1453
|
1406 child = child.nextElementSibling;
|
nickjillings@1453
|
1407 }
|
nickjillings@1453
|
1408 }
|
nickjillings@1453
|
1409 };
|
nickjillings@1453
|
1410
|
nickjillings@1453
|
1411 var audioHolders = projectXML.getElementsByTagName('audioHolder');
|
nickjillings@1453
|
1412 for (var i=0; i<audioHolders.length; i++) {
|
nickjillings@1453
|
1413 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
|
nickjillings@1453
|
1414 }
|
nickjillings@1453
|
1415
|
nickjillings@1453
|
1416 // New check if we need to randomise the test order
|
nickjillings@1453
|
1417 if (this.randomiseOrder)
|
nickjillings@1453
|
1418 {
|
nickjillings@1453
|
1419 this.audioHolders = randomiseOrder(this.audioHolders);
|
nickjillings@1453
|
1420 for (var i=0; i<this.audioHolders.length; i++)
|
nickjillings@1453
|
1421 {
|
nickjillings@1453
|
1422 this.audioHolders[i].presentedId = i;
|
nickjillings@1453
|
1423 }
|
nickjillings@1453
|
1424 }
|
nickjillings@1453
|
1425
|
nickjillings@1453
|
1426 if (this.testPages != null || this.testPages != undefined)
|
nickjillings@1453
|
1427 {
|
nickjillings@1453
|
1428 if (this.testPages > audioHolders.length)
|
nickjillings@1453
|
1429 {
|
nickjillings@1453
|
1430 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!');
|
nickjillings@1453
|
1431 this.testPages = audioHolders.length;
|
nickjillings@1453
|
1432 }
|
nickjillings@1453
|
1433 var aH = this.audioHolders;
|
nickjillings@1453
|
1434 this.audioHolders = [];
|
nickjillings@1453
|
1435 for (var i=0; i<this.testPages; i++)
|
nickjillings@1453
|
1436 {
|
nickjillings@1453
|
1437 this.audioHolders.push(aH[i]);
|
nickjillings@1453
|
1438 }
|
nickjillings@1453
|
1439 }
|
nickjillings@1453
|
1440 };
|
nickjillings@1453
|
1441
|
nickjillings@1453
|
1442 this.prepostNode = function(type,Collection) {
|
nickjillings@1453
|
1443 this.type = type;
|
nickjillings@1453
|
1444 this.options = [];
|
nickjillings@1453
|
1445
|
nickjillings@1453
|
1446 this.OptionNode = function(child) {
|
nickjillings@1453
|
1447
|
nickjillings@1453
|
1448 this.childOption = function(element) {
|
nickjillings@1453
|
1449 this.type = 'option';
|
nickjillings@1453
|
1450 this.id = element.id;
|
nickjillings@1453
|
1451 this.name = element.getAttribute('name');
|
nickjillings@1453
|
1452 this.text = element.textContent;
|
nickjillings@1453
|
1453 };
|
nickjillings@1453
|
1454
|
nickjillings@1453
|
1455 this.type = child.nodeName;
|
nickjillings@1453
|
1456 if (child.nodeName == "question") {
|
nickjillings@1453
|
1457 this.id = child.id;
|
nickjillings@1453
|
1458 this.mandatory;
|
nickjillings@1453
|
1459 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
|
nickjillings@1453
|
1460 else {this.mandatory = false;}
|
nickjillings@1453
|
1461 this.question = child.textContent;
|
nickjillings@1453
|
1462 if (child.getAttribute('boxsize') == null) {
|
nickjillings@1453
|
1463 this.boxsize = 'normal';
|
nickjillings@1453
|
1464 } else {
|
nickjillings@1453
|
1465 this.boxsize = child.getAttribute('boxsize');
|
nickjillings@1453
|
1466 }
|
nickjillings@1453
|
1467 } else if (child.nodeName == "statement") {
|
nickjillings@1453
|
1468 this.statement = child.textContent;
|
nickjillings@1453
|
1469 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
|
nickjillings@1453
|
1470 var element = child.firstElementChild;
|
nickjillings@1453
|
1471 this.id = child.id;
|
nickjillings@1453
|
1472 if (element == null) {
|
nickjillings@1453
|
1473 console.log('Malformed' +child.nodeName+ 'entry');
|
nickjillings@1453
|
1474 this.statement = 'Malformed' +child.nodeName+ 'entry';
|
nickjillings@1453
|
1475 this.type = 'statement';
|
nickjillings@1453
|
1476 } else {
|
nickjillings@1453
|
1477 this.options = [];
|
nickjillings@1453
|
1478 while (element != null) {
|
nickjillings@1453
|
1479 if (element.nodeName == 'statement' && this.statement == undefined){
|
nickjillings@1453
|
1480 this.statement = element.textContent;
|
nickjillings@1453
|
1481 } else if (element.nodeName == 'option') {
|
nickjillings@1453
|
1482 this.options.push(new this.childOption(element));
|
nickjillings@1453
|
1483 }
|
nickjillings@1453
|
1484 element = element.nextElementSibling;
|
nickjillings@1453
|
1485 }
|
nickjillings@1453
|
1486 }
|
nickjillings@1453
|
1487 } else if (child.nodeName == "number") {
|
nickjillings@1453
|
1488 this.statement = child.textContent;
|
nickjillings@1453
|
1489 this.id = child.id;
|
nickjillings@1453
|
1490 this.min = child.getAttribute('min');
|
nickjillings@1453
|
1491 this.max = child.getAttribute('max');
|
nickjillings@1453
|
1492 this.step = child.getAttribute('step');
|
nickjillings@1453
|
1493 }
|
nickjillings@1453
|
1494 };
|
nickjillings@1453
|
1495
|
nickjillings@1453
|
1496 // On construction:
|
nickjillings@1453
|
1497 if (Collection.length != 0) {
|
nickjillings@1453
|
1498 Collection = Collection[0];
|
nickjillings@1453
|
1499 if (Collection.childElementCount != 0) {
|
nickjillings@1453
|
1500 var child = Collection.firstElementChild;
|
nickjillings@1453
|
1501 this.options.push(new this.OptionNode(child));
|
nickjillings@1453
|
1502 while (child.nextElementSibling != null) {
|
nickjillings@1453
|
1503 child = child.nextElementSibling;
|
nickjillings@1453
|
1504 this.options.push(new this.OptionNode(child));
|
nickjillings@1453
|
1505 }
|
nickjillings@1453
|
1506 }
|
nickjillings@1453
|
1507 }
|
nickjillings@1453
|
1508 };
|
nickjillings@1453
|
1509
|
nickjillings@1453
|
1510 this.metricNode = function(name) {
|
nickjillings@1453
|
1511 this.enabled = name;
|
nickjillings@1453
|
1512 };
|
nickjillings@1453
|
1513
|
nickjillings@1453
|
1514 this.audioHolderNode = function(parent,xml) {
|
nickjillings@1453
|
1515 this.type = 'audioHolder';
|
nickjillings@1453
|
1516 this.presentedId = parent.audioHolders.length;
|
nickjillings@1453
|
1517 this.interfaceNode = function(DOM) {
|
nickjillings@1453
|
1518 var title = DOM.getElementsByTagName('title');
|
nickjillings@1453
|
1519 if (title.length == 0) {this.title = null;}
|
nickjillings@1453
|
1520 else {this.title = title[0].textContent;}
|
nickjillings@1453
|
1521 this.options = parent.commonInterface.options;
|
nickjillings@1453
|
1522 var scale = DOM.getElementsByTagName('scale');
|
nickjillings@1453
|
1523 this.scale = [];
|
nickjillings@1453
|
1524 for (var i=0; i<scale.length; i++) {
|
nickjillings@1453
|
1525 var arr = [null, null];
|
nickjillings@1453
|
1526 arr[0] = scale[i].getAttribute('position');
|
nickjillings@1453
|
1527 arr[1] = scale[i].textContent;
|
nickjillings@1453
|
1528 this.scale.push(arr);
|
nickjillings@1453
|
1529 }
|
nickjillings@1453
|
1530 };
|
nickjillings@1453
|
1531
|
nickjillings@1453
|
1532 this.audioElementNode = function(parent,xml) {
|
nickjillings@1453
|
1533 this.url = xml.getAttribute('url');
|
nickjillings@1453
|
1534 this.id = xml.id;
|
nickjillings@1453
|
1535 this.parent = parent;
|
nickjillings@1453
|
1536 this.type = xml.getAttribute('type');
|
nickjillings@1453
|
1537 if (this.type == null) {this.type = "normal";}
|
nickjillings@1453
|
1538 if (this.type == 'anchor') {this.anchor = true;}
|
nickjillings@1453
|
1539 else {this.anchor = false;}
|
nickjillings@1453
|
1540 if (this.type == 'reference') {this.reference = true;}
|
nickjillings@1453
|
1541 else {this.reference = false;}
|
nickjillings@1453
|
1542
|
nickjillings@1455
|
1543 if (this.anchor == true || this.reference == true)
|
nickjillings@1455
|
1544 {
|
nickjillings@1455
|
1545 this.marker = xml.getAttribute('marker');
|
nickjillings@1455
|
1546 if (this.marker != undefined)
|
nickjillings@1455
|
1547 {
|
nickjillings@1455
|
1548 this.marker = Number(this.marker);
|
nickjillings@1455
|
1549 if (isNaN(this.marker) == false)
|
nickjillings@1455
|
1550 {
|
nickjillings@1455
|
1551 if (this.marker > 1)
|
nickjillings@1455
|
1552 { this.marker /= 100.0;}
|
nickjillings@1455
|
1553 if (this.marker >= 0 && this.marker <= 1)
|
nickjillings@1455
|
1554 {
|
nickjillings@1455
|
1555 this.enforce = true;
|
nickjillings@1455
|
1556 return;
|
nickjillings@1455
|
1557 } else {
|
nickjillings@1455
|
1558 console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!");
|
nickjillings@1455
|
1559 console.log("ERROR - Marker not enforced!");
|
nickjillings@1455
|
1560 }
|
nickjillings@1455
|
1561 } else {
|
nickjillings@1455
|
1562 console.log("ERROR - Marker of audioElement "+this.id+" is not a number!");
|
nickjillings@1455
|
1563 console.log("ERROR - Marker not enforced!");
|
nickjillings@1455
|
1564 }
|
nickjillings@1455
|
1565 }
|
nickjillings@1453
|
1566 }
|
nickjillings@1455
|
1567 this.marker = false;
|
nickjillings@1455
|
1568 this.enforce = false;
|
nickjillings@1453
|
1569 };
|
nickjillings@1453
|
1570
|
nickjillings@1453
|
1571 this.commentQuestionNode = function(xml) {
|
nickjillings@1453
|
1572 this.childOption = function(element) {
|
nickjillings@1453
|
1573 this.type = 'option';
|
nickjillings@1453
|
1574 this.name = element.getAttribute('name');
|
nickjillings@1453
|
1575 this.text = element.textContent;
|
nickjillings@1453
|
1576 };
|
nickjillings@1453
|
1577 this.id = xml.id;
|
nickjillings@1453
|
1578 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
|
nickjillings@1453
|
1579 else {this.mandatory = false;}
|
nickjillings@1453
|
1580 this.type = xml.getAttribute('type');
|
nickjillings@1453
|
1581 if (this.type == undefined) {this.type = 'text';}
|
nickjillings@1453
|
1582 switch (this.type) {
|
nickjillings@1453
|
1583 case 'text':
|
nickjillings@1453
|
1584 this.question = xml.textContent;
|
nickjillings@1453
|
1585 break;
|
nickjillings@1453
|
1586 case 'radio':
|
nickjillings@1453
|
1587 var child = xml.firstElementChild;
|
nickjillings@1453
|
1588 this.options = [];
|
nickjillings@1453
|
1589 while (child != undefined) {
|
nickjillings@1453
|
1590 if (child.nodeName == 'statement' && this.statement == undefined) {
|
nickjillings@1453
|
1591 this.statement = child.textContent;
|
nickjillings@1453
|
1592 } else if (child.nodeName == 'option') {
|
nickjillings@1453
|
1593 this.options.push(new this.childOption(child));
|
nickjillings@1453
|
1594 }
|
nickjillings@1453
|
1595 child = child.nextElementSibling;
|
nickjillings@1453
|
1596 }
|
nickjillings@1453
|
1597 break;
|
nickjillings@1453
|
1598 case 'checkbox':
|
nickjillings@1453
|
1599 var child = xml.firstElementChild;
|
nickjillings@1453
|
1600 this.options = [];
|
nickjillings@1453
|
1601 while (child != undefined) {
|
nickjillings@1453
|
1602 if (child.nodeName == 'statement' && this.statement == undefined) {
|
nickjillings@1453
|
1603 this.statement = child.textContent;
|
nickjillings@1453
|
1604 } else if (child.nodeName == 'option') {
|
nickjillings@1453
|
1605 this.options.push(new this.childOption(child));
|
nickjillings@1453
|
1606 }
|
nickjillings@1453
|
1607 child = child.nextElementSibling;
|
nickjillings@1453
|
1608 }
|
nickjillings@1453
|
1609 break;
|
nickjillings@1453
|
1610 }
|
nickjillings@1453
|
1611 };
|
nickjillings@1453
|
1612
|
nickjillings@1453
|
1613 this.id = xml.id;
|
nickjillings@1453
|
1614 this.hostURL = xml.getAttribute('hostURL');
|
nickjillings@1453
|
1615 this.sampleRate = xml.getAttribute('sampleRate');
|
nickjillings@1453
|
1616 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
|
nickjillings@1453
|
1617 else {this.randomiseOrder = false;}
|
nickjillings@1453
|
1618 this.repeatCount = xml.getAttribute('repeatCount');
|
nickjillings@1453
|
1619 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
|
nickjillings@1453
|
1620 else {this.loop == false;}
|
nickjillings@1453
|
1621 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
|
nickjillings@1453
|
1622 else {this.elementComments = false;}
|
nickjillings@1453
|
1623
|
nickjillings@1453
|
1624 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
|
nickjillings@1453
|
1625 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
|
nickjillings@1453
|
1626
|
nickjillings@1453
|
1627 this.interfaces = [];
|
nickjillings@1453
|
1628 var interfaceDOM = xml.getElementsByTagName('interface');
|
nickjillings@1453
|
1629 for (var i=0; i<interfaceDOM.length; i++) {
|
nickjillings@1453
|
1630 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
|
nickjillings@1453
|
1631 }
|
nickjillings@1453
|
1632
|
nickjillings@1453
|
1633 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
|
nickjillings@1453
|
1634 if (this.commentBoxPrefix.length != 0) {
|
nickjillings@1453
|
1635 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
|
nickjillings@1453
|
1636 } else {
|
nickjillings@1453
|
1637 this.commentBoxPrefix = "Comment on track";
|
nickjillings@1453
|
1638 }
|
nickjillings@1453
|
1639
|
nickjillings@1453
|
1640 this.audioElements =[];
|
nickjillings@1453
|
1641 var audioElementsDOM = xml.getElementsByTagName('audioElements');
|
nickjillings@1453
|
1642 this.outsideReference = null;
|
nickjillings@1453
|
1643 for (var i=0; i<audioElementsDOM.length; i++) {
|
nickjillings@1453
|
1644 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
|
nickjillings@1453
|
1645 if (this.outsideReference == null) {
|
nickjillings@1453
|
1646 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]);
|
nickjillings@1453
|
1647 } else {
|
nickjillings@1453
|
1648 console.log('Error only one audioelement can be of type outsidereference per audioholder');
|
nickjillings@1453
|
1649 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
|
nickjillings@1453
|
1650 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
|
nickjillings@1453
|
1651 }
|
nickjillings@1453
|
1652 } else {
|
nickjillings@1453
|
1653 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
|
nickjillings@1453
|
1654 }
|
nickjillings@1453
|
1655 }
|
nickjillings@1453
|
1656
|
nickjillings@1453
|
1657 if (this.randomiseOrder) {
|
nickjillings@1453
|
1658 this.audioElements = randomiseOrder(this.audioElements);
|
nickjillings@1453
|
1659 }
|
nickjillings@1453
|
1660
|
nickjillings@1453
|
1661 this.commentQuestions = [];
|
nickjillings@1453
|
1662 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
|
nickjillings@1453
|
1663 for (var i=0; i<commentQuestionsDOM.length; i++) {
|
nickjillings@1453
|
1664 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
|
nickjillings@1453
|
1665 }
|
nickjillings@1453
|
1666 };
|
nickjillings@1453
|
1667 }
|
nickjillings@1453
|
1668
|
nickjillings@1453
|
1669 function Interface(specificationObject) {
|
nickjillings@1453
|
1670 // This handles the bindings between the interface and the audioEngineContext;
|
nickjillings@1453
|
1671 this.specification = specificationObject;
|
nickjillings@1453
|
1672 this.insertPoint = document.getElementById("topLevelBody");
|
nickjillings@1453
|
1673
|
nickjillings@1453
|
1674 // Bounded by interface!!
|
nickjillings@1453
|
1675 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
|
nickjillings@1453
|
1676 // For example, APE returns the slider position normalised in a <value> tag.
|
nickjillings@1453
|
1677 this.interfaceObjects = [];
|
nickjillings@1453
|
1678 this.interfaceObject = function(){};
|
nickjillings@1453
|
1679
|
nickjillings@1453
|
1680 this.resizeWindow = function(event)
|
nickjillings@1453
|
1681 {
|
nickjillings@1453
|
1682 for(var i=0; i<this.commentBoxes.length; i++)
|
nickjillings@1453
|
1683 {this.commentBoxes[i].resize();}
|
nickjillings@1453
|
1684 for(var i=0; i<this.commentQuestions.length; i++)
|
nickjillings@1453
|
1685 {this.commentQuestions[i].resize();}
|
nickjillings@1453
|
1686 try
|
nickjillings@1453
|
1687 {
|
nickjillings@1453
|
1688 resizeWindow(event);
|
nickjillings@1453
|
1689 }
|
nickjillings@1453
|
1690 catch(err)
|
nickjillings@1453
|
1691 {
|
nickjillings@1453
|
1692 console.log("Warning - Interface does not have Resize option");
|
nickjillings@1453
|
1693 console.log(err);
|
nickjillings@1453
|
1694 }
|
nickjillings@1453
|
1695 };
|
nickjillings@1453
|
1696
|
nickjillings@1453
|
1697 this.commentBoxes = [];
|
nickjillings@1453
|
1698 this.elementCommentBox = function(audioObject) {
|
nickjillings@1453
|
1699 var element = audioObject.specification;
|
nickjillings@1453
|
1700 this.audioObject = audioObject;
|
nickjillings@1453
|
1701 this.id = audioObject.id;
|
nickjillings@1453
|
1702 var audioHolderObject = audioObject.specification.parent;
|
nickjillings@1453
|
1703 // Create document objects to hold the comment boxes
|
nickjillings@1453
|
1704 this.trackComment = document.createElement('div');
|
nickjillings@1453
|
1705 this.trackComment.className = 'comment-div';
|
nickjillings@1453
|
1706 this.trackComment.id = 'comment-div-'+audioObject.id;
|
nickjillings@1453
|
1707 // Create a string next to each comment asking for a comment
|
nickjillings@1453
|
1708 this.trackString = document.createElement('span');
|
nickjillings@1453
|
1709 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
|
nickjillings@1453
|
1710 // Create the HTML5 comment box 'textarea'
|
nickjillings@1453
|
1711 this.trackCommentBox = document.createElement('textarea');
|
nickjillings@1453
|
1712 this.trackCommentBox.rows = '4';
|
nickjillings@1453
|
1713 this.trackCommentBox.cols = '100';
|
nickjillings@1453
|
1714 this.trackCommentBox.name = 'trackComment'+audioObject.id;
|
nickjillings@1453
|
1715 this.trackCommentBox.className = 'trackComment';
|
nickjillings@1453
|
1716 var br = document.createElement('br');
|
nickjillings@1453
|
1717 // Add to the holder.
|
nickjillings@1453
|
1718 this.trackComment.appendChild(this.trackString);
|
nickjillings@1453
|
1719 this.trackComment.appendChild(br);
|
nickjillings@1453
|
1720 this.trackComment.appendChild(this.trackCommentBox);
|
nickjillings@1453
|
1721
|
nickjillings@1453
|
1722 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1723 var root = document.createElement('comment');
|
nickjillings@1453
|
1724 if (this.audioObject.specification.parent.elementComments) {
|
nickjillings@1453
|
1725 var question = document.createElement('question');
|
nickjillings@1453
|
1726 question.textContent = this.trackString.textContent;
|
nickjillings@1453
|
1727 var response = document.createElement('response');
|
nickjillings@1453
|
1728 response.textContent = this.trackCommentBox.value;
|
nickjillings@1453
|
1729 console.log("Comment frag-"+this.id+": "+response.textContent);
|
nickjillings@1453
|
1730 root.appendChild(question);
|
nickjillings@1453
|
1731 root.appendChild(response);
|
nickjillings@1453
|
1732 }
|
nickjillings@1453
|
1733 return root;
|
nickjillings@1453
|
1734 };
|
nickjillings@1453
|
1735 this.resize = function()
|
nickjillings@1453
|
1736 {
|
nickjillings@1453
|
1737 var boxwidth = (window.innerWidth-100)/2;
|
nickjillings@1453
|
1738 if (boxwidth >= 600)
|
nickjillings@1453
|
1739 {
|
nickjillings@1453
|
1740 boxwidth = 600;
|
nickjillings@1453
|
1741 }
|
nickjillings@1453
|
1742 else if (boxwidth < 400)
|
nickjillings@1453
|
1743 {
|
nickjillings@1453
|
1744 boxwidth = 400;
|
nickjillings@1453
|
1745 }
|
nickjillings@1453
|
1746 this.trackComment.style.width = boxwidth+"px";
|
nickjillings@1453
|
1747 this.trackCommentBox.style.width = boxwidth-6+"px";
|
nickjillings@1453
|
1748 };
|
nickjillings@1453
|
1749 this.resize();
|
nickjillings@1453
|
1750 };
|
nickjillings@1453
|
1751
|
nickjillings@1453
|
1752 this.commentQuestions = [];
|
nickjillings@1453
|
1753
|
nickjillings@1453
|
1754 this.commentBox = function(commentQuestion) {
|
nickjillings@1453
|
1755 this.specification = commentQuestion;
|
nickjillings@1453
|
1756 // Create document objects to hold the comment boxes
|
nickjillings@1453
|
1757 this.holder = document.createElement('div');
|
nickjillings@1453
|
1758 this.holder.className = 'comment-div';
|
nickjillings@1453
|
1759 // Create a string next to each comment asking for a comment
|
nickjillings@1453
|
1760 this.string = document.createElement('span');
|
nickjillings@1453
|
1761 this.string.innerHTML = commentQuestion.question;
|
nickjillings@1453
|
1762 // Create the HTML5 comment box 'textarea'
|
nickjillings@1453
|
1763 this.textArea = document.createElement('textarea');
|
nickjillings@1453
|
1764 this.textArea.rows = '4';
|
nickjillings@1453
|
1765 this.textArea.cols = '100';
|
nickjillings@1453
|
1766 this.textArea.className = 'trackComment';
|
nickjillings@1453
|
1767 var br = document.createElement('br');
|
nickjillings@1453
|
1768 // Add to the holder.
|
nickjillings@1453
|
1769 this.holder.appendChild(this.string);
|
nickjillings@1453
|
1770 this.holder.appendChild(br);
|
nickjillings@1453
|
1771 this.holder.appendChild(this.textArea);
|
nickjillings@1453
|
1772
|
nickjillings@1453
|
1773 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1774 var root = document.createElement('comment');
|
nickjillings@1453
|
1775 root.id = this.specification.id;
|
nickjillings@1453
|
1776 root.setAttribute('type',this.specification.type);
|
nickjillings@1453
|
1777 root.textContent = this.textArea.value;
|
nickjillings@1453
|
1778 console.log("Question: "+this.string.textContent);
|
nickjillings@1453
|
1779 console.log("Response: "+root.textContent);
|
nickjillings@1453
|
1780 return root;
|
nickjillings@1453
|
1781 };
|
nickjillings@1453
|
1782 this.resize = function()
|
nickjillings@1453
|
1783 {
|
nickjillings@1453
|
1784 var boxwidth = (window.innerWidth-100)/2;
|
nickjillings@1453
|
1785 if (boxwidth >= 600)
|
nickjillings@1453
|
1786 {
|
nickjillings@1453
|
1787 boxwidth = 600;
|
nickjillings@1453
|
1788 }
|
nickjillings@1453
|
1789 else if (boxwidth < 400)
|
nickjillings@1453
|
1790 {
|
nickjillings@1453
|
1791 boxwidth = 400;
|
nickjillings@1453
|
1792 }
|
nickjillings@1453
|
1793 this.holder.style.width = boxwidth+"px";
|
nickjillings@1453
|
1794 this.textArea.style.width = boxwidth-6+"px";
|
nickjillings@1453
|
1795 };
|
nickjillings@1453
|
1796 this.resize();
|
nickjillings@1453
|
1797 };
|
nickjillings@1453
|
1798
|
nickjillings@1453
|
1799 this.radioBox = function(commentQuestion) {
|
nickjillings@1453
|
1800 this.specification = commentQuestion;
|
nickjillings@1453
|
1801 // Create document objects to hold the comment boxes
|
nickjillings@1453
|
1802 this.holder = document.createElement('div');
|
nickjillings@1453
|
1803 this.holder.className = 'comment-div';
|
nickjillings@1453
|
1804 // Create a string next to each comment asking for a comment
|
nickjillings@1453
|
1805 this.string = document.createElement('span');
|
nickjillings@1453
|
1806 this.string.innerHTML = commentQuestion.statement;
|
nickjillings@1453
|
1807 var br = document.createElement('br');
|
nickjillings@1453
|
1808 // Add to the holder.
|
nickjillings@1453
|
1809 this.holder.appendChild(this.string);
|
nickjillings@1453
|
1810 this.holder.appendChild(br);
|
nickjillings@1453
|
1811 this.options = [];
|
nickjillings@1453
|
1812 this.inputs = document.createElement('div');
|
nickjillings@1453
|
1813 this.span = document.createElement('div');
|
nickjillings@1453
|
1814 this.inputs.align = 'center';
|
nickjillings@1453
|
1815 this.inputs.style.marginLeft = '12px';
|
nickjillings@1453
|
1816 this.span.style.marginLeft = '12px';
|
nickjillings@1453
|
1817 this.span.align = 'center';
|
nickjillings@1453
|
1818 this.span.style.marginTop = '15px';
|
nickjillings@1453
|
1819
|
nickjillings@1453
|
1820 var optCount = commentQuestion.options.length;
|
nickjillings@1453
|
1821 for (var i=0; i<optCount; i++)
|
nickjillings@1453
|
1822 {
|
nickjillings@1453
|
1823 var div = document.createElement('div');
|
nickjillings@1453
|
1824 div.style.width = '80px';
|
nickjillings@1453
|
1825 div.style.float = 'left';
|
nickjillings@1453
|
1826 var input = document.createElement('input');
|
nickjillings@1453
|
1827 input.type = 'radio';
|
nickjillings@1453
|
1828 input.name = commentQuestion.id;
|
nickjillings@1453
|
1829 input.setAttribute('setvalue',commentQuestion.options[i].name);
|
nickjillings@1453
|
1830 input.className = 'comment-radio';
|
nickjillings@1453
|
1831 div.appendChild(input);
|
nickjillings@1453
|
1832 this.inputs.appendChild(div);
|
nickjillings@1453
|
1833
|
nickjillings@1453
|
1834
|
nickjillings@1453
|
1835 div = document.createElement('div');
|
nickjillings@1453
|
1836 div.style.width = '80px';
|
nickjillings@1453
|
1837 div.style.float = 'left';
|
nickjillings@1453
|
1838 div.align = 'center';
|
nickjillings@1453
|
1839 var span = document.createElement('span');
|
nickjillings@1453
|
1840 span.textContent = commentQuestion.options[i].text;
|
nickjillings@1453
|
1841 span.className = 'comment-radio-span';
|
nickjillings@1453
|
1842 div.appendChild(span);
|
nickjillings@1453
|
1843 this.span.appendChild(div);
|
nickjillings@1453
|
1844 this.options.push(input);
|
nickjillings@1453
|
1845 }
|
nickjillings@1453
|
1846 this.holder.appendChild(this.span);
|
nickjillings@1453
|
1847 this.holder.appendChild(this.inputs);
|
nickjillings@1453
|
1848
|
nickjillings@1453
|
1849 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1850 var root = document.createElement('comment');
|
nickjillings@1453
|
1851 root.id = this.specification.id;
|
nickjillings@1453
|
1852 root.setAttribute('type',this.specification.type);
|
nickjillings@1453
|
1853 var question = document.createElement('question');
|
nickjillings@1453
|
1854 question.textContent = this.string.textContent;
|
nickjillings@1453
|
1855 var response = document.createElement('response');
|
nickjillings@1453
|
1856 var i=0;
|
nickjillings@1453
|
1857 while(this.options[i].checked == false) {
|
nickjillings@1453
|
1858 i++;
|
nickjillings@1453
|
1859 if (i >= this.options.length) {
|
nickjillings@1453
|
1860 break;
|
nickjillings@1453
|
1861 }
|
nickjillings@1453
|
1862 }
|
nickjillings@1453
|
1863 if (i >= this.options.length) {
|
nickjillings@1453
|
1864 response.textContent = 'null';
|
nickjillings@1453
|
1865 } else {
|
nickjillings@1453
|
1866 response.textContent = this.options[i].getAttribute('setvalue');
|
nickjillings@1453
|
1867 response.setAttribute('number',i);
|
nickjillings@1453
|
1868 }
|
nickjillings@1453
|
1869 console.log('Comment: '+question.textContent);
|
nickjillings@1453
|
1870 console.log('Response: '+response.textContent);
|
nickjillings@1453
|
1871 root.appendChild(question);
|
nickjillings@1453
|
1872 root.appendChild(response);
|
nickjillings@1453
|
1873 return root;
|
nickjillings@1453
|
1874 };
|
nickjillings@1453
|
1875 this.resize = function()
|
nickjillings@1453
|
1876 {
|
nickjillings@1453
|
1877 var boxwidth = (window.innerWidth-100)/2;
|
nickjillings@1453
|
1878 if (boxwidth >= 600)
|
nickjillings@1453
|
1879 {
|
nickjillings@1453
|
1880 boxwidth = 600;
|
nickjillings@1453
|
1881 }
|
nickjillings@1453
|
1882 else if (boxwidth < 400)
|
nickjillings@1453
|
1883 {
|
nickjillings@1453
|
1884 boxwidth = 400;
|
nickjillings@1453
|
1885 }
|
nickjillings@1453
|
1886 this.holder.style.width = boxwidth+"px";
|
nickjillings@1453
|
1887 var text = this.holder.children[2];
|
nickjillings@1453
|
1888 var options = this.holder.children[3];
|
nickjillings@1453
|
1889 var optCount = options.children.length;
|
nickjillings@1453
|
1890 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
|
nickjillings@1453
|
1891 var options = options.firstChild;
|
nickjillings@1453
|
1892 var text = text.firstChild;
|
nickjillings@1453
|
1893 options.style.marginRight = spanMargin;
|
nickjillings@1453
|
1894 options.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1895 text.style.marginRight = spanMargin;
|
nickjillings@1453
|
1896 text.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1897 while(options.nextSibling != undefined)
|
nickjillings@1453
|
1898 {
|
nickjillings@1453
|
1899 options = options.nextSibling;
|
nickjillings@1453
|
1900 text = text.nextSibling;
|
nickjillings@1453
|
1901 options.style.marginRight = spanMargin;
|
nickjillings@1453
|
1902 options.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1903 text.style.marginRight = spanMargin;
|
nickjillings@1453
|
1904 text.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1905 }
|
nickjillings@1453
|
1906 };
|
nickjillings@1453
|
1907 this.resize();
|
nickjillings@1453
|
1908 };
|
nickjillings@1453
|
1909
|
nickjillings@1453
|
1910 this.checkboxBox = function(commentQuestion) {
|
nickjillings@1453
|
1911 this.specification = commentQuestion;
|
nickjillings@1453
|
1912 // Create document objects to hold the comment boxes
|
nickjillings@1453
|
1913 this.holder = document.createElement('div');
|
nickjillings@1453
|
1914 this.holder.className = 'comment-div';
|
nickjillings@1453
|
1915 // Create a string next to each comment asking for a comment
|
nickjillings@1453
|
1916 this.string = document.createElement('span');
|
nickjillings@1453
|
1917 this.string.innerHTML = commentQuestion.statement;
|
nickjillings@1453
|
1918 var br = document.createElement('br');
|
nickjillings@1453
|
1919 // Add to the holder.
|
nickjillings@1453
|
1920 this.holder.appendChild(this.string);
|
nickjillings@1453
|
1921 this.holder.appendChild(br);
|
nickjillings@1453
|
1922 this.options = [];
|
nickjillings@1453
|
1923 this.inputs = document.createElement('div');
|
nickjillings@1453
|
1924 this.span = document.createElement('div');
|
nickjillings@1453
|
1925 this.inputs.align = 'center';
|
nickjillings@1453
|
1926 this.inputs.style.marginLeft = '12px';
|
nickjillings@1453
|
1927 this.span.style.marginLeft = '12px';
|
nickjillings@1453
|
1928 this.span.align = 'center';
|
nickjillings@1453
|
1929 this.span.style.marginTop = '15px';
|
nickjillings@1453
|
1930
|
nickjillings@1453
|
1931 var optCount = commentQuestion.options.length;
|
nickjillings@1453
|
1932 for (var i=0; i<optCount; i++)
|
nickjillings@1453
|
1933 {
|
nickjillings@1453
|
1934 var div = document.createElement('div');
|
nickjillings@1453
|
1935 div.style.width = '80px';
|
nickjillings@1453
|
1936 div.style.float = 'left';
|
nickjillings@1453
|
1937 var input = document.createElement('input');
|
nickjillings@1453
|
1938 input.type = 'checkbox';
|
nickjillings@1453
|
1939 input.name = commentQuestion.id;
|
nickjillings@1453
|
1940 input.setAttribute('setvalue',commentQuestion.options[i].name);
|
nickjillings@1453
|
1941 input.className = 'comment-radio';
|
nickjillings@1453
|
1942 div.appendChild(input);
|
nickjillings@1453
|
1943 this.inputs.appendChild(div);
|
nickjillings@1453
|
1944
|
nickjillings@1453
|
1945
|
nickjillings@1453
|
1946 div = document.createElement('div');
|
nickjillings@1453
|
1947 div.style.width = '80px';
|
nickjillings@1453
|
1948 div.style.float = 'left';
|
nickjillings@1453
|
1949 div.align = 'center';
|
nickjillings@1453
|
1950 var span = document.createElement('span');
|
nickjillings@1453
|
1951 span.textContent = commentQuestion.options[i].text;
|
nickjillings@1453
|
1952 span.className = 'comment-radio-span';
|
nickjillings@1453
|
1953 div.appendChild(span);
|
nickjillings@1453
|
1954 this.span.appendChild(div);
|
nickjillings@1453
|
1955 this.options.push(input);
|
nickjillings@1453
|
1956 }
|
nickjillings@1453
|
1957 this.holder.appendChild(this.span);
|
nickjillings@1453
|
1958 this.holder.appendChild(this.inputs);
|
nickjillings@1453
|
1959
|
nickjillings@1453
|
1960 this.exportXMLDOM = function() {
|
nickjillings@1453
|
1961 var root = document.createElement('comment');
|
nickjillings@1453
|
1962 root.id = this.specification.id;
|
nickjillings@1453
|
1963 root.setAttribute('type',this.specification.type);
|
nickjillings@1453
|
1964 var question = document.createElement('question');
|
nickjillings@1453
|
1965 question.textContent = this.string.textContent;
|
nickjillings@1453
|
1966 root.appendChild(question);
|
nickjillings@1453
|
1967 console.log('Comment: '+question.textContent);
|
nickjillings@1453
|
1968 for (var i=0; i<this.options.length; i++) {
|
nickjillings@1453
|
1969 var response = document.createElement('response');
|
nickjillings@1453
|
1970 response.textContent = this.options[i].checked;
|
nickjillings@1453
|
1971 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
|
nickjillings@1453
|
1972 root.appendChild(response);
|
nickjillings@1453
|
1973 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
|
nickjillings@1453
|
1974 }
|
nickjillings@1453
|
1975 return root;
|
nickjillings@1453
|
1976 };
|
nickjillings@1453
|
1977 this.resize = function()
|
nickjillings@1453
|
1978 {
|
nickjillings@1453
|
1979 var boxwidth = (window.innerWidth-100)/2;
|
nickjillings@1453
|
1980 if (boxwidth >= 600)
|
nickjillings@1453
|
1981 {
|
nickjillings@1453
|
1982 boxwidth = 600;
|
nickjillings@1453
|
1983 }
|
nickjillings@1453
|
1984 else if (boxwidth < 400)
|
nickjillings@1453
|
1985 {
|
nickjillings@1453
|
1986 boxwidth = 400;
|
nickjillings@1453
|
1987 }
|
nickjillings@1453
|
1988 this.holder.style.width = boxwidth+"px";
|
nickjillings@1453
|
1989 var text = this.holder.children[2];
|
nickjillings@1453
|
1990 var options = this.holder.children[3];
|
nickjillings@1453
|
1991 var optCount = options.children.length;
|
nickjillings@1453
|
1992 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
|
nickjillings@1453
|
1993 var options = options.firstChild;
|
nickjillings@1453
|
1994 var text = text.firstChild;
|
nickjillings@1453
|
1995 options.style.marginRight = spanMargin;
|
nickjillings@1453
|
1996 options.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1997 text.style.marginRight = spanMargin;
|
nickjillings@1453
|
1998 text.style.marginLeft = spanMargin;
|
nickjillings@1453
|
1999 while(options.nextSibling != undefined)
|
nickjillings@1453
|
2000 {
|
nickjillings@1453
|
2001 options = options.nextSibling;
|
nickjillings@1453
|
2002 text = text.nextSibling;
|
nickjillings@1453
|
2003 options.style.marginRight = spanMargin;
|
nickjillings@1453
|
2004 options.style.marginLeft = spanMargin;
|
nickjillings@1453
|
2005 text.style.marginRight = spanMargin;
|
nickjillings@1453
|
2006 text.style.marginLeft = spanMargin;
|
nickjillings@1453
|
2007 }
|
nickjillings@1453
|
2008 };
|
nickjillings@1453
|
2009 this.resize();
|
nickjillings@1453
|
2010 };
|
nickjillings@1453
|
2011
|
nickjillings@1453
|
2012 this.createCommentBox = function(audioObject) {
|
nickjillings@1453
|
2013 var node = new this.elementCommentBox(audioObject);
|
nickjillings@1453
|
2014 this.commentBoxes.push(node);
|
nickjillings@1453
|
2015 audioObject.commentDOM = node;
|
nickjillings@1453
|
2016 return node;
|
nickjillings@1453
|
2017 };
|
nickjillings@1453
|
2018
|
nickjillings@1453
|
2019 this.sortCommentBoxes = function() {
|
nickjillings@1453
|
2020 var holder = [];
|
nickjillings@1453
|
2021 while (this.commentBoxes.length > 0) {
|
nickjillings@1453
|
2022 var node = this.commentBoxes.pop(0);
|
nickjillings@1453
|
2023 holder[node.id] = node;
|
nickjillings@1453
|
2024 }
|
nickjillings@1453
|
2025 this.commentBoxes = holder;
|
nickjillings@1453
|
2026 };
|
nickjillings@1453
|
2027
|
nickjillings@1453
|
2028 this.showCommentBoxes = function(inject, sort) {
|
nickjillings@1453
|
2029 if (sort) {interfaceContext.sortCommentBoxes();}
|
nickjillings@1453
|
2030 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
|
nickjillings@1453
|
2031 inject.appendChild(this.commentBoxes[i].trackComment);
|
nickjillings@1453
|
2032 }
|
nickjillings@1453
|
2033 };
|
nickjillings@1453
|
2034
|
nickjillings@1453
|
2035 this.deleteCommentBoxes = function() {
|
nickjillings@1453
|
2036 this.commentBoxes = [];
|
nickjillings@1453
|
2037 };
|
nickjillings@1453
|
2038
|
nickjillings@1453
|
2039 this.createCommentQuestion = function(element) {
|
nickjillings@1453
|
2040 var node;
|
nickjillings@1453
|
2041 if (element.type == 'text') {
|
nickjillings@1453
|
2042 node = new this.commentBox(element);
|
nickjillings@1453
|
2043 } else if (element.type == 'radio') {
|
nickjillings@1453
|
2044 node = new this.radioBox(element);
|
nickjillings@1453
|
2045 } else if (element.type == 'checkbox') {
|
nickjillings@1453
|
2046 node = new this.checkboxBox(element);
|
nickjillings@1453
|
2047 }
|
nickjillings@1453
|
2048 this.commentQuestions.push(node);
|
nickjillings@1453
|
2049 return node;
|
nickjillings@1453
|
2050 };
|
nickjillings@1453
|
2051
|
nickjillings@1453
|
2052 this.deleteCommentQuestions = function()
|
nickjillings@1453
|
2053 {
|
nickjillings@1453
|
2054 this.commentQuestions = [];
|
nickjillings@1453
|
2055 };
|
nickjillings@1453
|
2056
|
nickjillings@1453
|
2057 this.playhead = new function()
|
nickjillings@1453
|
2058 {
|
nickjillings@1453
|
2059 this.object = document.createElement('div');
|
nickjillings@1453
|
2060 this.object.className = 'playhead';
|
nickjillings@1453
|
2061 this.object.align = 'left';
|
nickjillings@1453
|
2062 var curTime = document.createElement('div');
|
nickjillings@1453
|
2063 curTime.style.width = '50px';
|
nickjillings@1453
|
2064 this.curTimeSpan = document.createElement('span');
|
nickjillings@1453
|
2065 this.curTimeSpan.textContent = '00:00';
|
nickjillings@1453
|
2066 curTime.appendChild(this.curTimeSpan);
|
nickjillings@1453
|
2067 this.object.appendChild(curTime);
|
nickjillings@1453
|
2068 this.scrubberTrack = document.createElement('div');
|
nickjillings@1453
|
2069 this.scrubberTrack.className = 'playhead-scrub-track';
|
nickjillings@1453
|
2070
|
nickjillings@1453
|
2071 this.scrubberHead = document.createElement('div');
|
nickjillings@1453
|
2072 this.scrubberHead.id = 'playhead-scrubber';
|
nickjillings@1453
|
2073 this.scrubberTrack.appendChild(this.scrubberHead);
|
nickjillings@1453
|
2074 this.object.appendChild(this.scrubberTrack);
|
nickjillings@1453
|
2075
|
nickjillings@1453
|
2076 this.timePerPixel = 0;
|
nickjillings@1453
|
2077 this.maxTime = 0;
|
nickjillings@1453
|
2078
|
nickjillings@1453
|
2079 this.playbackObject;
|
nickjillings@1453
|
2080
|
nickjillings@1453
|
2081 this.setTimePerPixel = function(audioObject) {
|
nickjillings@1453
|
2082 //maxTime must be in seconds
|
nickjillings@1453
|
2083 this.playbackObject = audioObject;
|
nickjillings@1453
|
2084 this.maxTime = audioObject.buffer.duration;
|
nickjillings@1453
|
2085 var width = 490; //500 - 10, 5 each side of the tracker head
|
nickjillings@1453
|
2086 this.timePerPixel = this.maxTime/490;
|
nickjillings@1453
|
2087 if (this.maxTime < 60) {
|
nickjillings@1453
|
2088 this.curTimeSpan.textContent = '0.00';
|
nickjillings@1453
|
2089 } else {
|
nickjillings@1453
|
2090 this.curTimeSpan.textContent = '00:00';
|
nickjillings@1453
|
2091 }
|
nickjillings@1453
|
2092 };
|
nickjillings@1453
|
2093
|
nickjillings@1453
|
2094 this.update = function() {
|
nickjillings@1453
|
2095 // Update the playhead position, startPlay must be called
|
nickjillings@1453
|
2096 if (this.timePerPixel > 0) {
|
nickjillings@1453
|
2097 var time = this.playbackObject.getCurrentPosition();
|
nickjillings@1453
|
2098 if (time > 0) {
|
nickjillings@1453
|
2099 var width = 490;
|
nickjillings@1453
|
2100 var pix = Math.floor(time/this.timePerPixel);
|
nickjillings@1453
|
2101 this.scrubberHead.style.left = pix+'px';
|
nickjillings@1453
|
2102 if (this.maxTime > 60.0) {
|
nickjillings@1453
|
2103 var secs = time%60;
|
nickjillings@1453
|
2104 var mins = Math.floor((time-secs)/60);
|
nickjillings@1453
|
2105 secs = secs.toString();
|
nickjillings@1453
|
2106 secs = secs.substr(0,2);
|
nickjillings@1453
|
2107 mins = mins.toString();
|
nickjillings@1453
|
2108 this.curTimeSpan.textContent = mins+':'+secs;
|
nickjillings@1453
|
2109 } else {
|
nickjillings@1453
|
2110 time = time.toString();
|
nickjillings@1453
|
2111 this.curTimeSpan.textContent = time.substr(0,4);
|
nickjillings@1453
|
2112 }
|
nickjillings@1453
|
2113 } else {
|
nickjillings@1453
|
2114 this.scrubberHead.style.left = '0px';
|
nickjillings@1453
|
2115 if (this.maxTime < 60) {
|
nickjillings@1453
|
2116 this.curTimeSpan.textContent = '0.00';
|
nickjillings@1453
|
2117 } else {
|
nickjillings@1453
|
2118 this.curTimeSpan.textContent = '00:00';
|
nickjillings@1453
|
2119 }
|
nickjillings@1453
|
2120 }
|
nickjillings@1453
|
2121 }
|
nickjillings@1453
|
2122 };
|
nickjillings@1453
|
2123
|
nickjillings@1453
|
2124 this.interval = undefined;
|
nickjillings@1453
|
2125
|
nickjillings@1453
|
2126 this.start = function() {
|
nickjillings@1453
|
2127 if (this.playbackObject != undefined && this.interval == undefined) {
|
nickjillings@1453
|
2128 if (this.maxTime < 60) {
|
nickjillings@1453
|
2129 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
|
nickjillings@1453
|
2130 } else {
|
nickjillings@1453
|
2131 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
|
nickjillings@1453
|
2132 }
|
nickjillings@1453
|
2133 }
|
nickjillings@1453
|
2134 };
|
nickjillings@1453
|
2135 this.stop = function() {
|
nickjillings@1453
|
2136 clearInterval(this.interval);
|
nickjillings@1453
|
2137 this.interval = undefined;
|
nickjillings@1453
|
2138 if (this.maxTime < 60) {
|
nickjillings@1453
|
2139 this.curTimeSpan.textContent = '0.00';
|
nickjillings@1453
|
2140 } else {
|
nickjillings@1453
|
2141 this.curTimeSpan.textContent = '00:00';
|
nickjillings@1453
|
2142 }
|
nickjillings@1453
|
2143 };
|
nickjillings@1453
|
2144 };
|
nickjillings@1453
|
2145
|
nickjillings@1453
|
2146 // Global Checkers
|
nickjillings@1453
|
2147 // These functions will help enforce the checkers
|
nickjillings@1453
|
2148 this.checkHiddenAnchor = function()
|
nickjillings@1453
|
2149 {
|
nickjillings@1453
|
2150 var audioHolder = testState.currentStateMap[testState.currentIndex];
|
nickjillings@1453
|
2151 if (audioHolder.anchorId != null)
|
nickjillings@1453
|
2152 {
|
nickjillings@1453
|
2153 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
|
nickjillings@1453
|
2154 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
|
nickjillings@1453
|
2155 {
|
nickjillings@1453
|
2156 // Anchor is not set below
|
nickjillings@1453
|
2157 console.log('Anchor node not below marker value');
|
nickjillings@1453
|
2158 alert('Please keep listening');
|
nickjillings@1453
|
2159 return false;
|
nickjillings@1453
|
2160 }
|
nickjillings@1453
|
2161 }
|
nickjillings@1453
|
2162 return true;
|
nickjillings@1453
|
2163 };
|
nickjillings@1453
|
2164
|
nickjillings@1453
|
2165 this.checkHiddenReference = function()
|
nickjillings@1453
|
2166 {
|
nickjillings@1453
|
2167 var audioHolder = testState.currentStateMap[testState.currentIndex];
|
nickjillings@1453
|
2168 if (audioHolder.referenceId != null)
|
nickjillings@1453
|
2169 {
|
nickjillings@1453
|
2170 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
|
nickjillings@1453
|
2171 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
|
nickjillings@1453
|
2172 {
|
nickjillings@1453
|
2173 // Anchor is not set below
|
nickjillings@1453
|
2174 console.log('Reference node not above marker value');
|
nickjillings@1453
|
2175 alert('Please keep listening');
|
nickjillings@1453
|
2176 return false;
|
nickjillings@1453
|
2177 }
|
nickjillings@1453
|
2178 }
|
nickjillings@1453
|
2179 return true;
|
nickjillings@1453
|
2180 };
|
nickjillings@1453
|
2181 } |