890
pakeitimų
SNėra keitimo santraukos |
SNėra keitimo santraukos |
||
| 1 eilutė: | 1 eilutė: | ||
;(function($, mw){ | ;(function($, mw){ | ||
var DARK_COOKIE = 'darkmode', | var DARK_COOKIE = 'darkmode', | ||
| 11 eilutė: | 5 eilutė: | ||
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"); | |||
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'}); | $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'}); | ||
portletLink = mw.util.addPortletLink( | portletLink = mw.util.addPortletLink( | ||
( isMobile ? 'p-navigation' : 'p-personal' ), | (isMobile ? 'p-navigation' : 'p-personal'), | ||
'', | '#', // use '#' so it doesn't redirect | ||
( isMobile ? 'Toggle dark mode' : '' ), | (isMobile ? 'Toggle dark mode' : '🌓 Toggle dark mode'), | ||
'pt-dm-toggle', | 'pt-dm-toggle', | ||
'Toggle dark mode', | 'Toggle dark mode', | ||
| 30 eilutė: | 21 eilutė: | ||
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0] | $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0] | ||
); | ); | ||
// If mw.util.addPortletLink returns an <a> directly, don't use .find('a') | |||
$(portletLink).click(function(e) { | |||
e.preventDefault(); | e.preventDefault(); | ||
isUsingDarkmode = !isUsingDarkmode; | isUsingDarkmode = !isUsingDarkmode; | ||
$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'}); | $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'}); | ||
$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'}); | $.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'}); | ||
if (isUsingDarkmode | if (isUsingDarkmode) { | ||
mw.loader.using(['wgl.theme.dark']).then(function() { | mw.loader.using(['wgl.theme.dark']).then(function() { | ||
$('body').addClass('wgl-theme-dark'); | |||
$('body').removeClass('wgl-theme-light'); | |||
mw.hook('wgl.themeChanged').fire('dark'); | |||
}); | }); | ||
} else { | } else { | ||
$('body').addClass('wgl-theme-light'); | |||
$('body').addClass('wgl-theme-light') | $('body').removeClass('wgl-theme-dark'); | ||
$('body').removeClass('wgl-theme-dark') | mw.hook('wgl.themeChanged').fire('light'); | ||
mw.hook('wgl.themeChanged').fire('light') | |||
} | } | ||
// Optional: update link text | |||
$(portletLink).text(isUsingDarkmode ? '🌙 Dark' : '☀️ Light'); | |||
}); | }); | ||
} | } | ||
} | }; | ||
$(self.init); | // Ensure mediawiki.util is loaded before init | ||
mw.loader.using(['mediawiki.util'], function () { | |||
$(self.init); | |||
}); | |||
}(jQuery, mediaWiki)); | }(jQuery, mediaWiki)); | ||