Thành viên:Plantaest/TwinkleGlobalMobile.js

Bách khoa toàn thư mở Wikipedia

Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.

(function ($, mw, OO) {
    function buildDialog(twinkleModulePortletLinks) {
        function TwinkleGlobalMobileDialog(config) {
            TwinkleGlobalMobileDialog.super.call(this, config);
        }
        OO.inheritClass(TwinkleGlobalMobileDialog, OO.ui.ProcessDialog);

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

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

            TwinkleGlobalMobileDialog.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 TwinkleGlobalMobile dialog when Twinkle dialog is opened
                        if ($('.morebitsglobal-dialog').length !== 0) {
                            dialog.close();
                        }
                    });
                }

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

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

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

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

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

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

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

        var twgmbLink = mw.util.addPortletLink('p-personal', '#', 'TwinkleGlobalMobile', 'twgmb', 'TwinkleGlobalMobile');
        $(twgmbLink).click(function (e) {
            windowManager.openWindow('TwinkleGlobalMobileDialog');
            e.preventDefault();
        });
    }

    function main() {
        var twinkleModulePortletLinks = [];

        // Load CSS
        mw.loader.load('//vi.wikipedia.org/w/index.php?title=User:Plantaest/TwinkleGlobalMobile.css&action=raw&ctype=text/css', 'text/css');

        // Override Twinkle.addPortletLink function
        TwinkleGlobal.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'));
            MorebitsGlobal.pageNameNorm = mw.config.get('wgRelevantPageName').replace(/_/g, ' ');
            window.history.replaceState(null, null, '?diff=' + mw.config.get('wgDiffNewId'));
        }

        // Run TwinkleGlobal modules
        TwinkleGlobal.arv();
        TwinkleGlobal.diff();
        TwinkleGlobal.speedy();
        TwinkleGlobal.addPortletLink(TwinkleGlobal.getPref('configPage'), 'Pref', 'twg-config', 'Set Twinkle preferences');

        buildDialog(twinkleModulePortletLinks);
    }

    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 {
            throw new Error('TwinkleGlobalMobile only works with Minerva skin and autoconfirmed users');
        }
    })
    .then(function () {
    	setTimeout(main, 1000);
    })
    .catch(function (e) {
        mw.log.error(e.message);
    });
})(jQuery, mediaWiki, OO);