bp/public/assets/js/base.js

65 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Скрипт тоггла сайдбара с улучшенной обработкой
document.addEventListener("DOMContentLoaded", function () {
const sidebarToggle = document.getElementById('sidebarToggle');
const sidebarWrapper = document.getElementById('sidebar-wrapper');
const body = document.body;
if (sidebarToggle) {
// Обработчик клика
sidebarToggle.addEventListener('click', function(event) {
event.preventDefault();
event.stopPropagation();
// Переключаем класс
body.classList.toggle('sb-sidenav-toggled');
// Сохраняем состояние в localStorage
const isToggled = body.classList.contains('sb-sidenav-toggled');
localStorage.setItem('sidebar-toggled', isToggled);
});
// Закрытие сайдбара при клике вне его (для мобильных)
document.addEventListener('click', function(event) {
if (window.innerWidth <= 768 &&
body.classList.contains('sb-sidenav-toggled') &&
!sidebarWrapper.contains(event.target) &&
event.target !== sidebarToggle &&
!sidebarToggle.contains(event.target)) {
body.classList.remove('sb-sidenav-toggled');
localStorage.setItem('sidebar-toggled', false);
}
});
// Восстановление состояния из localStorage
const sidebarToggled = localStorage.getItem('sidebar-toggled');
if (sidebarToggled === 'true' && window.innerWidth > 768) {
body.classList.add('sb-sidenav-toggled');
}
}
// Адаптивное поведение при изменении размера окна
window.addEventListener('resize', function() {
if (window.innerWidth > 768) {
// На десктопе всегда показываем сайдбар
body.classList.remove('sb-sidenav-toggled');
}
});
// Подсветка активного пункта меню при загрузке
highlightActiveMenuItem();
function highlightActiveMenuItem() {
const currentPath = window.location.pathname;
const menuItems = document.querySelectorAll('.sidebar-link');
menuItems.forEach(item => {
const href = item.getAttribute('href');
if (href && href !== '#' && currentPath.includes(href.replace(baseUrl, ''))) {
item.classList.add('active');
}
});
}
// Базовая функция для получения base URL
const baseUrl = '{{ base_url("/") }}'.replace(/\/+$/, '');
});