跳转到内容

英文维基 | 中文维基 | 日文维基 | 草榴社区

User:桐生ここ/js/Gadget-TwinkleMobile.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
var require =  mw.loader.require; //修复HanAssist产生的问题

(function ($, mw, OO) {
if (mw.config.get( 'wgMFAmc' ) === true){

    var userLanguage = mw.config.get('wgUserLanguage'),
        translations = {
            default: {
                title: 'TwinkleMobile',
                nothing: '无可执行操作'
            },
            en: {
                title: 'TwinkleMobile',
                nothing: 'Nothing'
            }
        };

    function i18n(key) {
        return (translations[userLanguage] || translations.default)[key];
    }

    function loadCSS(resources) {
        resources.forEach(function (resource) {
            mw.loader.load('/w/index.php?title=MediaWiki:Gadget-' + resource + '&action=raw&ctype=text/css', 'text/css');
        });
    }

    function createBasketItems(resources) {
        var basketItems = [];
        resources.forEach(function (resource) {
            basketItems.push({
                url: '/w/index.php?title=MediaWiki:Gadget-' + resource + '&action=raw&ctype=text/javascript',
                key: resource,
                expire: 168 // Cache for 7 days
            });
        });
        return basketItems;
    }

    function utils() {
        // Don't zoom-in when focusing input
        $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>');
        // CSS for better Twinkle dialog on mobile
        mw.loader.load('//zh-two.iwiki.icu/w/index.php?title=User:桐生ここ/js/Gadget-TwinkleMobile.css&action=raw&ctype=text/css', 'text/css');
    }

    function main() {
        var twinkleDependencies = ['morebits.js', 'select2.min.js', 'Twinkle.js'],
            twinkleModules = [
                'twinkleconfig.js',
                'twinkleprod.js',
                'twinkleimage.js',
                'twinklewarn.js',
                'twinkleblock.js',
                'twinklespeedy.js',
                'friendlyshared.js',
                'twinklediff.js',
                'twinkleunlink.js',
                'friendlytag.js',
                'twinkledeprod.js',
                'friendlywelcome.js',
                'twinklexfd.js',
                'twinklebatchdelete.js',
                'twinklebatchundelete.js',
                'twinklebatchprotect.js',
                'twinkleprotect.js',
                'twinklearv.js',
                'friendlytalkback.js'
            ],
            twinkleModulePortletLinks = [];

        utils();
        loadCSS(['morebits.css', 'select2.min.css']);

        basket.require.apply(basket, createBasketItems(twinkleDependencies))
        .then(function () {
            // Override Twinkle.addPortletLink function
            Twinkle.addPortletLink = function (task, text, id, tooltip) {
                twinkleModulePortletLinks.push({
                    task: task,
                    text: text,
                    id: id,
                    tooltip: tooltip
                });
            };

            // Fix bug on Minerva (it doesn't have this)
            mw.config.set('wgCategories', []);

            // Fix bug on Special:MobileDiff pages
            if (mw.config.get('wgCanonicalSpecialPageName') === 'MobileDiff') {
                mw.config.set('wgNamespaceNumber', mw.Title.newFromText(mw.config.get('wgRelevantPageName')).getNamespaceId());
                mw.config.set('wgArticleId', mw.config.get('wgRelevantArticleId'));
                mw.config.set('wgPageName', mw.config.get('wgRelevantPageName'));
                mw.config.set('wgCurRevisionId', mw.config.get('wgDiffNewId'));
                Morebits.pageNameNorm = mw.config.get('wgRelevantPageName').replace(/_/g, ' ');
                window.history.replaceState(null, null, '?diff=' + mw.config.get('wgDiffNewId'));
            }

            return basket.require.apply(basket, createBasketItems(twinkleModules));
        })
        .then(function () {
            setTimeout(function () {
                // Create TwinkleMobile portlet link and dialog
                function TwinkleMobileDialog(config) {
                    TwinkleMobileDialog.super.call(this, config);
                }
                OO.inheritClass(TwinkleMobileDialog, OO.ui.ProcessDialog);

                TwinkleMobileDialog.static.name = 'TwinkleMobileDialog';
                TwinkleMobileDialog.static.title = i18n('title');
                TwinkleMobileDialog.static.actions = [
                    {
                        action: 'save',
                        label: mw.message('ooui-popup-widget-close-button-aria-label').text(),
                        flags: ['primary', 'progressive']
                    }
                ];

                TwinkleMobileDialog.prototype.initialize = function () {
                    var dialog = this;

                    TwinkleMobileDialog.super.prototype.initialize.apply(this, arguments);
                    this.panel = new OO.ui.PanelLayout({
                        padded: true,
                        expanded: false
                    });
                    this.content = new OO.ui.FieldsetLayout();

                    var fields = [];
                    twinkleModulePortletLinks.forEach(function (link) {
                        var button;

                        if (typeof link.task === 'string') {
                            button = new OO.ui.ButtonWidget({
                                href: link.task,
                                label: link.text,
                                id: link.id,
                                title: link.tooltip
                            });
                        }

                        if (typeof link.task === 'function') {
                            button = new OO.ui.ButtonWidget({
                                label: link.text,
                                id: link.id,
                                title: link.tooltip
                            });
                            button.on('click', function (e) {
                                link.task();
                                // Only close TwinkleMobile dialog when Twinkle dialog is opened
                                if ($('.morebits-dialog').length !== 0) {
                                    dialog.close();
                                }
                            });
                        }

                        fields.push(new OO.ui.FieldLayout(button));
                    });

                    if (fields.length === 0) {
                        fields.push(new OO.ui.LabelWidget({
                            label: i18n('nothing')
                        }));
                    }

                    this.content.addItems(fields);
                    this.panel.$element.append(this.content.$element);
                    this.$body.append(this.panel.$element);
                };

                TwinkleMobileDialog.prototype.getActionProcess = function (action) {
                    var dialog = this;
                    if (action) {
                        return new OO.ui.Process(function () {
                            dialog.close({
                                action: action
                            });
                        });
                    }
                    return TwinkleMobileDialog.super.prototype.getActionProcess.call(this, action);
                };

                TwinkleMobileDialog.prototype.getBodyHeight = function () {
                    return this.panel.$element.outerHeight(true);
                };

                var windowManager = new OO.ui.WindowManager();
                $(document.body).append(windowManager.$element);

                windowManager.addWindows([new TwinkleMobileDialog({
                    size: 'small'
                })]);

                var twmbLink = mw.util.addPortletLink('p-navigation', '#', i18n('title'), 'twmb', i18n('title'));
                $(twmbLink).click(function (e) {
                    windowManager.openWindow('TwinkleMobileDialog');
                    e.preventDefault();
                });
            }, 1000); // Execute this after all Twinkle modules is executed
        })
        .catch(function (e) {
            mw.log.error(e.message);
        });
    }

    mw.loader.using([
        'mediawiki.user', 'mediawiki.util', 'mediawiki.Title',
        'mediawiki.api', 'mediawiki.language', 'jquery.ui',
        'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows'
    ])
    .then(function () {
        if (mw.config.get('wgUserGroups').includes('autoconfirmed')
            && mw.config.get('skin') === 'minerva') {
            return new mw.Api().loadMessagesIfMissing([
                'ooui-popup-widget-close-button-aria-label'
            ]);
        } else {
            console.log('TwinkleMobile只能在Minerva皮肤上使用,您需要具有自动确认用户权限。');
        }
    })
    .then(function () {
        return mw.loader.getScript('https://tools-static.wmflabs.org/cdnjs/ajax/libs/basket.js/0.5.2/basket.full.min.js');
    })
    .then(main)
    .catch(function (e) {
        mw.log.error(e.message);
    });
}
})(jQuery, mediaWiki, OO);