demo.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {{ $uniq := .Inner | htmlEscape | md5 | lower }}
  2. <div class="demo-container">
  3. {{ if .Get "caption" }}
  4. <figure role="group" aria-labelledby="caption-{{ $uniq }}">
  5. {{ end }}
  6. <div class="demo-inner" {{ if .Get "backgroundColor" }}style="background-color: {{ .Get "backgroundColor" | safeCSS }}"{{ end }}>
  7. <div class="demo" id="js-demo-{{ $uniq }}"></div>
  8. <button data-launch="js-demo-{{ $uniq }}">Launch in separate window</button>
  9. </div>
  10. {{ if .Get "caption" }}
  11. <figcaption id="caption-{{ $uniq }}">{{ .Get "caption" | markdownify }}</figcaption>
  12. {{ end }}
  13. {{ if .Get "caption" }}
  14. </figure>
  15. {{ end }}
  16. <template id="template-{{ $uniq }}">
  17. {{ .Inner }}
  18. </template>
  19. <script>
  20. (function() {
  21. var root = document.getElementById('js-demo-{{ $uniq }}');
  22. var template = document.getElementById('template-{{ $uniq }}');
  23. var demoDiv = document.createElement('div');
  24. demoDiv.innerHTML = template.innerHTML;
  25. var standaloneScript = demoDiv.querySelector('script');
  26. standaloneScript.textContent = '(function() { document.getElementsByTagName(\'html\')[0].setAttribute(\'lang\', \'en\'); var demo = document; ' + standaloneScript.textContent + ' })();';
  27. if (document.head.attachShadow) {
  28. var templateScript = template.content.querySelector('script');
  29. if (templateScript) {
  30. var wrappedScript = '(function() { var demo = document.getElementById(\'js-demo-{{ $uniq }}\').shadowRoot;' + templateScript.textContent + '})();';
  31. templateScript.textContent = wrappedScript;
  32. }
  33. root.attachShadow({mode: 'open'});
  34. root.shadowRoot.appendChild(document.importNode(template.content, true));
  35. } else {
  36. root.innerHTML = '<p class="site-error"><strong style="font-weight:bold">Site error:</strong> A browser supporting Shadow DOM is needed to run encapsulated demos. You can launch the demo in a separate window ↓</p>';
  37. }
  38. var launchButton = document.querySelector('[data-launch="js-demo-{{ $uniq }}"]');
  39. launchButton.addEventListener('click', function () {
  40. var standalone = window.open();
  41. standalone.document.body.appendChild(demoDiv);
  42. // For activating the script :-(
  43. var newScript = standalone.document.createElement('script');
  44. var oldScript = standalone.document.querySelector('script');
  45. newScript.textContent = oldScript.textContent;
  46. oldScript.parentNode.removeChild(oldScript);
  47. standalone.document.body.appendChild(newScript);
  48. {{ if .Get "backgroundColor" }}
  49. standalone.document.body.style.backgroundColor = {{ .Get "backgroundColor" | safeCSS }};
  50. {{ end }}
  51. standalone.document.title = 'demo ' + {{ $uniq }};
  52. });
  53. })();
  54. /*(function() {
  55. var root = document.getElementById('js-demo-{{ $uniq }}');
  56. var template = document.getElementById('template-{{ $uniq }}');
  57. var script = template.content.querySelector('script');
  58. if (script) {
  59. var standaloneScript = '(function() { document.getElementsByTagName(\'html\')[0].setAttribute(\'lang\', \'en\'); var demo = document; ' + script.textContent + ' })();';
  60. var wrappedScript = '(function() { var demo = document.getElementById(\'js-demo-{{ $uniq }}\').shadowRoot;' + script.textContent + '})();';
  61. script.textContent = wrappedScript;
  62. }
  63. if (document.head.attachShadow) {
  64. root.attachShadow({mode: 'open'});
  65. root.shadowRoot.appendChild(document.importNode(template.content, true));
  66. } else {
  67. root.innerHTML = '<p class="site-error"><strong style="font-weight:bold">Site error:</strong> A browser supporting Shadow DOM is needed to run encapsulated demos. You can launch the demo in a separate window ↓</p>';
  68. }
  69. var launchButton = document.querySelector('[data-launch="js-demo-{{ $uniq }}"]');
  70. launchButton.addEventListener('click', function () {
  71. var standalone = window.open();
  72. script.innerHTML = standaloneScript;
  73. var demoDiv = document.createElement('div');
  74. demoDiv.innerHTML = template.innerHTML;
  75. standalone.document.body.appendChild(demoDiv);
  76. // For activating the script :-(
  77. var newScript = standalone.document.createElement('script');
  78. var oldScript = standalone.document.querySelector('script');
  79. newScript.textContent = oldScript.textContent;
  80. oldScript.parentNode.removeChild(oldScript);
  81. standalone.document.body.appendChild(newScript);
  82. {{ if .Get "backgroundColor" }}
  83. standalone.document.body.style.backgroundColor = {{ .Get "backgroundColor" | safeCSS }};
  84. {{ end }}
  85. standalone.document.title = 'demo ' + {{ $uniq }};
  86. });
  87. })();*/
  88. </script>
  89. </div>