debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/file_row.vue

236 lines
5.2 KiB
Vue
Raw Normal View History

2018-12-05 23:21:45 +05:30
<script>
import Icon from '~/vue_shared/components/icon.vue';
2019-03-02 22:35:43 +05:30
import FileHeader from '~/vue_shared/components/file_row_header.vue';
2018-12-05 23:21:45 +05:30
import FileIcon from '~/vue_shared/components/file_icon.vue';
import ChangedFileIcon from '~/vue_shared/components/changed_file_icon.vue';
export default {
name: 'FileRow',
components: {
2019-03-02 22:35:43 +05:30
FileHeader,
2018-12-05 23:21:45 +05:30
FileIcon,
Icon,
ChangedFileIcon,
},
props: {
file: {
type: Object,
required: true,
},
level: {
type: Number,
required: true,
},
extraComponent: {
type: Object,
required: false,
default: null,
},
hideExtraOnTree: {
type: Boolean,
required: false,
default: false,
},
showChangedIcon: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
2019-05-18 00:54:41 +05:30
dropdownOpen: false,
2018-12-05 23:21:45 +05:30
};
},
computed: {
isTree() {
return this.file.type === 'tree';
},
isBlob() {
return this.file.type === 'blob';
},
levelIndentation() {
return {
2019-03-02 22:35:43 +05:30
marginLeft: this.level ? `${this.level * 16}px` : null,
2018-12-05 23:21:45 +05:30
};
},
fileClass() {
return {
'file-open': this.isBlob && this.file.opened,
'is-active': this.isBlob && this.file.active,
folder: this.isTree,
'is-open': this.file.opened,
};
},
2019-03-02 22:35:43 +05:30
childFilesLevel() {
return this.file.isHeader ? 0 : this.level + 1;
2018-12-13 13:39:08 +05:30
},
2018-12-05 23:21:45 +05:30
},
watch: {
'file.active': function fileActiveWatch(active) {
if (this.file.type === 'blob' && active) {
this.scrollIntoView();
}
},
},
mounted() {
if (this.hasPathAtCurrentRoute()) {
this.scrollIntoView(true);
}
},
methods: {
toggleTreeOpen(path) {
this.$emit('toggleTreeOpen', path);
},
clickedFile(path) {
this.$emit('clickFile', path);
},
clickFile() {
// Manual Action if a tree is selected/opened
if (this.isTree && this.hasUrlAtCurrentRoute()) {
this.toggleTreeOpen(this.file.path);
}
if (this.$router) this.$router.push(`/project${this.file.url}`);
if (this.isBlob) this.clickedFile(this.file.path);
},
scrollIntoView(isInit = false) {
const block = isInit && this.isTree ? 'center' : 'nearest';
this.$el.scrollIntoView({
behavior: 'smooth',
block,
});
},
hasPathAtCurrentRoute() {
if (!this.$router || !this.$router.currentRoute) {
return false;
}
// - strip route up to "/-/" and ending "/"
const routePath = this.$router.currentRoute.path
.replace(/^.*?[/]-[/]/g, '')
.replace(/[/]$/g, '');
// - strip ending "/"
const filePath = this.file.path.replace(/[/]$/g, '');
return filePath === routePath;
},
hasUrlAtCurrentRoute() {
if (!this.$router || !this.$router.currentRoute) return true;
return this.$router.currentRoute.path === `/project${this.file.url}`;
},
2019-05-18 00:54:41 +05:30
toggleDropdown(val) {
this.dropdownOpen = val;
2018-12-05 23:21:45 +05:30
},
},
};
</script>
<template>
<div>
2019-03-02 22:35:43 +05:30
<file-header v-if="file.isHeader" :path="file.path" />
2018-12-05 23:21:45 +05:30
<div
2019-03-02 22:35:43 +05:30
v-else
2018-12-05 23:21:45 +05:30
:class="fileClass"
2019-05-18 00:54:41 +05:30
:title="file.name"
2018-12-05 23:21:45 +05:30
class="file-row"
role="button"
@click="clickFile"
2019-05-18 00:54:41 +05:30
@mouseleave="toggleDropdown(false)"
2018-12-05 23:21:45 +05:30
>
2019-02-15 15:39:39 +05:30
<div class="file-row-name-container">
<span ref="textOutput" :style="levelIndentation" class="file-row-name str-truncated">
2018-12-05 23:21:45 +05:30
<file-icon
v-if="!showChangedIcon || file.type === 'tree'"
:file-name="file.name"
:loading="file.loading"
:folder="isTree"
:opened="file.opened"
:size="16"
/>
2019-02-15 15:39:39 +05:30
<changed-file-icon v-else :file="file" :size="16" class="append-right-5" />
2019-03-02 22:35:43 +05:30
{{ file.name }}
2018-12-05 23:21:45 +05:30
</span>
<component
:is="extraComponent"
v-if="extraComponent && !(hideExtraOnTree && file.type === 'tree')"
:file="file"
2019-05-18 00:54:41 +05:30
:dropdown-open="dropdownOpen"
@toggle="toggleDropdown($event)"
2018-12-05 23:21:45 +05:30
/>
</div>
</div>
2019-03-02 22:35:43 +05:30
<template v-if="file.opened || file.isHeader">
2018-12-05 23:21:45 +05:30
<file-row
v-for="childFile in file.tree"
:key="childFile.key"
:file="childFile"
2019-03-02 22:35:43 +05:30
:level="childFilesLevel"
2018-12-05 23:21:45 +05:30
:hide-extra-on-tree="hideExtraOnTree"
:extra-component="extraComponent"
:show-changed-icon="showChangedIcon"
@toggleTreeOpen="toggleTreeOpen"
@clickFile="clickedFile"
/>
</template>
</div>
</template>
<style>
.file-row {
display: flex;
align-items: center;
height: 32px;
padding: 4px 8px;
margin-left: -8px;
margin-right: -8px;
border-radius: 3px;
text-align: left;
cursor: pointer;
}
.file-row:hover,
.file-row:focus {
background: #f2f2f2;
}
.file-row:active {
background: #dfdfdf;
}
.file-row.is-active {
background: #f2f2f2;
}
.file-row-name-container {
display: flex;
width: 100%;
align-items: center;
overflow: visible;
}
.file-row-name {
display: inline-block;
flex: 1;
max-width: inherit;
height: 18px;
line-height: 16px;
text-overflow: ellipsis;
white-space: nowrap;
}
.file-row-name svg {
margin-right: 2px;
vertical-align: middle;
}
.file-row-name .loading-container {
display: inline-block;
margin-right: 4px;
}
</style>