1
0
mirror of https://github.com/golang/go synced 2024-11-21 18:54:43 -07:00

doc: sync playground.js

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5956043
This commit is contained in:
Andrew Gerrand 2012-04-03 08:10:21 +10:00
parent a978ead6e1
commit f1a39ff003

View File

@ -6,13 +6,14 @@
// codeEl - code editor element // codeEl - code editor element
// outputEl - program output element // outputEl - program output element
// runEl - run button element // runEl - run button element
// fmtEl - fmt button element (optional)
// shareEl - share button element (optional) // shareEl - share button element (optional)
// shareURLEl - share URL text input element (optional) // shareURLEl - share URL text input element (optional)
// shareRedirect - base URL to redirect to on share (optional) // shareRedirect - base URL to redirect to on share (optional)
// preCompile - callback to mutate request data before compiling // preCompile - callback to mutate request data before compiling (optional)
// postCompile - callback to read response data after compiling // postCompile - callback to read response data after compiling (optional)
// simple - use plain textarea instead of CodeMirror. // simple - use plain textarea instead of CodeMirror. (optional)
// toysEl - select element with a list of toys. // toysEl - select element with a list of toys. (optional)
function playground(opts) { function playground(opts) {
var simple = opts['simple']; var simple = opts['simple'];
var code = $(opts['codeEl']); var code = $(opts['codeEl']);
@ -120,13 +121,23 @@ function playground(opts) {
function origin(href) { function origin(href) {
return (""+href).split("/").slice(0, 3).join("/"); return (""+href).split("/").slice(0, 3).join("/");
} }
function loading() {
output.removeClass("error").html(
'<div class="loading">Waiting for remote server...</div>'
);
}
function setOutput(text, error) {
output.empty();
if (error) {
output.addClass("error");
}
$("<pre/>").text(text).appendTo(output);
}
var seq = 0; var seq = 0;
function run() { function run() {
clearErrors(); clearErrors();
output.removeClass("error").html( loading();
'<div class="loading">Waiting for remote server...</div>'
);
seq++; seq++;
var cur = seq; var cur = seq;
var data = {"body": body()}; var data = {"body": body()};
@ -141,8 +152,6 @@ function playground(opts) {
if (seq != cur) { if (seq != cur) {
return; return;
} }
pre = $("<pre/>");
output.empty().append(pre);
if (opts['postCompile']) { if (opts['postCompile']) {
opts['postCompile'](data); opts['postCompile'](data);
} }
@ -150,8 +159,7 @@ function playground(opts) {
return; return;
} }
if (data.compile_errors != "") { if (data.compile_errors != "") {
pre.text(data.compile_errors); setOutput(data.compile_errors, true);
output.addClass("error");
highlightErrors(data.compile_errors); highlightErrors(data.compile_errors);
return; return;
} }
@ -164,11 +172,10 @@ function playground(opts) {
output.empty().append(img); output.empty().append(img);
return; return;
} }
pre.text(out); setOutput(out, false);
}, },
error: function(xhr) { error: function(xhr) {
var text = "Error communicating with remote server."; var text = "Error communicating with remote server.";
console.log(xhr.status);
if (xhr.status == 501) { if (xhr.status == 501) {
text = xhr.responseText; text = xhr.responseText;
} }
@ -178,6 +185,41 @@ function playground(opts) {
} }
$(opts['runEl']).click(run); $(opts['runEl']).click(run);
$(opts['fmtEl']).click(function() {
loading();
$.ajax("/fmt", {
data: {"body": body()},
type: "POST",
dataType: "json",
success: function(data) {
if (data.Error) {
setOutput(data.Error, true);
highlightErrors(data.Error);
return;
}
setBody(data.Body);
setOutput("", false);
}
});
});
$(opts['toysEl']).bind('change', function() {
var toy = $(this).val();
loading();
$.ajax("/doc/play/"+toy, {
processData: false,
type: "GET",
complete: function(xhr) {
if (xhr.status != 200) {
setOutput("Server error; try again.", true);
return;
}
setBody(xhr.responseText);
setOutput("", false);
}
});
});
if (opts['shareEl'] != null && (opts['shareURLEl'] != null || opts['shareRedirect'] != null)) { if (opts['shareEl'] != null && (opts['shareURLEl'] != null || opts['shareRedirect'] != null)) {
var shareURL; var shareURL;
if (opts['shareURLEl']) { if (opts['shareURLEl']) {
@ -213,22 +255,5 @@ function playground(opts) {
}); });
} }
if (opts['toysEl'] != null) {
$(opts['toysEl']).bind('change', function() {
var toy = $(this).val();
$.ajax("/doc/play/"+toy, {
processData: false,
type: "GET",
complete: function(xhr) {
if (xhr.status != 200) {
alert("Server error; try again.")
return;
}
setBody(xhr.responseText);
}
});
});
}
return editor; return editor;
} }