1
- import { createApp , ref } from 'vue' ;
1
+ import { createApp } from 'vue' ;
2
2
import { toggleElem } from '../utils/dom.ts' ;
3
- import { pathEscapeSegments , pathUnescapeSegments } from '../utils/url.ts' ;
4
- import { GET , POST } from '../modules/fetch.ts' ;
3
+ import { POST } from '../modules/fetch.ts' ;
5
4
import ViewFileTree from '../components/ViewFileTree.vue' ;
6
5
7
6
const { appSubUrl} = window . config ;
@@ -24,70 +23,26 @@ async function toggleSidebar(sidebarEl: HTMLElement, shouldShow: boolean) {
24
23
codeViewShowFileTree : shouldShow ,
25
24
} ,
26
25
} ) ;
27
- }
28
-
29
- function childrenLoader ( sidebarEl : HTMLElement ) {
30
- return async ( treePath : string , subPath : string = '' ) => {
31
- const fileTree = sidebarEl . querySelector ( '#view-file-tree' ) ;
32
- const baseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
33
- const refTypeNameSubURL = fileTree . getAttribute ( 'data-current-ref-type-name-sub-url' ) ;
34
- const response = await GET ( `${ baseUrl } /tree/${ refTypeNameSubURL } /${ pathEscapeSegments ( treePath ) } ?sub_path=${ encodeURIComponent ( subPath ) } ` ) ;
35
- const json = await response . json ( ) ;
36
- return json . fileTreeNodes ?? null ;
37
- } ;
38
- }
39
26
40
- async function loadContent ( sidebarEl : HTMLElement ) {
41
- // load content by path (content based on home_content.tmpl)
42
- const response = await GET ( `${ window . location . href } ?only_content=true` ) ;
43
- const contentEl = sidebarEl . parentElement . querySelector ( '.repo-view-content' ) ;
44
- contentEl . innerHTML = await response . text ( ) ;
45
- reloadContentScript ( sidebarEl , contentEl ) ;
46
- }
47
-
48
- function reloadContentScript ( sidebarEl : HTMLElement , contentEl : Element ) {
49
- contentEl . querySelector ( '.show-tree-sidebar-button' ) ?. addEventListener ( 'click' , ( ) => {
50
- toggleSidebar ( sidebarEl , true ) ;
51
- } ) ;
27
+ // FIXME: add event listener for "show-tree-sidebar-button"
52
28
}
53
29
54
30
export async function initViewFileTreeSidebar ( ) {
55
- const sidebarEl = document . querySelector ( '.repo-view-file-tree-sidebar' ) ;
56
- if ( ! sidebarEl || ! ( sidebarEl instanceof HTMLElement ) ) return ;
31
+ const sidebar = document . querySelector < HTMLElement > ( '.repo-view-file-tree-sidebar' ) ;
32
+ const repoViewContent = document . querySelector ( '.repo-view-content' ) ;
33
+ if ( ! sidebar || ! repoViewContent ) return ;
57
34
58
- sidebarEl . querySelector ( '.hide-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
59
- toggleSidebar ( sidebarEl , false ) ;
35
+ sidebar . querySelector ( '.hide-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
36
+ toggleSidebar ( sidebar , false ) ;
60
37
} ) ;
61
- sidebarEl . parentElement . querySelector ( '.repo-view-content .show-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
62
- toggleSidebar ( sidebarEl , true ) ;
38
+ repoViewContent . querySelector ( '.show-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
39
+ toggleSidebar ( sidebar , true ) ;
63
40
} ) ;
64
41
65
- const fileTree = sidebarEl . querySelector ( '#view-file-tree' ) ;
66
- const baseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
67
- const treePath = fileTree . getAttribute ( 'data-tree-path' ) ;
68
- const refType = fileTree . getAttribute ( 'data-current-ref-type' ) ;
69
- const refName = fileTree . getAttribute ( 'data-current-ref-short-name' ) ;
70
- const refString = ( refType ? ( `/${ refType } ` ) : '' ) + ( refName ? ( `/${ refName } ` ) : '' ) ;
71
-
72
- const selectedItem = ref ( getSelectedPath ( refString ) ) ;
73
-
74
- const files = await childrenLoader ( sidebarEl ) ( '' , treePath ) ;
75
-
76
- fileTree . classList . remove ( 'is-loading' ) ;
77
- const fileTreeView = createApp ( ViewFileTree , { files, selectedItem, loadChildren : childrenLoader ( sidebarEl ) , loadContent : ( path : string ) => {
78
- window . history . pushState ( null , null , `${ baseUrl } /src${ refString } /${ pathEscapeSegments ( path ) } ` ) ;
79
- selectedItem . value = path ;
80
- loadContent ( sidebarEl ) ;
81
- } } ) ;
82
- fileTreeView . mount ( fileTree ) ;
83
-
84
- window . addEventListener ( 'popstate' , ( ) => {
85
- selectedItem . value = getSelectedPath ( refString ) ;
86
- loadContent ( sidebarEl ) ;
87
- } ) ;
88
- }
89
-
90
- function getSelectedPath ( ref : string ) {
91
- const path = pathUnescapeSegments ( new URL ( window . location . href ) . pathname ) ;
92
- return path . substring ( path . indexOf ( ref ) + ref . length + 1 ) ;
42
+ const fileTree = sidebar . querySelector ( '#view-file-tree' ) ;
43
+ createApp ( ViewFileTree , {
44
+ repoLink : fileTree . getAttribute ( 'data-repo-link' ) ,
45
+ treePath : fileTree . getAttribute ( 'data-tree-path' ) ,
46
+ currentRefNameSubURL : fileTree . getAttribute ( 'data-current-ref-name-sub-url' ) ,
47
+ } ) . mount ( fileTree ) ;
93
48
}
0 commit comments