demo.html 2.8 KB

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