| 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 /** | 7 /** | 
| 8  * All the functionality is wrapped up in this anonymous closure, but we need | 8  * All the functionality is wrapped up in this anonymous closure, but we need | 
| 9  * to be told if we are on the workspace page or a normal try page, so the | 9  * to be told if we are on the workspace page or a normal try page, so the | 
| 10  * workspaceName is passed into the closure, it must be set in the global | 10  * workspaceName is passed into the closure, it must be set in the global | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 38       var chooseList     = document.getElementById('chooseList'); | 38       var chooseList     = document.getElementById('chooseList'); | 
| 39 | 39 | 
| 40       // Id of the source image to use, 0 if no source image is used. | 40       // Id of the source image to use, 0 if no source image is used. | 
| 41       var sourceId = 0; | 41       var sourceId = 0; | 
| 42 | 42 | 
| 43       sourceId = parseInt(enableSource.getAttribute('data-id')); | 43       sourceId = parseInt(enableSource.getAttribute('data-id')); | 
| 44       if (sourceId) { | 44       if (sourceId) { | 
| 45         sourceSelectByID(sourceId); | 45         sourceSelectByID(sourceId); | 
| 46       } | 46       } | 
| 47 | 47 | 
|  | 48       function setIFrameURL() { | 
|  | 49         var url = document.URL; | 
|  | 50         url = url.replace('/c/', '/iframe/'); | 
|  | 51         embed.value = '<iframe src="' + url + '" width="740" height="550" style=
     "border: solid #00a 5px; border-radius: 5px;"/>' | 
|  | 52       } | 
| 48 | 53 | 
| 49       function beginWait() { | 54       function beginWait() { | 
| 50         document.body.classList.add('waiting'); | 55         document.body.classList.add('waiting'); | 
| 51         run.disabled = true; | 56         run.disabled = true; | 
| 52       } | 57       } | 
| 53 | 58 | 
| 54 | 59 | 
| 55       function endWait() { | 60       function endWait() { | 
| 56         document.body.classList.remove('waiting'); | 61         document.body.classList.remove('waiting'); | 
| 57         run.disabled = false; | 62         run.disabled = false; | 
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 126         if (enableSource.checked) { | 131         if (enableSource.checked) { | 
| 127           beginWait(); | 132           beginWait(); | 
| 128           var req = new XMLHttpRequest(); | 133           var req = new XMLHttpRequest(); | 
| 129           req.addEventListener('load', sourcesComplete); | 134           req.addEventListener('load', sourcesComplete); | 
| 130           req.addEventListener('error', xhrError); | 135           req.addEventListener('error', xhrError); | 
| 131           req.overrideMimeType('application/json'); | 136           req.overrideMimeType('application/json'); | 
| 132           req.open('GET', '/sources/', true); | 137           req.open('GET', '/sources/', true); | 
| 133           req.send(); | 138           req.send(); | 
| 134         } else { | 139         } else { | 
| 135           sourceId = 0; | 140           sourceId = 0; | 
|  | 141           chooseSource.classList.remove('show'); | 
| 136         } | 142         } | 
| 137       } | 143       } | 
| 138 | 144 | 
| 139       enableSource.addEventListener('click', sourceClick, true); | 145       enableSource.addEventListener('click', sourceClick, true); | 
| 140       selectedSource.addEventListener('click', sourceClick, true); | 146       selectedSource.addEventListener('click', sourceClick, true); | 
| 141 | 147 | 
| 142 | 148 | 
| 143       var editor = CodeMirror.fromTextArea(code, { | 149       var editor = CodeMirror.fromTextArea(code, { | 
| 144         theme: "default", | 150         theme: "default", | 
| 145         lineNumbers: true, | 151         lineNumbers: true, | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 159        * Callback when there's an XHR error. | 165        * Callback when there's an XHR error. | 
| 160        * @param e The callback event. | 166        * @param e The callback event. | 
| 161        */ | 167        */ | 
| 162       function xhrError(e) { | 168       function xhrError(e) { | 
| 163         endWait(); | 169         endWait(); | 
| 164         alert('Something bad happened: ' + e); | 170         alert('Something bad happened: ' + e); | 
| 165       } | 171       } | 
| 166 | 172 | 
| 167       function clearOutput() { | 173       function clearOutput() { | 
| 168         output.textContent = ""; | 174         output.textContent = ""; | 
|  | 175         output.style.display='none'; | 
| 169         if (stdout) { | 176         if (stdout) { | 
| 170           stdout.textContent = ""; | 177           stdout.textContent = ""; | 
|  | 178           stdout.style.display='none'; | 
| 171         } | 179         } | 
| 172         embed.style.display='none'; | 180         if (embed) { | 
|  | 181           embed.style.display='none'; | 
|  | 182         } | 
| 173       } | 183       } | 
| 174 | 184 | 
| 175       /** | 185       /** | 
| 176        * Called when an image in the workspace history is clicked. | 186        * Called when an image in the workspace history is clicked. | 
| 177        */ | 187        */ | 
| 178       function historyClick() { | 188       function historyClick() { | 
| 179         beginWait(); | 189         beginWait(); | 
| 180         clearOutput(); | 190         clearOutput(); | 
| 181         var req = new XMLHttpRequest(); | 191         var req = new XMLHttpRequest(); | 
| 182         req.addEventListener('load', historyComplete); | 192         req.addEventListener('load', historyComplete); | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 197         //   "code": "source code for try" | 207         //   "code": "source code for try" | 
| 198         // } | 208         // } | 
| 199         endWait(); | 209         endWait(); | 
| 200         body = JSON.parse(e.target.response); | 210         body = JSON.parse(e.target.response); | 
| 201         code.value = body.code; | 211         code.value = body.code; | 
| 202         editor.setValue(body.code); | 212         editor.setValue(body.code); | 
| 203         img.src = '/i/'+body.hash+'.png'; | 213         img.src = '/i/'+body.hash+'.png'; | 
| 204         sourceSelectByID(body.source); | 214         sourceSelectByID(body.source); | 
| 205         if (permalink) { | 215         if (permalink) { | 
| 206           permalink.href = '/c/' + body.hash; | 216           permalink.href = '/c/' + body.hash; | 
|  | 217           permalink.style.display='inline-block'; | 
| 207         } | 218         } | 
| 208       } | 219       } | 
| 209 | 220 | 
| 210 | 221 | 
| 211       /** | 222       /** | 
| 212        * Add the given try image to the history of a workspace. | 223        * Add the given try image to the history of a workspace. | 
| 213        */ | 224        */ | 
| 214       function addToHistory(hash, imgUrl) { | 225       function addToHistory(hash, imgUrl) { | 
| 215         var clone = tryTemplate.content.cloneNode(true); | 226         var clone = tryTemplate.content.cloneNode(true); | 
| 216         clone.querySelector('img').src = imgUrl; | 227         clone.querySelector('img').src = imgUrl; | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 230         //   "message": "you had an error...", | 241         //   "message": "you had an error...", | 
| 231         //   "img": "<base64 encoded image but only on success>" | 242         //   "img": "<base64 encoded image but only on success>" | 
| 232         // } | 243         // } | 
| 233         // | 244         // | 
| 234         // The img is optional and only appears if there is a valid | 245         // The img is optional and only appears if there is a valid | 
| 235         // image to display. | 246         // image to display. | 
| 236         endWait(); | 247         endWait(); | 
| 237         console.log(e.target.response); | 248         console.log(e.target.response); | 
| 238         body = JSON.parse(e.target.response); | 249         body = JSON.parse(e.target.response); | 
| 239         output.textContent = body.message; | 250         output.textContent = body.message; | 
|  | 251         if (body.message) { | 
|  | 252           output.style.display = 'block'; | 
|  | 253         } | 
| 240         if (stdout) { | 254         if (stdout) { | 
| 241           stdout.textContent = body.stdout; | 255           stdout.textContent = body.stdout; | 
|  | 256           if (body.stdout) { | 
|  | 257             stdout.style.display = 'block'; | 
|  | 258           } | 
| 242         } | 259         } | 
| 243         if (body.hasOwnProperty('img')) { | 260         if (body.hasOwnProperty('img')) { | 
| 244           img.src = 'data:image/png;base64,' + body.img; | 261           img.src = 'data:image/png;base64,' + body.img; | 
| 245         } else { | 262         } else { | 
| 246           img.src = ''; | 263           img.src = ''; | 
| 247         } | 264         } | 
| 248         // Add the image to the history if we are on a workspace page. | 265         // Add the image to the history if we are on a workspace page. | 
| 249         if (tryHistory) { | 266         if (tryHistory) { | 
| 250           addToHistory(body.hash, 'data:image/png;base64,' + body.img); | 267           addToHistory(body.hash, 'data:image/png;base64,' + body.img); | 
| 251         } else { | 268         } else { | 
| 252           window.history.pushState(null, null, '/c/' + body.hash); | 269           window.history.pushState(null, null, '/c/' + body.hash); | 
| 253         } | 270         } | 
| 254         if (permalink) { | 271         if (permalink) { | 
| 255           permalink.href = '/c/' + body.hash; | 272           permalink.href = '/c/' + body.hash; | 
|  | 273           permalink.style.display = 'inline-block'; | 
| 256         } | 274         } | 
| 257         if (embed) { | 275         if (embed) { | 
| 258           var url = document.URL; | 276           setIFrameURL(); | 
| 259           url = url.replace('/c/', '/iframe/'); |  | 
| 260           embed.value = '<iframe src="' + url + '" width="740" height="550" styl
     e="border: solid #00a 5px; border-radius: 5px;"/>' |  | 
| 261         } | 277         } | 
| 262         if (embedButton && embedButton.hasAttribute('disabled')) { | 278         if (embedButton) { | 
| 263           embedButton.removeAttribute('disabled'); | 279           embedButton.style.display = 'inline-block'; | 
| 264         } | 280         } | 
| 265       } | 281       } | 
| 266 | 282 | 
| 267 | 283 | 
| 268       function onSubmitCode() { | 284       function onSubmitCode() { | 
| 269         beginWait(); | 285         beginWait(); | 
| 270         clearOutput(); | 286         clearOutput(); | 
| 271         var req = new XMLHttpRequest(); | 287         var req = new XMLHttpRequest(); | 
| 272         req.addEventListener('load', codeComplete); | 288         req.addEventListener('load', codeComplete); | 
| 273         req.addEventListener('error', xhrError); | 289         req.addEventListener('error', xhrError); | 
| 274         req.overrideMimeType('application/json'); | 290         req.overrideMimeType('application/json'); | 
| 275         req.open('POST', '/', true); | 291         req.open('POST', '/', true); | 
| 276         req.setRequestHeader('content-type', 'application/json'); | 292         req.setRequestHeader('content-type', 'application/json'); | 
| 277         req.send(JSON.stringify({'code': editor.getValue(), 'name': workspaceNam
     e, 'source': sourceId})); | 293         req.send(JSON.stringify({'code': editor.getValue(), 'name': workspaceNam
     e, 'source': sourceId})); | 
| 278       } | 294       } | 
| 279       run.addEventListener('click', onSubmitCode); | 295       run.addEventListener('click', onSubmitCode); | 
| 280 | 296 | 
| 281 | 297 | 
| 282       function onEmbedClick() { | 298       function onEmbedClick() { | 
| 283         embed.style.display='inline'; | 299         embed.style.display='inline'; | 
| 284       } | 300       } | 
| 285 | 301 | 
| 286       if (embedButton) { | 302       if (embedButton) { | 
| 287         embedButton.addEventListener('click', onEmbedClick); | 303         embedButton.addEventListener('click', onEmbedClick); | 
| 288       } | 304       } | 
| 289 | 305 | 
|  | 306       setIFrameURL(); | 
|  | 307 | 
| 290       // Add the images to the history if we are on a workspace page. | 308       // Add the images to the history if we are on a workspace page. | 
| 291       if (tryHistory && history_) { | 309       if (tryHistory && history_) { | 
| 292         for (var i=0; i<history_.length; i++) { | 310         for (var i=0; i<history_.length; i++) { | 
| 293           addToHistory(history_[i].hash, '/i/'+history_[i].hash+'.png'); | 311           addToHistory(history_[i].hash, '/i/'+history_[i].hash+'.png'); | 
| 294         } | 312         } | 
| 295       } | 313       } | 
| 296     } | 314     } | 
| 297 | 315 | 
| 298     // If loaded via HTML Imports then DOMContentLoaded will be long done. | 316     // If loaded via HTML Imports then DOMContentLoaded will be long done. | 
| 299     if (document.readyState != "loading") { | 317     if (document.readyState != "loading") { | 
| 300       onLoad(); | 318       onLoad(); | 
| 301     } else { | 319     } else { | 
| 302       this.addEventListener('load', onLoad); | 320       this.addEventListener('load', onLoad); | 
| 303     } | 321     } | 
| 304 | 322 | 
| 305 })(); | 323 })(); | 
| OLD | NEW | 
|---|