cupper-hugo-theme/layouts/shortcodes/demo.html

66 lines
3.0 KiB
HTML
Raw Normal View History

2017-11-02 16:39:45 +01:00
{{ $uniq := .Inner | htmlEscape | md5 | lower }}
<div class="demo-container">
{{ if .Get "caption" }}
<figure role="group" aria-labelledby="caption-{{ $uniq }}">
{{ end }}
2017-12-02 13:18:34 +01:00
<div class="demo-inner" {{ if .Get "style" }}style="{{ .Get "style" | safeCSS }}"{{ end }}>
2017-11-02 16:39:45 +01:00
<div class="demo" id="js-demo-{{ $uniq }}"></div>
2017-12-02 13:18:34 +01:00
<button data-launch="js-demo-{{ $uniq }}">Launch <span aria-hidden="true"></span></button>
2017-11-02 16:39:45 +01:00
</div>
{{ if .Get "caption" }}
<figcaption id="caption-{{ $uniq }}">{{ .Get "caption" | markdownify }}</figcaption>
{{ end }}
{{ if .Get "caption" }}
</figure>
{{ end }}
<template id="template-{{ $uniq }}">
{{ .Inner }}
</template>
<script>
(function() {
var root = document.getElementById('js-demo-{{ $uniq }}');
var template = document.getElementById('template-{{ $uniq }}');
// Save a version for the new window
var demoDiv = document.createElement('div');
demoDiv.innerHTML = template.innerHTML;
var standaloneScript = demoDiv.querySelector('script');
if (standaloneScript) {
2017-12-02 13:18:34 +01:00
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 + ' })();';
2017-11-02 16:39:45 +01:00
}
if (document.head.attachShadow) {
var templateScript = template.content.querySelector('script');
if (templateScript) {
var wrappedScript = '(function() { var demo = document.getElementById(\'js-demo-{{ $uniq }}\').shadowRoot;' + templateScript.textContent + '})();';
templateScript.textContent = wrappedScript;
}
root.attachShadow({mode: 'open'});
root.shadowRoot.appendChild(document.importNode(template.content, true));
} else {
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>';
}
var launchButton = document.querySelector('[data-launch="js-demo-{{ $uniq }}"]');
launchButton.addEventListener('click', function () {
var standalone = window.open();
standalone.document.body.innerHTML = demoDiv.innerHTML;
// For activating the script :-(
var newScript = standalone.document.createElement('script');
var oldScript = standalone.document.querySelector('script');
if (oldScript) {
newScript.textContent = oldScript.textContent;
oldScript.parentNode.removeChild(oldScript);
standalone.document.body.appendChild(newScript);
}
{{ if .Get "backgroundColor" }}
standalone.document.body.style.backgroundColor = {{ .Get "backgroundColor" | safeCSS }};
{{ end }}
standalone.document.title = 'demo ' + {{ $uniq }};
});
})();
</script>
</div>