demo.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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" id="demo-{{ $uniq }}"></div>
  7. {{ if .Get "caption" }}
  8. <figcaption id="caption-{{ $uniq }}">{{ .Get "caption" | markdownify }}</figcaption>
  9. {{ end }}
  10. {{ if .Get "caption" }}
  11. </figure>
  12. {{ end }}
  13. <template id="template-{{ $uniq }}">
  14. {{ .Inner }}
  15. </template>
  16. <button data-launch="demo-{{ $uniq }}">Launch in separate window</button>
  17. <script>
  18. (function() {
  19. var root = document.getElementById('demo-{{ $uniq }}');
  20. var template = document.getElementById('template-{{ $uniq }}');
  21. var script = template.content.querySelector('script');
  22. if (script) {
  23. var standaloneScript = '(function() { var demo = document; ' + script.textContent + ' })()';
  24. var wrappedScript = '(function() { var demo = document.getElementById(\'demo-{{ $uniq }}\').shadowRoot;' + script.textContent + '})()';
  25. script.textContent = wrappedScript;
  26. }
  27. if (document.head.attachShadow) {
  28. root.attachShadow({mode: 'open'});
  29. root.shadowRoot.appendChild(document.importNode(template.content, true));
  30. } else {
  31. 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. The browser does not have an issue with the demo code itself.</p>';
  32. }
  33. var launchButton = document.querySelector('[data-launch="demo-{{ $uniq }}"]');
  34. launchButton.addEventListener('click', function () {
  35. //var wrap = '<!DOCTYPE html><html lang="en"><head><title>demo</title></head><body></body></html>';
  36. var demoContent = template.content;
  37. var standalone = window.open();
  38. //standalone.document.write(wrap);
  39. script.textContent = standaloneScript;
  40. standalone.document.body.appendChild(document.importNode(template.content, true));
  41. });
  42. })();
  43. </script>
  44. </div>