nicholas@2
|
1 /**
|
nicholas@2
|
2 * ape.js
|
nicholas@2
|
3 * Create the APE interface
|
nicholas@2
|
4 */
|
nicholas@2
|
5
|
nicholas@2
|
6 // Once this is loaded and parsed, begin execution
|
nicholas@2
|
7 loadInterface(projectXML);
|
nicholas@2
|
8
|
nicholas@2
|
9 function loadInterface(xmlDoc) {
|
nicholas@2
|
10
|
n@16
|
11 // Get the dimensions of the screen available to the page
|
nicholas@2
|
12 var width = window.innerWidth;
|
nicholas@2
|
13 var height = window.innerHeight;
|
nicholas@2
|
14
|
nicholas@2
|
15 // Set background to grey #ddd
|
nicholas@2
|
16 document.getElementsByTagName('body')[0].style.backgroundColor = '#ddd';
|
nicholas@2
|
17
|
nicholas@2
|
18 // The injection point into the HTML page
|
nicholas@2
|
19 var insertPoint = document.getElementById("topLevelBody");
|
n@22
|
20 var testContent = document.createElement('div');
|
n@22
|
21 testContent.id = 'testContent';
|
nicholas@2
|
22
|
nicholas@7
|
23
|
nicholas@2
|
24 // Decode parts of the xmlDoc that are needed
|
nicholas@2
|
25 // xmlDoc MUST already be parsed by jQuery!
|
nicholas@2
|
26 var xmlSetup = xmlDoc.find('setup');
|
nicholas@2
|
27 // Should put in an error function here incase of malprocessed or malformed XML
|
nicholas@2
|
28
|
nicholas@2
|
29 // Create the top div for the Title element
|
nicholas@2
|
30 var titleAttr = xmlSetup[0].attributes['title'];
|
nicholas@2
|
31 var title = document.createElement('div');
|
nicholas@2
|
32 title.className = "title";
|
nicholas@2
|
33 title.align = "center";
|
nicholas@2
|
34 var titleSpan = document.createElement('span');
|
nicholas@2
|
35
|
nicholas@2
|
36 // Set title to that defined in XML, else set to default
|
nicholas@2
|
37 if (titleAttr != undefined) {
|
nicholas@2
|
38 titleSpan.innerText = titleAttr.value;
|
nicholas@2
|
39 } else {
|
nicholas@2
|
40 titleSpan.innerText = 'APE Tool';
|
nicholas@2
|
41 }
|
nicholas@2
|
42 // Insert the titleSpan element into the title div element.
|
nicholas@2
|
43 title.appendChild(titleSpan);
|
nicholas@2
|
44
|
nicholas@7
|
45 // Store the return URL path in global projectReturn
|
nicholas@7
|
46 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
|
nicholas@7
|
47
|
nicholas@7
|
48 // Create Interface buttons!
|
nicholas@7
|
49 var interfaceButtons = document.createElement('div');
|
nicholas@7
|
50 interfaceButtons.id = 'interface-buttons';
|
nicholas@7
|
51
|
nicholas@7
|
52 // MANUAL DOWNLOAD POINT
|
nicholas@7
|
53 // If project return is null, this MUST be specified as the location to create the download link
|
nicholas@7
|
54 var downloadPoint = document.createElement('div');
|
nicholas@7
|
55 downloadPoint.id = 'download-point';
|
nicholas@7
|
56
|
nicholas@7
|
57 // Create playback start/stop points
|
nicholas@7
|
58 var playback = document.createElement("button");
|
nicholas@7
|
59 playback.innerText = 'Start';
|
n@16
|
60 // onclick function. Check if it is playing or not, call the correct function in the
|
n@16
|
61 // audioEngine, change the button text to reflect the next state.
|
nicholas@7
|
62 playback.onclick = function() {
|
nicholas@7
|
63 if (audioEngineContext.status == 0) {
|
nicholas@7
|
64 audioEngineContext.play();
|
nicholas@7
|
65 this.innerText = 'Stop';
|
nicholas@7
|
66 } else {
|
nicholas@7
|
67 audioEngineContext.stop();
|
nicholas@7
|
68 this.innerText = 'Start';
|
nicholas@7
|
69 }
|
n@16
|
70 };
|
nicholas@7
|
71 // Create Submit (save) button
|
nicholas@7
|
72 var submit = document.createElement("button");
|
nicholas@7
|
73 submit.innerText = 'Submit';
|
nicholas@7
|
74 submit.onclick = function() {
|
n@15
|
75 // TODO: Update this for postTest tags
|
nicholas@7
|
76 createProjectSave(projectReturn)
|
n@16
|
77 };
|
n@16
|
78 // Append the interface buttons into the interfaceButtons object.
|
nicholas@7
|
79 interfaceButtons.appendChild(playback);
|
nicholas@7
|
80 interfaceButtons.appendChild(submit);
|
nicholas@7
|
81 interfaceButtons.appendChild(downloadPoint);
|
nicholas@7
|
82
|
nicholas@2
|
83 // Now create the slider and HTML5 canvas boxes
|
nicholas@2
|
84
|
n@16
|
85 // Create the div box to center align
|
nicholas@2
|
86 var sliderBox = document.createElement('div');
|
nicholas@2
|
87 sliderBox.className = 'sliderCanvasDiv';
|
n@16
|
88 sliderBox.id = 'sliderCanvasHolder';
|
nicholas@2
|
89 sliderBox.align = 'center';
|
nicholas@2
|
90
|
n@16
|
91 // Create the slider box to hold the slider elements
|
nicholas@5
|
92 var canvas = document.createElement('div');
|
nicholas@2
|
93 canvas.id = 'slider';
|
n@16
|
94 // Must have a known EXACT width, as this is used later to determine the ratings
|
nicholas@5
|
95 canvas.style.width = width - 100 +"px";
|
nicholas@5
|
96 canvas.style.height = 150 + "px";
|
n@16
|
97 canvas.style.marginBottom = "25px";
|
nicholas@2
|
98 canvas.style.backgroundColor = '#eee';
|
nicholas@5
|
99 canvas.align = "left";
|
nicholas@2
|
100 sliderBox.appendChild(canvas);
|
n@16
|
101
|
n@16
|
102 // Global parent for the comment boxes on the page
|
nicholas@3
|
103 var feedbackHolder = document.createElement('div');
|
n@16
|
104 // Find the parent audioHolder object.
|
n@16
|
105 var audioHolder = xmlDoc.find('audioHolder');
|
n@16
|
106 audioHolder = audioHolder[0]; // Remove from one field array
|
n@16
|
107 // Extract the hostURL attribute. If not set, create an empty string.
|
n@16
|
108 var hostURL = audioHolder.attributes['hostURL'];
|
nicholas@7
|
109 if (hostURL == undefined) {
|
nicholas@7
|
110 hostURL = "";
|
nicholas@7
|
111 } else {
|
nicholas@7
|
112 hostURL = hostURL.value;
|
nicholas@7
|
113 }
|
n@16
|
114 // Extract the sampleRate. If set, convert the string to a Number.
|
n@16
|
115 var hostFs = audioHolder.attributes['sampleRate'];
|
n@15
|
116 if (hostFs != undefined) {
|
n@15
|
117 hostFs = Number(hostFs.value);
|
nicholas@7
|
118 }
|
nicholas@7
|
119
|
nicholas@7
|
120 /// CHECK FOR SAMPLE RATE COMPATIBILITY
|
n@15
|
121 if (hostFs != undefined) {
|
nicholas@7
|
122 if (Number(hostFs) != audioContext.sampleRate) {
|
nicholas@7
|
123 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
|
nicholas@7
|
124 alert(errStr);
|
nicholas@7
|
125 return;
|
nicholas@7
|
126 }
|
nicholas@7
|
127 }
|
n@16
|
128 // Find all the audioElements from the audioHolder
|
n@16
|
129 var audioElements = $(audioHolder).find('audioElements');
|
n@16
|
130 audioElements.each(function(index,element){
|
nicholas@7
|
131 // Find URL of track
|
n@16
|
132 // In this jQuery loop, variable 'this' holds the current audioElement.
|
n@16
|
133
|
n@16
|
134 // Now load each audio sample. First create the new track by passing the full URL
|
nicholas@7
|
135 var trackURL = hostURL + this.attributes['url'].value;
|
nicholas@7
|
136 audioEngineContext.newTrack(trackURL);
|
n@16
|
137 // Create document objects to hold the comment boxes
|
n@16
|
138 var trackComment = document.createElement('div');
|
n@16
|
139 // Create a string next to each comment asking for a comment
|
n@16
|
140 var trackString = document.createElement('span');
|
n@16
|
141 trackString.innerText = 'Comment on track '+index;
|
n@16
|
142 // Create the HTML5 comment box 'textarea'
|
n@16
|
143 var trackCommentBox = document.createElement('textarea');
|
n@16
|
144 trackCommentBox.rows = '4';
|
n@16
|
145 trackCommentBox.cols = '100';
|
n@16
|
146 trackCommentBox.name = 'trackComment'+index;
|
n@16
|
147 trackCommentBox.className = 'trackComment';
|
n@16
|
148 // Add to the holder.
|
n@16
|
149 trackComment.appendChild(trackString);
|
n@16
|
150 trackComment.appendChild(trackCommentBox);
|
n@16
|
151 feedbackHolder.appendChild(trackComment);
|
n@16
|
152
|
nicholas@5
|
153 // Create a slider per track
|
nicholas@5
|
154
|
nicholas@5
|
155 var trackSliderObj = document.createElement('div');
|
nicholas@5
|
156 trackSliderObj.className = 'track-slider';
|
nicholas@5
|
157 trackSliderObj.id = 'track-slider-'+index;
|
nicholas@5
|
158 trackSliderObj.style.position = 'absolute';
|
nicholas@5
|
159 // Distribute it randomnly
|
nicholas@5
|
160 var w = window.innerWidth - 100;
|
nicholas@5
|
161 w = Math.random()*w;
|
nicholas@5
|
162 trackSliderObj.style.left = Math.floor(w)+50+'px';
|
nicholas@5
|
163 trackSliderObj.style.height = "150px";
|
nicholas@5
|
164 trackSliderObj.style.width = "10px";
|
nicholas@5
|
165 trackSliderObj.style.backgroundColor = 'rgb(100,200,100)';
|
nicholas@5
|
166 trackSliderObj.innerHTML = '<span>'+index+'</span>';
|
nicholas@5
|
167 trackSliderObj.style.float = "left";
|
nicholas@5
|
168 trackSliderObj.draggable = true;
|
nicholas@5
|
169 trackSliderObj.ondragend = dragEnd;
|
nicholas@8
|
170
|
nicholas@8
|
171 // Onclick, switch playback to that track
|
nicholas@8
|
172 trackSliderObj.onclick = function() {
|
nicholas@8
|
173 // Get the track ID from the object ID
|
nicholas@8
|
174 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
|
nicholas@8
|
175 audioEngineContext.selectedTrack(id);
|
n@15
|
176 };
|
nicholas@8
|
177
|
nicholas@5
|
178 canvas.appendChild(trackSliderObj);
|
n@15
|
179 });
|
nicholas@3
|
180
|
nicholas@2
|
181
|
n@22
|
182 // Create pre and post test questions
|
n@22
|
183
|
nicholas@2
|
184 // Inject into HTML
|
nicholas@2
|
185 insertPoint.innerHTML = null; // Clear the current schema
|
n@22
|
186 testContent.appendChild(title); // Insert the title
|
n@22
|
187 testContent.appendChild(interfaceButtons);
|
n@22
|
188 testContent.appendChild(sliderBox);
|
n@22
|
189 testContent.appendChild(feedbackHolder);
|
n@22
|
190 insertPoint.appendChild(testContent);
|
n@22
|
191
|
n@22
|
192 var preTest = xmlDoc.find('PreTest');
|
n@22
|
193 var postTest = xmlDoc.find('PostTest');
|
n@22
|
194 preTest = preTest[0];
|
n@22
|
195 postTest = postTest[0];
|
n@22
|
196 if (preTest != undefined || postTest != undefined)
|
n@22
|
197 {
|
n@22
|
198 testContent.style.zIndex = 1;
|
n@22
|
199 var blank = document.createElement('div');
|
n@22
|
200 blank.id = 'testHalt';
|
n@22
|
201 blank.style.zIndex = 2;
|
n@22
|
202 blank.style.width = window.innerWidth + 'px';
|
n@22
|
203 blank.style.height = window.innerHeight + 'px';
|
n@22
|
204 blank.style.position = 'absolute';
|
n@22
|
205 blank.style.top = '0';
|
n@22
|
206 blank.style.left = '0';
|
n@22
|
207 insertPoint.appendChild(blank);
|
n@22
|
208 }
|
n@22
|
209
|
n@22
|
210 // Create Pre-Test Box
|
n@22
|
211 if (preTest != undefined && preTest.children.length >= 1)
|
n@22
|
212 {
|
n@22
|
213
|
n@22
|
214 var preTestHolder = document.createElement('div');
|
n@22
|
215 preTestHolder.id = 'preTestHolder';
|
n@22
|
216 preTestHolder.style.zIndex = 2;
|
n@22
|
217 preTestHolder.style.width = '500px';
|
n@22
|
218 preTestHolder.style.height = '250px';
|
n@22
|
219 preTestHolder.style.backgroundColor = '#fff';
|
n@22
|
220 preTestHolder.style.position = 'absolute';
|
n@22
|
221 preTestHolder.style.left = (window.innerWidth/2)-250 + 'px';
|
n@22
|
222 preTestHolder.style.top = (window.innerHeight/2)-125 + 'px';
|
n@22
|
223 // Parse the first box
|
n@22
|
224 var preTestOption = document.createElement('div');
|
n@22
|
225 preTestOption.id = 'preTest';
|
n@22
|
226 preTestOption.style.marginTop = '25px';
|
n@22
|
227 preTestOption.align = "center";
|
n@22
|
228 var child = preTest.children[0];
|
n@22
|
229 if (child.nodeName == 'statement')
|
n@22
|
230 {
|
n@22
|
231 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
|
n@22
|
232 } else if (child.nodeName == 'question')
|
n@22
|
233 {
|
n@22
|
234 var textHold = document.createElement('span');
|
n@22
|
235 textHold.innerHTML = child.innerHTML;
|
n@22
|
236 var textEnter = document.createElement('textarea');
|
n@22
|
237 preTestOption.appendChild(textHold);
|
n@22
|
238 preTestOption.appendChild(textEnter);
|
n@22
|
239 }
|
n@22
|
240 var nextButton = document.createElement('button');
|
n@22
|
241 nextButton.id = 'preTestNext';
|
n@22
|
242 nextButton.value = '1';
|
n@22
|
243 nextButton.innerHTML = 'next';
|
n@22
|
244 nextButton.style.position = 'relative';
|
n@22
|
245 nextButton.style.left = '450px';
|
n@22
|
246 nextButton.style.top = '175px';
|
n@22
|
247 nextButton.onclick = function() {
|
n@22
|
248 // Need to find and parse preTest again!
|
n@22
|
249 var preTest = projectXML.find('PreTest')[0];
|
n@22
|
250 if (this.value < preTest.children.length)
|
n@22
|
251 {
|
n@22
|
252 // More to process
|
n@22
|
253 var child = preTest.children[this.value];
|
n@22
|
254 if (child.nodeName == 'statement')
|
n@22
|
255 {
|
n@22
|
256 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
|
n@22
|
257 } else if (child.nodeName == 'question')
|
n@22
|
258 {
|
n@22
|
259 var textHold = document.createElement('span');
|
n@22
|
260 textHold.innerHTML = child.innerHTML;
|
n@22
|
261 var textEnter = document.createElement('textarea');
|
n@22
|
262 preTestOption.innerHTML = 'null';
|
n@22
|
263 preTestOption.appendChild(textHold);
|
n@22
|
264 preTestOption.appendChild(textEnter);
|
n@22
|
265 }
|
n@22
|
266 } else {
|
n@22
|
267 // Time to clear
|
n@22
|
268 preTestHolder.style.zIndex = -1;
|
n@22
|
269 preTestHolder.style.visibility = 'hidden';
|
n@22
|
270 var blank = document.getElementById('testHalt');
|
n@22
|
271 blank.style.zIndex = -2;
|
n@22
|
272 blank.style.visibility = 'hidden';
|
n@22
|
273 }
|
n@22
|
274 this.value++;
|
n@22
|
275 };
|
n@22
|
276
|
n@22
|
277 preTestHolder.appendChild(preTestOption);
|
n@22
|
278 preTestHolder.appendChild(nextButton);
|
n@22
|
279 insertPoint.appendChild(preTestHolder);
|
n@22
|
280 }
|
n@22
|
281
|
nicholas@2
|
282 }
|
nicholas@5
|
283
|
nicholas@5
|
284 function dragEnd(ev) {
|
nicholas@5
|
285 // Function call when a div has been dropped
|
nicholas@5
|
286 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
|
nicholas@5
|
287 this.style.left = (ev.x)+'px';
|
nicholas@5
|
288 } else {
|
nicholas@5
|
289 if (ev.x<50) {
|
nicholas@5
|
290 this.style.left = '50px';
|
nicholas@5
|
291 } else {
|
nicholas@5
|
292 this.style.left = window.innerWidth-50 + 'px';
|
nicholas@5
|
293 }
|
nicholas@5
|
294 }
|
nicholas@5
|
295 }
|
nicholas@7
|
296
|
nicholas@7
|
297 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
|
nicholas@7
|
298 function interfaceXMLSave(){
|
nicholas@7
|
299 // Create the XML string to be exported with results
|
nicholas@7
|
300 var xmlDoc = document.createElement("BrowserEvaluationResult");
|
nicholas@7
|
301 var trackSliderObjects = document.getElementsByClassName('track-slider');
|
nicholas@7
|
302 var commentObjects = document.getElementsByClassName('trackComment');
|
nicholas@7
|
303 var rateMin = 50;
|
nicholas@7
|
304 var rateMax = window.innerWidth-50;
|
nicholas@7
|
305 for (var i=0; i<trackSliderObjects.length; i++)
|
nicholas@7
|
306 {
|
nicholas@7
|
307 var trackObj = document.createElement("Track");
|
nicholas@7
|
308 trackObj.id = i;
|
nicholas@7
|
309 var slider = document.createElement("Rating");
|
nicholas@7
|
310 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
|
nicholas@7
|
311 rate = (rate-rateMin)/rateMax;
|
nicholas@7
|
312 slider.innerText = Math.floor(rate*100);
|
nicholas@7
|
313 var comment = document.createElement("Comment");
|
nicholas@7
|
314 comment.innerText = commentObjects[i].value;
|
nicholas@7
|
315 trackObj.appendChild(slider);
|
nicholas@7
|
316 trackObj.appendChild(comment);
|
nicholas@7
|
317 xmlDoc.appendChild(trackObj);
|
nicholas@7
|
318 }
|
nicholas@7
|
319
|
nicholas@7
|
320 return xmlDoc;
|
nicholas@7
|
321 }
|
nicholas@7
|
322
|