2013-09-18 09:12:51 +04: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.
|
|
|
|
|
|
|
|
function initPlayground(transport) {
|
2019-06-26 11:45:20 +03:00
|
|
|
'use strict';
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
function text(node) {
|
|
|
|
var s = '';
|
|
|
|
for (var i = 0; i < node.childNodes.length; i++) {
|
|
|
|
var n = node.childNodes[i];
|
|
|
|
if (n.nodeType === 1) {
|
|
|
|
if (n.tagName === 'BUTTON') continue;
|
|
|
|
if (n.tagName === 'SPAN' && n.className === 'number') continue;
|
2020-08-31 00:00:24 +03:00
|
|
|
if (n.tagName === 'DIV' || n.tagName === 'BR' || n.tagName === 'PRE') {
|
2019-06-26 11:45:20 +03:00
|
|
|
s += '\n';
|
|
|
|
}
|
|
|
|
s += text(n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (n.nodeType === 3) {
|
|
|
|
s += n.nodeValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.replace('\xA0', ' '); // replace non-breaking spaces
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
// When presenter notes are enabled, the index passed
|
|
|
|
// here will identify the playground to be synced
|
|
|
|
function init(code, index) {
|
|
|
|
var output = document.createElement('div');
|
|
|
|
var outpre = document.createElement('pre');
|
|
|
|
var running;
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
if ($ && $(output).resizable) {
|
|
|
|
$(output).resizable({
|
|
|
|
handles: 'n,w,nw',
|
|
|
|
minHeight: 27,
|
|
|
|
minWidth: 135,
|
|
|
|
maxHeight: 608,
|
|
|
|
maxWidth: 990,
|
|
|
|
});
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
function onKill() {
|
|
|
|
if (running) running.Kill();
|
|
|
|
if (window.notesEnabled) updatePlayStorage('onKill', index);
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
function onRun(e) {
|
|
|
|
var sk = e.shiftKey || localStorage.getItem('play-shiftKey') === 'true';
|
|
|
|
if (running) running.Kill();
|
|
|
|
output.style.display = 'block';
|
2020-05-28 18:50:37 +03:00
|
|
|
outpre.textContent = '';
|
2019-06-26 11:45:20 +03:00
|
|
|
run1.style.display = 'none';
|
|
|
|
var options = { Race: sk };
|
|
|
|
running = transport.Run(text(code), PlaygroundOutput(outpre), options);
|
|
|
|
if (window.notesEnabled) updatePlayStorage('onRun', index, e);
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
function onClose() {
|
|
|
|
if (running) running.Kill();
|
|
|
|
output.style.display = 'none';
|
|
|
|
run1.style.display = 'inline-block';
|
|
|
|
if (window.notesEnabled) updatePlayStorage('onClose', index);
|
|
|
|
}
|
2016-04-04 02:36:34 +03:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
if (window.notesEnabled) {
|
|
|
|
playgroundHandlers.onRun.push(onRun);
|
|
|
|
playgroundHandlers.onClose.push(onClose);
|
|
|
|
playgroundHandlers.onKill.push(onKill);
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
var run1 = document.createElement('button');
|
2020-05-28 18:50:37 +03:00
|
|
|
run1.textContent = 'Run';
|
2019-06-26 11:45:20 +03:00
|
|
|
run1.className = 'run';
|
|
|
|
run1.addEventListener('click', onRun, false);
|
|
|
|
var run2 = document.createElement('button');
|
|
|
|
run2.className = 'run';
|
2020-05-28 18:50:37 +03:00
|
|
|
run2.textContent = 'Run';
|
2019-06-26 11:45:20 +03:00
|
|
|
run2.addEventListener('click', onRun, false);
|
|
|
|
var kill = document.createElement('button');
|
|
|
|
kill.className = 'kill';
|
2020-05-28 18:50:37 +03:00
|
|
|
kill.textContent = 'Kill';
|
2019-06-26 11:45:20 +03:00
|
|
|
kill.addEventListener('click', onKill, false);
|
|
|
|
var close = document.createElement('button');
|
|
|
|
close.className = 'close';
|
2020-05-28 18:50:37 +03:00
|
|
|
close.textContent = 'Close';
|
2019-06-26 11:45:20 +03:00
|
|
|
close.addEventListener('click', onClose, false);
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
var button = document.createElement('div');
|
|
|
|
button.classList.add('buttons');
|
|
|
|
button.appendChild(run1);
|
|
|
|
// Hack to simulate insertAfter
|
|
|
|
code.parentNode.insertBefore(button, code.nextSibling);
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
var buttons = document.createElement('div');
|
|
|
|
buttons.classList.add('buttons');
|
|
|
|
buttons.appendChild(run2);
|
|
|
|
buttons.appendChild(kill);
|
|
|
|
buttons.appendChild(close);
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
output.classList.add('output');
|
|
|
|
output.appendChild(buttons);
|
|
|
|
output.appendChild(outpre);
|
|
|
|
output.style.display = 'none';
|
|
|
|
code.parentNode.insertBefore(output, button.nextSibling);
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
|
2019-06-26 11:45:20 +03:00
|
|
|
var play = document.querySelectorAll('div.playground');
|
|
|
|
for (var i = 0; i < play.length; i++) {
|
|
|
|
init(play[i], i);
|
|
|
|
}
|
2013-09-18 09:12:51 +04:00
|
|
|
}
|