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

м Отмена версии 82176, сделанной Pok (обсуждение)
Метка: отмена
мНет описания правки
Строка 6: Строка 6:
             THEME: 'theme'
             THEME: 'theme'
         },
         },
         $tools = $('#user-tools'),
         isDark = $.cookie(COOKIES.THEME) === 'dark' ||
         currentMode;
                ($.cookie(COOKIES.THEME) == null && $.cookie(COOKIES.DARK) === 'true'),
         currentMode = isDark ? 'dark' : 'light';


     function loadMode() {
     $.cookie(COOKIES.THEME, currentMode, { expires: 365, path: '/' });
        var theme = $.cookie(COOKIES.THEME);
    $.cookie(COOKIES.DARK, isDark, { expires: 365, path: '/' });
        if (theme) return theme;
        return $.cookie(COOKIES.DARK) === 'true' ? 'dark' : 'light';
    }
 
    function saveMode(mode) {
        $.cookie(COOKIES.THEME, mode, { expires: 365, path: '/' });
        $.cookie(COOKIES.DARK, mode === 'dark', { expires: 365, path: '/' });
    }


     function applyMode(mode) {
     function applyMode(mode) {
         $('body')
         if (mode === 'dark') {
            .toggleClass('wgl-theme-dark', mode === 'dark')
            mw.loader.using(['wgl.theme.dark']).then(function() {
             .toggleClass('wgl-theme-light', mode === 'light');
                $('body').addClass('wgl-theme-dark').removeClass('wgl-theme-light');
        mw.hook('wgl.themeChanged').fire(mode);
                mw.hook('wgl.themeChanged').fire('dark');
            });
        } else {
             $('body').addClass('wgl-theme-light').removeClass('wgl-theme-dark');
            mw.hook('wgl.themeChanged').fire('light');
        }
     }
     }


    currentMode = loadMode();
     applyMode(currentMode);
     applyMode(currentMode);


     var $toggle = $(
     $(function() {
        '<div id="pt-dm-toggle">' +
        var $tools = $('#user-tools'),
            '<a href="#" title="Переключить тёмную тему"><span></span></a>' +
            $toggle = $(
        '</div>'
                '<div id="pt-dm-toggle">' +
    );
                    '<a href="#" title="Переключить тёмную тему"><span></span></a>' +
    $tools.prepend($toggle);
                '</div>'
            );
        $tools.prepend($toggle);


    $toggle.on('click', 'a', function(e) {
        $toggle.on('click', 'a', function(e) {
        e.preventDefault();
            e.preventDefault();
        currentMode = currentMode === 'dark' ? 'light' : 'dark';
            currentMode = currentMode === 'dark' ? 'light' : 'dark';
        saveMode(currentMode);
            $.cookie(COOKIES.THEME, currentMode, { expires: 365, path: '/' });
        applyMode(currentMode);
            $.cookie(COOKIES.DARK, currentMode === 'dark', { expires: 365, path: '/' });
            applyMode(currentMode);
        });
     });
     });
}(jQuery, mediaWiki));
}(jQuery, mediaWiki));