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

Iš MCSlime.LT.
Pereiti į navigaciją Jump to search
SNėra keitimo santraukos
Nėra keitimo santraukos
 
(nerodomos 5 tarpinės versijos, sukurtos to paties naudotojo)
1 eilutė: 1 eilutė:
;(function($, mw){
(function() {
var DARK_COOKIE = 'darkmode',
  var isDark = document.cookie.includes('theme=dark');
THEME_COOKIE = 'theme',
  if (isDark) {
isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
    document.documentElement.classList.add('wgl-theme-dark', 'wgl-darkmode');
isMobile = mw.config.get('wgMFMode') !== null,
  } else {
portletLink;
    document.documentElement.classList.add('wgl-theme-light', 'wgl-lightmode');
  }
})();


var self = {
(function($, mw) {
init: function () {
  var DARK_COOKIE = 'darkmode',
console.log("Dark mode toggle initialized");
      THEME_COOKIE = 'theme',
      isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
      isMobile = mw.config.get('wgMFMode') !== null,
      portletLink;


$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
  var self = {
    init: function() {
      // Apply the current theme on page load
      self.applyTheme(isUsingDarkmode);


portletLink = mw.util.addPortletLink(
      // Create the dark mode toggle link
(isMobile ? 'p-navigation' : 'p-personal'),
      portletLink = mw.util.addPortletLink(
'#', // use '#' so it doesn't redirect
        (isMobile ? 'p-navigation' : 'p-personal'),
(isMobile ? 'Toggle dark mode' : '🌓 '),
        '#', // Use '#' to prevent redirection
'pt-dm-toggle',
        self.getToggleIcon(isUsingDarkmode), // Set initial icon based on current state
'Toggle dark mode',
        'pt-dm-toggle',
null,
        'Toggle dark mode',
$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
        null,
);
        $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
      );


// If mw.util.addPortletLink returns an <a> directly, don't use .find('a')
      // Add pointer cursor style
$(portletLink).click(function(e) {
      $(portletLink).css('cursor', 'pointer');
e.preventDefault();


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


if (isUsingDarkmode) {
        isUsingDarkmode = !isUsingDarkmode;
    $('body')
        $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
        .addClass('wgl-theme-dark wgl-darkmode')
        $.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});
        .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');
}


// Optional: update link text
        self.applyTheme(isUsingDarkmode);
$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
});
}
};


// Ensure mediawiki.util is loaded before init
        // Update link text with appropriate icon
mw.loader.using(['mediawiki.util'], function () {
        $(portletLink).html(self.getToggleIcon(isUsingDarkmode));
$(self.init);
      });
});
    },


}(jQuery, mediaWiki));
    // Helper function to get the appropriate icon based on current theme
    getToggleIcon: function(isDark) {
      if (isMobile) {
        return 'Toggle dark mode';
      } else {
        return isDark ? '🌙 ' : '☀️ ';
      }
    },
 
    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');
      }
      // Make the body visible after applying the theme
      $('body').css({visibility: 'visible', opacity: 1});
    }
  };
 
  // Ensure mediawiki.util is loaded before init
  mw.loader.using(['mediawiki.util'], function () {
    $(self.init);
  });
 
})(jQuery, mediaWiki);

Dabartinė 08:32, 14 balandžio 2025 versija

(function() {
  var isDark = document.cookie.includes('theme=dark');
  if (isDark) {
    document.documentElement.classList.add('wgl-theme-dark', 'wgl-darkmode');
  } else {
    document.documentElement.classList.add('wgl-theme-light', 'wgl-lightmode');
  }
})();

(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
        self.getToggleIcon(isUsingDarkmode), // Set initial icon based on current state
        'pt-dm-toggle',
        'Toggle dark mode',
        null,
        $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
      );

      // Add pointer cursor style
      $(portletLink).css('cursor', 'pointer');

      // 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);

        // Update link text with appropriate icon
        $(portletLink).html(self.getToggleIcon(isUsingDarkmode));
      });
    },

    // Helper function to get the appropriate icon based on current theme
    getToggleIcon: function(isDark) {
      if (isMobile) {
        return 'Toggle dark mode';
      } else {
        return isDark ? '🌙 ' : '☀️ ';
      }
    },

    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');
      }
      // Make the body visible after applying the theme
      $('body').css({visibility: 'visible', opacity: 1});
    }
  };

  // Ensure mediawiki.util is loaded before init
  mw.loader.using(['mediawiki.util'], function () {
    $(self.init);
  });

})(jQuery, mediaWiki);