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

Iš MCSlime.LT.
Pereiti į navigaciją Jump to search
Nėra keitimo santraukos
Žyma: Atmesta
(Anuoliuoti M0dii (aptarimas) versiją 1028)
Žyma: Anuliuoti
1 eilutė: 1 eilutė:
(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) {
(function($, mw) {
   const DARK_COOKIE = 'darkmode';
   var DARK_COOKIE = 'darkmode',
  const THEME_COOKIE = 'theme';
      THEME_COOKIE = 'theme',
  const TOGGLE_ID = 'pt-darkmode-toggle';
      isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
      isMobile = mw.config.get('wgMFMode') !== null,
      portletLink;


   const isMobile = mw.config.get('wgMFMode') !== null;
   var self = {
  const portletId = isMobile ? 'p-navigation' : 'p-personal';
    init: function() {
      // Apply the current theme on page load
      self.applyTheme(isUsingDarkmode);


  // Determine initial theme preference
      // Create the dark mode toggle link
  const themeCookie = $.cookie(THEME_COOKIE);
      portletLink = mw.util.addPortletLink(
  const darkCookie = $.cookie(DARK_COOKIE);
        (isMobile ? 'p-navigation' : 'p-personal'),
  let isDark = themeCookie === 'dark' || (!themeCookie && darkCookie === 'true');
        '#', // Use '#' to prevent redirection
        (isMobile ? 'Toggle dark mode' : '🌓 '),
        'pt-dm-toggle',
        'Toggle dark mode',
        null,
        $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
      );


  // Apply theme classes to <html> element immediately
      // Toggle theme on link click
  document.documentElement.classList.add(isDark ? 'wgl-theme-dark' : 'wgl-theme-light');
      $(portletLink).click(function(e) {
        e.preventDefault();


  // Define icon representations
        isUsingDarkmode = !isUsingDarkmode;
  const icons = {
        $.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
    dark: '🌙',
        $.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});
    light: '☀️'
  };


  // Function to apply theme classes to <body>
        self.applyTheme(isUsingDarkmode);
  function applyTheme(isDarkMode) {
    $('body')
      .toggleClass('wgl-theme-dark wgl-darkmode', isDarkMode)
      .toggleClass('wgl-theme-light wgl-lightmode', !isDarkMode)
      .css({ visibility: 'visible', opacity: 1 });


    mw.hook('wgl.themeChanged').fire(isDarkMode ? 'dark' : 'light');
        // Optional: update link text
  }
        $(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
      });
    },


  // Function to update the toggle icon
    applyTheme: function(isDark) {
  function updateToggleIcon(isDarkMode) {
      if (isDark) {
    const icon = isDarkMode ? icons.dark : icons.light;
        $('body')
    $(`#${TOGGLE_ID} a`).text(icon);
          .addClass('wgl-theme-dark wgl-darkmode')
  }
          .removeClass('wgl-theme-light wgl-lightmode');
 
        mw.hook('wgl.themeChanged').fire('dark');
  // Initialize the toggle functionality
       } else {
  function initToggle() {
        $('body')
    // Create the toggle link
          .addClass('wgl-theme-light wgl-lightmode')
    const toggleLink = mw.util.addPortletLink(
          .removeClass('wgl-theme-dark wgl-darkmode');
      portletId,
        mw.hook('wgl.themeChanged').fire('light');
      '#',
       }
      isDark ? icons.dark : icons.light,
       // Make the body visible after applying the theme
      TOGGLE_ID,
       $('body').css({visibility: 'visible', opacity: 1});
      'Toggle dark mode',
     }
       null,
   };
      $('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
    );
 
    // Add a class for cursor styling
    $(toggleLink).addClass('darkmode-toggle');
 
    // Apply the initial theme
    applyTheme(isDark);
 
    // Handle toggle click event
    $(toggleLink).on('click', function(e) {
      e.preventDefault();
       isDark = !isDark;
 
       // Update cookies
       $.cookie(THEME_COOKIE, isDark ? 'dark' : 'light', { expires: 365, path: '/' });
      $.cookie(DARK_COOKIE, isDark, { expires: 365, path: '/' });
 
      // Apply the new theme and update icon
      applyTheme(isDark);
      updateToggleIcon(isDark);
     });
   }


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


})(jQuery, mediaWiki);
})(jQuery, mediaWiki);

09:30, 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
        (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);

        // Optional: update link text
        $(portletLink).text(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');
      }
      // 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);