demo.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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="js-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="js-demo-{{ $uniq }}">Launch in separate window</button>
  17. <script>
  18. (function() {
  19. var root = document.getElementById('js-demo-{{ $uniq }}');
  20. var template = document.getElementById('template-{{ $uniq }}');
  21. var script = template.content.querySelector('script');
  22. if (script) {
  23. var standaloneScript = '(function() { document.getElementsByTagName(\'html\')[0].setAttribute(\'lang\', \'en\'); var demo = document; ' + script.textContent + ' })()';
  24. var wrappedScript = '(function() { var demo = document.getElementById(\'js-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. You can launch the demo in a separate window ↓</p>';
  32. }
  33. var launchButton = document.querySelector('[data-launch="js-demo-{{ $uniq }}"]');
  34. launchButton.addEventListener('click', function () {
  35. var demoContent = template.content;
  36. var standalone = window.open();
  37. script.textContent = standaloneScript;
  38. standalone.document.body.appendChild(document.importNode(template.content, true));
  39. standalone.document.title = 'demo ' + {{ $uniq }}
  40. });
  41. })();
  42. </script>
  43. </div>