Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

MediaWiki:Test.js: различия между версиями

Страница интерфейса MediaWiki
Нет описания правки
Нет описания правки
 
(не показано 139 промежуточных версий этого же участника)
Строка 1: Строка 1:
$(function () {
(function() {  
     var players = document.getElementsByClassName('audio-player');
     if (typeof mw === 'undefined' || !window.document) return;
    for (var i = 0; i < players.length; i++) {
   
        (function (container) {
    function initCollapse() {
            var src = container.getAttribute('data-src');
        var headings = document.querySelectorAll('.chem-heading');  
            if (!src) return;
        var hi = 0;  
 
        var len = headings.length;  
            var audio = document.createElement('audio');
        for (; hi < len; hi++) {  
            audio.src = src;
            (function(node) {  
 
                if (node.getAttribute('data-chem-attached')) return;  
            var wrapper = document.createElement('div');
                node.setAttribute('data-chem-attached', '1');  
            wrapper.className = 'custom-audio-wrapper';
                node.style.cursor = 'pointer';  
 
                node.addEventListener('click', function() {
            var playBtn = document.createElement('button');
                    var kind = node.getAttribute('data-kind') || '';  
            playBtn.className = 'custom-audio-play';
                    var wrapper = findWrapper(node, kind);  
            playBtn.innerHTML = '';
                    if (!wrapper) return;  
 
                    var btn = node.querySelector('.collapse-btn');  
            var progress = document.createElement('input');
                   
            progress.setAttribute('type', 'range');
                    if (wrapper.classList.contains('collapsed')) {
            progress.className = 'custom-audio-progress';
                        wrapper.classList.remove('collapsed');  
            progress.setAttribute('min', 0);
                        wrapper.classList.add('expanded');  
            progress.value = 0;
                        wrapper.style.maxHeight = wrapper.scrollHeight + 'px';  
            progress.step = 0.01;
                        if (btn) btn.textContent = 'свернуть';  
 
                        var cleanup = function() {
            var time = document.createElement('span');
                            wrapper.style.maxHeight = '';  
            time.className = 'custom-audio-time';
                            wrapper.removeEventListener('transitionend', cleanup);  
            time.innerHTML = '0:00';
                        };  
 
                        wrapper.addEventListener('transitionend', cleanup);  
            // Добавляем кнопку громкости
                    } else {
            var volumeBtn = document.createElement('button');
                        var currentHeight = wrapper.scrollHeight;  
            volumeBtn.className = 'custom-audio-volume-btn';
                        wrapper.style.maxHeight = currentHeight + 'px';  
            volumeBtn.innerHTML = '🔊';
                        wrapper.offsetHeight;  
 
                        wrapper.classList.remove('expanded');  
            // Блок для ползунка громкости с подсветкой
                        wrapper.classList.add('collapsed');  
            var volumeControlWrapper = document.createElement('div');
                        wrapper.style.maxHeight = '0px';  
            volumeControlWrapper.className = 'custom-audio-volume-wrapper';
                        if (btn) btn.textContent = 'развернуть';  
 
                    }  
            // Ползунок громкости
                });  
            var volumeControl = document.createElement('input');
             })(headings[hi]);
            volumeControl.setAttribute('type', 'range');
        }
            volumeControl.setAttribute('min', 0);
    }  
            volumeControl.setAttribute('max', 1);
   
            volumeControl.setAttribute('step', 0.01);
    function findWrapper(node, kind) {  
            volumeControl.setAttribute('value', 1);
        var parent = node.parentNode;  
            volumeControl.className = 'custom-audio-volume';
        if (!parent) return null;
            volumeControl.style.transform = 'rotate(270deg)'; // Вертикальный ползунок
        var wrappers = parent.querySelectorAll('.collapsible');  
 
        var wi = 0;  
            volumeControlWrapper.appendChild(volumeControl);
        var wlen = wrappers.length;
 
        for (; wi < wlen; wi++) {
            wrapper.appendChild(playBtn);
             if (wrappers[wi].getAttribute('data-kind') === kind) {  
            wrapper.appendChild(progress);
                 return wrappers[wi];  
            wrapper.appendChild(time);
             }  
            wrapper.appendChild(volumeBtn);
        }
            wrapper.appendChild(volumeControlWrapper);
        return null;  
            container.appendChild(wrapper);
    }
 
   
            playBtn.onclick = function () {
    if (document.readyState === 'complete' || document.readyState === 'interactive') {  
                if (audio.paused) {
        initCollapse();  
                    audio.play();
    } else {  
                    playBtn.innerHTML = '';
        document.addEventListener('DOMContentLoaded', initCollapse);  
                } else {
    }  
                    audio.pause();
})();
                    playBtn.innerHTML = '';
                }
            };
 
             // Исправим ситуацию, если duration ещё не прогрузилась
            audio.addEventListener('loadedmetadata', function () {
                progress.max = audio.duration;
            });
 
            audio.addEventListener('timeupdate', function () {
                progress.value = audio.currentTime;
                if (!isNaN(audio.duration)) {
                    progress.max = audio.duration;
                }
                time.innerHTML = formatTime(audio.currentTime);
            });
 
            progress.addEventListener('input', function () {
                audio.currentTime = parseFloat(progress.value);
            });
 
             volumeControl.addEventListener('input', function () {
                 audio.volume = volumeControl.value;
             });
 
            volumeBtn.onclick = function () {
                // Переключаем видимость блока с ползунком громкости
                if (volumeControlWrapper.style.display === 'none' || volumeControlWrapper.style.display === '') {
                    volumeControlWrapper.style.display = 'block';
                } else {
                    volumeControlWrapper.style.display = 'none';
                }
            };
 
            function formatTime(seconds) {
                var mins = Math.floor(seconds / 60);
                var secs = Math.floor(seconds % 60);
                if (secs < 10) secs = '0' + secs;
                return mins + ':' + secs;
            }
        })(players[i]);
    }
});

Текущая версия от 13:10, 4 апреля 2026

(function() { 
    if (typeof mw === 'undefined' || !window.document) return; 
    
    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; 
                    var btn = node.querySelector('.collapse-btn'); 
                    
                    if (wrapper.classList.contains('collapsed')) { 
                        wrapper.classList.remove('collapsed'); 
                        wrapper.classList.add('expanded'); 
                        wrapper.style.maxHeight = wrapper.scrollHeight + 'px'; 
                        if (btn) btn.textContent = 'свернуть'; 
                        var cleanup = function() { 
                            wrapper.style.maxHeight = ''; 
                            wrapper.removeEventListener('transitionend', cleanup); 
                        }; 
                        wrapper.addEventListener('transitionend', cleanup); 
                    } else { 
                        var currentHeight = wrapper.scrollHeight; 
                        wrapper.style.maxHeight = currentHeight + 'px'; 
                        wrapper.offsetHeight; 
                        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); 
    } 
})();