Skip to content

Commit 9fc2ba3

Browse files
committed
Refactor initViewFileTreeSidebar using the new registerGlobalInitFunc
1 parent 2d5a570 commit 9fc2ba3

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

templates/repo/home.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" (Iif .RepoPreferences.ShowFileViewTreeSidebar "repo-grid-tree-sidebar" "repo-grid-filelist-only")}}">
2121
{{if .TreeNames}}
22-
<div class="repo-view-file-tree-sidebar not-mobile {{if not .RepoPreferences.ShowFileViewTreeSidebar}}tw-hidden{{end}}" {{if .IsSigned}} data-is-signed {{end}}>{{template "repo/view_file_tree_sidebar" .}}</div>
22+
<div class="repo-view-file-tree-sidebar not-mobile {{if not .RepoPreferences.ShowFileViewTreeSidebar}}tw-hidden{{end}}" data-global-init="initViewFileTreeSidebar" {{if .IsSigned}} data-is-signed {{end}}>{{template "repo/view_file_tree_sidebar" .}}</div>
2323
{{end}}
2424

2525
<div class="repo-home-filelist">

web_src/js/features/repo-view-file-tree-sidebar.ts

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import {createApp, ref} from 'vue';
22
import {toggleElem} from '../utils/dom.ts';
33
import {GET, PUT} from '../modules/fetch.ts';
4+
import {registerGlobalInitFunc} from '../modules/observer.ts';
45
import ViewFileTree from '../components/ViewFileTree.vue';
56

6-
async function toggleSidebar(visibility: boolean, isSigned: boolean) {
7-
const sidebarEl = document.querySelector('.repo-view-file-tree-sidebar');
7+
async function toggleSidebar(sidebarEl: HTMLElement, visibility: boolean) {
88
const showBtnEl = document.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);
1212
toggleElem(sidebarEl, visibility);
1313
toggleElem(showBtnEl, !visibility);
1414

15-
if (!isSigned) return;
15+
if (!sidebarEl.hasAttribute('data-is-signed')) return;
1616

1717
// save to session
1818
await PUT('/repo/preferences', {
@@ -40,55 +40,52 @@ async function loadChildren(path: string, recursive?: boolean) {
4040
return null;
4141
}
4242

43-
async function loadContent() {
43+
async function loadContent(sidebarEl: HTMLElement) {
4444
// load content by path (content based on home_content.tmpl)
4545
const response = await GET(`${window.location.href}?only_content=true`);
4646
const contentEl = document.querySelector('.repo-home-filelist');
4747
contentEl.innerHTML = await response.text();
48-
reloadContentScript(contentEl);
48+
reloadContentScript(sidebarEl, contentEl);
4949
}
5050

51-
function reloadContentScript(contentEl: Element) {
51+
function reloadContentScript(sidebarEl: HTMLElement, contentEl: Element) {
5252
contentEl.querySelector('.show-tree-sidebar-button').addEventListener('click', () => {
53-
toggleSidebar(true, document.querySelector('.repo-view-file-tree-sidebar').hasAttribute('data-is-signed'));
53+
toggleSidebar(sidebarEl, true);
5454
});
5555
}
5656

57-
export async function initViewFileTreeSidebar() {
58-
const sidebarElement = document.querySelector('.repo-view-file-tree-sidebar');
59-
if (!sidebarElement) return;
60-
61-
const isSigned = sidebarElement.hasAttribute('data-is-signed');
62-
63-
document.querySelector('.hide-tree-sidebar-button').addEventListener('click', () => {
64-
toggleSidebar(false, isSigned);
65-
});
66-
document.querySelector('.repo-home-filelist .show-tree-sidebar-button').addEventListener('click', () => {
67-
toggleSidebar(true, isSigned);
68-
});
69-
70-
const fileTree = document.querySelector('#view-file-tree');
71-
const baseUrl = fileTree.getAttribute('data-api-base-url');
72-
const treePath = fileTree.getAttribute('data-tree-path');
73-
const refType = fileTree.getAttribute('data-current-ref-type');
74-
const refName = fileTree.getAttribute('data-current-ref-short-name');
75-
const refString = (refType ? (`/${refType}`) : '') + (refName ? (`/${refName}`) : '');
76-
77-
const selectedItem = ref(treePath);
78-
79-
const files = await loadChildren(treePath, true);
80-
81-
fileTree.classList.remove('is-loading');
82-
const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (path: string) => {
83-
window.history.pushState(null, null, `${baseUrl}/src${refString}/${path}`);
84-
selectedItem.value = path;
85-
loadContent();
86-
}});
87-
fileTreeView.mount(fileTree);
88-
89-
window.addEventListener('popstate', () => {
90-
selectedItem.value = extractPath(window.location.href);
91-
loadContent();
57+
export function initViewFileTreeSidebar() {
58+
registerGlobalInitFunc('initViewFileTreeSidebar', async (el: HTMLElement) => {
59+
document.querySelector('.hide-tree-sidebar-button').addEventListener('click', () => {
60+
toggleSidebar(el, false);
61+
});
62+
document.querySelector('.repo-home-filelist .show-tree-sidebar-button').addEventListener('click', () => {
63+
toggleSidebar(el, true);
64+
});
65+
66+
const fileTree = document.querySelector('#view-file-tree');
67+
const baseUrl = fileTree.getAttribute('data-api-base-url');
68+
const treePath = fileTree.getAttribute('data-tree-path');
69+
const refType = fileTree.getAttribute('data-current-ref-type');
70+
const refName = fileTree.getAttribute('data-current-ref-short-name');
71+
const refString = (refType ? (`/${refType}`) : '') + (refName ? (`/${refName}`) : '');
72+
73+
const selectedItem = ref(treePath);
74+
75+
const files = await loadChildren(treePath, true);
76+
77+
fileTree.classList.remove('is-loading');
78+
const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (path: string) => {
79+
window.history.pushState(null, null, `${baseUrl}/src${refString}/${path}`);
80+
selectedItem.value = path;
81+
loadContent(el);
82+
}});
83+
fileTreeView.mount(fileTree);
84+
85+
window.addEventListener('popstate', () => {
86+
selectedItem.value = extractPath(window.location.href);
87+
loadContent(el);
88+
});
9289
});
9390
}
9491

0 commit comments

Comments
 (0)