all files / src/components/FileTree/Folder/ index.vue

85.71% Statements 12/14
80% Branches 8/10
0% Functions 0/1
92.31% Lines 12/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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                                                                                                                                                                                                                                               
<template>
  <div class="folder" v-show="isShow">
    <a href="#" class="name" :style="{ paddingLeft: (depth + 1) * 20 + 'px' }" @click="open = !open">
      <Icon class="icon" :extension="open ? 'folder-active' : 'folder'"/>
      <span class="text">
        <span v-for="(item, index) in nameArr.arr" :key="index"><span class="label">{{ item }}</span><b v-if="index !== nameArr.arr.length - 1">{{nameArr.keyword}}</b></span>
      </span>
    </a>
    <div v-show="open">
      <Folder ref="child" v-for="(item, index) in content.folders" :key="'folder -' + index" :content="item" :depth="depth + 1" :directory="fullPath + '/'" />
      <File ref="child" v-for="(item, index) in content.files.concat().sort((a, b) => a.name > b.name)" :key="'file -' + index" :content="item" :depth="depth + 1" />
    </div>
  </div>
</template>
 
<script>
import Icon from '../Icon'
import File from '../File'
import { mapGetters } from 'vuex'
 
export default {
  name: 'Folder',
  components: {
    Icon,
    File
  },
  props: {
    depth: Number,
    directory: {
      type: String,
      default: ''
    },
    content: Object
  },
  data () {
    return {
      open: true
    }
  },
  computed: {
    ...mapGetters('files', ['search']),
    fullPath () {
      return `${this.directory}${this.content.name}`
    },
    nameArr () {
      if (this.search) {
        if (this.search.split('/').indexOf(this.content.name) !== -1) {
          return {
            arr: ['', ''],
            keyword: this.content.name
          }
        } else {
          let keyword = this.search.split('/').pop()
 
          return {
            arr: this.content.name.split(keyword),
            keyword
          }
        }
      } else {
        return {
          arr: [this.content.name],
          keyword: ''
        }
      }
    },
    isShow () {
      // child node need to display
      Iif (this.$refs.child && this.$refs.child.find(item => item.isShow)) {
        return true
      }
 
      // judge by search input
      if (this.search) {
        return this.nameArr.length > 1
      } else {
        return true
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
.folder {
  display: inline-block;
  min-width: 100%;
  .name {
    display: block;
    padding: 6px;
    height: 16px;
    line-height: 16px;
    font-size: 14px;
    white-space: nowrap;
    color: #fff;
    text-decoration: none;
 
    .icon {
      opacity: .6;
      width: 12px;
      height: 12px;
    }
 
    .text {
      display: inline-block;
      padding: 0 21px 0 6px;
      .label {
        opacity: .6;
        font-weight: 300;
      }
      b {
        color: #ff6;
        font-weight: bold;
      }
    }
 
    &:hover {
      .icon {
        opacity: 1;
      }
 
      .text {
        .label {
          color: #4bd1c5;
          opacity: 1;
        }
      }
    }
  }
}
</style>