hugo-theme-zzo/layouts/shortcodes/codes.html

47 lines
1.3 KiB
HTML
Raw Normal View History

{{ $id := substr (md5 .Inner) 0 16 }}
2020-01-23 17:46:03 +00:00
<div id="{{ $id }}" class="codetab">
<div class="codetab__links">
{{ range .Params }}
2020-01-23 17:46:03 +00:00
<button class="codetab__link" aria-label="Tab link">{{ . }}</button>
{{ end }}
</div>
{{ .Inner }}
</div>
<script>
'use strict';
var containerId = JSON.parse({{ $id | jsonify }});
var containerElem = document.getElementById(containerId);
2020-01-23 17:46:03 +00:00
var codetabLinks = null;
var codetabContents = null;
var ids = [];
if (containerElem) {
2020-01-23 17:46:03 +00:00
codetabLinks = containerElem.querySelectorAll('.codetab__link');
codetabContents = containerElem.querySelectorAll('.codetab__content');
}
2020-01-23 17:46:03 +00:00
for (var i = 0; i < codetabContents.length; i++) {
ids = ids.concat(codetabContents[i].getAttribute('id'));
codetabContents[i].style.display = 'none';
2020-01-23 17:46:03 +00:00
if (0 === parseInt(i, 10) && !codetabContents[i].classList.contains('active')) {
codetabContents[i].classList.add('active');
}
}
2020-01-23 17:46:03 +00:00
for (var i = 0; i < codetabLinks.length; i++) {
codetabLinks[i].setAttribute('id', ids[i]);
2020-01-23 17:46:03 +00:00
if (0 === parseInt(i, 10) && !codetabLinks[i].classList.contains('active')) {
codetabLinks[i].classList.add('active');
} else {
2020-01-23 17:46:03 +00:00
codetabLinks[i].classList.remove('active');
}
}
2020-01-23 17:46:03 +00:00
if (codetabContents.length > 0) {
codetabContents[0].style.display = 'block';
}
</script>