github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/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") {
    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  	function init(code) {
    29  		var output = document.createElement('div');
    30  		var outpre = document.createElement('pre');
    31  		var running;
    32  
    33  		if ($ && $(output).resizable) {
    34  			$(output).resizable({
    35  				handles: "n,w,nw",
    36  				minHeight:	27,
    37  				minWidth:	135,
    38  				maxHeight: 608,
    39  				maxWidth:	990
    40  			});
    41  		}
    42  
    43  		function onKill() {
    44  			if (running) running.Kill();
    45  		}
    46  
    47  		function onRun(e) {
    48  			onKill();
    49  			output.style.display = "block";
    50  			outpre.innerHTML = "";
    51  			run1.style.display = "none";
    52  			var options = {Race: e.shiftKey};
    53  			running = transport.Run(text(code), PlaygroundOutput(outpre), options);
    54  		}
    55  
    56  		function onClose() {
    57  			onKill();
    58  			output.style.display = "none";
    59  			run1.style.display = "inline-block";
    60  		}
    61  
    62  		var run1 = document.createElement('button');
    63  		run1.innerHTML = 'Run';
    64  		run1.className = 'run';
    65  		run1.addEventListener("click", onRun, false);
    66  		var run2 = document.createElement('button');
    67  		run2.className = 'run';
    68  		run2.innerHTML = 'Run';
    69  		run2.addEventListener("click", onRun, false);
    70  		var kill = document.createElement('button');
    71  		kill.className = 'kill';
    72  		kill.innerHTML = 'Kill';
    73  		kill.addEventListener("click", onKill, false);
    74  		var close = document.createElement('button');
    75  		close.className = 'close';
    76  		close.innerHTML = 'Close';
    77  		close.addEventListener("click", onClose, false);
    78  
    79  		var button = document.createElement('div');
    80  		button.classList.add('buttons');
    81  		button.appendChild(run1);
    82  		// Hack to simulate insertAfter
    83  		code.parentNode.insertBefore(button, code.nextSibling);
    84  
    85  		var buttons = document.createElement('div');
    86  		buttons.classList.add('buttons');
    87  		buttons.appendChild(run2);
    88  		buttons.appendChild(kill);
    89  		buttons.appendChild(close);
    90  
    91  		output.classList.add('output');
    92  		output.appendChild(buttons);
    93  		output.appendChild(outpre);
    94  		output.style.display = "none";
    95  		code.parentNode.insertBefore(output, button.nextSibling);
    96  	}
    97  
    98  	var play = document.querySelectorAll('div.playground');
    99  	for (var i = 0; i < play.length; i++) {
   100  		init(play[i]);
   101  	}
   102  }
   103