Files: 6207dfb384bd6c38a6fc733519bf99a5c0bc13af / tmpl / js / code-examples.js
827 bytesRaw
1 | window.addEventListener('load', function () { |
2 | Array.prototype.forEach.call(document.querySelectorAll('.code-examples'), setupCodeExample) |
3 | }) |
4 | |
5 | function setupCodeExample (codeExamples) { |
6 | // select the current code example |
7 | selectTab(codeExamples, 1) |
8 | // attach click handlers |
9 | Array.prototype.forEach.call(codeExamples.querySelectorAll('.tab'), function (tab, n) { |
10 | tab.addEventListener('click', selectTab.bind(null, codeExamples, (n+1))) |
11 | }) |
12 | } |
13 | |
14 | function selectTab (el, n) { |
15 | // deselect current |
16 | try { el.querySelector('pre.current').classList.remove('current') } catch (e) {} |
17 | try { el.querySelector('.tab.current').classList.remove('current') } catch (e) {} |
18 | // select new |
19 | el.querySelector('pre:nth-child('+n+')').classList.add('current') |
20 | el.querySelector('.tab:nth-child('+n+')').classList.add('current') |
21 | } |
Built with git-ssb-web