| OLD | NEW |
| 1 /** | 1 /** |
| 2 * Common JS that talks XHR back to the server and runs the code and receives | 2 * Common JS that talks XHR back to the server and runs the code and receives |
| 3 * the results. | 3 * the results. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * A polyfill for HTML Templates. | 7 * A polyfill for HTML Templates. |
| 8 * | 8 * |
| 9 * This just adds in the content attribute, it doesn't stop scripts | 9 * This just adds in the content attribute, it doesn't stop scripts |
| 10 * from running nor does it stop other side-effects. | 10 * from running nor does it stop other side-effects. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 var embed = document.getElementById('embed'); | 121 var embed = document.getElementById('embed'); |
| 122 var embedButton = document.getElementById('embedButton'); | 122 var embedButton = document.getElementById('embedButton'); |
| 123 var code = document.getElementById('code'); | 123 var code = document.getElementById('code'); |
| 124 var output = document.getElementById('output'); | 124 var output = document.getElementById('output'); |
| 125 var stdout = document.getElementById('stdout'); | 125 var stdout = document.getElementById('stdout'); |
| 126 var img = document.getElementById('img'); | 126 var img = document.getElementById('img'); |
| 127 var tryHistory = document.getElementById('tryHistory'); | 127 var tryHistory = document.getElementById('tryHistory'); |
| 128 var parser = new DOMParser(); | 128 var parser = new DOMParser(); |
| 129 var tryTemplate = document.getElementById('tryTemplate'); | 129 var tryTemplate = document.getElementById('tryTemplate'); |
| 130 | 130 |
| 131 var editor = CodeMirror.fromTextArea(code, { |
| 132 theme: "ambiance", |
| 133 lineNumbers: true, |
| 134 matchBrackets: true, |
| 135 mode: "text/x-c++src", |
| 136 indentUnit: 4, |
| 137 }); |
| 138 |
| 139 // Match the initial textarea size. |
| 140 editor.setSize(editor.defaultCharWidth() * code.cols, |
| 141 editor.defaultTextHeight() * code.rows); |
| 142 |
| 143 // Suppress changes to the first/last line (draw wrapper method) |
| 144 editor.on('beforeChange', function(cm, change) { |
| 145 if (change.from.line < 1 || change.from.line == cm.lineCount() - 1) |
| 146 change.cancel(); |
| 147 }); |
| 131 | 148 |
| 132 function beginWait() { | 149 function beginWait() { |
| 133 document.body.classList.add('waiting'); | 150 document.body.classList.add('waiting'); |
| 134 run.disabled = true; | 151 run.disabled = true; |
| 135 } | 152 } |
| 136 | 153 |
| 137 | 154 |
| 138 function endWait() { | 155 function endWait() { |
| 139 document.body.classList.remove('waiting'); | 156 document.body.classList.remove('waiting'); |
| 140 run.disabled = false; | 157 run.disabled = false; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 268 |
| 252 function onSubmitCode() { | 269 function onSubmitCode() { |
| 253 beginWait(); | 270 beginWait(); |
| 254 clearOutput(); | 271 clearOutput(); |
| 255 var req = new XMLHttpRequest(); | 272 var req = new XMLHttpRequest(); |
| 256 req.addEventListener('load', codeComplete); | 273 req.addEventListener('load', codeComplete); |
| 257 req.addEventListener('error', xhrError); | 274 req.addEventListener('error', xhrError); |
| 258 req.overrideMimeType('application/json'); | 275 req.overrideMimeType('application/json'); |
| 259 req.open('POST', '/', true); | 276 req.open('POST', '/', true); |
| 260 req.setRequestHeader('content-type', 'application/json'); | 277 req.setRequestHeader('content-type', 'application/json'); |
| 261 req.send(JSON.stringify({'code': code.value, 'name': workspaceName})); | 278 req.send(JSON.stringify({'code': editor.getValue(), 'name': workspaceName}
)); |
| 262 } | 279 } |
| 263 run.addEventListener('click', onSubmitCode); | 280 run.addEventListener('click', onSubmitCode); |
| 264 | 281 |
| 265 | 282 |
| 266 function onEmbedClick() { | 283 function onEmbedClick() { |
| 267 embed.style.display='inline'; | 284 embed.style.display='inline'; |
| 268 } | 285 } |
| 269 | 286 |
| 270 if (embedButton) { | 287 if (embedButton) { |
| 271 embedButton.addEventListener('click', onEmbedClick); | 288 embedButton.addEventListener('click', onEmbedClick); |
| 272 } | 289 } |
| 273 | 290 |
| 274 | 291 |
| 275 // Add the images to the history if we are on a workspace page. | 292 // Add the images to the history if we are on a workspace page. |
| 276 if (tryHistory && history) { | 293 if (tryHistory && history) { |
| 277 for (var i=0; i<history.length; i++) { | 294 for (var i=0; i<history.length; i++) { |
| 278 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); | 295 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); |
| 279 } | 296 } |
| 280 } | 297 } |
| 281 | 298 |
| 282 })(workspaceName); | 299 })(workspaceName); |
| OLD | NEW |