1+ // 动态加载HTML内容的工具函数
2+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
3+ // 加载所有部分页面内容
4+ loadPartials ( ) ;
5+ } ) ;
6+
7+ function loadPartials ( ) {
8+ // 定义需要加载的部分页面
9+ const partials = [
10+ { id : 'header-placeholder' , file : 'partials/header.html' } ,
11+ { id : 'sidebar-placeholder' , file : 'partials/sidebar.html' } ,
12+ { id : 'files-placeholder' , file : 'partials/files.html' } ,
13+ { id : 'groups-placeholder' , file : 'partials/groups.html' } ,
14+ { id : 'tags-placeholder' , file : 'partials/tags.html' } ,
15+ { id : 'file-groups-placeholder' , file : 'partials/file-groups.html' } ,
16+ { id : 'group-tags-placeholder' , file : 'partials/group-tags.html' } ,
17+ { id : 'group-relations-placeholder' , file : 'partials/group-relations.html' } ,
18+ { id : 'modal-placeholder' , file : 'partials/modal.html' }
19+ ] ;
20+
21+ // 加载每个部分页面
22+ partials . forEach ( partial => {
23+ fetch ( partial . file )
24+ . then ( response => response . text ( ) )
25+ . then ( html => {
26+ const placeholder = document . getElementById ( partial . id ) ;
27+ if ( placeholder ) {
28+ placeholder . innerHTML = html ;
29+ }
30+
31+ // 当所有内容加载完成后,初始化应用
32+ if ( partial . id === 'modal-placeholder' ) {
33+ initializeApp ( ) ;
34+ }
35+ } )
36+ . catch ( error => {
37+ console . error ( `加载 ${ partial . file } 失败:` , error ) ;
38+ } ) ;
39+ } ) ;
40+ }
41+
42+ function initializeApp ( ) {
43+ // 初始化应用功能
44+ // 这里可以添加一些初始化逻辑
45+
46+ // 默认显示第一个tab并自动搜索
47+ const firstTab = document . querySelector ( '.tab-content' ) ;
48+ if ( firstTab ) {
49+ firstTab . style . display = 'block' ;
50+ const firstNavLink = document . querySelector ( '.nav-link' ) ;
51+ if ( firstNavLink ) {
52+ firstNavLink . classList . add ( 'active' ) ;
53+ autoSearch ( firstNavLink . getAttribute ( 'data-target' ) ) ;
54+ }
55+ }
56+ }
0 commit comments