summaryrefslogtreecommitdiff
path: root/doc/play/playground.js
diff options
context:
space:
mode:
Diffstat (limited to 'doc/play/playground.js')
-rw-r--r--doc/play/playground.js102
1 files changed, 65 insertions, 37 deletions
diff --git a/doc/play/playground.js b/doc/play/playground.js
index e060e203c..947f8a4ec 100644
--- a/doc/play/playground.js
+++ b/doc/play/playground.js
@@ -12,6 +12,7 @@
// preCompile - callback to mutate request data before compiling
// postCompile - callback to read response data after compiling
// simple - use plain textarea instead of CodeMirror.
+// toysEl - select element with a list of toys.
function playground(opts) {
var simple = opts['simple'];
var code = $(opts['codeEl']);
@@ -109,6 +110,16 @@ function playground(opts) {
}
return $(opts['codeEl']).val();
}
+ function setBody(text) {
+ if (editor) {
+ editor.setValue(text);
+ return;
+ }
+ $(opts['codeEl']).val(text);
+ }
+ function origin(href) {
+ return (""+href).split("/").slice(0, 3).join("/");
+ }
var seq = 0;
function run() {
@@ -155,52 +166,69 @@ function playground(opts) {
}
pre.text(out);
},
- error: function() {
- output.addClass("error").text(
- "Error communicating with remote server."
- );
+ error: function(xhr) {
+ var text = "Error communicating with remote server.";
+ console.log(xhr.status);
+ if (xhr.status == 501) {
+ text = xhr.responseText;
+ }
+ output.addClass("error").text(text);
}
});
}
$(opts['runEl']).click(run);
- if (opts['shareEl'] == null || (opts['shareURLEl'] == null && opts['shareRedirect'] == null)) {
- return editor;
- }
-
- function origin(href) {
- return (""+href).split("/").slice(0, 3).join("/");
+ if (opts['shareEl'] != null && (opts['shareURLEl'] != null || opts['shareRedirect'] != null)) {
+ var shareURL;
+ if (opts['shareURLEl']) {
+ shareURL = $(opts['shareURLEl']).hide();
+ }
+ var sharing = false;
+ $(opts['shareEl']).click(function() {
+ if (sharing) return;
+ sharing = true;
+ $.ajax("/share", {
+ processData: false,
+ data: body(),
+ type: "POST",
+ complete: function(xhr) {
+ sharing = false;
+ if (xhr.status == 501) {
+ alert(xhr.responseText);
+ return;
+ }
+ if (xhr.status != 200) {
+ alert("Server error; try again.");
+ return;
+ }
+ if (opts['shareRedirect']) {
+ window.location = opts['shareRedirect'] + xhr.responseText;
+ }
+ if (shareURL) {
+ var url = origin(window.location) + "/p/" + xhr.responseText;
+ shareURL.show().val(url).focus().select();
+ }
+ }
+ });
+ });
}
- var shareURL;
- if (opts['shareURLEl']) {
- shareURL = $(opts['shareURLEl']).hide();
- }
- var sharing = false;
- $(opts['shareEl']).click(function() {
- if (sharing) return;
- sharing = true;
- $.ajax("/share", {
- processData: false,
- data: body(),
- type: "POST",
- complete: function(xhr) {
- sharing = false;
- if (xhr.status != 200) {
- alert("Server error; try again.");
- return;
- }
- if (opts['shareRedirect']) {
- window.location = opts['shareRedirect'] + xhr.responseText;
- }
- if (shareURL) {
- var url = origin(window.location) + "/p/" +
- xhr.responseText;
- shareURL.show().val(url).focus().select();
+ 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;
}