User:MetalManeMc/vector.js: Difference between revisions

From bg3.wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 31: Line 31:
isUsingLightmode = !isUsingLightmode;
isUsingLightmode = !isUsingLightmode;
$.cookie(THEME_COOKIE, isUsingLightmode ? 'dark' : 'light', {expires: 365, path: '/'});
$.cookie(THEME_COOKIE, isUsingLightmode ? 'dark' : 'light', {expires: 365, path: '/'});
$.cookie(DARK_COOKIE, isUsingLightmode, {expires: 365, path: '/'});
$.cookie(LIGHT_COOKIE, isUsingLightmode, {expires: 365, path: '/'});
if (isUsingLightmode === true) {
if (isUsingLightmode === true) {

Revision as of 22:14, 18 December 2023

/**
 * Toggle for dark mode testing
 * 
 * @author [[User:Jayden]] for https://minecraft.wiki
 * @author [[User:MetalManeMc]] for adaptation on Baldur's Gate 3 Wiki
 * @see Based on https://runescape.wiki/w/MediaWiki:Gadget-skinTogglesNew.js
 */
;(function($, mw){
	var LIGHT_COOKIE = 'lightmode',
		THEME_COOKIE = 'theme',
		isUsingLightmode = $.cookie(THEME_COOKIE) === 'light' || ($.cookie(THEME_COOKIE) == null && $.cookie(LIGHT_COOKIE) === 'true'),
		portletLink;

	var self = {
		init: function () {
			$.cookie(THEME_COOKIE, isUsingLightmode ? 'light' : 'dark', {expires: 365, path: '/'});

			portletLink = mw.util.addPortletLink(
				'p-personal',
				'',
				'',
				'pt-dm-toggle',
				'Toggle light mode',
				null,
				$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
			);
			
			$(portletLink).find('a').click(function(e) {
				e.preventDefault();
				
				isUsingLightmode = !isUsingLightmode;
				$.cookie(THEME_COOKIE, isUsingLightmode ? 'dark' : 'light', {expires: 365, path: '/'});
				$.cookie(LIGHT_COOKIE, isUsingLightmode, {expires: 365, path: '/'});
				
				if (isUsingLightmode === true) {
					mw.loader.using(['theme-light']).then(function() {
						$('body').addClass('theme-light')
						$('body').removeClass('theme-dark-grey')
					});
				} else {
					$('body').addClass('theme-dark-grey')
					$('body').removeClass('theme-light')
				}
			});
		}
	}
	
	$(self.init);

}(jQuery, mediaWiki));