demo.html 3.2 KB

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