OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Common JS that talks XHR back to the server and runs the code and receives |
| 3 * the results. |
| 4 */ |
| 5 |
| 6 /** |
| 7 * All the functionality is wrapped up in this anonymous closure, but we need |
| 8 * to be told if we are on the workspace page or a normal try page, so the |
| 9 * workspaceName is passed into the closure, it must be set in the global |
| 10 * namespace. If workspaceName is the empty string then we know we aren't |
| 11 * running on a workspace page. |
| 12 */ |
| 13 (function(workspaceName) { |
| 14 var run = document.getElementById('run'); |
| 15 var code = document.getElementById('code'); |
| 16 var output = document.getElementById('output'); |
| 17 var img = document.getElementById('img'); |
| 18 var tryHistory = document.getElementById('tryHistory'); |
| 19 var parser = new DOMParser(); |
| 20 |
| 21 |
| 22 function beginWait() { |
| 23 document.body.classList.add('waiting'); |
| 24 run.disabled = true; |
| 25 } |
| 26 |
| 27 |
| 28 function endWait() { |
| 29 document.body.classList.remove('waiting'); |
| 30 run.disabled = false; |
| 31 } |
| 32 |
| 33 |
| 34 /** |
| 35 * Callback for when the XHR returns after attempting to run the code. |
| 36 * @param e The callback event. |
| 37 */ |
| 38 function codeComplete(e) { |
| 39 // The response is JSON of the form: |
| 40 // { |
| 41 // "message": "you had an error...", |
| 42 // "img": "<base64 encoded image but only on success>" |
| 43 // } |
| 44 // |
| 45 // The img is optional and only appears if there is a valid |
| 46 // image to display. |
| 47 endWait(); |
| 48 console.log(e.target.response); |
| 49 body = JSON.parse(e.target.response); |
| 50 output.innerText = body.message; |
| 51 if (body.hasOwnProperty('img')) { |
| 52 img.src = 'data:image/png;base64,' + body.img; |
| 53 } else { |
| 54 img.src = ''; |
| 55 } |
| 56 // Add the image to the history if we are on a workspace page. |
| 57 if (tryHistory) { |
| 58 var newHistoryStr = '<div class=tries>' + |
| 59 '<a href="/c/' + body.hash + '">' + |
| 60 ' <img width=64 height=64 src="/i/' + body.hash + '.png">' + |
| 61 '</a></div>'; |
| 62 |
| 63 var newHistory = parser.parseFromString(newHistoryStr, "text/html"); |
| 64 tryHistory.insertBefore(newHistory.body.firstChild, tryHistory.firstChil
d); |
| 65 } |
| 66 } |
| 67 |
| 68 |
| 69 /** |
| 70 * Callback where there's an XHR error. |
| 71 * @param e The callback event. |
| 72 */ |
| 73 function codeError(e) { |
| 74 endWait(); |
| 75 alert('Something bad happened: ' + e); |
| 76 } |
| 77 |
| 78 |
| 79 function onSubmitCode() { |
| 80 beginWait(); |
| 81 var req = new XMLHttpRequest(); |
| 82 req.addEventListener('load', codeComplete); |
| 83 req.addEventListener('error', codeError); |
| 84 req.overrideMimeType('application/json'); |
| 85 req.open('POST', '/', true); |
| 86 req.setRequestHeader('content-type', 'application/json'); |
| 87 req.send(JSON.stringify({"code": code.value, "name": workspaceName})); |
| 88 } |
| 89 run.addEventListener('click', onSubmitCode); |
| 90 })(workspaceName); |
OLD | NEW |