22import FileTree from ' ./FileTree.vue' ;
33import {GET } from ' ../modules/fetch.ts' ;
44import {toggleElem } from ' ../utils/dom.ts' ;
5- import {viewTreeStore } from ' ../modules/stores.ts' ;
6- import {setFileFolding } from ' ../features/file-fold.ts' ;
75import {ref , onMounted , onUnmounted } from ' vue' ;
86
9- const LOCAL_STORAGE_KEY = ' view_file_tree_visible' ;
10-
11- const store = viewTreeStore ();
12-
7+ const files = ref (null );
8+ const visible = ref (false );
139const isLoading = ref (false );
1410
1511onMounted (async () => {
1612 // Default to false
1713 updateVisibility (false );
1814 document .querySelector (' .view-toggle-file-tree-button' ).addEventListener (' click' , toggleVisibility );
1915
20- hashChangeListener ();
21- window .addEventListener (' hashchange' , hashChangeListener );
22-
2316 isLoading .value = true ;
24- const files = await loadChildren ();
25- store . files = files ;
17+ const children = await loadChildren ();
18+ files . value = children ;
2619 isLoading .value = false ;
2720});
2821
2922onUnmounted (() => {
3023 document .querySelector (' .view-toggle-file-tree-button' ).removeEventListener (' click' , toggleVisibility );
31- window .removeEventListener (' hashchange' , hashChangeListener );
3224});
3325
34- function hashChangeListener() {
35- store .selectedItem = window .location .hash ;
36- expandSelectedFile ();
37- }
38-
39- function expandSelectedFile() {
40- // expand file if the selected file is folded
41- if (store .selectedItem ) {
42- const box = document .querySelector (store .selectedItem );
43- const folded = box ?.getAttribute (' data-folded' ) === ' true' ;
44- if (folded ) setFileFolding (box , box .querySelector (' .fold-file' ), false );
45- }
46- }
47-
4826function toggleVisibility() {
49- updateVisibility (! store . fileTreeIsVisible );
27+ updateVisibility (! visible . value );
5028}
5129
52- function updateVisibility(visible ) {
53- store .fileTreeIsVisible = visible ;
54- localStorage .setItem (LOCAL_STORAGE_KEY , store .fileTreeIsVisible );
55- updateState (store .fileTreeIsVisible );
30+ function updateVisibility(_visible ) {
31+ visible .value = _visible ;
32+ updateState (visible .value );
5633}
5734
5835function updateState(visible ) {
@@ -67,7 +44,9 @@ function updateState(visible) {
6744}
6845
6946async function loadChildren(item ? ) {
70- const response = await GET (` /api/v1/repos/${window .config .pageData .viewFileInfo .apiBaseUrl }/contents/${item ? item .file .path : ' ' } ` );
47+ const el = document .querySelector (' #view-file-tree' );
48+ const apiBaseUrl = el .getAttribute (' data-api-base-url' );
49+ const response = await GET (` /api/v1/repos/${apiBaseUrl }/contents/${item ? item .file .path : ' ' } ` );
7150 const json = await response .json ();
7251 return json .map ((i ) => ({
7352 file: i ,
@@ -79,12 +58,11 @@ async function loadChildren(item?) {
7958
8059<template >
8160 <FileTree
82- v-if =" store.fileTreeIsVisible "
61+ v-if =" visible "
8362 id =" view-file-tree"
8463 :is-loading =" isLoading"
85- :files =" store. files"
64+ :files =" files"
8665 :collapsed =" true"
87- :selected =" store.selectedItem"
8866 :file-url-getter =" item => item.file.html_url"
8967 :load-children =" loadChildren"
9068 />
0 commit comments