You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.2 KiB

  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. function initPlayground(transport) {
  5. 'use strict';
  6. function text(node) {
  7. var s = '';
  8. for (var i = 0; i < node.childNodes.length; i++) {
  9. var n = node.childNodes[i];
  10. if (n.nodeType === 1) {
  11. if (n.tagName === 'BUTTON') continue
  12. if (n.tagName === 'SPAN' && n.className === 'number') continue;
  13. if (n.tagName === 'DIV' || n.tagName == 'BR') {
  14. s += "\n";
  15. }
  16. s += text(n);
  17. continue;
  18. }
  19. if (n.nodeType === 3) {
  20. s += n.nodeValue;
  21. }
  22. }
  23. return s.replace('\xA0', ' '); // replace non-breaking spaces
  24. }
  25. // When presenter notes are enabled, the index passed
  26. // here will identify the playground to be synced
  27. function init(code, index) {
  28. var output = document.createElement('div');
  29. var outpre = document.createElement('pre');
  30. var running;
  31. if ($ && $(output).resizable) {
  32. $(output).resizable({
  33. handles: 'n,w,nw',
  34. minHeight: 27,
  35. minWidth: 135,
  36. maxHeight: 608,
  37. maxWidth: 990
  38. });
  39. }
  40. function onKill() {
  41. if (running) running.Kill();
  42. if (window.notesEnabled) updatePlayStorage('onKill', index);
  43. }
  44. function onRun(e) {
  45. var sk = e.shiftKey || localStorage.getItem('play-shiftKey') === 'true';
  46. if (running) running.Kill();
  47. output.style.display = 'block';
  48. outpre.innerHTML = '';
  49. run1.style.display = 'none';
  50. var options = {Race: sk};
  51. running = transport.Run(text(code), PlaygroundOutput(outpre), options);
  52. if (window.notesEnabled) updatePlayStorage('onRun', index, e);
  53. }
  54. function onClose() {
  55. if (running) running.Kill();
  56. output.style.display = 'none';
  57. run1.style.display = 'inline-block';
  58. if (window.notesEnabled) updatePlayStorage('onClose', index);
  59. }
  60. if (window.notesEnabled) {
  61. playgroundHandlers.onRun.push(onRun);
  62. playgroundHandlers.onClose.push(onClose);
  63. playgroundHandlers.onKill.push(onKill);
  64. }
  65. var run1 = document.createElement('button');
  66. run1.innerHTML = 'Run';
  67. run1.className = 'run';
  68. run1.addEventListener("click", onRun, false);
  69. var run2 = document.createElement('button');
  70. run2.className = 'run';
  71. run2.innerHTML = 'Run';
  72. run2.addEventListener("click", onRun, false);
  73. var kill = document.createElement('button');
  74. kill.className = 'kill';
  75. kill.innerHTML = 'Kill';
  76. kill.addEventListener("click", onKill, false);
  77. var close = document.createElement('button');
  78. close.className = 'close';
  79. close.innerHTML = 'Close';
  80. close.addEventListener("click", onClose, false);
  81. var button = document.createElement('div');
  82. button.classList.add('buttons');
  83. button.appendChild(run1);
  84. // Hack to simulate insertAfter
  85. code.parentNode.insertBefore(button, code.nextSibling);
  86. var buttons = document.createElement('div');
  87. buttons.classList.add('buttons');
  88. buttons.appendChild(run2);
  89. buttons.appendChild(kill);
  90. buttons.appendChild(close);
  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. var play = document.querySelectorAll('div.playground');
  98. for (var i = 0; i < play.length; i++) {
  99. init(play[i], i);
  100. }
  101. }