跳转到内容
打开/关闭菜单
  • 1 条目
  • 1 用户
  • 147 编辑
Anachronism
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
Ancimen​(留言 | 贡献)
无编辑摘要
Ancimen​(留言 | 贡献)
无编辑摘要
第1行: 第1行:
/* ===== 侧边工具栏 Rail ===== */
/* ===== 侧边工具栏 Rail 管理器 ===== */
(function () {
/* ===== 侧边工具栏 Rail 管理器 ===== */
     function initRail() {
window.Rail = (function () {
        // 避免重复注入
     var items = [];
        if (document.getElementById('custom-rail')) return;
    var activeId = null;
    var panelEl, overlayEl, buttonsEl, contentEl, titleEl;


         // 创建按钮
    function ensureDOM() {
         var btn = document.createElement('button');
         if (document.getElementById('rail-container')) return;
         btn.id = 'custom-rail-toggle';
 
         btn.className = 'custom-rail-toggle';
         buttonsEl = document.createElement('div');
         btn.setAttribute('aria-label', '打开工具栏');
         buttonsEl.id = 'rail-container';
         btn.innerHTML = '<span class="custom-rail-toggle__icon">★</span>';
         buttonsEl.className = 'rail-container';
 
         overlayEl = document.createElement('div');
        overlayEl.id = 'rail-overlay';
         overlayEl.className = 'rail-overlay';


         // 创建面板
         panelEl = document.createElement('aside');
        var panel = document.createElement('aside');
         panelEl.id = 'rail-panel';
         panel.id = 'custom-rail';
         panelEl.className = 'rail-panel';
         panel.className = 'custom-rail';
         panelEl.innerHTML = [
         panel.innerHTML = [
             '<div class="rail-panel__header">',
             '<div class="custom-rail__header">',
             '  <span class="rail-panel__title"></span>',
             '  <span class="custom-rail__title">工具栏</span>',
             '  <button class="rail-panel__close" type="button" aria-label="关闭">×</button>',
             '  <button class="custom-rail__close" aria-label="关闭">×</button>',
             '</div>',
             '</div>',
             '<div class="custom-rail__content">',
             '<div class="rail-panel__content" id="rail-panel-content"></div>'
            '  <h3>快速导航</h3>',
            '  <ul>',
            '    <li><a href="/index.php/首页">首页</a></li>',
            '    <li><a href="/index.php/特殊:最近更改">最近更改</a></li>',
            '    <li><a href="/index.php/特殊:随机">随机页面</a></li>',
            '    <li><a href="/index.php/Category:站点样式">站点样式</a></li>',
            '  </ul>',
            '  <h3>公告</h3>',
            '  <p>本 Wiki 正在建设中。</p>',
            '</div>'
         ].join('');
         ].join('');


         // 创建遮罩
         titleEl = panelEl.querySelector('.rail-panel__title');
        var overlay = document.createElement('div');
         contentEl = panelEl.querySelector('#rail-panel-content');
        overlay.id = 'custom-rail-overlay';
         overlay.className = 'custom-rail-overlay';


         document.body.appendChild(btn);
         document.body.appendChild(overlayEl);
         document.body.appendChild(overlay);
         document.body.appendChild(panelEl);
         document.body.appendChild(panel);
         document.body.appendChild(buttonsEl);


         var closeBtn = panel.querySelector('.custom-rail__close');
         panelEl.querySelector('.rail-panel__close').addEventListener('click', close);
 
        overlayEl.addEventListener('click', close);
        function openRail() {
         document.addEventListener('keydown', function (e) {
            document.body.classList.add('custom-rail-open');
             if (e.key === 'Escape') close();
            btn.setAttribute('aria-label', '关闭工具栏');
        });
         }
    }
        function closeRail() {
             document.body.classList.remove('custom-rail-open');
            btn.setAttribute('aria-label', '打开工具栏');
        }


    function createButton(item) {
        var btn = document.createElement('button');
        btn.type = 'button';
        btn.className = 'rail-button';
        btn.setAttribute('data-rail-id', item.id);
        btn.setAttribute('aria-label', item.label);
        btn.setAttribute('title', item.label);
        btn.innerHTML = '<span class="rail-button__icon">' + item.icon + '</span>';
         btn.addEventListener('click', function () {
         btn.addEventListener('click', function () {
             if (document.body.classList.contains('custom-rail-open')) {
             if (activeId === item.id) {
                 closeRail();
                 close();
             } else {
             } else {
                 openRail();
                 open(item);
             }
             }
         });
         });
        return btn;
    }


         closeBtn.addEventListener('click', closeRail);
    function highlightButtons() {
         overlay.addEventListener('click', closeRail);
         var all = buttonsEl.querySelectorAll('.rail-button');
         for (var i = 0; i < all.length; i++) {
            var id = all[i].getAttribute('data-rail-id');
            all[i].classList.toggle('is-active', id === activeId);
        }
    }


         // 按 Esc 关闭
    function open(item) {
         document.addEventListener('keydown', function (e) {
         activeId = item.id;
             if (e.key === 'Escape') closeRail();
        titleEl.textContent = item.label;
        contentEl.innerHTML = '<p class="rail-panel__loading">加载中…</p>';
        highlightButtons();
         document.body.classList.add('rail-open');
 
        if (item.html) {
            contentEl.innerHTML = item.html;
            return;
        }
 
        if (item.template) {
            var api = new mw.Api();
            api.get({
                action: 'parse',
                page: item.template,
                prop: 'text',
                formatversion: '2'
            }).done(function (data) {
                if (activeId !== item.id) return; // 用户切换了按钮
                if (data && data.parse && data.parse.text) {
                    contentEl.innerHTML = data.parse.text;
                } else {
                    contentEl.innerHTML = '<p>加载失败。</p>';
                }
             }).fail(function () {
                if (activeId !== item.id) return;
                contentEl.innerHTML = '<p>加载失败。</p>';
            });
        } else {
            contentEl.innerHTML = '<p>未配置内容。</p>';
        }
    }
 
    function close() {
        activeId = null;
        document.body.classList.remove('rail-open');
        if (buttonsEl) highlightButtons();
    }
 
    function init() {
        ensureDOM();
        buttonsEl.innerHTML = '';
        items.forEach(function (item) {
            buttonsEl.appendChild(createButton(item));
         });
         });
     }
     }


     if (document.readyState === 'loading') {
     return {
        document.addEventListener('DOMContentLoaded', initRail);
        register: function (id, options) {
    } else {
            options = options || {};
         initRail();
            items.push({
     }
                id: id,
                icon: options.icon || '●',
                label: options.label || id,
                html: options.html || null,
                template: options.template || null
            });
        },
        init: init,
        open: open,
         close: close
     };
})();
})();
/* ===== 在这里注册工具栏按钮 ===== */
Rail.register('navigation', {
    icon: '☰',
    label: '导航',
    template: 'Template:工具栏导航'
});
Rail.register('settings', {
    icon: '⚙',
    label: '设置',
    template: 'Template:工具栏设置'
});
Rail.register('help', {
    icon: '?',
    label: '帮助',
    template: 'Template:工具栏帮助'
});
/* ===== 初始化 ===== */
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () { Rail.init(); });
} else {
    Rail.init();
}
/* ===== 侧边工具栏 Rail 管理器 ===== */
/* ===== 侧边工具栏 Rail 管理器 ===== */


/* ===== 首页轮播图 ===== */
/* ===== 首页轮播图 ===== */

2026年9月27日 (日) 17:32的版本

/* ===== 侧边工具栏 Rail 管理器 ===== */
/* ===== 侧边工具栏 Rail 管理器 ===== */
window.Rail = (function () {
    var items = [];
    var activeId = null;
    var panelEl, overlayEl, buttonsEl, contentEl, titleEl;

    function ensureDOM() {
        if (document.getElementById('rail-container')) return;

        buttonsEl = document.createElement('div');
        buttonsEl.id = 'rail-container';
        buttonsEl.className = 'rail-container';

        overlayEl = document.createElement('div');
        overlayEl.id = 'rail-overlay';
        overlayEl.className = 'rail-overlay';

        panelEl = document.createElement('aside');
        panelEl.id = 'rail-panel';
        panelEl.className = 'rail-panel';
        panelEl.innerHTML = [
            '<div class="rail-panel__header">',
            '  <span class="rail-panel__title"></span>',
            '  <button class="rail-panel__close" type="button" aria-label="关闭">×</button>',
            '</div>',
            '<div class="rail-panel__content" id="rail-panel-content"></div>'
        ].join('');

        titleEl = panelEl.querySelector('.rail-panel__title');
        contentEl = panelEl.querySelector('#rail-panel-content');

        document.body.appendChild(overlayEl);
        document.body.appendChild(panelEl);
        document.body.appendChild(buttonsEl);

        panelEl.querySelector('.rail-panel__close').addEventListener('click', close);
        overlayEl.addEventListener('click', close);
        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') close();
        });
    }

    function createButton(item) {
        var btn = document.createElement('button');
        btn.type = 'button';
        btn.className = 'rail-button';
        btn.setAttribute('data-rail-id', item.id);
        btn.setAttribute('aria-label', item.label);
        btn.setAttribute('title', item.label);
        btn.innerHTML = '<span class="rail-button__icon">' + item.icon + '</span>';
        btn.addEventListener('click', function () {
            if (activeId === item.id) {
                close();
            } else {
                open(item);
            }
        });
        return btn;
    }

    function highlightButtons() {
        var all = buttonsEl.querySelectorAll('.rail-button');
        for (var i = 0; i < all.length; i++) {
            var id = all[i].getAttribute('data-rail-id');
            all[i].classList.toggle('is-active', id === activeId);
        }
    }

    function open(item) {
        activeId = item.id;
        titleEl.textContent = item.label;
        contentEl.innerHTML = '<p class="rail-panel__loading">加载中…</p>';
        highlightButtons();
        document.body.classList.add('rail-open');

        if (item.html) {
            contentEl.innerHTML = item.html;
            return;
        }

        if (item.template) {
            var api = new mw.Api();
            api.get({
                action: 'parse',
                page: item.template,
                prop: 'text',
                formatversion: '2'
            }).done(function (data) {
                if (activeId !== item.id) return; // 用户切换了按钮
                if (data && data.parse && data.parse.text) {
                    contentEl.innerHTML = data.parse.text;
                } else {
                    contentEl.innerHTML = '<p>加载失败。</p>';
                }
            }).fail(function () {
                if (activeId !== item.id) return;
                contentEl.innerHTML = '<p>加载失败。</p>';
            });
        } else {
            contentEl.innerHTML = '<p>未配置内容。</p>';
        }
    }

    function close() {
        activeId = null;
        document.body.classList.remove('rail-open');
        if (buttonsEl) highlightButtons();
    }

    function init() {
        ensureDOM();
        buttonsEl.innerHTML = '';
        items.forEach(function (item) {
            buttonsEl.appendChild(createButton(item));
        });
    }

    return {
        register: function (id, options) {
            options = options || {};
            items.push({
                id: id,
                icon: options.icon || '●',
                label: options.label || id,
                html: options.html || null,
                template: options.template || null
            });
        },
        init: init,
        open: open,
        close: close
    };
})();

/* ===== 在这里注册工具栏按钮 ===== */
Rail.register('navigation', {
    icon: '☰',
    label: '导航',
    template: 'Template:工具栏导航'
});

Rail.register('settings', {
    icon: '⚙',
    label: '设置',
    template: 'Template:工具栏设置'
});

Rail.register('help', {
    icon: '?',
    label: '帮助',
    template: 'Template:工具栏帮助'
});

/* ===== 初始化 ===== */
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () { Rail.init(); });
} else {
    Rail.init();
}
/* ===== 侧边工具栏 Rail 管理器 ===== */
/* ===== 侧边工具栏 Rail 管理器 ===== */



/* ===== 首页轮播图 ===== */
(function () {
    function initHomeCarousel() {
        var carousel = document.getElementById('home-carousel');
        if (!carousel) return;

        var items = carousel.querySelectorAll('.home-carousel-item');
        var dotsBox = document.getElementById('home-carousel-dots');
        var prevBtn = document.getElementById('home-carousel-prev');
        var nextBtn = document.getElementById('home-carousel-next');
        if (!items.length || !dotsBox || !prevBtn || !nextBtn) return;

        var current = 0;
        var timer = null;

        items.forEach(function (_, i) {
            var dot = document.createElement('span');
            dot.className = 'home-carousel-dot' + (i === 0 ? ' active' : '');
            dot.addEventListener('click', function () { goTo(i); });
            dotsBox.appendChild(dot);
        });
        var dots = dotsBox.querySelectorAll('.home-carousel-dot');

        function goTo(index) {
            items[current].classList.remove('active');
            dots[current].classList.remove('active');
            current = (index + items.length) % items.length;
            items[current].classList.add('active');
            dots[current].classList.add('active');
        }

        function next() { goTo(current + 1); }
        function prev() { goTo(current - 1); }

        function startAuto() {
            stopAuto();
            timer = setInterval(next, 5000);
        }
        function stopAuto() {
            if (timer) { clearInterval(timer); timer = null; }
        }

        nextBtn.addEventListener('click', function () { next(); startAuto(); });
        prevBtn.addEventListener('click', function () { prev(); startAuto(); });
        carousel.addEventListener('mouseenter', stopAuto);
        carousel.addEventListener('mouseleave', startAuto);

        startAuto();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initHomeCarousel);
    } else {
        initHomeCarousel();
    }
})();