2013-07-31 23:08:46 -06:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
|
|
|
In the absence of any formal way to specify interfaces in JavaScript,
|
|
|
|
here's a skeleton implementation of a playground transport.
|
|
|
|
|
|
|
|
function Transport() {
|
2015-07-24 03:36:53 -06:00
|
|
|
// Set up any transport state (eg, make a websocket connection).
|
2013-07-31 23:08:46 -06:00
|
|
|
return {
|
|
|
|
Run: function(body, output, options) {
|
|
|
|
// Compile and run the program 'body' with 'options'.
|
|
|
|
// Call the 'output' callback to display program output.
|
|
|
|
return {
|
|
|
|
Kill: function() {
|
|
|
|
// Kill the running program.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// The output callback is called multiple times, and each time it is
|
|
|
|
// passed an object of this form.
|
|
|
|
var write = {
|
|
|
|
Kind: 'string', // 'start', 'stdout', 'stderr', 'end'
|
|
|
|
Body: 'string' // content of write or end status message
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first call must be of Kind 'start' with no body.
|
|
|
|
// Subsequent calls may be of Kind 'stdout' or 'stderr'
|
|
|
|
// and must have a non-null Body string.
|
|
|
|
// The final call should be of Kind 'end' with an optional
|
|
|
|
// Body string, signifying a failure ("killed", for example).
|
|
|
|
|
|
|
|
// The output callback must be of this form.
|
|
|
|
// See PlaygroundOutput (below) for an implementation.
|
|
|
|
function outputCallback(write) {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-04-16 10:40:48 -06:00
|
|
|
// HTTPTransport is the default transport.
|
|
|
|
// enableVet enables running vet if a program was compiled and ran successfully.
|
|
|
|
// If vet returned any errors, display them before the output of a program.
|
|
|
|
function HTTPTransport(enableVet) {
|
2019-06-26 02:45:20 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function playback(output, data) {
|
|
|
|
// Backwards compatibility: default values do not affect the output.
|
|
|
|
var events = data.Events || [];
|
|
|
|
var errors = data.Errors || '';
|
|
|
|
var status = data.Status || 0;
|
|
|
|
var isTest = data.IsTest || false;
|
|
|
|
var testsFailed = data.TestsFailed || 0;
|
|
|
|
|
|
|
|
var timeout;
|
|
|
|
output({ Kind: 'start' });
|
|
|
|
function next() {
|
|
|
|
if (!events || events.length === 0) {
|
|
|
|
if (isTest) {
|
|
|
|
if (testsFailed > 0) {
|
|
|
|
output({
|
|
|
|
Kind: 'system',
|
|
|
|
Body:
|
|
|
|
'\n' +
|
|
|
|
testsFailed +
|
|
|
|
' test' +
|
|
|
|
(testsFailed > 1 ? 's' : '') +
|
|
|
|
' failed.',
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
output({ Kind: 'system', Body: '\nAll tests passed.' });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (status > 0) {
|
|
|
|
output({ Kind: 'end', Body: 'status ' + status + '.' });
|
|
|
|
} else {
|
|
|
|
if (errors !== '') {
|
|
|
|
// errors are displayed only in the case of timeout.
|
|
|
|
output({ Kind: 'end', Body: errors + '.' });
|
|
|
|
} else {
|
|
|
|
output({ Kind: 'end' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var e = events.shift();
|
|
|
|
if (e.Delay === 0) {
|
|
|
|
output({ Kind: e.Kind, Body: e.Message });
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timeout = setTimeout(function() {
|
|
|
|
output({ Kind: e.Kind, Body: e.Message });
|
|
|
|
next();
|
|
|
|
}, e.Delay / 1000000);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
return {
|
|
|
|
Stop: function() {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function error(output, msg) {
|
|
|
|
output({ Kind: 'start' });
|
|
|
|
output({ Kind: 'stderr', Body: msg });
|
|
|
|
output({ Kind: 'end' });
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildFailed(output, msg) {
|
|
|
|
output({ Kind: 'start' });
|
|
|
|
output({ Kind: 'stderr', Body: msg });
|
|
|
|
output({ Kind: 'system', Body: '\nGo build failed.' });
|
|
|
|
}
|
|
|
|
|
|
|
|
var seq = 0;
|
|
|
|
return {
|
|
|
|
Run: function(body, output, options) {
|
|
|
|
seq++;
|
|
|
|
var cur = seq;
|
|
|
|
var playing;
|
|
|
|
$.ajax('/compile', {
|
|
|
|
type: 'POST',
|
|
|
|
data: { version: 2, body: body, withVet: enableVet },
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
if (seq != cur) return;
|
|
|
|
if (!data) return;
|
|
|
|
if (playing != null) playing.Stop();
|
|
|
|
if (data.Errors) {
|
|
|
|
if (data.Errors === 'process took too long') {
|
|
|
|
// Playback the output that was captured before the timeout.
|
|
|
|
playing = playback(output, data);
|
|
|
|
} else {
|
|
|
|
buildFailed(output, data.Errors);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!data.Events) {
|
|
|
|
data.Events = [];
|
|
|
|
}
|
|
|
|
if (data.VetErrors) {
|
|
|
|
// Inject errors from the vet as the first events in the output.
|
|
|
|
data.Events.unshift({
|
|
|
|
Message: 'Go vet exited.\n\n',
|
|
|
|
Kind: 'system',
|
|
|
|
Delay: 0,
|
|
|
|
});
|
|
|
|
data.Events.unshift({
|
|
|
|
Message: data.VetErrors,
|
|
|
|
Kind: 'stderr',
|
|
|
|
Delay: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enableVet || data.VetOK || data.VetErrors) {
|
|
|
|
playing = playback(output, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the server support doesn't support
|
|
|
|
// compile+vet in same request signaled by the
|
|
|
|
// 'withVet' parameter above, also try the old way.
|
|
|
|
// TODO: remove this when it falls out of use.
|
|
|
|
// It is 2019-05-13 now.
|
|
|
|
$.ajax('/vet', {
|
|
|
|
data: { body: body },
|
|
|
|
type: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(dataVet) {
|
|
|
|
if (dataVet.Errors) {
|
|
|
|
// inject errors from the vet as the first events in the output
|
|
|
|
data.Events.unshift({
|
|
|
|
Message: 'Go vet exited.\n\n',
|
|
|
|
Kind: 'system',
|
|
|
|
Delay: 0,
|
|
|
|
});
|
|
|
|
data.Events.unshift({
|
|
|
|
Message: dataVet.Errors,
|
|
|
|
Kind: 'stderr',
|
|
|
|
Delay: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
playing = playback(output, data);
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
playing = playback(output, data);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
error(output, 'Error communicating with remote server.');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
Kill: function() {
|
|
|
|
if (playing != null) playing.Stop();
|
|
|
|
output({ Kind: 'end', Body: 'killed' });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function SocketTransport() {
|
2019-06-26 02:45:20 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var id = 0;
|
|
|
|
var outputs = {};
|
|
|
|
var started = {};
|
|
|
|
var websocket;
|
|
|
|
if (window.location.protocol == 'http:') {
|
|
|
|
websocket = new WebSocket('ws://' + window.location.host + '/socket');
|
|
|
|
} else if (window.location.protocol == 'https:') {
|
|
|
|
websocket = new WebSocket('wss://' + window.location.host + '/socket');
|
|
|
|
}
|
|
|
|
|
|
|
|
websocket.onclose = function() {
|
|
|
|
console.log('websocket connection closed');
|
|
|
|
};
|
|
|
|
|
|
|
|
websocket.onmessage = function(e) {
|
|
|
|
var m = JSON.parse(e.data);
|
|
|
|
var output = outputs[m.Id];
|
|
|
|
if (output === null) return;
|
|
|
|
if (!started[m.Id]) {
|
|
|
|
output({ Kind: 'start' });
|
|
|
|
started[m.Id] = true;
|
|
|
|
}
|
|
|
|
output({ Kind: m.Kind, Body: m.Body });
|
|
|
|
};
|
|
|
|
|
|
|
|
function send(m) {
|
|
|
|
websocket.send(JSON.stringify(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
Run: function(body, output, options) {
|
|
|
|
var thisID = id + '';
|
|
|
|
id++;
|
|
|
|
outputs[thisID] = output;
|
|
|
|
send({ Id: thisID, Kind: 'run', Body: body, Options: options });
|
|
|
|
return {
|
|
|
|
Kill: function() {
|
|
|
|
send({ Id: thisID, Kind: 'kill' });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function PlaygroundOutput(el) {
|
2019-06-26 02:45:20 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
return function(write) {
|
|
|
|
if (write.Kind == 'start') {
|
|
|
|
el.innerHTML = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cl = 'system';
|
|
|
|
if (write.Kind == 'stdout' || write.Kind == 'stderr') cl = write.Kind;
|
|
|
|
|
|
|
|
var m = write.Body;
|
|
|
|
if (write.Kind == 'end') {
|
|
|
|
m = '\nProgram exited' + (m ? ': ' + m : '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.indexOf('IMAGE:') === 0) {
|
|
|
|
// TODO(adg): buffer all writes before creating image
|
|
|
|
var url = 'data:image/png;base64,' + m.substr(6);
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.src = url;
|
|
|
|
el.appendChild(img);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ^L clears the screen.
|
|
|
|
var s = m.split('\x0c');
|
|
|
|
if (s.length > 1) {
|
|
|
|
el.innerHTML = '';
|
|
|
|
m = s.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
m = m.replace(/&/g, '&');
|
|
|
|
m = m.replace(/</g, '<');
|
|
|
|
m = m.replace(/>/g, '>');
|
|
|
|
|
|
|
|
var needScroll = el.scrollTop + el.offsetHeight == el.scrollHeight;
|
|
|
|
|
|
|
|
var span = document.createElement('span');
|
|
|
|
span.className = cl;
|
|
|
|
span.innerHTML = m;
|
|
|
|
el.appendChild(span);
|
|
|
|
|
|
|
|
if (needScroll) el.scrollTop = el.scrollHeight - el.offsetHeight;
|
|
|
|
};
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
function lineHighlight(error) {
|
|
|
|
var regex = /prog.go:([0-9]+)/g;
|
|
|
|
var r = regex.exec(error);
|
|
|
|
while (r) {
|
2019-06-26 02:45:20 -06:00
|
|
|
$('.lines div')
|
|
|
|
.eq(r[1] - 1)
|
|
|
|
.addClass('lineerror');
|
2013-07-31 23:08:46 -06:00
|
|
|
r = regex.exec(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function highlightOutput(wrappedOutput) {
|
|
|
|
return function(write) {
|
|
|
|
if (write.Body) lineHighlight(write.Body);
|
|
|
|
wrappedOutput(write);
|
2018-04-16 10:40:48 -06:00
|
|
|
};
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
function lineClear() {
|
2019-06-26 02:45:20 -06:00
|
|
|
$('.lineerror').removeClass('lineerror');
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// opts is an object with these keys
|
|
|
|
// codeEl - code editor element
|
|
|
|
// outputEl - program output element
|
|
|
|
// runEl - run button element
|
|
|
|
// fmtEl - fmt button element (optional)
|
2013-12-17 15:32:51 -07:00
|
|
|
// fmtImportEl - fmt "imports" checkbox element (optional)
|
2013-07-31 23:08:46 -06:00
|
|
|
// shareEl - share button element (optional)
|
|
|
|
// shareURLEl - share URL text input element (optional)
|
|
|
|
// shareRedirect - base URL to redirect to on share (optional)
|
|
|
|
// toysEl - toys select element (optional)
|
|
|
|
// enableHistory - enable using HTML5 history API (optional)
|
|
|
|
// transport - playground transport to use (default is HTTPTransport)
|
2017-02-07 11:29:28 -07:00
|
|
|
// enableShortcuts - whether to enable shortcuts (Ctrl+S/Cmd+S to save) (default is false)
|
2018-04-16 10:40:48 -06:00
|
|
|
// enableVet - enable running vet and displaying its errors
|
2013-07-31 23:08:46 -06:00
|
|
|
function playground(opts) {
|
|
|
|
var code = $(opts.codeEl);
|
2018-04-16 10:40:48 -06:00
|
|
|
var transport = opts['transport'] || new HTTPTransport(opts['enableVet']);
|
2013-07-31 23:08:46 -06:00
|
|
|
var running;
|
2017-02-07 11:29:28 -07:00
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
// autoindent helpers.
|
|
|
|
function insertTabs(n) {
|
|
|
|
// find the selection start and end
|
|
|
|
var start = code[0].selectionStart;
|
2019-06-26 02:45:20 -06:00
|
|
|
var end = code[0].selectionEnd;
|
2013-07-31 23:08:46 -06:00
|
|
|
// split the textarea content into two, and insert n tabs
|
|
|
|
var v = code[0].value;
|
|
|
|
var u = v.substr(0, start);
|
2019-06-26 02:45:20 -06:00
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
u += '\t';
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
u += v.substr(end);
|
|
|
|
// set revised content
|
|
|
|
code[0].value = u;
|
|
|
|
// reset caret position after inserted tabs
|
2019-06-26 02:45:20 -06:00
|
|
|
code[0].selectionStart = start + n;
|
|
|
|
code[0].selectionEnd = start + n;
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
function autoindent(el) {
|
|
|
|
var curpos = el.selectionStart;
|
|
|
|
var tabs = 0;
|
|
|
|
while (curpos > 0) {
|
|
|
|
curpos--;
|
2019-06-26 02:45:20 -06:00
|
|
|
if (el.value[curpos] == '\t') {
|
2013-07-31 23:08:46 -06:00
|
|
|
tabs++;
|
2019-06-26 02:45:20 -06:00
|
|
|
} else if (tabs > 0 || el.value[curpos] == '\n') {
|
2013-07-31 23:08:46 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(function() {
|
|
|
|
insertTabs(tabs);
|
|
|
|
}, 1);
|
|
|
|
}
|
2017-02-07 11:29:28 -07:00
|
|
|
|
|
|
|
// NOTE(cbro): e is a jQuery event, not a DOM event.
|
|
|
|
function handleSaveShortcut(e) {
|
|
|
|
if (e.isDefaultPrevented()) return false;
|
|
|
|
if (!e.metaKey && !e.ctrlKey) return false;
|
2019-06-26 02:45:20 -06:00
|
|
|
if (e.key != 'S' && e.key != 's') return false;
|
2017-02-07 11:29:28 -07:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Share and save
|
|
|
|
share(function(url) {
|
2019-06-26 02:45:20 -06:00
|
|
|
window.location.href = url + '.go?download=true';
|
2017-02-07 11:29:28 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
function keyHandler(e) {
|
2017-02-07 11:29:28 -07:00
|
|
|
if (opts.enableShortcuts && handleSaveShortcut(e)) return;
|
|
|
|
|
2019-06-26 02:45:20 -06:00
|
|
|
if (e.keyCode == 9 && !e.ctrlKey) {
|
|
|
|
// tab (but not ctrl-tab)
|
2013-07-31 23:08:46 -06:00
|
|
|
insertTabs(1);
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-26 02:45:20 -06:00
|
|
|
if (e.keyCode == 13) {
|
|
|
|
// enter
|
|
|
|
if (e.shiftKey) {
|
|
|
|
// +shift
|
2013-07-31 23:08:46 -06:00
|
|
|
run();
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
2019-06-26 02:45:20 -06:00
|
|
|
}
|
|
|
|
if (e.ctrlKey) {
|
|
|
|
// +control
|
2014-08-04 16:55:39 -06:00
|
|
|
fmt();
|
|
|
|
e.preventDefault();
|
2013-07-31 23:08:46 -06:00
|
|
|
} else {
|
|
|
|
autoindent(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
code.unbind('keydown').bind('keydown', keyHandler);
|
|
|
|
var outdiv = $(opts.outputEl).empty();
|
|
|
|
var output = $('<pre/>').appendTo(outdiv);
|
2017-02-07 11:29:28 -07:00
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
function body() {
|
|
|
|
return $(opts.codeEl).val();
|
|
|
|
}
|
|
|
|
function setBody(text) {
|
|
|
|
$(opts.codeEl).val(text);
|
|
|
|
}
|
|
|
|
function origin(href) {
|
2019-06-26 02:45:20 -06:00
|
|
|
return ('' + href)
|
|
|
|
.split('/')
|
|
|
|
.slice(0, 3)
|
|
|
|
.join('/');
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
2017-02-07 11:29:28 -07:00
|
|
|
|
2019-06-26 02:45:20 -06:00
|
|
|
var pushedEmpty = window.location.pathname == '/';
|
2013-07-31 23:08:46 -06:00
|
|
|
function inputChanged() {
|
|
|
|
if (pushedEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pushedEmpty = true;
|
|
|
|
$(opts.shareURLEl).hide();
|
2019-06-26 02:45:20 -06:00
|
|
|
window.history.pushState(null, '', '/');
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
function popState(e) {
|
|
|
|
if (e === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e && e.state && e.state.code) {
|
|
|
|
setBody(e.state.code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var rewriteHistory = false;
|
2019-06-26 02:45:20 -06:00
|
|
|
if (
|
|
|
|
window.history &&
|
|
|
|
window.history.pushState &&
|
|
|
|
window.addEventListener &&
|
|
|
|
opts.enableHistory
|
|
|
|
) {
|
2013-07-31 23:08:46 -06:00
|
|
|
rewriteHistory = true;
|
|
|
|
code[0].addEventListener('input', inputChanged);
|
|
|
|
window.addEventListener('popstate', popState);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setError(error) {
|
|
|
|
if (running) running.Kill();
|
|
|
|
lineClear();
|
|
|
|
lineHighlight(error);
|
2019-06-26 02:45:20 -06:00
|
|
|
output
|
|
|
|
.empty()
|
|
|
|
.addClass('error')
|
|
|
|
.text(error);
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
function loading() {
|
|
|
|
lineClear();
|
|
|
|
if (running) running.Kill();
|
2019-06-26 02:45:20 -06:00
|
|
|
output.removeClass('error').text('Waiting for remote server...');
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
|
|
|
function run() {
|
|
|
|
loading();
|
2019-06-26 02:45:20 -06:00
|
|
|
running = transport.Run(
|
|
|
|
body(),
|
|
|
|
highlightOutput(PlaygroundOutput(output[0]))
|
|
|
|
);
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
2013-12-17 15:32:51 -07:00
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
function fmt() {
|
|
|
|
loading();
|
2019-06-26 02:45:20 -06:00
|
|
|
var data = { body: body() };
|
|
|
|
if ($(opts.fmtImportEl).is(':checked')) {
|
|
|
|
data['imports'] = 'true';
|
2013-12-17 15:32:51 -07:00
|
|
|
}
|
2019-06-26 02:45:20 -06:00
|
|
|
$.ajax('/fmt', {
|
2013-12-17 15:32:51 -07:00
|
|
|
data: data,
|
2019-06-26 02:45:20 -06:00
|
|
|
type: 'POST',
|
|
|
|
dataType: 'json',
|
2013-07-31 23:08:46 -06:00
|
|
|
success: function(data) {
|
|
|
|
if (data.Error) {
|
|
|
|
setError(data.Error);
|
|
|
|
} else {
|
|
|
|
setBody(data.Body);
|
2019-06-26 02:45:20 -06:00
|
|
|
setError('');
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
2019-06-26 02:45:20 -06:00
|
|
|
},
|
2013-07-31 23:08:46 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:29:28 -07:00
|
|
|
var shareURL; // jQuery element to show the shared URL.
|
|
|
|
var sharing = false; // true if there is a pending request.
|
|
|
|
var shareCallbacks = [];
|
|
|
|
function share(opt_callback) {
|
|
|
|
if (opt_callback) shareCallbacks.push(opt_callback);
|
|
|
|
|
|
|
|
if (sharing) return;
|
|
|
|
sharing = true;
|
|
|
|
|
|
|
|
var sharingData = body();
|
2019-06-26 02:45:20 -06:00
|
|
|
$.ajax('/share', {
|
2017-02-07 11:29:28 -07:00
|
|
|
processData: false,
|
|
|
|
data: sharingData,
|
2019-06-26 02:45:20 -06:00
|
|
|
type: 'POST',
|
|
|
|
contentType: 'text/plain; charset=utf-8',
|
2017-02-07 11:29:28 -07:00
|
|
|
complete: function(xhr) {
|
|
|
|
sharing = false;
|
|
|
|
if (xhr.status != 200) {
|
2019-06-26 02:45:20 -06:00
|
|
|
alert('Server error; try again.');
|
2017-02-07 11:29:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.shareRedirect) {
|
|
|
|
window.location = opts.shareRedirect + xhr.responseText;
|
|
|
|
}
|
2019-06-26 02:45:20 -06:00
|
|
|
var path = '/p/' + xhr.responseText;
|
2017-02-07 11:29:28 -07:00
|
|
|
var url = origin(window.location) + path;
|
|
|
|
|
|
|
|
for (var i = 0; i < shareCallbacks.length; i++) {
|
|
|
|
shareCallbacks[i](url);
|
|
|
|
}
|
|
|
|
shareCallbacks = [];
|
|
|
|
|
|
|
|
if (shareURL) {
|
2019-06-26 02:45:20 -06:00
|
|
|
shareURL
|
|
|
|
.show()
|
|
|
|
.val(url)
|
|
|
|
.focus()
|
|
|
|
.select();
|
2017-02-07 11:29:28 -07:00
|
|
|
|
|
|
|
if (rewriteHistory) {
|
2019-06-26 02:45:20 -06:00
|
|
|
var historyData = { code: sharingData };
|
|
|
|
window.history.pushState(historyData, '', path);
|
2017-02-07 11:29:28 -07:00
|
|
|
pushedEmpty = false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 02:45:20 -06:00
|
|
|
},
|
2017-02-07 11:29:28 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
$(opts.runEl).click(run);
|
|
|
|
$(opts.fmtEl).click(fmt);
|
2013-12-17 15:32:51 -07:00
|
|
|
|
2019-06-26 02:45:20 -06:00
|
|
|
if (
|
|
|
|
opts.shareEl !== null &&
|
|
|
|
(opts.shareURLEl !== null || opts.shareRedirect !== null)
|
|
|
|
) {
|
2013-07-31 23:08:46 -06:00
|
|
|
if (opts.shareURLEl) {
|
|
|
|
shareURL = $(opts.shareURLEl).hide();
|
|
|
|
}
|
2017-02-07 11:29:28 -07:00
|
|
|
$(opts.shareEl).click(function() {
|
|
|
|
share();
|
|
|
|
});
|
2013-07-31 23:08:46 -06:00
|
|
|
}
|
2017-02-07 11:29:28 -07:00
|
|
|
|
2013-07-31 23:08:46 -06:00
|
|
|
if (opts.toysEl !== null) {
|
|
|
|
$(opts.toysEl).bind('change', function() {
|
|
|
|
var toy = $(this).val();
|
2019-06-26 02:45:20 -06:00
|
|
|
$.ajax('/doc/play/' + toy, {
|
2013-07-31 23:08:46 -06:00
|
|
|
processData: false,
|
2019-06-26 02:45:20 -06:00
|
|
|
type: 'GET',
|
2013-07-31 23:08:46 -06:00
|
|
|
complete: function(xhr) {
|
|
|
|
if (xhr.status != 200) {
|
2019-06-26 02:45:20 -06:00
|
|
|
alert('Server error; try again.');
|
2013-07-31 23:08:46 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setBody(xhr.responseText);
|
2019-06-26 02:45:20 -06:00
|
|
|
},
|
2013-07-31 23:08:46 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.playground = playground;
|
|
|
|
})();
|