Mediawiki:test.js
| Строка 1: | Строка 1: | ||
(function(){ | (function() { | ||
if(typeof mw==='undefined') return; | if (typeof mw === 'undefined' || !window.document) return; | ||
var | function initCollapse() { | ||
var headings = document.querySelectorAll('.chem-heading'); | |||
var hi = 0; | |||
var len = headings.length; | |||
for (; hi < len; hi++) { | |||
(function(node) { | |||
if (node.getAttribute('data-chem-attached')) return; | |||
node.setAttribute('data-chem-attached', '1'); | |||
node.style.cursor = 'pointer'; | |||
node.addEventListener('click', function() { | |||
var kind = node.getAttribute('data-kind') || ''; | |||
var wrapper = findWrapper(node, kind); | |||
if (!wrapper) return; | |||
function | var btn = node.querySelector('.collapse-btn'); | ||
function | if (wrapper.classList.contains('collapsed')) { | ||
wrapper.classList.remove('collapsed'); | |||
var | wrapper.classList.add('expanded'); | ||
wrapper.style.maxHeight = wrapper.scrollHeight + 'px'; | |||
if (btn) btn.textContent = 'свернуть'; | |||
} | var cleanup = function() { | ||
wrapper.style.maxHeight = ''; | |||
var | wrapper.removeEventListener('transitionend', cleanup); | ||
}; | |||
wrapper.addEventListener('transitionend', cleanup); | |||
} | } else { | ||
} | var currentHeight = wrapper.scrollHeight; | ||
} | wrapper.style.maxHeight = currentHeight + 'px'; | ||
wrapper.offsetHeight; | |||
var | wrapper.classList.remove('expanded'); | ||
wrapper.classList.add('collapsed'); | |||
wrapper.style.maxHeight = '0px'; | |||
if (btn) btn.textContent = 'развернуть'; | |||
} | } | ||
}); | |||
})(headings[hi]); | |||
} | |||
} | |||
function findWrapper(node, kind) { | |||
var parent = node.parentNode; | |||
if (!parent) return null; | |||
var wrappers = parent.querySelectorAll('.collapsible'); | |||
var wi = 0; | |||
var wlen = wrappers.length; | |||
for (; wi < wlen; wi++) { | |||
if (wrappers[wi].getAttribute('data-kind') === kind) { | |||
return wrappers[wi]; | |||
} | |||
} | |||
return null; | |||
} | |||
if (document.readyState === 'complete' || document.readyState === 'interactive') { | |||
initCollapse(); | |||
} else { | |||
document.addEventListener('DOMContentLoaded', initCollapse); | |||
} | |||
})(); | })(); | ||