MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 564: | Строка 564: | ||
// Функция для логики меню создаваемым модулем CategoryMenu | // Функция для логики меню создаваемым модулем CategoryMenu | ||
function initCategorySwitcher() { | function initCategorySwitcher() { | ||
var categories = document.querySelectorAll('.categories div'); | |||
var menus = document.querySelectorAll('.menu'); | |||
var contentDivs = document.querySelectorAll('.content div'); | |||
var menuItems = document.querySelectorAll('.menu div'); | |||
var currentCategoryIndex = 0; // Индекс текущей активной категории | |||
// Устанавливаем ID для категорий | |||
categories.forEach(function(category, index) { | |||
var categoryText = category.textContent.trim().toLowerCase().replace(/\s+/g, '-'); // Преобразуем текст в id | |||
category.id = categoryText || 'category-' + index; // Если текста нет, используем индекс | |||
}); | |||
// Устанавливаем ID для пунктов меню | |||
menuItems.forEach(function(menuItem, index) { | |||
var menuItemText = menuItem.textContent.trim().toLowerCase().replace(/\s+/g, '-'); // Преобразуем текст в id | |||
menuItem.id = menuItemText || 'menu-item-' + index; // Если текста нет, используем индекс | |||
}); | |||
function clearActiveContent() { | |||
for (var i = 0; i < contentDivs.length; i++) { | |||
contentDivs[i].classList.remove('active'); | |||
} | |||
} | |||
function clearActiveMenu() { | |||
for (var i = 0; i < menus.length; i++) { | |||
menus[i].classList.remove('active'); | |||
} | |||
} | |||
function clearActiveMenuItems() { | |||
for (var i = 0; i < menuItems.length; i++) { | |||
menuItems[i].classList.remove('active'); | |||
} | |||
} | |||
function switchCategory(index) { | |||
if (categories.length === 0 || menus.length === 0) return; // Защита от пустых данных | |||
clearActiveMenu(); | |||
clearActiveContent(); | |||
categories.forEach(function(category) { | |||
category.classList.remove('active'); | |||
}); | |||
var selectedCategory = categories[index]; | |||
if (selectedCategory) { | |||
selectedCategory.classList.add('active'); | |||
var categoryClass = selectedCategory.classList[0]; | |||
var selectedMenu = document.querySelector('.' + categoryClass + '-menu'); | |||
if (selectedMenu) { | |||
selectedMenu.classList.add('active'); | |||
var firstParagraph = selectedMenu.querySelector('div'); | |||
if (firstParagraph) { | |||
switchContent(firstParagraph); | |||
} | |||
} | |||
} | |||
currentCategoryIndex = index; | |||
updateArrowStates(); // Обновляем состояние стрелок | |||
} | |||
function switchContent(menuItem) { | |||
clearActiveMenuItems(); | |||
clearActiveContent(); | |||
var contentClass = menuItem.className + '-content'; | |||
var content = document.querySelector('.' + contentClass); | |||
if (content) { | |||
content.classList.add('active'); | |||
menuItem.classList.add('active'); | |||
} | |||
} | |||
// Обновляем состояние стрелок (активные/неактивные) | |||
function updateArrowStates() { | |||
const prevArrow = document.getElementById('prev-category'); | |||
const nextArrow = document.getElementById('next-category'); | |||
if (currentCategoryIndex === 0) { | |||
prevArrow.classList.add('disabled'); | |||
prevArrow.style.pointerEvents = 'none'; // Отключаем клики | |||
} else { | |||
prevArrow.classList.remove('disabled'); | |||
prevArrow.style.pointerEvents = 'auto'; // Включаем клики | |||
} | |||
if (currentCategoryIndex === categories.length - 1) { | |||
nextArrow.classList.add('disabled'); | |||
nextArrow.style.pointerEvents = 'none'; | |||
} else { | |||
nextArrow.classList.remove('disabled'); | |||
nextArrow.style.pointerEvents = 'auto'; | |||
} | |||
} | |||
// Функция для обработки якорей | |||
function handleAnchorLink() { | |||
var hash = window.location.hash.substring(1); // Получаем якорь без '#' | |||
if (hash) { | |||
var categoryIndex = Array.from(categories).findIndex(category => category.id === hash); | |||
if (categoryIndex !== -1) { | |||
switchCategory(categoryIndex); | |||
return; | |||
} | |||
var menuItem = document.querySelector('.menu div[id="' + hash + '"]'); | |||
if (menuItem) { | |||
var categoryClass = menuItem.closest('.menu').classList[0].replace('-menu', ''); | |||
var categoryIndex = Array.from(categories).findIndex(category => category.classList.contains(categoryClass)); | |||
if (categoryIndex !== -1) { | |||
switchCategory(categoryIndex); | |||
switchContent(menuItem); | |||
} | |||
} | |||
} | |||
} | |||
// Стрелки для переключения категорий | |||
document.getElementById('prev-category').addEventListener('click', function() { | |||
if (currentCategoryIndex > 0) { | |||
currentCategoryIndex = currentCategoryIndex - 1; | |||
switchCategory(currentCategoryIndex); // Переключаем на новую категорию | |||
} | |||
}); | |||
document.getElementById('next-category').addEventListener('click', function() { | |||
if (currentCategoryIndex < categories.length - 1) { | |||
currentCategoryIndex = currentCategoryIndex + 1; | |||
switchCategory(currentCategoryIndex); // Переключаем на новую категорию | |||
} | |||
}); | |||
// Инициализация: открываем первую категорию и первый пункт | |||
if (categories.length > 0 && menus.length > 0) { | |||
switchCategory(0); // Отображаем первую категорию и первый пункт | |||
updateArrowStates(); // Обновляем состояние стрелок | |||
} | |||
categories.forEach(function(category) { | |||
category.addEventListener('click', function(event) { | |||
event.preventDefault(); // Предотвращаем любое действие при клике | |||
}); | |||
}); | |||
for (var i = 0; i < menuItems.length; i++) { | |||
menuItems[i].addEventListener('click', function() { | |||
switchContent(this); | |||
}); | |||
} | |||
document.querySelector('.menu-toggle').addEventListener('click', function() { | |||
var menu = document.querySelector('.menu-container'); | |||
menu.classList.toggle('active'); | |||
var toggleButton = document.querySelector('.menu-toggle'); | |||
toggleButton.classList.toggle('active'); | |||
}); | |||
handleAnchorLink(); | |||
window.addEventListener('hashchange', handleAnchorLink); | |||
} | } | ||
const currentPageTitle = document.title; | const currentPageTitle = document.title; | ||