demo.html 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "style" }}style="{{ .Get "style" | safeCSS }}"{{ end }}>
  7. <div class="demo" id="js-demo-{{ $uniq }}"></div>
  8. <button data-launch="js-demo-{{ $uniq }}">Launch <span aria-hidden="true">↗</span></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. // Save a version for the new window
  24. var demoDiv = document.createElement('div');
  25. demoDiv.innerHTML = template.innerHTML;
  26. var standaloneScript = demoDiv.querySelector('script');
  27. if (standaloneScript) {
  28. standaloneScript.textContent = '(function() { document.getElementsByTagName(\'html\')[0].setAttribute(\'lang\', \'en\'); var demo = document; {{ if .Get "style" }}document.body.setAttribute(\'style\', \'{{ .Get "style" | safeCSS }}\'){{ end }}' + standaloneScript.textContent + ' })();';
  29. }
  30. if (document.head.attachShadow) {
  31. var templateScript = template.content.querySelector('script');
  32. if (templateScript) {
  33. var wrappedScript = '(function() { var demo = document.getElementById(\'js-demo-{{ $uniq }}\').shadowRoot;' + templateScript.textContent + '})();';
  34. templateScript.textContent = wrappedScript;
  35. }
  36. root.attachShadow({mode: 'open'});
  37. root.shadowRoot.appendChild(document.importNode(template.content, true));
  38. } else {
  39. 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>';
  40. }
  41. var launchButton = document.querySelector('[data-launch="js-demo-{{ $uniq }}"]');
  42. launchButton.addEventListener('click', function () {
  43. var standalone = window.open();
  44. standalone.document.body.innerHTML = demoDiv.innerHTML;
  45. // For activating the script :-(
  46. var newScript = standalone.document.createElement('script');
  47. var oldScript = standalone.document.querySelector('script');
  48. if (oldScript) {
  49. newScript.textContent = oldScript.textContent;
  50. oldScript.parentNode.removeChild(oldScript);
  51. standalone.document.body.appendChild(newScript);
  52. }
  53. {{ if .Get "backgroundColor" }}
  54. standalone.document.body.style.backgroundColor = {{ .Get "backgroundColor" | safeCSS }};
  55. {{ end }}
  56. standalone.document.title = 'demo ' + {{ $uniq }};
  57. });
  58. })();
  59. </script>
  60. </div>