aboutsummaryrefslogtreecommitdiff
path: root/static/play/play.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/play/play.js')
-rw-r--r--static/play/play.js175
1 files changed, 83 insertions, 92 deletions
diff --git a/static/play/play.js b/static/play/play.js
index b6bd939..3b8c340 100644
--- a/static/play/play.js
+++ b/static/play/play.js
@@ -2,107 +2,98 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-(function() {
- "use strict";
+function initPlayground(transport) {
+ "use strict";
- var runFunc;
- var count = 0;
+ function text(node) {
+ var s = "";
+ for (var i = 0; i < node.childNodes.length; i++) {
+ var n = node.childNodes[i];
+ if (n.nodeType === 1 && n.tagName === "SPAN" && n.className != "number") {
+ var innerText = n.innerText === undefined ? "textContent" : "innerText";
+ s += n[innerText] + "\n";
+ continue;
+ }
+ if (n.nodeType === 1 && n.tagName !== "BUTTON") {
+ s += text(n);
+ }
+ }
+ return s;
+ }
- function getId() {
- return "code" + (count++);
- }
+ function init(code) {
+ var output = document.createElement('div');
+ var outpre = document.createElement('pre');
+ var running;
- function text(node) {
- var s = "";
- for (var i = 0; i < node.childNodes.length; i++) {
- var n = node.childNodes[i];
- if (n.nodeType === 1 && n.tagName === "SPAN" && n.className != "number") {
- var innerText = n.innerText === undefined ? "textContent" : "innerText";
- s += n[innerText] + "\n";
- continue;
- }
- if (n.nodeType === 1 && n.tagName !== "BUTTON") {
- s += text(n);
- }
- }
- return s;
- }
+ if ($ && $(output).resizable) {
+ $(output).resizable({
+ handles: "n,w,nw",
+ minHeight: 27,
+ minWidth: 135,
+ maxHeight: 608,
+ maxWidth: 990
+ });
+ }
- function init(code) {
- var id = getId();
+ function onKill() {
+ if (running) running.Kill();
+ }
- var output = document.createElement('div');
- var outpre = document.createElement('pre');
- var stopFunc;
+ function onRun(e) {
+ onKill();
+ output.style.display = "block";
+ outpre.innerHTML = "";
+ run1.style.display = "none";
+ var options = {Race: e.shiftKey};
+ running = transport.Run(text(code), PlaygroundOutput(outpre), options);
+ }
- function onKill() {
- if (stopFunc) {
- stopFunc();
- }
- }
+ function onClose() {
+ onKill();
+ output.style.display = "none";
+ run1.style.display = "inline-block";
+ }
- function onRun(e) {
- onKill();
- outpre.innerHTML = "";
- output.style.display = "block";
- run.style.display = "none";
- var options = {Race: e.shiftKey};
- stopFunc = runFunc(text(code), outpre, options);
- }
+ var run1 = document.createElement('button');
+ run1.innerHTML = 'Run';
+ run1.className = 'run';
+ run1.addEventListener("click", onRun, false);
+ var run2 = document.createElement('button');
+ run2.className = 'run';
+ run2.innerHTML = 'Run';
+ run2.addEventListener("click", onRun, false);
+ var kill = document.createElement('button');
+ kill.className = 'kill';
+ kill.innerHTML = 'Kill';
+ kill.addEventListener("click", onKill, false);
+ var close = document.createElement('button');
+ close.className = 'close';
+ close.innerHTML = 'Close';
+ close.addEventListener("click", onClose, false);
- function onClose() {
- onKill();
- output.style.display = "none";
- run.style.display = "inline-block";
- }
+ var button = document.createElement('div');
+ button.classList.add('buttons');
+ button.appendChild(run1);
+ // Hack to simulate insertAfter
+ code.parentNode.insertBefore(button, code.nextSibling);
- var run = document.createElement('button');
- run.innerHTML = 'Run';
- run.className = 'run';
- run.addEventListener("click", onRun, false);
- var run2 = document.createElement('button');
- run2.className = 'run';
- run2.innerHTML = 'Run';
- run2.addEventListener("click", onRun, false);
- var kill = document.createElement('button');
- kill.className = 'kill';
- kill.innerHTML = 'Kill';
- kill.addEventListener("click", onKill, false);
- var close = document.createElement('button');
- close.className = 'close';
- close.innerHTML = 'Close';
- close.addEventListener("click", onClose, false);
+ var buttons = document.createElement('div');
+ buttons.classList.add('buttons');
+ buttons.appendChild(run2);
+ buttons.appendChild(kill);
+ buttons.appendChild(close);
- var button = document.createElement('div');
- button.classList.add('buttons');
- button.appendChild(run);
- // Hack to simulate insertAfter
- code.parentNode.insertBefore(button, code.nextSibling);
+ output.classList.add('output');
+ output.appendChild(buttons);
+ output.appendChild(outpre);
+ output.style.display = "none";
+ code.parentNode.insertBefore(output, button.nextSibling);
+ }
- var buttons = document.createElement('div');
- buttons.classList.add('buttons');
- buttons.appendChild(run2);
- buttons.appendChild(kill);
- buttons.appendChild(close);
+ var play = document.querySelectorAll('div.playground');
+ for (var i = 0; i < play.length; i++) {
+ init(play[i]);
+ }
+}
- output.classList.add('output');
- output.appendChild(buttons);
- output.appendChild(outpre);
- output.style.display = "none";
- code.parentNode.insertBefore(output, button.nextSibling);
- }
-
- var play = document.querySelectorAll('div.playground');
- for (var i = 0; i < play.length; i++) {
- init(play[i]);
- }
- if (play.length > 0) {
- if (window.connectPlayground) {
- runFunc = window.connectPlayground("ws://" + window.location.host + "/socket");
- } else {
- // If this message is logged,
- // we have neglected to include socket.js or playground.js.
- console.log("No playground transport available.");
- }
- }
-})();