MediaWiki:Common.js: различия между версиями

Нет описания правки
Нет описания правки
Строка 42: Строка 42:
     });
     });


     setTimeout(function() {
     function scrollToLastOpenedCategory() {
         var lastOpenedPageTitle = localStorage.getItem('lastOpenedPageTitle');
         var lastOpenedPageTitle = localStorage.getItem('lastOpenedPageTitle');
         if (lastOpenedPageTitle) {
         if (lastOpenedPageTitle) {
Строка 53: Строка 53:
                     scrollTop: $lastOpenedItem.position().top - 50
                     scrollTop: $lastOpenedItem.position().top - 50
                 }, 500, function() {
                 }, 500, function() {
                    // Убираем старую подсветку, добавляем новую
                     $('.CategoryTreeItem').removeClass('highlighted');
                     $('.CategoryTreeItem').removeClass('highlighted');
                     $lastOpenedItem.addClass('highlighted');
                     $lastOpenedItem.addClass('highlighted');
Строка 61: Строка 60:
             }
             }
         }
         }
     }, 500);
     }
 
    function scrollToCurrentPageCategory() {
        console.log("Определяем категорию текущей страницы...");
 
        // Очищаем хранилище перед проверкой
        localStorage.removeItem('lastOpenedPageTitle');
        localStorage.removeItem('highlightedCategory');
 
        var currentPageTitle = mw.config.get('wgTitle'); // Получаем заголовок текущей страницы
        if (!currentPageTitle) {
            console.log("Не удалось определить текущую страницу.");
            return;
        }
 
        console.log("Текущая страница:", currentPageTitle);


    // Восстанавливаем подсветку при загрузке страницы
         var $currentCategoryItem = $('#p-categorytree-portlet .CategoryTreeItem').filter(function() {
    var highlightedCategory = localStorage.getItem('highlightedCategory');
             return $(this).text().trim() === currentPageTitle;
    if (highlightedCategory) {
         var $highlightedItem = $('#p-categorytree-portlet .CategoryTreeItem').filter(function() {
             return $(this).text().trim() === highlightedCategory;
         });
         });


         if ($highlightedItem.length) {
         if ($currentCategoryItem.length) {
             $('.CategoryTreeItem').removeClass('highlighted');
             console.log("Категория найдена, перематываем...");
            $highlightedItem.addClass('highlighted');
            $('#p-categorytree-portlet').animate({
                scrollTop: $currentCategoryItem.position().top - 50
            }, 500, function() {
                $('.CategoryTreeItem').removeClass('highlighted');
                $currentCategoryItem.addClass('highlighted');
            });
 
            localStorage.setItem('highlightedCategory', currentPageTitle);
        } else {
            console.log("Категория для текущей страницы не найдена.");
         }
         }
     }
     }
    setTimeout(function() {
        if (document.referrer) {
            console.log("Переход со страницы, перематываем к последнему открытому элементу...");
            scrollToLastOpenedCategory();
        } else {
            console.log("Прямой заход на страницу, определяем категорию...");
            scrollToCurrentPageCategory();
        }
    }, 500);


     $(window).on('beforeunload', saveTreeHTML);
     $(window).on('beforeunload', saveTreeHTML);
});
});