file_row.vue 5.21 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1 2
<script>
import Icon from '~/vue_shared/components/icon.vue';
3
import FileHeader from '~/vue_shared/components/file_row_header.vue';
Phil Hughes's avatar
Phil Hughes committed
4
import FileIcon from '~/vue_shared/components/file_icon.vue';
5
import ChangedFileIcon from '~/vue_shared/components/changed_file_icon.vue';
Phil Hughes's avatar
Phil Hughes committed
6 7 8 9

export default {
  name: 'FileRow',
  components: {
10
    FileHeader,
Phil Hughes's avatar
Phil Hughes committed
11 12
    FileIcon,
    Icon,
13
    ChangedFileIcon,
Phil Hughes's avatar
Phil Hughes committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  },
  props: {
    file: {
      type: Object,
      required: true,
    },
    level: {
      type: Number,
      required: true,
    },
    extraComponent: {
      type: Object,
      required: false,
      default: null,
    },
29 30 31 32 33 34 35 36 37 38
    hideExtraOnTree: {
      type: Boolean,
      required: false,
      default: false,
    },
    showChangedIcon: {
      type: Boolean,
      required: false,
      default: false,
    },
Phil Hughes's avatar
Phil Hughes committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  },
  data() {
    return {
      mouseOver: false,
    };
  },
  computed: {
    isTree() {
      return this.file.type === 'tree';
    },
    isBlob() {
      return this.file.type === 'blob';
    },
    levelIndentation() {
      return {
54
        marginLeft: this.level ? `${this.level * 16}px` : null,
Phil Hughes's avatar
Phil Hughes committed
55 56 57 58 59 60 61 62 63 64
      };
    },
    fileClass() {
      return {
        'file-open': this.isBlob && this.file.opened,
        'is-active': this.isBlob && this.file.active,
        folder: this.isTree,
        'is-open': this.file.opened,
      };
    },
65 66
    childFilesLevel() {
      return this.file.isHeader ? 0 : this.level + 1;
67
    },
Phil Hughes's avatar
Phil Hughes committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  },
  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);
    },
85 86 87
    clickedFile(path) {
      this.$emit('clickFile', path);
    },
Phil Hughes's avatar
Phil Hughes committed
88 89 90 91 92 93 94
    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}`);
95 96

      if (this.isBlob) this.clickedFile(this.file.path);
Phil Hughes's avatar
Phil Hughes committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    },
    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}`;
    },
    toggleHover(over) {
      this.mouseOver = over;
    },
  },
};
</script>

<template>
  <div>
135
    <file-header v-if="file.isHeader" :path="file.path" />
Phil Hughes's avatar
Phil Hughes committed
136
    <div
137
      v-else
Phil Hughes's avatar
Phil Hughes committed
138
      :class="fileClass"
139
      :title="file.name"
Phil Hughes's avatar
Phil Hughes committed
140 141 142
      class="file-row"
      role="button"
      @click="clickFile"
143
      @mouseover="toggleHover(true)"
144
      @mouseleave="toggleHover(false)"
Phil Hughes's avatar
Phil Hughes committed
145
    >
Mike Greiling's avatar
Mike Greiling committed
146 147
      <div class="file-row-name-container">
        <span ref="textOutput" :style="levelIndentation" class="file-row-name str-truncated">
Phil Hughes's avatar
Phil Hughes committed
148
          <file-icon
149
            v-if="!showChangedIcon || file.type === 'tree'"
Phil Hughes's avatar
Phil Hughes committed
150 151 152 153 154 155
            :file-name="file.name"
            :loading="file.loading"
            :folder="isTree"
            :opened="file.opened"
            :size="16"
          />
Mike Greiling's avatar
Mike Greiling committed
156
          <changed-file-icon v-else :file="file" :size="16" class="append-right-5" />
157
          {{ file.name }}
Phil Hughes's avatar
Phil Hughes committed
158 159 160
        </span>
        <component
          :is="extraComponent"
161
          v-if="extraComponent && !(hideExtraOnTree && file.type === 'tree')"
Phil Hughes's avatar
Phil Hughes committed
162 163 164 165 166
          :file="file"
          :mouse-over="mouseOver"
        />
      </div>
    </div>
167
    <template v-if="file.opened || file.isHeader">
Phil Hughes's avatar
Phil Hughes committed
168 169 170 171
      <file-row
        v-for="childFile in file.tree"
        :key="childFile.key"
        :file="childFile"
172
        :level="childFilesLevel"
173
        :hide-extra-on-tree="hideExtraOnTree"
Phil Hughes's avatar
Phil Hughes committed
174
        :extra-component="extraComponent"
175
        :show-changed-icon="showChangedIcon"
Phil Hughes's avatar
Phil Hughes committed
176
        @toggleTreeOpen="toggleTreeOpen"
177
        @clickFile="clickedFile"
Phil Hughes's avatar
Phil Hughes committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      />
    </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>