MediaWiki:Gadget-darkmode.js: Skirtumas tarp puslapio versijų

Iš MCSlime.LT.
Pereiti į navigaciją Jump to search
SNėra keitimo santraukos
Nėra keitimo santraukos
1 eilutė: 1 eilutė:
;(function($, mw){
(function($, mw) {
var DARK_COOKIE = 'darkmode',
    var DARK_COOKIE = 'darkmode',
THEME_COOKIE = 'theme',
        THEME_COOKIE = 'theme',
isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
        isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
isMobile = mw.config.get('wgMFMode') !== null,
        isMobile = mw.config.get('wgMFMode') !== null,
portletLink;
        portletLink;


var self = {
    var self = {
init: function () {
        init: function() {
console.log("Dark mode toggle initialized");
            // Apply the current theme on page load
            self.applyTheme(isUsingDarkmode);


$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
            // Create the dark mode toggle link
            portletLink = mw.util.addPortletLink(
                (isMobile ? 'p-navigation' : 'p-personal'),
                '#', // Use '#' to prevent redirection
                (isMobile ? 'Toggle dark mode' : '🌓 '),
                'pt-dm-toggle',
                'Toggle dark mode',
                null,
                $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
            );


portletLink = mw.util.addPortletLink(
            // Toggle theme on link click
(isMobile ? 'p-navigation' : 'p-personal'),
            $(portletLink).click(function(e) {
'#', // use '#' so it doesn't redirect
                e.preventDefault();
(isMobile ? 'Toggle dark mode' : '🌓 '),
                isUsingDarkmode = !isUsingDarkmode;
'pt-dm-toggle',
                $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', { expires: 365, path: '/' });
'Toggle dark mode',
                $.cookie(DARK_COOKIE, isUsingDarkmode, { expires: 365, path: '/' });
null,
                self.applyTheme(isUsingDarkmode);
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
            });
);
        },


// If mw.util.addPortletLink returns an <a> directly, don't use .find('a')
        applyTheme: function(isDark) {
$(portletLink).click(function(e) {
            if (isDark) {
e.preventDefault();
                $('body').addClass('wgl-theme-dark wgl-darkmode')
                        .removeClass('wgl-theme-light wgl-lightmode');
                mw.hook('wgl.themeChanged').fire('dark');
            } else {
                $('body').addClass('wgl-theme-light wgl-lightmode')
                        .removeClass('wgl-theme-dark wgl-darkmode');
                mw.hook('wgl.themeChanged').fire('light');
            }
        }
    };


isUsingDarkmode = !isUsingDarkmode;
    // Ensure jQuery is loaded before initializing
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
    if (typeof $ !== 'undefined') {
$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});
        $(self.init);
 
    } else {
if (isUsingDarkmode) {
        mw.loader.using(['jquery'], function() {
    $('body')
            $(self.init);
        .addClass('wgl-theme-dark wgl-darkmode')
        });
        .removeClass('wgl-theme-light wgl-lightmode');
    }
    mw.hook('wgl.themeChanged').fire('dark');
})(jQuery, mediaWiki);
} else {
    $('body')
        .addClass('wgl-theme-light wgl-lightmode')
        .removeClass('wgl-theme-dark wgl-darkmode');
    mw.hook('wgl.themeChanged').fire('light');
}
 
// Optional: update link text
$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
});
}
};
 
// Ensure mediawiki.util is loaded before init
mw.loader.using(['mediawiki.util'], function () {
$(self.init);
});
 
}(jQuery, mediaWiki));

23:26, 13 balandžio 2025 versija

(function($, mw) {
    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 self = {
        init: function() {
            // Apply the current theme on page load
            self.applyTheme(isUsingDarkmode);

            // Create the dark mode toggle link
            portletLink = mw.util.addPortletLink(
                (isMobile ? 'p-navigation' : 'p-personal'),
                '#', // Use '#' to prevent redirection
                (isMobile ? 'Toggle dark mode' : '🌓 '),
                'pt-dm-toggle',
                'Toggle dark mode',
                null,
                $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
            );

            // Toggle theme on link click
            $(portletLink).click(function(e) {
                e.preventDefault();
                isUsingDarkmode = !isUsingDarkmode;
                $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', { expires: 365, path: '/' });
                $.cookie(DARK_COOKIE, isUsingDarkmode, { expires: 365, path: '/' });
                self.applyTheme(isUsingDarkmode);
            });
        },

        applyTheme: function(isDark) {
            if (isDark) {
                $('body').addClass('wgl-theme-dark wgl-darkmode')
                         .removeClass('wgl-theme-light wgl-lightmode');
                mw.hook('wgl.themeChanged').fire('dark');
            } else {
                $('body').addClass('wgl-theme-light wgl-lightmode')
                         .removeClass('wgl-theme-dark wgl-darkmode');
                mw.hook('wgl.themeChanged').fire('light');
            }
        }
    };

    // Ensure jQuery is loaded before initializing
    if (typeof $ !== 'undefined') {
        $(self.init);
    } else {
        mw.loader.using(['jquery'], function() {
            $(self.init);
        });
    }
})(jQuery, mediaWiki);