Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: experimental/webtry/js/run.js

Issue 246823004: Factor out common template code. Send stdout back to web page. (Closed) Base URL: https://skia.googlesource.com/skia.git@clickhistory
Patch Set: rebase Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/webtry/css/webtry.css ('k') | experimental/webtry/templates/content.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 25 matching lines...) Expand all
36 * that are included in this workspace. That variable is used to 36 * that are included in this workspace. That variable is used to
37 * populate the history list. 37 * populate the history list.
38 */ 38 */
39 (function(workspaceName) { 39 (function(workspaceName) {
40 var run = document.getElementById('run'); 40 var run = document.getElementById('run');
41 var permalink = document.getElementById('permalink'); 41 var permalink = document.getElementById('permalink');
42 var embed = document.getElementById('embed'); 42 var embed = document.getElementById('embed');
43 var embedButton = document.getElementById('embedButton'); 43 var embedButton = document.getElementById('embedButton');
44 var code = document.getElementById('code'); 44 var code = document.getElementById('code');
45 var output = document.getElementById('output'); 45 var output = document.getElementById('output');
46 var stdout = document.getElementById('stdout');
46 var img = document.getElementById('img'); 47 var img = document.getElementById('img');
47 var tryHistory = document.getElementById('tryHistory'); 48 var tryHistory = document.getElementById('tryHistory');
48 var parser = new DOMParser(); 49 var parser = new DOMParser();
49 var tryTemplate = document.getElementById('tryTemplate'); 50 var tryTemplate = document.getElementById('tryTemplate');
50 51
51 52
52 function beginWait() { 53 function beginWait() {
53 document.body.classList.add('waiting'); 54 document.body.classList.add('waiting');
54 run.disabled = true; 55 run.disabled = true;
55 } 56 }
56 57
57 58
58 function endWait() { 59 function endWait() {
59 document.body.classList.remove('waiting'); 60 document.body.classList.remove('waiting');
60 run.disabled = false; 61 run.disabled = false;
61 } 62 }
62 63
63 64
64 /** 65 /**
65 * Callback when there's an XHR error. 66 * Callback when there's an XHR error.
66 * @param e The callback event. 67 * @param e The callback event.
67 */ 68 */
68 function xhrError(e) { 69 function xhrError(e) {
69 endWait(); 70 endWait();
70 alert('Something bad happened: ' + e); 71 alert('Something bad happened: ' + e);
71 } 72 }
72 73
74 function clearOutput() {
75 output.innerText = "";
76 if (stdout) {
77 stdout.innerText = "";
78 }
79 embed.style.display='none';
80 }
81
73 /** 82 /**
74 * Called when an image in the workspace history is clicked. 83 * Called when an image in the workspace history is clicked.
75 */ 84 */
76 function historyClick() { 85 function historyClick() {
77 beginWait(); 86 beginWait();
87 clearOutput();
78 var req = new XMLHttpRequest(); 88 var req = new XMLHttpRequest();
79 req.addEventListener('load', historyComplete); 89 req.addEventListener('load', historyComplete);
80 req.addEventListener('error', xhrError); 90 req.addEventListener('error', xhrError);
81 req.overrideMimeType('application/json'); 91 req.overrideMimeType('application/json');
82 req.open('GET', this.getAttribute('data-try'), true); 92 req.open('GET', this.getAttribute('data-try'), true);
83 req.send(); 93 req.send();
84 } 94 }
85 95
86 96
87 /** 97 /**
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 // "message": "you had an error...", 135 // "message": "you had an error...",
126 // "img": "<base64 encoded image but only on success>" 136 // "img": "<base64 encoded image but only on success>"
127 // } 137 // }
128 // 138 //
129 // The img is optional and only appears if there is a valid 139 // The img is optional and only appears if there is a valid
130 // image to display. 140 // image to display.
131 endWait(); 141 endWait();
132 console.log(e.target.response); 142 console.log(e.target.response);
133 body = JSON.parse(e.target.response); 143 body = JSON.parse(e.target.response);
134 output.innerText = body.message; 144 output.innerText = body.message;
145 if (stdout) {
146 stdout.innerText = body.stdout;
147 }
135 if (body.hasOwnProperty('img')) { 148 if (body.hasOwnProperty('img')) {
136 img.src = 'data:image/png;base64,' + body.img; 149 img.src = 'data:image/png;base64,' + body.img;
137 } else { 150 } else {
138 img.src = ''; 151 img.src = '';
139 } 152 }
140 // Add the image to the history if we are on a workspace page. 153 // Add the image to the history if we are on a workspace page.
141 if (tryHistory) { 154 if (tryHistory) {
142 addToHistory(body.hash, 'data:image/png;base64,' + body.img); 155 addToHistory(body.hash, 'data:image/png;base64,' + body.img);
143 } else { 156 } else {
144 window.history.pushState(null, null, './' + body.hash); 157 window.history.pushState(null, null, './' + body.hash);
145 } 158 }
146 if (permalink) { 159 if (permalink) {
147 permalink.href = '/c/' + body.hash; 160 permalink.href = '/c/' + body.hash;
148 } 161 }
149 if (embed) { 162 if (embed) {
150 var url = document.URL; 163 var url = document.URL;
151 url = url.replace('/c/', '/iframe/'); 164 url = url.replace('/c/', '/iframe/');
152 embed.value = '<iframe src="' + url + '" width="740" height="550" style= "border: solid #00a 5px; border-radius: 5px;"/>' 165 embed.value = '<iframe src="' + url + '" width="740" height="550" style= "border: solid #00a 5px; border-radius: 5px;"/>'
153 } 166 }
154 if (embedButton && embedButton.hasAttribute('disabled')) { 167 if (embedButton && embedButton.hasAttribute('disabled')) {
155 embedButton.removeAttribute('disabled'); 168 embedButton.removeAttribute('disabled');
156 } 169 }
157 } 170 }
158 171
159 172
160 function onSubmitCode() { 173 function onSubmitCode() {
161 beginWait(); 174 beginWait();
175 clearOutput();
162 var req = new XMLHttpRequest(); 176 var req = new XMLHttpRequest();
163 req.addEventListener('load', codeComplete); 177 req.addEventListener('load', codeComplete);
164 req.addEventListener('error', xhrError); 178 req.addEventListener('error', xhrError);
165 req.overrideMimeType('application/json'); 179 req.overrideMimeType('application/json');
166 req.open('POST', '/', true); 180 req.open('POST', '/', true);
167 req.setRequestHeader('content-type', 'application/json'); 181 req.setRequestHeader('content-type', 'application/json');
168 req.send(JSON.stringify({'code': code.value, 'name': workspaceName})); 182 req.send(JSON.stringify({'code': code.value, 'name': workspaceName}));
169 } 183 }
170 run.addEventListener('click', onSubmitCode); 184 run.addEventListener('click', onSubmitCode);
171 185
172 186
173 function onEmbedClick() { 187 function onEmbedClick() {
174 embed.style.display='inline'; 188 embed.style.display='inline';
175 } 189 }
176 190
177 if (embedButton) { 191 if (embedButton) {
178 embedButton.addEventListener('click', onEmbedClick); 192 embedButton.addEventListener('click', onEmbedClick);
179 } 193 }
180 194
181 195
182 // Add the images to the history if we are on a workspace page. 196 // Add the images to the history if we are on a workspace page.
183 if (tryHistory && history) { 197 if (tryHistory && history) {
184 for (var i=0; i<history.length; i++) { 198 for (var i=0; i<history.length; i++) {
185 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png'); 199 addToHistory(history[i].hash, '/i/'+history[i].hash+'.png');
186 } 200 }
187 } 201 }
188 202
189 })(workspaceName); 203 })(workspaceName);
OLDNEW
« no previous file with comments | « experimental/webtry/css/webtry.css ('k') | experimental/webtry/templates/content.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698