all files / src/components/FileContent/Content/Version/ index.vue

97.44% Statements 38/39
100% Branches 12/12
100% Functions 1/1
97.44% Lines 38/39
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                                              14× 11×                                                                                                                                                                                                        
<template>
  <div class="version">
    <div class="info">
      <span class="info-count">{{ lines.added + lines.removed }}</span>
      <div class="info-diff">
        <span v-for="(item, index) in addNodes" :key="index" :class="item.class"></span>
        <Tooltip position="top">{{lines.added}} addition & {{lines.removed}} deletion</Tooltip>
      </div>
    </div>
    <div class="time">
      <span>{{ date }}</span>
      <span>{{ fromNow }}</span>
    </div>
  </div>
</template>
 
<script>
import moment from 'moment'
import Tooltip from '../../../Tooltip'
 
export default {
  name: 'Version',
  components: {
    Tooltip
  },
  props: {
    version: {
      type: Object,
      default: {}
    }
  },
  date () {
    return {
      fromNow: ''
    }
  },
  computed: {
    date () {
      return this.moment.format('hh:mm')
    },
 
    // calc diff data
    lines () {
      let ret = {
        'added': 0,
        'removed': 0,
        'unchange': 0
      }
 
      this.version.diff.map(d => {
        if (d.added) {
          ret.added += d.count
        } else if (d.removed) {
          ret.removed += d.count
        } else {
          ret.unchange += d.count
        }
      })
 
      return ret
    },
 
    // add state nodes
    addNodes () {
      let arr = []
 
      if (this.lines.added === 0) {
        if (this.lines.removed <= 5) {
          for (let i = 0; i < this.lines.removed; i++) {
            arr.push({
              class: 'removed-block'
            })
          }
 
          for (let j = this.lines.removed; j < 5; j++) {
            arr.push({
              class: 'normal-block'
            })
          }
        } else {
          for (let i = 0; i < 5; i++) {
            arr.push({
              class: 'removed-block'
            })
          }
        }
      } else if (this.lines.removed === 0) {
        if (this.lines.added <= 5) {
          for (let i = 0; i < this.lines.added; i++) {
            arr.push({
              class: 'added-block'
            })
          }
 
          for (let j = this.lines.added; j < 5; j++) {
            arr.push({
              class: 'normal-block'
            })
          }
        } else {
          for (let i = 0; i < 5; i++) {
            arr.push({
              class: 'added-block'
            })
          }
        }
      } else {
        let added = Math.floor(this.lines.added / (this.lines.added + this.lines.removed) * 5)
        let removed = Math.floor(this.lines.removed / (this.lines.added + this.lines.removed) * 5)
        let normal = 5 - added - removed
 
        for (let i = 0; i < added; i++) {
          arr.push({
            class: 'added-block'
          })
        }
 
        for (let j = 0; j < removed; j++) {
          arr.push({
            class: 'removed-block'
          })
        }
 
        for (let k = 0; k < normal; k++) {
          arr.push({
            class: 'normal-block'
          })
        }
      }
 
      return arr
    }
  },
  created () {
    this.moment = moment(this.version.date)
    this.fromNow = this.moment.fromNow()
  }
}
</script>
 
<style lang="scss" scoped>
.version {
  padding: 8px 12px;
  .info {
    display: flex;
    justify-content: space-between;
    height: 20px;
    .info-count {
      line-height: 20px;
      font-size: 14px;
      font-weight: 300;
    }
    .info-diff {
      position: relative;
      span {
        display: inline-block;
        width: 6px;
        height: 6px;
        margin-left: 1px;
      }
      .added-block {
        background: #2cbe4e;
      }
      .removed-block {
        background: #cb2431;
      }
      .normal-block {
        background: #d1d5da;
      }
    }
  }
  .time {
    display: flex;
    justify-content: space-between;
    height: 20px;
    font-size: 13px;
    font-weight: 300;
    opacity: .6;
    span {
      line-height: 20px;
    }
  }
}
</style>