@@ -5,7 +5,7 @@ import {registerGlobalInitFunc} from '../modules/observer.ts';
55import ViewFileTree from '../components/ViewFileTree.vue' ;
66
77async function toggleSidebar ( sidebarEl : HTMLElement , visibility : boolean ) {
8- const showBtnEl = document . querySelector ( '.show-tree-sidebar-button' ) ;
8+ const showBtnEl = sidebarEl . parentElement . querySelector ( '.show-tree-sidebar-button' ) ;
99 const containerClassList = sidebarEl . parentElement . classList ;
1010 containerClassList . toggle ( 'repo-grid-tree-sidebar' , visibility ) ;
1111 containerClassList . toggle ( 'repo-grid-filelist-only' , ! visibility ) ;
@@ -22,28 +22,30 @@ async function toggleSidebar(sidebarEl: HTMLElement, visibility: boolean) {
2222 } ) ;
2323}
2424
25- async function loadChildren ( path : string , recursive ?: boolean ) {
26- const fileTree = document . querySelector ( '#view-file-tree' ) ;
27- const apiBaseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
28- const refTypeNameSubURL = fileTree . getAttribute ( 'data-current-ref-type-name-sub-url' ) ;
29- const response = await GET ( `${ apiBaseUrl } /tree/${ refTypeNameSubURL } /${ encodeURIComponent ( path ?? '' ) } ?recursive=${ recursive ?? false } ` ) ;
30- const json = await response . json ( ) ;
31- if ( json instanceof Array ) {
32- return json . map ( ( i ) => ( {
33- name : i . name ,
34- type : i . type ,
35- path : i . path ,
36- sub_module_url : i . sub_module_url ,
37- children : i . children ,
38- } ) ) ;
39- }
40- return null ;
25+ function childrenLoader ( sidebarEl : HTMLElement ) {
26+ return async ( path : string , recursive ?: boolean ) => {
27+ const fileTree = sidebarEl . querySelector ( '#view-file-tree' ) ;
28+ const apiBaseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
29+ const refTypeNameSubURL = fileTree . getAttribute ( 'data-current-ref-type-name-sub-url' ) ;
30+ const response = await GET ( `${ apiBaseUrl } /tree/${ refTypeNameSubURL } /${ encodeURIComponent ( path ?? '' ) } ?recursive=${ recursive ?? false } ` ) ;
31+ const json = await response . json ( ) ;
32+ if ( json instanceof Array ) {
33+ return json . map ( ( i ) => ( {
34+ name : i . name ,
35+ type : i . type ,
36+ path : i . path ,
37+ sub_module_url : i . sub_module_url ,
38+ children : i . children ,
39+ } ) ) ;
40+ }
41+ return null ;
42+ } ;
4143}
4244
4345async function loadContent ( sidebarEl : HTMLElement ) {
4446 // load content by path (content based on home_content.tmpl)
4547 const response = await GET ( `${ window . location . href } ?only_content=true` ) ;
46- const contentEl = document . querySelector ( '.repo-home-filelist' ) ;
48+ const contentEl = sidebarEl . parentElement . querySelector ( '.repo-home-filelist' ) ;
4749 contentEl . innerHTML = await response . text ( ) ;
4850 reloadContentScript ( sidebarEl , contentEl ) ;
4951}
@@ -56,14 +58,14 @@ function reloadContentScript(sidebarEl: HTMLElement, contentEl: Element) {
5658
5759export function initViewFileTreeSidebar ( ) {
5860 registerGlobalInitFunc ( 'initViewFileTreeSidebar' , async ( el : HTMLElement ) => {
59- document . querySelector ( '.hide-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
61+ el . querySelector ( '.hide-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
6062 toggleSidebar ( el , false ) ;
6163 } ) ;
62- document . querySelector ( '.repo-home-filelist .show-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
64+ el . parentElement . querySelector ( '.repo-home-filelist .show-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
6365 toggleSidebar ( el , true ) ;
6466 } ) ;
6567
66- const fileTree = document . querySelector ( '#view-file-tree' ) ;
68+ const fileTree = el . querySelector ( '#view-file-tree' ) ;
6769 const baseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
6870 const treePath = fileTree . getAttribute ( 'data-tree-path' ) ;
6971 const refType = fileTree . getAttribute ( 'data-current-ref-type' ) ;
@@ -72,10 +74,10 @@ export function initViewFileTreeSidebar() {
7274
7375 const selectedItem = ref ( getSelectedPath ( refString ) ) ;
7476
75- const files = await loadChildren ( treePath , true ) ;
77+ const files = await childrenLoader ( el ) ( treePath , true ) ;
7678
7779 fileTree . classList . remove ( 'is-loading' ) ;
78- const fileTreeView = createApp ( ViewFileTree , { files, selectedItem, loadChildren, loadContent : ( path : string ) => {
80+ const fileTreeView = createApp ( ViewFileTree , { files, selectedItem, loadChildren : childrenLoader ( el ) , loadContent : ( path : string ) => {
7981 selectedItem . value = getSelectedPath ( refString , `${ baseUrl } /src${ refString } /${ path } ` ) ;
8082 window . history . pushState ( null , null , `${ baseUrl } /src${ refString } /${ encodeURIComponent ( path ) } ` ) ;
8183 loadContent ( el ) ;
0 commit comments