github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/godoc/static/play.js (about) 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. 4 5 function initPlayground(transport) { 6 'use strict'; 7 8 function text(node) { 9 var s = ''; 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') { 16 s += '\n'; 17 } 18 s += text(n); 19 continue; 20 } 21 if (n.nodeType === 3) { 22 s += n.nodeValue; 23 } 24 } 25 return s.replace('\xA0', ' '); // replace non-breaking spaces 26 } 27 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'); 33 var running; 34 35 if ($ && $(output).resizable) { 36 $(output).resizable({ 37 handles: 'n,w,nw', 38 minHeight: 27, 39 minWidth: 135, 40 maxHeight: 608, 41 maxWidth: 990, 42 }); 43 } 44 45 function onKill() { 46 if (running) running.Kill(); 47 if (window.notesEnabled) updatePlayStorage('onKill', index); 48 } 49 50 function onRun(e) { 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); 59 } 60 61 function onClose() { 62 if (running) running.Kill(); 63 output.style.display = 'none'; 64 run1.style.display = 'inline-block'; 65 if (window.notesEnabled) updatePlayStorage('onClose', index); 66 } 67 68 if (window.notesEnabled) { 69 playgroundHandlers.onRun.push(onRun); 70 playgroundHandlers.onClose.push(onClose); 71 playgroundHandlers.onKill.push(onKill); 72 } 73 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); 90 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); 96 97 var buttons = document.createElement('div'); 98 buttons.classList.add('buttons'); 99 buttons.appendChild(run2); 100 buttons.appendChild(kill); 101 buttons.appendChild(close); 102 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); 108 } 109 110 var play = document.querySelectorAll('div.playground'); 111 for (var i = 0; i < play.length; i++) { 112 init(play[i], i); 113 } 114 }