Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reactive store to share data between components #23996

Merged
merged 7 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions web_src/js/components/DiffFileTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
<div class="ui list">
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item" :selected-file="selectedFile"/>
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item"/>
</div>
<div v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2">
<span class="gt-mr-2">{{ tooManyFilesMessage }}</span><a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a>
Expand All @@ -17,6 +17,7 @@
import DiffFileTreeItem from './DiffFileTreeItem.vue';
import {doLoadMoreFiles} from '../features/repo-diff.js';
import {toggleElem} from '../utils/dom.js';
import {store} from './DiffFileTreeStore.js';

const {pageData} = window.config;
const LOCAL_STORAGE_KEY = 'diff_file_tree_visible';
Expand All @@ -28,7 +29,7 @@ export default {
pageData.diffFileInfo.fileTreeIsVisible = fileTreeIsVisible;
return {
...pageData.diffFileInfo,
selectedFile: ''
store,
};
},
computed: {
Expand Down Expand Up @@ -102,10 +103,10 @@ export default {
document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility);

this.hashChangeListener = () => {
this.selectedFile = window.location.hash;
this.store.selectedItem = window.location.hash;
};
this.hashListener = window.addEventListener('hashchange', this.hashChangeListener);
this.selectedFile = window.location.hash;
this.hashChangeListener();
window.addEventListener('hashchange', this.hashChangeListener);
},
unmounted() {
document.querySelector('.diff-toggle-file-tree-button').removeEventListener('click', this.toggleVisibility);
Expand Down
14 changes: 4 additions & 10 deletions web_src/js/components/DiffFileTreeItem.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div v-show="show" :title="item.name">
<!--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"-->
<div class="item" :class="[item.isFile ? 'filewrapper gt-p-1 gt-ac' : '', selectedFile === genCompleteFileHash(item.file?.NameHash) ? 'selected' : '']">
<div class="item" :class="[item.isFile ? 'filewrapper gt-p-1 gt-ac' : '', store.selectedItem === '#diff-' + item.file?.NameHash ? 'selected' : '']">
<!-- Files -->
<SvgIcon
v-if="item.isFile"
Expand Down Expand Up @@ -32,14 +32,15 @@
<span class="gt-ellipsis">{{ item.name }}</span>
</div>
<div v-show="!collapsed">
<DiffFileTreeItem v-for="childItem in item.children" :key="childItem.name" :item="childItem" class="list" :selected-file="selectedFile"/>
<DiffFileTreeItem v-for="childItem in item.children" :key="childItem.name" :item="childItem" class="list"/>
</div>
</div>
</div>
</template>

<script>
import {SvgIcon} from '../svg.js';
import {store} from './DiffFileTreeStore.js';

export default {
components: {SvgIcon},
Expand All @@ -53,13 +54,9 @@ export default {
required: false,
default: true
},
selectedFile: {
type: String,
default: '',
required: true
}
},
data: () => ({
store,
collapsed: false,
}),
methods: {
Expand All @@ -79,9 +76,6 @@ export default {
};
return diffTypes[pType];
},
genCompleteFileHash(hash) {
return `#diff-${hash}`;
}
},
};
</script>
Expand Down
5 changes: 5 additions & 0 deletions web_src/js/components/DiffFileTreeStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {reactive} from 'vue';

export const store = reactive({
selectedItem: '',
});