MediaWiki:Common.js:修订间差异

来自广州交通维基 - Canton Trans Wiki
跳转到导航 跳转到搜索
Zhbus留言 | 贡献
无编辑摘要
Zhbus留言 | 贡献
无编辑摘要
第86行: 第86行:
// 102. 功能:封面画册JS代码
// 102. 功能:封面画册JS代码


// 简单外部图片淡入轮播(支持多组)
/* 外部图片淡入轮播 - 修复版(MediaWiki 1.39+ 兼容) */
document.addEventListener('DOMContentLoaded', function() {
/* 使用 jQuery + mw.hook('wikipage.content') 确保内容就绪 */
     document.querySelectorAll('.simple-fade-carousel').forEach(carousel => {
/* 支持动态内容(Parsoid/预览)、多实例、ES5兼容 */
 
(function($, mw) {
     'use strict';
 
    // 核心函数:初始化单个轮播
    function initCarousel(carousel) {
         const slides = carousel.querySelectorAll('.slide');
         const slides = carousel.querySelectorAll('.slide');
        if (slides.length === 0) return;  // 无slides跳过
         const prevBtn = carousel.querySelector('.prev');
         const prevBtn = carousel.querySelector('.prev');
         const nextBtn = carousel.querySelector('.next');
         const nextBtn = carousel.querySelector('.next');
第96行: 第104行:


         function showSlide(index) {
         function showSlide(index) {
             slides.forEach((s, i) => {
             slides.forEach((s, i) => s.classList.toggle('active', i === index));
                s.classList.toggle('active', i === index);
            });
             current = index;
             current = index;
         }
         }


         function nextSlide() {
         function nextSlide() {
             let next = (current + 1) % slides.length;
             showSlide((current + 1) % slides.length);
             showSlide(next);
        }
 
        function prevSlide() {
             showSlide((current - 1 + slides.length) % slides.length);
         }
         }


        // 自动播放(每5秒切换)
         function startAuto() {
         function startAuto() {
             interval = setInterval(nextSlide, 5000);
             interval = setInterval(nextSlide, 5000);
第116行: 第124行:
         }
         }


         // 按钮事件
         // 按钮事件(防重复绑定)
         if (prevBtn) prevBtn.addEventListener('click', () => {
         function bindButtons() {
            stopAuto();
            if (prevBtn) {
             let prev = (current - 1 + slides.length) % slides.length;
                prevBtn.onclick = function(e) {
            showSlide(prev);
                    e.preventDefault();
            startAuto();
                    stopAuto();
         });
                    prevSlide();
                    setTimeout(startAuto, 1000);  // 1s后重启自动
                };
            }
             if (nextBtn) {
                nextBtn.onclick = function(e) {
                    e.preventDefault();
                    stopAuto();
                    nextSlide();
                    setTimeout(startAuto, 1000);
                };
            }
         }


         if (nextBtn) nextBtn.addEventListener('click', () => {
         // 鼠标事件
            stopAuto();
        carousel.onmouseenter = stopAuto;
            nextSlide();
        carousel.onmouseleave = startAuto;
            startAuto();
        });


         // 鼠标悬停暂停(可选)
         // 键盘支持(可选)
         carousel.addEventListener('mouseenter', stopAuto);
         carousel.onkeydown = function(e) {
        carousel.addEventListener('mouseleave', startAuto);
            if (e.key === 'ArrowLeft') prevSlide();
            if (e.key === 'ArrowRight') nextSlide();
            if (e.key === ' ') { e.preventDefault(); nextSlide(); }
        };
        carousel.tabIndex = 0; // 焦点支持


         // 启动
         bindButtons();
         showSlide(0);
         showSlide(0);
         startAuto();
         startAuto();
     });
     }
});
 
    // 绑定所有轮播(MutationObserver 捕获动态添加)
    function bindAllCarousels() {
        const selector = '.simple-fade-carousel';
        let carousels = document.querySelectorAll(selector);
        carousels.forEach(initCarousel);
 
        // 监听动态内容(预览/Parsoid/AJAX)
        const observer = new MutationObserver(function(mutations) {
            let added = false;
            mutations.forEach(function(mutation) {
                mutation.addedNodes.forEach(function(node) {
                    if (node.nodeType === 1 && (node.matches && node.matches(selector) || node.querySelector(selector))) {
                        added = true;
                    }
                });
            });
            if (added) {
                // 延迟100ms再绑(图片加载)
                setTimeout(bindAllCarousels, 100);
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }
 
    // MediaWiki Hook:内容就绪后执行(防ResourceLoader延迟)
    mw.hook('wikipage.content').add(bindAllCarousels);
    $(document).ready(bindAllCarousels);  // 双重保险(jQuery ready)
 
    // Fallback:window.load(图片全载后)
    $(window).on('load', bindAllCarousels);
 
})(window.jQuery, mediaWiki);  // MediaWiki jQuery + mw 全局

2025年12月25日 (四) 22:41的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/*加载ToolsWIKI的DiffTool 以逐字显示更改,0缓存,具体可见https://wiki.biligame.com/tools/%E4%BC%98%E5%8C%96%E7%89%88%E6%9C%AC%E5%B7%AE%E5%BC%82-DiffTools */
try { mw.loader.load("https://wiki.biligame.com/tools/MediaWiki:DiffTool.js?action=raw&ctype=text/javascript&rand="+Math.random(), "text/javascript");} catch(e) { console.log("Error, MediaWiki:DiffTool.js 加载失败");console.error(e, e.stack);}
/* 参见[[模板:ResourceLoader]]*/

//居中与右对齐,文本默认为左对齐,不用另外添加此按钮。(仅适用于源代码编辑模式)
/*居中*/
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
    // Add a hook handler.
    mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
        // Configure a new toolbar entry on the given $textarea jQuery object.
        $textarea.wikiEditor( 'addToToolbar', {
            section: 'advanced',
            group: 'format',
            tools: {
                AlignCenter: {
                    label: '居中',
                    type: 'button',
                    oouiIcon: 'alignCenter',
                    action: {
                        type: 'encapsulate',
                        options: {
                            pre: "<center>",
                            post: "</center>"
                        }
                    }
                }
            }
        } );
    } );
}

/*右对齐*/
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
    // Add a hook handler.
    mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
        // Configure a new toolbar entry on the given $textarea jQuery object.
        $textarea.wikiEditor( 'addToToolbar', {
            section: 'advanced',
            group: 'format',
            tools: {
                AlignCenter: {
                    label: '右对齐',
                    type: 'button',
                    oouiIcon: 'alignRight',
                    action: {
                        type: 'encapsulate',
                        options: {
                            pre: "<div align=right>",
                            post: "</div>"
                        }
                    }
                }
            }
        } );
    } );
}

// 101. 功能:快捷编辑理由。支持编辑摘要时,点击快捷选项填充编辑理由。(仅适用于源代码编辑模式)
//参考Wikipedia:MediaWiki:Common.js/edit.js
if (mw.config.get('wgAction') == "edit" || mw.config.get('wgAction') == "submit" || mw.config.get('wgCanonicalSpecialPageName') == 'Search') { // 编辑页面
  
( function( $, mw ) { $( function() {
    if ( $( '#editform input[name=wpSection]' ).val() === 'new' ) {
        if ( $( '#no-new-title' ).length ) {
            $( '#wpSummary' ).attr( 'disabled', true );
        }
        return;
    }
    $( '#wpSummaryLabel .mw-summary-preset' ).on( 'click', '.mw-summary-preset-item a', function( e ) {
        e.preventDefault();
        var $this = $( this ), summary = $( '#wpSummary' ).val();
        var $item = $this.parent( '.mw-summary-preset-item' );
        summary = summary.replace( /\s+$/g, '' );
        if ( summary != '' ) {
            summary += ' ';
        }
        summary += $item.attr( 'title' ) || $this.text();
        $this.replaceWith( $this.contents() );
        $( '#wpSummary' ).val( summary ).focus();
    } );
} ); } )( jQuery, mediaWiki );

}

// 102. 功能:封面画册JS代码

/* 外部图片淡入轮播 - 修复版(MediaWiki 1.39+ 兼容) */
/* 使用 jQuery + mw.hook('wikipage.content') 确保内容就绪 */
/* 支持动态内容(Parsoid/预览)、多实例、ES5兼容 */

(function($, mw) {
    'use strict';

    // 核心函数:初始化单个轮播
    function initCarousel(carousel) {
        const slides = carousel.querySelectorAll('.slide');
        if (slides.length === 0) return;  // 无slides跳过

        const prevBtn = carousel.querySelector('.prev');
        const nextBtn = carousel.querySelector('.next');
        let current = 0;
        let interval;

        function showSlide(index) {
            slides.forEach((s, i) => s.classList.toggle('active', i === index));
            current = index;
        }

        function nextSlide() {
            showSlide((current + 1) % slides.length);
        }

        function prevSlide() {
            showSlide((current - 1 + slides.length) % slides.length);
        }

        function startAuto() {
            interval = setInterval(nextSlide, 5000);
        }

        function stopAuto() {
            clearInterval(interval);
        }

        // 按钮事件(防重复绑定)
        function bindButtons() {
            if (prevBtn) {
                prevBtn.onclick = function(e) {
                    e.preventDefault();
                    stopAuto();
                    prevSlide();
                    setTimeout(startAuto, 1000);  // 1s后重启自动
                };
            }
            if (nextBtn) {
                nextBtn.onclick = function(e) {
                    e.preventDefault();
                    stopAuto();
                    nextSlide();
                    setTimeout(startAuto, 1000);
                };
            }
        }

        // 鼠标事件
        carousel.onmouseenter = stopAuto;
        carousel.onmouseleave = startAuto;

        // 键盘支持(可选)
        carousel.onkeydown = function(e) {
            if (e.key === 'ArrowLeft') prevSlide();
            if (e.key === 'ArrowRight') nextSlide();
            if (e.key === ' ') { e.preventDefault(); nextSlide(); }
        };
        carousel.tabIndex = 0;  // 焦点支持

        bindButtons();
        showSlide(0);
        startAuto();
    }

    // 绑定所有轮播(MutationObserver 捕获动态添加)
    function bindAllCarousels() {
        const selector = '.simple-fade-carousel';
        let carousels = document.querySelectorAll(selector);
        carousels.forEach(initCarousel);

        // 监听动态内容(预览/Parsoid/AJAX)
        const observer = new MutationObserver(function(mutations) {
            let added = false;
            mutations.forEach(function(mutation) {
                mutation.addedNodes.forEach(function(node) {
                    if (node.nodeType === 1 && (node.matches && node.matches(selector) || node.querySelector(selector))) {
                        added = true;
                    }
                });
            });
            if (added) {
                // 延迟100ms再绑(图片加载)
                setTimeout(bindAllCarousels, 100);
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    // MediaWiki Hook:内容就绪后执行(防ResourceLoader延迟)
    mw.hook('wikipage.content').add(bindAllCarousels);
    $(document).ready(bindAllCarousels);  // 双重保险(jQuery ready)

    // Fallback:window.load(图片全载后)
    $(window).on('load', bindAllCarousels);

})(window.jQuery, mediaWiki);  // MediaWiki jQuery + mw 全局