跳转到内容

MediaWiki:Common.js:修订间差异

来自电棍ottowiki
第132行: 第132行:
     // 设置 loading 图像
     // 设置 loading 图像
     var loadingImgUrl = 'https://wiki.ottohub.cn/images/0/02/Loading.png'; // 替换路径为你上传后的图片地址
     var loadingImgUrl = 'https://wiki.ottohub.cn/images/0/02/Loading.png'; // 替换路径为你上传后的图片地址
     // 创建 loading 图标容器
     // ✅ 构建并添加 loading 图标
     var $loadingDiv = $('<div>', {
     var $loadingDiv = $('<div>', {
         id: 'loadingIndicator',
         id: 'loadingIndicator',
第144行: 第144行:
             'pointer-events': 'none'
             'pointer-events': 'none'
         }
         }
     });
     }).append(
 
        $('<img>', {
    // 图片元素
            src: loadingImgUrl,
    var $img = $('<img>', {
            alt: 'Loading...',
        src: loadingImgUrl,
            css: {
        alt: 'Loading...',
                width: '100%',
        css: {
                height: '100%'
            width: '100%',
            }
            height: '100%'
         })
         }
     );
     });


    // 添加到页面
    $loadingDiv.append($img);
     $('body').append($loadingDiv);
     $('body').append($loadingDiv);


     // 保证 loading 能在页面资源全部加载后移除
     // ✅ 移除图标的方法(可任意调用)
     var removeLoading = function () {
     var removeLoading = function () {
         var $indicator = $('#loadingIndicator');
         $('#loadingIndicator').stop(true, true).fadeOut(300, function () {
        if ($indicator.length) {
            $(this).remove();
            $indicator.fadeOut(300, function () {
            console.log('[Common.js] Loading removed');
                $(this).remove();
         });
                console.log('[Common.js] Loading icon removed.');
            });
         } else {
            console.warn('[Common.js] Loading icon not found.');
        }
     };
     };


     // 调用移除函数
     // ✅ 方式1: DOM 内容构建完成后尝试关闭(即使资源未加载完)
     if (document.readyState === 'complete') {
     document.addEventListener('DOMContentLoaded', function () {
         // 页面可能已经加载完
         console.log('[Common.js] DOMContentLoaded - removing loading');
         removeLoading();
         removeLoading();
     } else {
     });
        // 正常监听
        $(window).on('load', removeLoading);


         // safeguard(强制移除)– 10秒后再删一次
    // ✅ 方式2: window.onload 仍然保留(防止覆盖加载过程)
         setTimeout(removeLoading, 10000);
    window.addEventListener('load', function () {
     }
         console.log('[Common.js] Window loaded - removing loading');
        removeLoading();
    });
 
    // ✅ 方式3: 加一个兜底:最多显示 10 秒
    setTimeout(function () {
         console.log('[Common.js] Timeout fallback - forcing loading removal');
        removeLoading();
     }, 10000);
});
});

2025年5月11日 (日) 00:10的版本

//上传重定向
mw.loader.using(['mediawiki.util', 'oojs-ui', 'mediawiki.storage'], function () {
    if (mw.config.get('wgCanonicalSpecialPageName') === 'Upload') {

        const STORAGE_KEY = 'uploadPreference';

        const savedChoice = mw.storage.get(STORAGE_KEY);

        // 用户之前选择了上传向导,自动跳转
        if (savedChoice === 'wizard') {
            window.location.href = mw.util.getUrl('Special:UploadWizard');
            return;
        }

        // 用户选择了继续使用传统上传,不再提示
        if (savedChoice === 'classic') {
            return;
        }

        // 否则弹出选择窗口
        var windowManager = new OO.ui.WindowManager();
        $(document.body).append(windowManager.$element);

        function UploadDialog(config) {
            UploadDialog.super.call(this, config);
        }

        OO.inheritClass(UploadDialog, OO.ui.ProcessDialog);

        UploadDialog.static.name = 'UploadDialog';
        UploadDialog.static.title = '请选择上传方式';
        UploadDialog.static.actions = [
            {
                action: 'classic',
                label: '❌ 使用传统上传方式(更快)',
                flags: ['safe']
            }
        ];

        UploadDialog.prototype.initialize = function () {
            UploadDialog.super.prototype.initialize.call(this);

            var panel = new OO.ui.PanelLayout({ padded: true, expanded: false });

            // 创建“记住我的选择”复选框
            var rememberCheckbox = new OO.ui.CheckboxInputWidget({ selected: false });
            this._rememberCheckbox = rememberCheckbox;

            var rememberField = new OO.ui.FieldLayout(rememberCheckbox, {
                label: '记住我的选择,下次不再提示',
                align: 'inline'
            });

            // 创建“上传向导”按钮(跳转链接形式)
            var wizardButton = new OO.ui.ButtonWidget({
                label: '✅ 使用上传向导(推荐)',
                flags: ['primary', 'progressive'],
                href: mw.util.getUrl('Special:UploadWizard'),
                target: '_self',
                framed: true
            });

            // 给按钮添加“记住选择”逻辑
            wizardButton.on('click', () => {
                if (rememberCheckbox.isSelected()) {
                    mw.storage.set(STORAGE_KEY, 'wizard');
                }
            });

            // 拼接内容
            panel.$element.append(
                $('<p>').text('请选择你要使用的上传方式:'),

                $('<div>').css({
                    'margin': '12px 0',
                    'background': '#f8f9fa',
                    'padding': '10px',
                    'border': '1px solid #ccc',
                    'border-radius': '5px'
                }).append(
                    $('<strong>').text('✅ 上传向导(推荐)'),
                    $('<ul>').append(
                        $('<li>').text('支持多文件批量上传'),
                        $('<li>').text('显示上传进度条'),
                        $('<li>').text('信息填写更完整')
                    ),
                    $('<div>').css('margin-top', '8px').append(wizardButton.$element)
                ),

                $('<div>').css({
                    'margin-top': '15px',
                    'background': '#ffffff',
                    'padding': '10px',
                    'border': '1px dashed #bbb',
                    'border-radius': '5px'
                }).append(
                    $('<strong>').text('⚡️ 传统上传方式'),
                    $('<ul>').append(
                        $('<li>').text('界面更简单'),
                        $('<li>').text('加载速度快'),
                        $('<li>').text('适合上传单个文件')
                    )
                ),

                $('<div>').css('margin-top', '15px').append(rememberField.$element)
            );

            this.$body.append(panel.$element);
        };

        UploadDialog.prototype.getActionProcess = function (action) {
            if (action === 'classic') {
                if (this._rememberCheckbox.isSelected()) {
                    mw.storage.set(STORAGE_KEY, 'classic');
                }
                return new OO.ui.Process(() => this.close());
            }
            return UploadDialog.super.prototype.getActionProcess.call(this, action);
        };

        var dialog = new UploadDialog();
        windowManager.addWindows([dialog]);
        windowManager.openWindow(dialog);
    }
});
//---
// 显示右下角 loading 图标
$(function () {
    // 如果已加载,不再添加 loading
    if (window.__loadingIndicatorInit) return;
    window.__loadingIndicatorInit = true;
    // 设置 loading 图像
    var loadingImgUrl = 'https://wiki.ottohub.cn/images/0/02/Loading.png'; // 替换路径为你上传后的图片地址
    // ✅ 构建并添加 loading 图标
    var $loadingDiv = $('<div>', {
        id: 'loadingIndicator',
        css: {
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            width: '48px',
            height: '48px',
            'z-index': 9999,
            'pointer-events': 'none'
        }
    }).append(
        $('<img>', {
            src: loadingImgUrl,
            alt: 'Loading...',
            css: {
                width: '100%',
                height: '100%'
            }
        })
    );

    $('body').append($loadingDiv);

    // ✅ 移除图标的方法(可任意调用)
    var removeLoading = function () {
        $('#loadingIndicator').stop(true, true).fadeOut(300, function () {
            $(this).remove();
            console.log('[Common.js] ✅ Loading removed');
        });
    };

    // ✅ 方式1: DOM 内容构建完成后尝试关闭(即使资源未加载完)
    document.addEventListener('DOMContentLoaded', function () {
        console.log('[Common.js] DOMContentLoaded - removing loading');
        removeLoading();
    });

    // ✅ 方式2: window.onload 仍然保留(防止覆盖加载过程)
    window.addEventListener('load', function () {
        console.log('[Common.js] Window loaded - removing loading');
        removeLoading();
    });

    // ✅ 方式3: 加一个兜底:最多显示 10 秒
    setTimeout(function () {
        console.log('[Common.js] Timeout fallback - forcing loading removal');
        removeLoading();
    }, 10000);
});