|
| 1 | +<template> |
| 2 | + <div |
| 3 | + v-show="fileTreeIsVisible" |
| 4 | + id="diff-file-tree" |
| 5 | + class="mr-3 mt-3 diff-detail-box" |
| 6 | + > |
| 7 | + <!-- only render the tree if we're visible. in many cases this is something that doesn't change very often --> |
| 8 | + <div class="ui list" v-if="fileTreeIsVisible"> |
| 9 | + <DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item" /> |
| 10 | + </div> |
| 11 | + <div v-if="isIncomplete" id="diff-too-many-files-stats" class="pt-2"> |
| 12 | + <span>{{ tooManyFilesMessage }}</span><a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a> |
| 13 | + </div> |
| 14 | + </div> |
| 15 | +</template> |
| 16 | + |
| 17 | +<script> |
| 18 | +import DiffFileTreeItem from './DiffFileTreeItem.vue'; |
| 19 | +import {doLoadMoreFiles} from '../features/repo-diff.js'; |
| 20 | +
|
| 21 | +const {pageData} = window.config; |
| 22 | +const LOCAL_STORAGE_KEY = 'diff_file_tree_visible'; |
| 23 | +
|
| 24 | +export default { |
| 25 | + name: 'DiffFileTree', |
| 26 | + components: {DiffFileTreeItem}, |
| 27 | +
|
| 28 | + data: () => { |
| 29 | + const fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true'; |
| 30 | + pageData.diffFileInfo.fileTreeIsVisible = fileTreeIsVisible; |
| 31 | + return pageData.diffFileInfo; |
| 32 | + }, |
| 33 | +
|
| 34 | + computed: { |
| 35 | + fileTree() { |
| 36 | + const result = []; |
| 37 | + for (const file of this.files) { |
| 38 | + // Split file into directories |
| 39 | + const splits = file.Name.split('/'); |
| 40 | + let index = 0; |
| 41 | + let parent = null; |
| 42 | + let isFile = false; |
| 43 | + for (const split of splits) { |
| 44 | + index += 1; |
| 45 | + // reached the end |
| 46 | + if (index === splits.length) { |
| 47 | + isFile = true; |
| 48 | + } |
| 49 | + let newParent = { |
| 50 | + name: split, |
| 51 | + children: [], |
| 52 | + isFile |
| 53 | + }; |
| 54 | +
|
| 55 | + if (isFile === true) { |
| 56 | + newParent.file = file; |
| 57 | + } |
| 58 | +
|
| 59 | + if (parent) { |
| 60 | + // check if the folder already exists |
| 61 | + const existingFolder = parent.children.find( |
| 62 | + (x) => x.name === split |
| 63 | + ); |
| 64 | + if (existingFolder) { |
| 65 | + newParent = existingFolder; |
| 66 | + } else { |
| 67 | + parent.children.push(newParent); |
| 68 | + } |
| 69 | + } else { |
| 70 | + const existingFolder = result.find((x) => x.name === split); |
| 71 | + if (existingFolder) { |
| 72 | + newParent = existingFolder; |
| 73 | + } else { |
| 74 | + result.push(newParent); |
| 75 | + } |
| 76 | + } |
| 77 | + parent = newParent; |
| 78 | + } |
| 79 | + } |
| 80 | + const mergeChildIfOnlyOneDir = (entries) => { |
| 81 | + for (const entry of entries) { |
| 82 | + if (entry.children) { |
| 83 | + mergeChildIfOnlyOneDir(entry.children); |
| 84 | + } |
| 85 | + if (entry.children.length === 1 && entry.children[0].isFile === false) { |
| 86 | + // Merge it to the parent |
| 87 | + entry.name = `${entry.name}/${entry.children[0].name}`; |
| 88 | + entry.children = entry.children[0].children; |
| 89 | + } |
| 90 | + } |
| 91 | + }; |
| 92 | + // Merge folders with just a folder as children in order to |
| 93 | + // reduce the depth of our tree. |
| 94 | + mergeChildIfOnlyOneDir(result); |
| 95 | + return result; |
| 96 | + } |
| 97 | + }, |
| 98 | +
|
| 99 | + mounted() { |
| 100 | + // ensure correct buttons when we are mounted to the dom |
| 101 | + this.adjustToggleButton(this.fileTreeIsVisible); |
| 102 | + document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility); |
| 103 | + }, |
| 104 | + unmounted() { |
| 105 | + document.querySelector('.diff-toggle-file-tree-button').removeEventListener('click', this.toggleVisibility); |
| 106 | + }, |
| 107 | + methods: { |
| 108 | + toggleVisibility() { |
| 109 | + this.updateVisibility(!this.fileTreeIsVisible); |
| 110 | + }, |
| 111 | + updateVisibility(visible) { |
| 112 | + this.fileTreeIsVisible = visible; |
| 113 | + localStorage.setItem(LOCAL_STORAGE_KEY, this.fileTreeIsVisible); |
| 114 | + this.adjustToggleButton(this.fileTreeIsVisible); |
| 115 | + }, |
| 116 | + adjustToggleButton(visible) { |
| 117 | + const [toShow, toHide] = document.querySelectorAll('.diff-toggle-file-tree-button .icon'); |
| 118 | + toShow.classList.toggle('hide', visible); // hide the toShow icon if the tree is visible |
| 119 | + toHide.classList.toggle('hide', !visible); // similarly |
| 120 | + }, |
| 121 | + loadMoreData() { |
| 122 | + this.isLoadingNewData = true; |
| 123 | + doLoadMoreFiles(this.link, this.diffEnd, () => { |
| 124 | + this.isLoadingNewData = false; |
| 125 | + }); |
| 126 | + } |
| 127 | + }, |
| 128 | +}; |
| 129 | +</script> |
0 commit comments