Skip to content

Commit c4e7f0c

Browse files
committed
add tree sidebar to file view
1 parent 59e46d4 commit c4e7f0c

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

templates/repo/home.tmpl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
{{template "base/head" .}}
2+
{{$treeNamesLen := len .TreeNames}}
3+
{{$isTreePathRoot := eq $treeNamesLen 0}}
4+
{{$showSidebar := $isTreePathRoot}}
5+
{{$hasTreeSidebar := not $isTreePathRoot}}
6+
{{$showTreeSidebar := .RepoPreferences.ShowFileViewTreeSidebar}}
7+
{{$hideTreeSidebar := not $showTreeSidebar}}
8+
{{$hasAndShowTreeSidebar := and $hasTreeSidebar $showTreeSidebar}}
29
<div role="main" aria-label="{{.Title}}" class="page-content repository file list {{if .IsBlame}}blame{{end}}">
310
{{template "repo/header" .}}
411
<div class="ui container {{if .IsBlame}}fluid padded{{end}}">
@@ -16,13 +23,6 @@
1623

1724
{{template "repo/code/recently_pushed_new_branches" .}}
1825

19-
{{$treeNamesLen := len .TreeNames}}
20-
{{$isTreePathRoot := eq $treeNamesLen 0}}
21-
{{$showSidebar := $isTreePathRoot}}
22-
{{$hasTreeSidebar := not $isTreePathRoot}}
23-
{{$showTreeSidebar := .RepoPreferences.ShowFileViewTreeSidebar}}
24-
{{$hideTreeSidebar := not $showTreeSidebar}}
25-
{{$hasAndShowTreeSidebar := and $hasTreeSidebar $showTreeSidebar}}
2626
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" (Iif $showTreeSidebar "repo-grid-tree-sidebar" "repo-grid-filelist-only")}}">
2727
{{if $hasTreeSidebar}}
2828
<div class="repo-view-file-tree-sidebar not-mobile {{if $hideTreeSidebar}}tw-hidden{{end}}">{{template "repo/view_file_tree_sidebar" .}}</div>

web_src/js/components/ViewFileTree.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import ViewFileTreeItem from './ViewFileTreeItem.vue';
33
44
defineProps<{
55
files: any,
6-
selectedItem: string,
6+
selectedItem: any,
77
loadChildren: any,
8+
loadContent: any;
89
}>();
910
</script>
1011

1112
<template>
1213
<div class="view-file-tree-items">
1314
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
14-
<ViewFileTreeItem v-for="item in files" :key="item.name" :item="item" :selected-item="selectedItem" :load-children="loadChildren"/>
15+
<ViewFileTreeItem v-for="item in files" :key="item.name" :item="item" :selected-item="selectedItem" :load-content="loadContent" :load-children="loadChildren"/>
1516
</div>
1617
</template>
1718

web_src/js/components/ViewFileTreeItem.vue

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ type Item = {
1212
1313
const props = defineProps<{
1414
item: Item,
15+
loadContent: any;
1516
loadChildren: any;
16-
selectedItem?: string;
17+
selectedItem?: any;
1718
}>();
1819
1920
const isLoading = ref(false);
20-
const collapsed = ref(!props.item.children);
2121
const children = ref(props.item.children);
22+
const collapsed = ref(!props.item.children);
2223
2324
const doLoadChildren = async () => {
2425
collapsed.value = !collapsed.value;
@@ -32,19 +33,19 @@ const doLoadChildren = async () => {
3233
3334
const doLoadDirContent = () => {
3435
doLoadChildren();
35-
window.location.href = props.item.htmlUrl;
36+
props.loadContent(props.item);
3637
};
3738
3839
const doLoadFileContent = () => {
39-
window.location.href = props.item.htmlUrl;
40+
props.loadContent(props.item);
4041
};
4142
</script>
4243

4344
<template>
4445
<!--title instead of tooltip above as the tooltip needs too much work with the current methods, i.e. not being loaded or staying open for "too long"-->
4546
<div
4647
v-if="item.isFile" class="item-file"
47-
:class="{'selected': selectedItem === item.path}"
48+
:class="{'selected': selectedItem.value === item.path}"
4849
:title="item.name"
4950
@click.stop="doLoadFileContent"
5051
>
@@ -54,7 +55,7 @@ const doLoadFileContent = () => {
5455
</div>
5556
<div
5657
v-else class="item-directory"
57-
:class="{'selected': selectedItem === item.path}"
58+
:class="{'selected': selectedItem.value === item.path}"
5859
:title="item.name"
5960
@click.stop="doLoadDirContent"
6061
>
@@ -66,7 +67,7 @@ const doLoadFileContent = () => {
6667
</div>
6768

6869
<div v-if="children?.length" v-show="!collapsed" class="sub-items">
69-
<ViewFileTreeItem v-for="childItem in children" :key="childItem.name" :item="childItem" :selected-item="selectedItem" :load-children="loadChildren"/>
70+
<ViewFileTreeItem v-for="childItem in children" :key="childItem.name" :item="childItem" :selected-item="selectedItem" :load-content="loadContent" :load-children="loadChildren"/>
7071
</div>
7172
</template>
7273
<style scoped>

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createApp} from 'vue';
1+
import {createApp, ref} from 'vue';
22
import {toggleElem} from '../utils/dom.ts';
33
import {GET, PUT} from '../modules/fetch.ts';
44
import ViewFileTree from '../components/ViewFileTree.vue';
@@ -64,6 +64,10 @@ async function loadRecursive(treePath) {
6464
return root;
6565
}
6666

67+
async function loadContent(item) {
68+
document.querySelector('.repo-home-filelist').innerHTML = `load content of ${item.path}`;
69+
}
70+
6771
export async function initViewFileTreeSidebar() {
6872
const sidebarElement = document.querySelector('.repo-view-file-tree-sidebar');
6973
if (!sidebarElement) return;
@@ -78,10 +82,15 @@ export async function initViewFileTreeSidebar() {
7882

7983
const fileTree = document.querySelector('#view-file-tree');
8084
const treePath = fileTree.getAttribute('data-tree-path');
85+
const selectedItem = ref(treePath);
8186

8287
const files = await loadRecursive(treePath);
8388

8489
fileTree.classList.remove('center');
85-
const fileTreeView = createApp(ViewFileTree, {files, selectedItem: treePath, loadChildren});
90+
const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (item) => {
91+
window.history.pushState(null, null, item.htmlUrl);
92+
selectedItem.value = item.path;
93+
loadContent(item);
94+
}});
8695
fileTreeView.mount(fileTree);
8796
}

0 commit comments

Comments
 (0)