Mediawiki:common.js
| Строка 266: | Строка 266: | ||
/* Химия */ | /* Химия */ | ||
(function(){ | /* Химия */ | ||
if (typeof mw === 'undefined' || !window.document) return; | (function() { | ||
if (typeof mw === 'undefined' || !window.document) return; | |||
function initCollapse() { | |||
var headings = document.querySelectorAll('.chem-heading'); | function initCollapse() { | ||
var hi = 0; | var headings = document.querySelectorAll('.chem-heading'); | ||
var len = headings.length; | var hi = 0; | ||
for (; hi < len; hi++) { | var len = headings.length; | ||
(function(node) { | for (; hi < len; hi++) { | ||
if (node.getAttribute('data-chem-attached')) return; | (function(node) { | ||
node.setAttribute('data-chem-attached', '1'); | if (node.getAttribute('data-chem-attached')) return; | ||
node.style.cursor = 'pointer'; | node.setAttribute('data-chem-attached', '1'); | ||
node.style.cursor = 'pointer'; | |||
node.addEventListener('click', function() { | node.addEventListener('click', function() { | ||
var kind = node.getAttribute('data-kind') || ''; | var kind = node.getAttribute('data-kind') || ''; | ||
var wrapper = findWrapper(node, kind); | var wrapper = findWrapper(node, kind); | ||
if (!wrapper) return; | if (!wrapper) return; | ||
var btn = node.querySelector('.collapse-btn'); | |||
var btn = node.querySelector('.collapse-btn'); | |||
if (wrapper.classList.contains('collapsed')) { | if (wrapper.classList.contains('collapsed')) { | ||
wrapper.classList.remove('collapsed'); | // === РАСКРЫТИЕ (плавно) === | ||
wrapper.classList.add('expanded'); | wrapper.style.maxHeight = wrapper.scrollHeight + 'px'; | ||
if (btn) btn.textContent = 'свернуть'; | wrapper.classList.remove('collapsed'); | ||
} else { | wrapper.classList.add('expanded'); | ||
wrapper.classList.remove('expanded'); | if (btn) btn.textContent = 'свернуть'; | ||
wrapper.classList.add('collapsed'); | |||
if (btn) btn.textContent = 'развернуть'; | // убираем фиксированную высоту после анимации | ||
} | var cleanup = function() { | ||
}); | wrapper.style.maxHeight = ''; | ||
})(headings[hi]); | wrapper.removeEventListener('transitionend', cleanup); | ||
} | }; | ||
} | wrapper.addEventListener('transitionend', cleanup); | ||
} else { | |||
function findWrapper(node, kind) { | // === СВЁРТЫВАНИЕ (плавно) === | ||
var parent = node.parentNode; | var currentHeight = wrapper.scrollHeight; | ||
if (!parent) return null; | wrapper.style.maxHeight = currentHeight + 'px'; | ||
// принудительный reflow | |||
var wrappers = parent.querySelectorAll('.collapsible'); | wrapper.offsetHeight; | ||
var wi = 0; | wrapper.style.maxHeight = '0px'; | ||
var wlen = wrappers.length; | wrapper.classList.remove('expanded'); | ||
for (; wi < wlen; wi++) { | wrapper.classList.add('collapsed'); | ||
if (wrappers[wi].getAttribute('data-kind') === kind) { | if (btn) btn.textContent = 'развернуть'; | ||
return wrappers[wi]; | } | ||
} | }); | ||
} | })(headings[hi]); | ||
return null; | } | ||
} | } | ||
if (document.readyState === 'complete' || document.readyState === 'interactive') { | function findWrapper(node, kind) { | ||
initCollapse(); | var parent = node.parentNode; | ||
} else { | if (!parent) return null; | ||
document.addEventListener('DOMContentLoaded', initCollapse); | 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); | |||
} | |||
})(); | })(); | ||