MediaWiki:Common.js: Różnice pomiędzy wersjami

Z Oświetleniowo
Przejdź do nawigacji Przejdź do wyszukiwania
Nie podano opisu zmian
Nie podano opisu zmian
 
Linia 1: Linia 1:
/* Umieszczony tutaj kod JavaScript zostanie załadowany przez każdego użytkownika, podczas każdego ładowania strony. */
// Dark Mode Manager with Cookie Storage
// Dark Mode Manager with Cookie Storage
(function() {
(function() {
Linia 61: Linia 59:
                 document.documentElement.classList.remove(DARK_MODE_CLASS);
                 document.documentElement.classList.remove(DARK_MODE_CLASS);
             }
             }
           
            // Protect images from any dark mode filters
             this.protectImages();
             this.protectImages();
         }
         }
Linia 71: Linia 67:
         }
         }
          
          
        // Protect images from any color modifications
         protectImages() {
         protectImages() {
             const images = document.querySelectorAll('img, .image, .thumbimage, .mw-file-element');
             const images = document.querySelectorAll('img, .image, .thumbimage, .mw-file-element');
Linia 80: Linia 75:
         }
         }
          
          
        // Process {{dark_toggle}} template markers
         processTemplateMarkers() {
         processTemplateMarkers() {
            // Method 1: Look for specific text patterns
             this.replaceCustomElements();
             this.replaceTextMarkers();
           
            // Method 2: Look for elements with specific classes (if your template adds them)
            this.replaceElementMarkers();
         }
         }
          
          
         replaceTextMarkers() {
         replaceCustomElements() {
             const walker = document.createTreeWalker(
             const customElements = document.querySelectorAll('dark-toggle');
                document.body,
             customElements.forEach(element => {
                NodeFilter.SHOW_TEXT,
                 element.innerHTML = this.createToggleButtonHTML(this.isDarkMode());
                null,
                setTimeout(() => {
                false
                     const button = element.querySelector('button');
            );
                     if (button) {
              
                         button.addEventListener('click', () => this.toggle());
            let node;
            while (node = walker.nextNode()) {
                 if (node.nodeValue.includes('{{dark_toggle}}')) {
                    const span = document.createElement('span');
                    span.className = 'dark-toggle-container';
                    span.innerHTML = this.createToggleButtonHTML(this.isDarkMode());
                   
                     const parent = node.parentNode;
                     if (parent.nodeType === Node.ELEMENT_NODE) {
                         parent.replaceChild(span, node);
                     }
                     }
                 }
                 }, 0);
            }
        }
       
        replaceElementMarkers() {
            const markers = document.querySelectorAll('[class*="dark_toggle"], [id*="dark_toggle"]');
            markers.forEach(marker => {
                marker.innerHTML = this.createToggleButtonHTML(this.isDarkMode());
                marker.className = 'dark-toggle-container';
             });
             });
         }
         }
Linia 125: Linia 97:
             const modeClass = isDark ? 'light' : '';
             const modeClass = isDark ? 'light' : '';
              
              
             return `<button class="dark-mode-toggle-button ${modeClass}" onclick="window.DarkModeManager.toggle()">
             return `<button class="dark-mode-toggle-button ${modeClass}">
                 <span class="toggle-icon">${icon}</span>
                 <span class="toggle-icon">${icon}</span>
                 <span class="toggle-text">${text}</span>
                 <span class="toggle-text">${text}</span>
Linia 132: Linia 104:
          
          
         createFixedToggle() {
         createFixedToggle() {
            // Remove existing fixed toggle if any
             const existingToggle = document.getElementById('darkModeToggleFixed');
             const existingToggle = document.getElementById('darkModeToggleFixed');
             if (existingToggle) {
             if (existingToggle) existingToggle.remove();
                existingToggle.remove();
            }
              
              
             const button = document.createElement('button');
             const button = document.createElement('button');
Linia 147: Linia 116:
              
              
             button.addEventListener('click', () => this.toggle());
             button.addEventListener('click', () => this.toggle());
           
             document.body.appendChild(button);
             document.body.appendChild(button);
         }
         }
          
          
         updateAllToggleButtons(isDark) {
         updateAllToggleButtons(isDark) {
            // Update inline toggle buttons
             const inlineButtons = document.querySelectorAll('.dark-mode-toggle-button');
             const inlineButtons = document.querySelectorAll('.dark-mode-toggle-button');
             inlineButtons.forEach(button => {
             inlineButtons.forEach(button => {
Linia 169: Linia 136:
             });
             });
              
              
            // Update fixed toggle button
             const fixedButton = document.getElementById('darkModeToggleFixed');
             const fixedButton = document.getElementById('darkModeToggleFixed');
             if (fixedButton) {
             if (fixedButton) {
Linia 190: Linia 156:
          
          
         watchForDynamicContent() {
         watchForDynamicContent() {
            // Watch for DOM changes to process new {{dark_toggle}} markers
             const observer = new MutationObserver((mutations) => {
             const observer = new MutationObserver((mutations) => {
                 mutations.forEach((mutation) => {
                 mutations.forEach((mutation) => {
Linia 196: Linia 161:
                         mutation.addedNodes.forEach((node) => {
                         mutation.addedNodes.forEach((node) => {
                             if (node.nodeType === Node.ELEMENT_NODE) {
                             if (node.nodeType === Node.ELEMENT_NODE) {
                                 if (node.textContent.includes('{{dark_toggle}}')) {
                                 if (node.querySelector && node.querySelector('dark-toggle')) {
                                     this.processTemplateMarkers();
                                     this.replaceCustomElements();
                                 }
                                 }
                             }
                             }

Aktualna wersja na dzień 15:23, 16 paź 2025

// Dark Mode Manager with Cookie Storage
(function() {
    'use strict';
    
    const DARK_MODE_CLASS = 'skin-dark-mode';
    const COOKIE_NAME = 'mw_dark_mode';
    const COOKIE_EXPIRE_DAYS = 365;
    
    class DarkModeManager {
        constructor() {
            this.init();
        }
        
        init() {
            this.applySavedPreference();
            this.processTemplateMarkers();
            this.createFixedToggle();
            this.watchForDynamicContent();
        }
        
        // Cookie management
        setCookie(name, value, days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            const expires = "expires=" + date.toUTCString();
            document.cookie = name + "=" + value + ";" + expires + ";path=/";
        }
        
        getCookie(name) {
            const nameEQ = name + "=";
            const ca = document.cookie.split(';');
            for(let i = 0; i < ca.length; i++) {
                let c = ca[i];
                while (c.charAt(0) === ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
        
        isDarkMode() {
            const cookie = this.getCookie(COOKIE_NAME);
            return cookie === 'true';
        }
        
        setDarkMode(enabled) {
            this.setCookie(COOKIE_NAME, enabled, COOKIE_EXPIRE_DAYS);
            this.applyDarkMode(enabled);
            this.updateAllToggleButtons(enabled);
        }
        
        toggle() {
            this.setDarkMode(!this.isDarkMode());
        }
        
        applyDarkMode(enabled) {
            if (enabled) {
                document.documentElement.classList.add(DARK_MODE_CLASS);
            } else {
                document.documentElement.classList.remove(DARK_MODE_CLASS);
            }
            this.protectImages();
        }
        
        applySavedPreference() {
            const isDark = this.isDarkMode();
            this.applyDarkMode(isDark);
        }
        
        protectImages() {
            const images = document.querySelectorAll('img, .image, .thumbimage, .mw-file-element');
            images.forEach(img => {
                img.style.filter = 'none';
                img.style.backgroundColor = 'transparent';
            });
        }
        
        processTemplateMarkers() {
            this.replaceCustomElements();
        }
        
        replaceCustomElements() {
            const customElements = document.querySelectorAll('dark-toggle');
            customElements.forEach(element => {
                element.innerHTML = this.createToggleButtonHTML(this.isDarkMode());
                setTimeout(() => {
                    const button = element.querySelector('button');
                    if (button) {
                        button.addEventListener('click', () => this.toggle());
                    }
                }, 0);
            });
        }
        
        createToggleButtonHTML(isDark) {
            const icon = isDark ? '☀️' : '🌙';
            const text = isDark ? ' Light Mode' : ' Dark Mode';
            const modeClass = isDark ? 'light' : '';
            
            return `<button class="dark-mode-toggle-button ${modeClass}">
                <span class="toggle-icon">${icon}</span>
                <span class="toggle-text">${text}</span>
            </button>`;
        }
        
        createFixedToggle() {
            const existingToggle = document.getElementById('darkModeToggleFixed');
            if (existingToggle) existingToggle.remove();
            
            const button = document.createElement('button');
            button.id = 'darkModeToggleFixed';
            button.className = `dark-mode-toggle-fixed ${this.isDarkMode() ? 'light' : ''}`;
            button.innerHTML = this.isDarkMode() ? 
                '<span class="toggle-icon">☀️</span><span class="toggle-text">Light Mode</span>' :
                '<span class="toggle-icon">🌙</span><span class="toggle-text">Dark Mode</span>';
            button.title = this.isDarkMode() ? 'Switch to light mode' : 'Switch to dark mode';
            
            button.addEventListener('click', () => this.toggle());
            document.body.appendChild(button);
        }
        
        updateAllToggleButtons(isDark) {
            const inlineButtons = document.querySelectorAll('.dark-mode-toggle-button');
            inlineButtons.forEach(button => {
                const icon = button.querySelector('.toggle-icon');
                const text = button.querySelector('.toggle-text');
                
                if (isDark) {
                    icon.textContent = '☀️';
                    text.textContent = ' Light Mode';
                    button.classList.add('light');
                } else {
                    icon.textContent = '🌙';
                    text.textContent = ' Dark Mode';
                    button.classList.remove('light');
                }
            });
            
            const fixedButton = document.getElementById('darkModeToggleFixed');
            if (fixedButton) {
                const icon = fixedButton.querySelector('.toggle-icon');
                const text = fixedButton.querySelector('.toggle-text');
                
                if (isDark) {
                    icon.textContent = '☀️';
                    text.textContent = 'Light Mode';
                    fixedButton.classList.add('light');
                    fixedButton.title = 'Switch to light mode';
                } else {
                    icon.textContent = '🌙';
                    text.textContent = 'Dark Mode';
                    fixedButton.classList.remove('light');
                    fixedButton.title = 'Switch to dark mode';
                }
            }
        }
        
        watchForDynamicContent() {
            const observer = new MutationObserver((mutations) => {
                mutations.forEach((mutation) => {
                    if (mutation.addedNodes.length) {
                        mutation.addedNodes.forEach((node) => {
                            if (node.nodeType === Node.ELEMENT_NODE) {
                                if (node.querySelector && node.querySelector('dark-toggle')) {
                                    this.replaceCustomElements();
                                }
                            }
                        });
                    }
                });
            });
            
            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        }
    }
    
    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', () => {
            window.DarkModeManager = new DarkModeManager();
        });
    } else {
        window.DarkModeManager = new DarkModeManager();
    }
})();