MediaWiki:Common.js: различия между версиями

мНет описания правки
м Отмена версии 82389, сделанной Pok (обсуждение)
Метка: отмена
Строка 1: Строка 1:
;(function($, mw, rs){
;(function($, mw, rs){
    // Only run on specific page
if (mw.config.get('wgPageName') !== 'Обсуждение_участника:Pok') return;
    if (mw.config.get('wgPageName') !== 'Обсуждение_участника:Pok') return;
var DARK_COOKIE = 'darkmode',
STICKY_HEADER_COOKIE = 'stickyheader',
THEME_COOKIE = 'theme',
fixedWidthEnabled = $.cookie('readermode') === 'true',
theme = $.cookie(THEME_COOKIE) || ($.cookie(DARK_COOKIE) === 'true' ? 'dark' : 'light'),
stickySwitch,
closeButton,
settingsPortletLink,
themePopup;


    var DARK_COOKIE = 'darkmode',
var self = {
        STICKY_HEADER_COOKIE = 'stickyheader',
init: function () {
        THEME_COOKIE = 'theme',
self.createThemeButton();
        fixedWidthEnabled = $.cookie('readermode') === 'true',
self.createSettingsButton();
        theme = $.cookie(THEME_COOKIE) || ($.cookie(DARK_COOKIE) === 'true' ? 'dark' : 'light'),
self.createFixedWidthToggle();
        themePopup;
if ( $.cookie(THEME_COOKIE) == null ) {
$.cookie(THEME_COOKIE, theme, {expires: 365, path: '/'});
}
},


    var self = {
createThemeButton: function() {
        init: function () {
// Add the theme toggle button inside #user-tools
            self.createThemeButton();
themePortletLink = mw.util.addPortletLink(
            self.createSettingsButton();
'user-tools',
            self.createFixedWidthToggle();
'',
            // Initialize theme cookie if absent
'pt-theme-toggle',
            if (!$.cookie(THEME_COOKIE)) {
'Change theme',
                $.cookie(THEME_COOKIE, theme, { expires: 365, path: '/' });
null,
            }
'#user-tools'
        },
);
$(themePortletLink).find('a')
.addClass('oo-ui-icon-advanced')
.on('click', function(e) {
e.preventDefault();
if (!themePopup) {
mw.loader.using(['oojs-ui-core','oojs-ui-windows','oojs-ui-widgets']).then(self.buildThemePopup);
} else {
themePopup.toggle();
}
});
},


        /**
buildThemePopup: function() {
        * Create a theme toggle button inside #user-tools
var themeSwitch = new OO.ui.ButtonSelectWidget({
        */
items: [
        createThemeButton: function() {
new OO.ui.ButtonOptionWidget({data: 'light', label: 'Light'}),
            var $container = $('#user-tools');
new OO.ui.ButtonOptionWidget({data: 'dark', label: 'Dark'})
            if (!$container.length) return;
]
});
themeSwitch.selectItemByData(theme);
themeSwitch.on('choose', function() {
theme = themeSwitch.findSelectedItem().getData();
$.cookie(THEME_COOKIE, theme, {expires: 365, path: '/'});
self.applyTheme(theme);
});


            var $button = $('<a>', {
themePopup = new OO.ui.PopupWidget({
                href: '#',
$content: themeSwitch.$element,
                id: 'pt-theme-toggle',
$floatableContainer: $(themePortletLink),
                title: 'Change theme'
autoClose: true
            }).addClass('oo-ui-icon-advanced')
});
              .on('click', function(e) {
$(document.body).append(themePopup.$element);
                  e.preventDefault();
themePopup.toggle(true);
                  if (!themePopup) {
},
                      mw.loader.using(['oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets']).then(self.buildThemePopup);
                  } else {
                      themePopup.toggle();
                  }
              });


            $container.append($button);
applyTheme: function(name) {
        },
$('body').removeClass(function(i, cls) {
return (cls.match(/wgl-theme-\S+/g) || []).join(' ');
});
if (name !== 'light') {
mw.loader.using(['wgl.theme.' + name]).then(function() {
$('body').addClass('wgl-theme-' + name);
});
}
},


        /**
toggleFixedWidth: function () {
        * Build the theme selection popup
fixedWidthEnabled = !fixedWidthEnabled;
        */
if (fixedWidthEnabled) {
        buildThemePopup: function() {
$('body').addClass('wgl-fixedWidth');
            var themeSwitch = new OO.ui.ButtonSelectWidget({
mw.loader.load('wg.fixedwidth');
                items: [
} else {
                    new OO.ui.ButtonOptionWidget({ data: 'light', label: 'Light' }),
$('body').removeClass('wgl-fixedWidth');
                    new OO.ui.ButtonOptionWidget({ data: 'dark', label: 'Dark' })
}
                ]
$.cookie('readermode', fixedWidthEnabled, {expires: 365, path: '/'});
            });
},
            themeSwitch.selectItemByData(theme);
            themeSwitch.on('choose', function() {
                theme = themeSwitch.findSelectedItem().getData();
                $.cookie(THEME_COOKIE, theme, { expires: 365, path: '/' });
                self.applyTheme(theme);
            });


            themePopup = new OO.ui.PopupWidget({
createFixedWidthToggle: function() {
                $content: themeSwitch.$element,
var link = mw.util.addPortletLink('p-personal', '', '', 'pt-fixed-width', 'Toggle fixed-width mode');
                $floatableContainer: $('#pt-theme-toggle'),
$(link).find('a').on('click', function(e) {
                autoClose: true
e.preventDefault();
            });
self.toggleFixedWidth();
            $(document.body).append(themePopup.$element);
});
            themePopup.toggle(true);
},
        },


        /**
createSettingsButton: function() {
        * Apply the selected theme class to <body>
settingsPortletLink = mw.util.addPortletLink('p-personal', '', '', 'pt-settings', 'Appearance settings');
        */
$(settingsPortletLink).find('a').on('click', function(e) {
        applyTheme: function(name) {
e.preventDefault();
            $('body').removeClass(function(i, cls) {
mw.loader.using(['oojs-ui-core','oojs-ui-windows','oojs-ui-widgets']).then(self.openSettingsModal);
                return (cls.match(/wgl-theme-\S+/g) || []).join(' ');
});
            });
},
            if (name !== 'light') {
                mw.loader.using(['wgl.theme.' + name]).then(function() {
                    $('body').addClass('wgl-theme-' + name);
                });
            }
        },


        /**
openSettingsModal: function() {
        * Toggle fixed-width mode
var stickySwitch = new OO.ui.ToggleSwitchWidget({value: $.cookie(STICKY_HEADER_COOKIE)==='true'});
        */
stickySwitch.on('change', function() {
        toggleFixedWidth: function () {
$.cookie(STICKY_HEADER_COOKIE, stickySwitch.getValue(), {expires:365, path:'/'});
            fixedWidthEnabled = !fixedWidthEnabled;
});
            if (fixedWidthEnabled) {
                $('body').addClass('wgl-fixedWidth');
                mw.loader.load('wg.fixedwidth');
            } else {
                $('body').removeClass('wgl-fixedWidth');
            }
            $.cookie('readermode', fixedWidthEnabled, { expires: 365, path: '/' });
        },


        /**
var closeBtn = new OO.ui.ButtonInputWidget({label: 'Close', flags: 'destructive'});
        * Create toggle link for fixed-width in personal portlet
        */
var $content = $('<div>').append(
        createFixedWidthToggle: function() {
$('<h3>').text('Sticky header'),
            var link = mw.util.addPortletLink('p-personal', '#', '', 'pt-fixed-width', 'Toggle fixed-width mode');
stickySwitch.$element,
            $(link).on('click', function(e) {
closeBtn.$element
                e.preventDefault();
);
                self.toggleFixedWidth();
            });
        },


        /**
rs.createOOUIWindow('settings', 'Appearance settings', {size:'medium'}, function(modal){
        * Create Appearance Settings button in personal portlet
modal.$body.append($content);
        */
closeBtn.on('click', function(){ window.OOUIWindowManager.closeWindow(modal); });
        createSettingsButton: function() {
}, true);
            var link = mw.util.addPortletLink('p-personal', '#', '', 'pt-settings', 'Appearance settings');
}
            $(link).on('click', function(e) {
};
                e.preventDefault();
                mw.loader.using(['oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets']).then(self.openSettingsModal);
            });
        },


        /**
mw.loader.using(['ext.gadget.rsw-util'], function(){
        * Open modal for appearance settings (sticky header)
$(self.init);
        */
});
        openSettingsModal: function() {
            var stickySwitch = new OO.ui.ToggleSwitchWidget({ value: $.cookie(STICKY_HEADER_COOKIE) === 'true' });
            stickySwitch.on('change', function() {
                $.cookie(STICKY_HEADER_COOKIE, stickySwitch.getValue(), { expires: 365, path: '/' });
            });
 
            var closeBtn = new OO.ui.ButtonInputWidget({ label: 'Close', flags: 'destructive' });
            var $content = $('<div>').append(
                $('<h3>').text('Sticky header'),
                stickySwitch.$element,
                closeBtn.$element
            );
 
            rs.createOOUIWindow('settings', 'Appearance settings', { size: 'medium' }, function(modal) {
                modal.$body.append($content);
                closeBtn.on('click', function() { window.OOUIWindowManager.closeWindow(modal); });
            }, true);
        }
    };
 
    // Initialize on DOM ready
    mw.loader.using(['ext.gadget.rsw-util'], function(){ $(self.init); });


}(jQuery, mediaWiki, rswiki));
}(jQuery, mediaWiki, rswiki));