PageSourceSearch

https://poe2lens.com/js/nav_render.js

js poe2lens.com collected 2026-10-02 01:39:47 UTC 5,746 bytes, 142 lines download raw bytes

1// 统一导航链接渲染功能
2// 只负责渲染导航链接部分(中间部分)
3// 根据选中的游戏类型,只渲染对应游戏的导航链接
4
5(function() {
6    'use strict';
7    
8    let currentPageId = null;
9    let currentGame = null;
10    
11    // 渲染导航链接(根据游戏类型)
12    function renderNavLinks(game) {
13        // game 为 null:桌面端不显示中间导航;移动端仍显示双列(POE1 | POE2)入口
14        if (game === null || game === undefined) {
15            var isMobileNav = false;
16            try {
17                isMobileNav = window.matchMedia && window.matchMedia('(max-width: 768px)').matches;
18            } catch (e1) {
19                isMobileNav = window.innerWidth <= 768;
20            }
21            if (isMobileNav && window.NavConfig) {
22                const navMenu = document.getElementById('mobile-nav-menu');
23                if (navMenu) {
24                    (async () => {
25                        try {
26                            await window.NavConfig.renderNavLinks('mobile-nav-menu', currentPageId, null);
27                            currentGame = null;
28                            const event = new CustomEvent('navLinksRendered', { detail: { currentPageId, game: null } });
29                            document.dispatchEvent(event);
30                        } catch (err) {
31                            console.error('渲染导航链接失败:', err);
32                        }
33                    })();
34                }
35                return;
36            }
37            const navMenuEmpty = document.getElementById('mobile-nav-menu');
38            if (navMenuEmpty) {
39                navMenuEmpty.innerHTML = '';
40            }
41            currentGame = null;
42            return;
43        }
44        
45        // 检查NavConfig是否已加载
46        if (!window.NavConfig) {
47            console.error('NavConfig未加载,请确保nav_config.js在nav_render.js之前加载');
48            // 最多重试10次,避免无限循环
49            if (!renderNavLinks.retryCount) {
50                renderNavLinks.retryCount = 0;
51            }
52            if (renderNavLinks.retryCount < 10) {
53                renderNavLinks.retryCount++;
54                setTimeout(() => renderNavLinks(game), 100);
55            } else {
56                console.error('NavConfig加载失败,已放弃重试');
57            }
58            return;
59        }
60        renderNavLinks.retryCount = 0; // 重置计数器
61        
62        // 渲染导航链接
63        const navMenu = document.getElementById('mobile-nav-menu');
64        if (navMenu) {
65            // 只渲染指定游戏的链接(renderNavLinks 现在是异步函数)
66            (async () => {
67                try {
68                    await window.NavConfig.renderNavLinks('mobile-nav-menu', currentPageId, game);
69                    currentGame = game;
70                    // 渲染完成后触发自定义事件(只在非重试时触发,避免循环)
71                    if (!renderNavLinks.isRetrying) {
72                        const event = new CustomEvent('navLinksRendered', { detail: { currentPageId, game } });
73                        document.dispatchEvent(event);
74                    }
75                } catch (err) {
76                    console.error('渲染导航链接失败:', err);
77                }
78            })();
79        } else {
80            console.warn('未找到导航菜单容器 #mobile-nav-menu');
81            // 延迟重试(最多重试5次)
82            if (!renderNavLinks.menuRetryCount) {
83                renderNavLinks.menuRetryCount = 0;
84            }
85            if (renderNavLinks.menuRetryCount < 5) {
86                renderNavLinks.menuRetryCount++;
87                renderNavLinks.isRetrying = true;
88                setTimeout(() => {
89                    renderNavLinks.isRetrying = false;
90                    renderNavLinks(game);
91                }, 100);
92            } else {
93                console.error('导航菜单容器未找到,已放弃重试');
94                renderNavLinks.menuRetryCount = 0;
95            }
96        }
97    }
98    
99    // 初始化导航链接(在导航栏渲染完成后调用)
100    function initNavLinks(pageId, defaultGame) {
101        currentPageId = pageId;
102        // 等待DOM加载完成
103        if (document.readyState === 'loading') {
104            document.addEventListener('DOMContentLoaded', function() {
105                renderNavLinks(defaultGame);
106            });
107        } else {
108            renderNavLinks(defaultGame);
109        }
110    }
111    
112    // 切换游戏类型(重新渲染对应游戏的链接)
113    function switchGame(game) {
114        console.log('switchNavGame被调用,game:', game, 'currentPageId:', currentPageId);
115        // 即使 currentPageId 为 null(如 privacy.html, about.html, terms.html),也可以切换游戏
116        renderNavLinks(game);
117    }
118    
119    // 导出到全局
120    window.initNavLinks = initNavLinks;
121    window.switchNavGame = switchGame;
122    
123    // 监听导航栏渲染完成事件
124    document.addEventListener('navBarRendered', function(e) {
125        // 如果是特殊页面(isSpecial为true),传递null表示不显示子标签
126        const game = e.detail.isSpecial ? null : e.detail.defaultGame;
127        initNavLinks(e.detail.currentPageId, game);
128    });
129
130    // 横竖屏 / 窗口跨越 768px 时重绘导航(移动端双列 ↔ 桌面单列)
131    var navResizeTimer = null;
132    window.addEventListener('resize', function() {
133        if (navResizeTimer) {
134            clearTimeout(navResizeTimer);
135        }
136        navResizeTimer = setTimeout(function() {
137            navResizeTimer = null;
138            renderNavLinks(currentGame);
139        }, 200);
140    });
141})();
142

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.