MediaWiki:Common.js: Różnice pomiędzy wersjami
Przejdź do nawigacji
Przejdź do wyszukiwania
Nie podano opisu zmian |
Nie podano opisu zmian |
||
| Linia 1: | Linia 1: | ||
// 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); | ||
} | } | ||
this.protectImages(); | this.protectImages(); | ||
} | } | ||
| Linia 71: | Linia 67: | ||
} | } | ||
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: | ||
} | } | ||
processTemplateMarkers() { | processTemplateMarkers() { | ||
this.replaceCustomElements(); | |||
this. | |||
} | } | ||
replaceCustomElements() { | |||
const | 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()); | |||
const | |||
if ( | |||
} | } | ||
} | }, 0); | ||
}); | }); | ||
} | } | ||
| Linia 125: | Linia 97: | ||
const modeClass = isDark ? 'light' : ''; | const modeClass = isDark ? 'light' : ''; | ||
return `<button class="dark-mode-toggle-button ${modeClass} | 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() { | ||
const existingToggle = document.getElementById('darkModeToggleFixed'); | const existingToggle = document.getElementById('darkModeToggleFixed'); | ||
if (existingToggle) | if (existingToggle) 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) { | ||
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: | ||
}); | }); | ||
const fixedButton = document.getElementById('darkModeToggleFixed'); | const fixedButton = document.getElementById('darkModeToggleFixed'); | ||
if (fixedButton) { | if (fixedButton) { | ||
| Linia 190: | Linia 156: | ||
watchForDynamicContent() { | watchForDynamicContent() { | ||
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. | if (node.querySelector && node.querySelector('dark-toggle')) { | ||
this. | 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();
}
})();