Mercurial > hg > webaudioevaluationtool
comparison interfaces/AB.js @ 470:1330c77d212c Dev_main
Moved interfaces into their own sub-directory
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Wed, 13 Jan 2016 09:34:46 +0000 |
parents | |
children | bc343b3f4cce |
comparison
equal
deleted
inserted
replaced
469:d39d243e6601 | 470:1330c77d212c |
---|---|
1 // Once this is loaded and parsed, begin execution | |
2 loadInterface(); | |
3 | |
4 function loadInterface() { | |
5 // Get the dimensions of the screen available to the page | |
6 var width = window.innerWidth; | |
7 var height = window.innerHeight; | |
8 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema | |
9 | |
10 // Custom Comparitor Object | |
11 Interface.prototype.comparitor = null; | |
12 | |
13 // The injection point into the HTML page | |
14 interfaceContext.insertPoint = document.getElementById("topLevelBody"); | |
15 var testContent = document.createElement('div'); | |
16 testContent.id = 'testContent'; | |
17 | |
18 // Create the top div for the Title element | |
19 var titleAttr = specification.title; | |
20 var title = document.createElement('div'); | |
21 title.className = "title"; | |
22 title.align = "center"; | |
23 var titleSpan = document.createElement('span'); | |
24 | |
25 // Set title to that defined in XML, else set to default | |
26 if (titleAttr != undefined) { | |
27 titleSpan.textContent = titleAttr; | |
28 } else { | |
29 titleSpan.textContent = 'Listening test'; | |
30 } | |
31 // Insert the titleSpan element into the title div element. | |
32 title.appendChild(titleSpan); | |
33 | |
34 var pagetitle = document.createElement('div'); | |
35 pagetitle.className = "pageTitle"; | |
36 pagetitle.align = "center"; | |
37 var titleSpan = document.createElement('span'); | |
38 titleSpan.id = "pageTitle"; | |
39 pagetitle.appendChild(titleSpan); | |
40 | |
41 // Create Interface buttons! | |
42 var interfaceButtons = document.createElement('div'); | |
43 interfaceButtons.id = 'interface-buttons'; | |
44 interfaceButtons.style.height = '25px'; | |
45 | |
46 // Create playback start/stop points | |
47 var playback = document.createElement("button"); | |
48 playback.innerHTML = 'Stop'; | |
49 playback.id = 'playback-button'; | |
50 playback.style.float = 'left'; | |
51 // onclick function. Check if it is playing or not, call the correct function in the | |
52 // audioEngine, change the button text to reflect the next state. | |
53 playback.onclick = function() { | |
54 if (audioEngineContext.status == 1) { | |
55 audioEngineContext.stop(); | |
56 this.innerHTML = 'Stop'; | |
57 var time = audioEngineContext.timer.getTestTime(); | |
58 console.log('Stopped at ' + time); // DEBUG/SAFETY | |
59 } | |
60 }; | |
61 // Append the interface buttons into the interfaceButtons object. | |
62 interfaceButtons.appendChild(playback); | |
63 | |
64 // Global parent for the comment boxes on the page | |
65 var feedbackHolder = document.createElement('div'); | |
66 feedbackHolder.id = 'feedbackHolder'; | |
67 | |
68 // Construct the AB Boxes | |
69 var boxes = document.createElement('div'); | |
70 boxes.align = "center"; | |
71 boxes.id = "box-holders"; | |
72 boxes.style.float = "left"; | |
73 | |
74 var submit = document.createElement('button'); | |
75 submit.id = "submit"; | |
76 submit.onclick = buttonSubmitClick; | |
77 submit.className = "big-button"; | |
78 submit.textContent = "submit"; | |
79 submit.style.position = "relative"; | |
80 submit.style.left = (window.innerWidth-250)/2 + 'px'; | |
81 | |
82 feedbackHolder.appendChild(boxes); | |
83 | |
84 // Inject into HTML | |
85 testContent.appendChild(title); // Insert the title | |
86 testContent.appendChild(pagetitle); | |
87 testContent.appendChild(interfaceButtons); | |
88 testContent.appendChild(feedbackHolder); | |
89 testContent.appendChild(submit); | |
90 interfaceContext.insertPoint.appendChild(testContent); | |
91 | |
92 // Load the full interface | |
93 testState.initialise(); | |
94 testState.advanceState(); | |
95 } | |
96 | |
97 function loadTest(audioHolderObject) | |
98 { | |
99 var feedbackHolder = document.getElementById('feedbackHolder'); | |
100 var interfaceObj = audioHolderObject.interfaces; | |
101 if (interfaceObj.length > 1) | |
102 { | |
103 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node"); | |
104 } | |
105 interfaceObj = interfaceObj[0]; | |
106 | |
107 if(interfaceObj.title != null) | |
108 { | |
109 document.getElementById("pageTitle").textContent = interfaceObj.title; | |
110 } | |
111 | |
112 // Populate the comparitor object | |
113 interfaceContext.comparitor = new Comparitor(audioHolderObject); | |
114 resizeWindow(null); | |
115 } | |
116 | |
117 function Comparitor(audioHolderObject) | |
118 { | |
119 this.comparitorBox = function(audioElement,id,text) | |
120 { | |
121 this.parent = audioElement; | |
122 this.id = id; | |
123 this.value = 0; | |
124 this.disabled = true; | |
125 this.box = document.createElement('div'); | |
126 this.box.className = 'comparitor-holder'; | |
127 this.box.setAttribute('track-id',audioElement.id); | |
128 this.box.id = 'comparitor-'+text; | |
129 this.selector = document.createElement('div'); | |
130 this.selector.className = 'comparitor-selector disabled'; | |
131 var selectorText = document.createElement('span'); | |
132 selectorText.textContent = text; | |
133 this.selector.appendChild(selectorText); | |
134 this.playback = document.createElement('button'); | |
135 this.playback.className = 'comparitor-button'; | |
136 this.playback.disabled = true; | |
137 this.playback.textContent = "Listen"; | |
138 this.box.appendChild(this.selector); | |
139 this.box.appendChild(this.playback); | |
140 this.selector.onclick = function() | |
141 { | |
142 var time = audioEngineContext.timer.getTestTime(); | |
143 if ($(event.currentTarget).hasClass('disabled')) | |
144 { | |
145 console.log("Please wait until sample has loaded"); | |
146 return; | |
147 } | |
148 if (audioEngineContext.status == 0) | |
149 { | |
150 alert("Please listen to the samples before making a selection"); | |
151 console.log("Please listen to the samples before making a selection"); | |
152 return; | |
153 } | |
154 $(".comparitor-selector").removeClass('selected'); | |
155 var id = event.currentTarget.parentElement.getAttribute('track-id'); | |
156 interfaceContext.comparitor.selected = id; | |
157 $(event.currentTarget).addClass('selected'); | |
158 for (var i=0; i<interfaceContext.comparitor.comparitors.length; i++) | |
159 { | |
160 var obj = interfaceContext.comparitor.comparitors[i]; | |
161 if (i == id) { | |
162 obj.value = 1; | |
163 } else { | |
164 obj.value = 0; | |
165 } | |
166 obj.parent.metric.moved(time,obj.value); | |
167 } | |
168 console.log("Selected "+id+' ('+time+')'); | |
169 }; | |
170 this.playback.onclick = function() | |
171 { | |
172 $('.comparitor-button').text('Listen'); | |
173 var id = event.currentTarget.parentElement.getAttribute('track-id'); | |
174 audioEngineContext.play(id); | |
175 $(event.currentTarget).text('Playing'); | |
176 }; | |
177 | |
178 this.enable = function() | |
179 { | |
180 if (this.parent.state == 1) | |
181 { | |
182 $(this.selector).removeClass('disabled'); | |
183 this.playback.disabled = false; | |
184 } | |
185 }; | |
186 this.updateLoading = function(progress) | |
187 { | |
188 if (progress != 100) | |
189 { | |
190 progress = String(progress); | |
191 progress = progress.split('.')[0]; | |
192 this.playback.textContent = progress+'%'; | |
193 } else { | |
194 this.playback.textContent = "Listen"; | |
195 } | |
196 }; | |
197 this.exportXMLDOM = function(audioObject) | |
198 { | |
199 var node = storage.document.createElement('value'); | |
200 node.textContent = this.value; | |
201 return node; | |
202 }; | |
203 this.getValue = function() { | |
204 return this.value; | |
205 }; | |
206 this.getPresentedId = function() | |
207 { | |
208 return this.selector.children[0].textContent; | |
209 }; | |
210 this.canMove = function() | |
211 { | |
212 return false; | |
213 }; | |
214 }; | |
215 | |
216 this.boxHolders = document.getElementById('box-holders'); | |
217 this.boxHolders.innerHTML = null; | |
218 this.comparitors = []; | |
219 this.selected = null; | |
220 | |
221 // First generate the Audio Objects for the Audio Engine | |
222 for (var index=0; index<audioHolderObject.audioElements.length; index++) | |
223 { | |
224 var element = audioHolderObject.audioElements[index]; | |
225 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference') | |
226 { | |
227 console.log("WARNING - AB cannot have fixed reference"); | |
228 } | |
229 var audioObject = audioEngineContext.newTrack(element); | |
230 var node = new this.comparitorBox(audioObject,index,String.fromCharCode(65 + index)); | |
231 audioObject.bindInterface(node); | |
232 this.comparitors.push(node); | |
233 this.boxHolders.appendChild(node.box); | |
234 } | |
235 return this; | |
236 } | |
237 | |
238 function resizeWindow(event) | |
239 { | |
240 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px'; | |
241 var numObj = interfaceContext.comparitor.comparitors.length; | |
242 var boxW = numObj*312; | |
243 var diff = window.innerWidth - boxW; | |
244 while (diff < 0) | |
245 { | |
246 numObj = Math.ceil(numObj/2); | |
247 boxW = numObj*312; | |
248 diff = window.innerWidth - boxW; | |
249 } | |
250 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px'; | |
251 document.getElementById('box-holders').style.marginRight = diff/2 + 'px'; | |
252 document.getElementById('box-holders').style.width = boxW + 'px'; | |
253 } | |
254 | |
255 function buttonSubmitClick() | |
256 { | |
257 var checks = []; | |
258 checks = checks.concat(testState.currentStateMap.interfaces[0].options); | |
259 checks = checks.concat(specification.interfaces.options); | |
260 var canContinue = true; | |
261 | |
262 for (var i=0; i<checks.length; i++) { | |
263 if (checks[i].type == 'check') | |
264 { | |
265 switch(checks[i].name) { | |
266 case 'fragmentPlayed': | |
267 // Check if all fragments have been played | |
268 var checkState = interfaceContext.checkAllPlayed(); | |
269 if (checkState == false) {canContinue = false;} | |
270 break; | |
271 case 'fragmentFullPlayback': | |
272 // Check all fragments have been played to their full length | |
273 var checkState = interfaceContext.checkFragmentsFullyPlayed(); | |
274 if (checkState == false) {canContinue = false;} | |
275 break; | |
276 case 'fragmentMoved': | |
277 // Check all fragment sliders have been moved. | |
278 var checkState = interfaceContext.checkAllMoved(); | |
279 if (checkState == false) {canContinue = false;} | |
280 break; | |
281 case 'fragmentComments': | |
282 // Check all fragment sliders have been moved. | |
283 var checkState = interfaceContext.checkAllCommented(); | |
284 if (checkState == false) {canContinue = false;} | |
285 break; | |
286 default: | |
287 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface"); | |
288 break; | |
289 } | |
290 | |
291 } | |
292 if (!canContinue) {break;} | |
293 } | |
294 if (canContinue) | |
295 { | |
296 if (audioEngineContext.status == 1) { | |
297 var playback = document.getElementById('playback-button'); | |
298 playback.click(); | |
299 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options | |
300 } else | |
301 { | |
302 if (audioEngineContext.timer.testStarted == false) | |
303 { | |
304 alert('You have not started the test! Please press start to begin the test!'); | |
305 return; | |
306 } | |
307 } | |
308 testState.advanceState(); | |
309 } | |
310 } | |
311 | |
312 function pageXMLSave(store, pageSpecification) | |
313 { | |
314 // MANDATORY | |
315 // Saves a specific test page | |
316 // You can use this space to add any extra nodes to your XML <audioHolder> saves | |
317 // Get the current <page> information in store (remember to appendChild your data to it) | |
318 // pageSpecification is the current page node configuration | |
319 // To create new XML nodes, use storage.document.createElement(); | |
320 } |