Chromium Code Reviews| Index: experimental/webtry/js/run.js |
| diff --git a/experimental/webtry/js/run.js b/experimental/webtry/js/run.js |
| index 5cd43ac93ab1beee0912483e854c6fd0af02fe71..7eb2ede174e5e71dcce5dca428e62f9bfd9a2395 100644 |
| --- a/experimental/webtry/js/run.js |
| +++ b/experimental/webtry/js/run.js |
| @@ -49,14 +49,6 @@ |
| var tryTemplate = document.getElementById('tryTemplate'); |
| - function addToHistory(hash, imgUrl) { |
| - var clone = tryTemplate.content.cloneNode(true); |
| - clone.querySelector('a').href = '/c/' + hash; |
| - clone.querySelector('img').src = imgUrl; |
| - tryHistory.insertBefore(clone, tryHistory.firstChild); |
| - } |
| - |
| - |
| function beginWait() { |
| document.body.classList.add('waiting'); |
| run.disabled = true; |
| @@ -70,6 +62,61 @@ |
| /** |
| + * Callback when there's an XHR error. |
| + * @param e The callback event. |
| + */ |
| + function xhrError(e) { |
| + endWait(); |
| + alert('Something bad happened: ' + e); |
| + } |
| + |
| + /** |
| + * Called when an image in the workspace history is clicked. |
| + */ |
| + function historyClick() { |
| + beginWait(); |
| + var req = new XMLHttpRequest(); |
| + req.addEventListener('load', historyComplete); |
| + req.addEventListener('error', xhrError); |
| + req.overrideMimeType('application/json'); |
| + req.open('GET', this.getAttribute('data-try'), true); |
| + req.send(); |
| + } |
| + |
| + |
| + /** |
| + * Callback for when the XHR kicked off in historyClick() returns. |
| + */ |
| + function historyComplete(e) { |
| + // The response is JSON of the form: |
|
mtklein
2014/04/22 17:24:18
I love this kind of note. Thank you.
|
| + // { |
| + // "hash": "unique id for a try", |
| + // "code": "source code for try" |
| + // } |
| + endWait(); |
| + console.log(e.target.response); |
|
mtklein
2014/04/22 17:24:18
Intentionally leaving the console.log in there?
jcgregorio
2014/04/22 18:02:08
Done.
|
| + body = JSON.parse(e.target.response); |
| + code.value = body.code; |
| + img.src = '/i/'+body.hash+'.png'; |
| + if (permalink) { |
| + permalink.href = '/c/' + body.hash; |
| + } |
| + } |
| + |
| + |
| + /** |
| + * Add the given try image to the history of a workspace. |
| + */ |
| + function addToHistory(hash, imgUrl) { |
| + var clone = tryTemplate.content.cloneNode(true); |
| + clone.querySelector('img').src = imgUrl; |
| + clone.querySelector('.tries').setAttribute('data-try', '/json/' + hash); |
| + tryHistory.insertBefore(clone, tryHistory.firstChild); |
| + tryHistory.querySelector('.tries').addEventListener('click', historyClick, true); |
| + } |
| + |
| + |
| + /** |
| * Callback for when the XHR returns after attempting to run the code. |
| * @param e The callback event. |
| */ |
| @@ -95,10 +142,10 @@ |
| if (tryHistory) { |
| addToHistory(body.hash, 'data:image/png;base64,' + body.img); |
| } else { |
| - window.history.pushState(null, null, "./" + body.hash); |
| + window.history.pushState(null, null, './' + body.hash); |
| } |
| if (permalink) { |
| - permalink.href = "/c/" + body.hash; |
| + permalink.href = '/c/' + body.hash; |
| } |
| if (embed) { |
| var url = document.URL; |
| @@ -111,25 +158,15 @@ |
| } |
| - /** |
| - * Callback where there's an XHR error. |
| - * @param e The callback event. |
| - */ |
| - function codeError(e) { |
| - endWait(); |
| - alert('Something bad happened: ' + e); |
| - } |
| - |
| - |
| function onSubmitCode() { |
| beginWait(); |
| var req = new XMLHttpRequest(); |
| req.addEventListener('load', codeComplete); |
| - req.addEventListener('error', codeError); |
| + req.addEventListener('error', xhrError); |
| req.overrideMimeType('application/json'); |
| req.open('POST', '/', true); |
| req.setRequestHeader('content-type', 'application/json'); |
| - req.send(JSON.stringify({"code": code.value, "name": workspaceName})); |
| + req.send(JSON.stringify({'code': code.value, 'name': workspaceName})); |
| } |
| run.addEventListener('click', onSubmitCode); |