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 * All the functionality is wrapped up in this anonymous closure, but we need | 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 | 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 | 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 | 10 * namespace. If workspaceName is the empty string then we know we aren't |
11 * running on a workspace page. | 11 * running on a workspace page. |
12 */ | 12 */ |
13 (function(workspaceName) { | 13 (function(workspaceName) { |
14 var run = document.getElementById('run'); | 14 var run = document.getElementById('run'); |
| 15 var permalink = document.getElementById('permalink'); |
| 16 var embed = document.getElementById('embed'); |
| 17 var embedButton = document.getElementById('embedButton'); |
15 var code = document.getElementById('code'); | 18 var code = document.getElementById('code'); |
16 var output = document.getElementById('output'); | 19 var output = document.getElementById('output'); |
17 var img = document.getElementById('img'); | 20 var img = document.getElementById('img'); |
18 var tryHistory = document.getElementById('tryHistory'); | 21 var tryHistory = document.getElementById('tryHistory'); |
19 var parser = new DOMParser(); | 22 var parser = new DOMParser(); |
20 | 23 |
21 | 24 |
22 function beginWait() { | 25 function beginWait() { |
23 document.body.classList.add('waiting'); | 26 document.body.classList.add('waiting'); |
24 run.disabled = true; | 27 run.disabled = true; |
(...skipping 25 matching lines...) Expand all Loading... |
50 output.innerText = body.message; | 53 output.innerText = body.message; |
51 if (body.hasOwnProperty('img')) { | 54 if (body.hasOwnProperty('img')) { |
52 img.src = 'data:image/png;base64,' + body.img; | 55 img.src = 'data:image/png;base64,' + body.img; |
53 } else { | 56 } else { |
54 img.src = ''; | 57 img.src = ''; |
55 } | 58 } |
56 // Add the image to the history if we are on a workspace page. | 59 // Add the image to the history if we are on a workspace page. |
57 if (tryHistory) { | 60 if (tryHistory) { |
58 var newHistoryStr = '<div class=tries>' + | 61 var newHistoryStr = '<div class=tries>' + |
59 '<a href="/c/' + body.hash + '">' + | 62 '<a href="/c/' + body.hash + '">' + |
60 ' <img width=64 height=64 src="/i/' + body.hash + '.png">' + | 63 ' <img width=64 height=64 src="' + img.src + '">' + |
61 '</a></div>'; | 64 '</a></div>'; |
62 | 65 |
63 var newHistory = parser.parseFromString(newHistoryStr, "text/html"); | 66 var newHistory = parser.parseFromString(newHistoryStr, "text/html"); |
64 tryHistory.insertBefore(newHistory.body.firstChild, tryHistory.firstChil
d); | 67 tryHistory.insertBefore(newHistory.body.firstChild, tryHistory.firstChil
d); |
| 68 } else { |
| 69 window.history.pushState(null, null, "./" + body.hash); |
| 70 } |
| 71 if (permalink) { |
| 72 permalink.href = "/c/" + body.hash; |
| 73 } |
| 74 if (embed) { |
| 75 var url = document.URL; |
| 76 url = url.replace('/c/', '/iframe/'); |
| 77 embed.value = '<iframe src="' + url + '" width="740" height="550" style=
"border: solid #00a 5px; border-radius: 5px;"/>' |
| 78 } |
| 79 if (embedButton && embedButton.hasAttribute('disabled')) { |
| 80 embedButton.removeAttribute('disabled'); |
65 } | 81 } |
66 } | 82 } |
67 | 83 |
68 | 84 |
69 /** | 85 /** |
70 * Callback where there's an XHR error. | 86 * Callback where there's an XHR error. |
71 * @param e The callback event. | 87 * @param e The callback event. |
72 */ | 88 */ |
73 function codeError(e) { | 89 function codeError(e) { |
74 endWait(); | 90 endWait(); |
75 alert('Something bad happened: ' + e); | 91 alert('Something bad happened: ' + e); |
76 } | 92 } |
77 | 93 |
78 | 94 |
79 function onSubmitCode() { | 95 function onSubmitCode() { |
80 beginWait(); | 96 beginWait(); |
81 var req = new XMLHttpRequest(); | 97 var req = new XMLHttpRequest(); |
82 req.addEventListener('load', codeComplete); | 98 req.addEventListener('load', codeComplete); |
83 req.addEventListener('error', codeError); | 99 req.addEventListener('error', codeError); |
84 req.overrideMimeType('application/json'); | 100 req.overrideMimeType('application/json'); |
85 req.open('POST', '/', true); | 101 req.open('POST', '/', true); |
86 req.setRequestHeader('content-type', 'application/json'); | 102 req.setRequestHeader('content-type', 'application/json'); |
87 req.send(JSON.stringify({"code": code.value, "name": workspaceName})); | 103 req.send(JSON.stringify({"code": code.value, "name": workspaceName})); |
88 } | 104 } |
89 run.addEventListener('click', onSubmitCode); | 105 run.addEventListener('click', onSubmitCode); |
| 106 |
| 107 |
| 108 function onEmbedClick() { |
| 109 embed.style.display='inline'; |
| 110 } |
| 111 |
| 112 if (embedButton) { |
| 113 embedButton.addEventListener('click', onEmbedClick); |
| 114 } |
| 115 |
90 })(workspaceName); | 116 })(workspaceName); |
OLD | NEW |