1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 function initPlayground(transport) {
10 for (var i = 0; i < node.childNodes.length; i++) {
11 var n = node.childNodes[i];
12 if (n.nodeType === 1) {
13 if (n.tagName === 'BUTTON') continue;
14 if (n.tagName === 'SPAN' && n.className === 'number') continue;
15 if (n.tagName === 'DIV' || n.tagName === 'BR' || n.tagName === 'PRE') {
21 if (n.nodeType === 3) {
25 return s.replace('\xA0', ' '); // replace non-breaking spaces
28 // When presenter notes are enabled, the index passed
29 // here will identify the playground to be synced
30 function init(code, index) {
31 var output = document.createElement('div');
32 var outpre = document.createElement('pre');
35 if ($ && $(output).resizable) {
46 if (running) running.Kill();
47 if (window.notesEnabled) updatePlayStorage('onKill', index);
51 var sk = e.shiftKey || localStorage.getItem('play-shiftKey') === 'true';
52 if (running) running.Kill();
53 output.style.display = 'block';
54 outpre.textContent = '';
55 run1.style.display = 'none';
56 var options = { Race: sk };
57 running = transport.Run(text(code), PlaygroundOutput(outpre), options);
58 if (window.notesEnabled) updatePlayStorage('onRun', index, e);
62 if (running) running.Kill();
63 output.style.display = 'none';
64 run1.style.display = 'inline-block';
65 if (window.notesEnabled) updatePlayStorage('onClose', index);
68 if (window.notesEnabled) {
69 playgroundHandlers.onRun.push(onRun);
70 playgroundHandlers.onClose.push(onClose);
71 playgroundHandlers.onKill.push(onKill);
74 var run1 = document.createElement('button');
75 run1.textContent = 'Run';
76 run1.className = 'run';
77 run1.addEventListener('click', onRun, false);
78 var run2 = document.createElement('button');
79 run2.className = 'run';
80 run2.textContent = 'Run';
81 run2.addEventListener('click', onRun, false);
82 var kill = document.createElement('button');
83 kill.className = 'kill';
84 kill.textContent = 'Kill';
85 kill.addEventListener('click', onKill, false);
86 var close = document.createElement('button');
87 close.className = 'close';
88 close.textContent = 'Close';
89 close.addEventListener('click', onClose, false);
91 var button = document.createElement('div');
92 button.classList.add('buttons');
93 button.appendChild(run1);
94 // Hack to simulate insertAfter
95 code.parentNode.insertBefore(button, code.nextSibling);
97 var buttons = document.createElement('div');
98 buttons.classList.add('buttons');
99 buttons.appendChild(run2);
100 buttons.appendChild(kill);
101 buttons.appendChild(close);
103 output.classList.add('output');
104 output.appendChild(buttons);
105 output.appendChild(outpre);
106 output.style.display = 'none';
107 code.parentNode.insertBefore(output, button.nextSibling);
110 var play = document.querySelectorAll('div.playground');
111 for (var i = 0; i < play.length; i++) {