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

мНет описания правки
мНет описания правки
Строка 1: Строка 1:
;(function($, mw){
;(function($, mw){
    if (mw.config.get('wgPageName') !== 'Обсуждение_участника:Pok') return;
var DARK_COOKIE = 'darkmode',
THEME_COOKIE = 'theme',
isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
isMobile = mw.config.get('wgMFMode') !== null,
portletLink;


    var COOKIES = {
var self = {
            DARK: 'darkmode',
init: function () {
            THEME: 'theme'
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
        },
        currentMode;


    function loadMode() {
portletLink = mw.util.addPortletLink(
        var theme = $.cookie(COOKIES.THEME);
( isMobile ? 'p-navigation' : 'p-personal' ),
        if (theme) return theme;
'',
        return $.cookie(COOKIES.DARK) === 'true' ? 'dark' : 'light';
( isMobile ? 'Переключить тёмную тему' : '' ),
    }
'pt-dm-toggle',
 
'Переключить тёмную тему',
    function saveMode(mode) {
null,
        $.cookie(COOKIES.THEME, mode, { expires: 365, path: '/' });
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
        $.cookie(COOKIES.DARK, mode === 'dark', { expires: 365, path: '/' });
);
    }
 
$(portletLink).find('a').click(function(e) {
    function applyMode(mode) {
e.preventDefault();
        $('body')
            .toggleClass('wgl-theme-dark', mode === 'dark')
isUsingDarkmode = !isUsingDarkmode;
            .toggleClass('wgl-theme-light', mode === 'light');
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
        mw.hook('wgl.themeChanged').fire(mode);
$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});
    }
 
if (isUsingDarkmode === true) {
    currentMode = loadMode();
mw.loader.using(['wgl.theme.dark']).then(function() {
    applyMode(currentMode);
  $('body').addClass('wgl-theme-dark')
 
  $('body').removeClass('wgl-theme-light')
    var $toggle = $('<div>', {
  mw.hook('wgl.themeChanged').fire('dark')
        id: 'pt-dm-toggle',
});
        html: $('<a>', {
} else {
            href: '#',
$('body').addClass('wgl-theme-light')
            title: 'Переключить тёмную тему',
$('body').removeClass('wgl-theme-dark')
            text: 'Переключить тему'
mw.hook('wgl.themeChanged').fire('light')
        })
}
    });
});
 
}
    $('#user-tools').append($toggle);
}
 
    $toggle.on('click', 'a', function(e) {
$(self.init);
        e.preventDefault();
        currentMode = currentMode === 'dark' ? 'light' : 'dark';
        saveMode(currentMode);
        applyMode(currentMode);
    });


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