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