item.vue 2.06 KB
Newer Older
1
<script>
Phil Hughes's avatar
Phil Hughes committed
2
import { escape } from 'underscore';
3
import fuzzaldrinPlus from 'fuzzaldrin-plus';
4
import FileIcon from '../../../vue_shared/components/file_icon.vue';
5
import ChangedFileIcon from '../changed_file_icon.vue';
6

Phil Hughes's avatar
Phil Hughes committed
7 8
const MAX_PATH_LENGTH = 60;

9 10
export default {
  components: {
11
    ChangedFileIcon,
12 13 14 15 16 17 18
    FileIcon,
  },
  props: {
    file: {
      type: Object,
      required: true,
    },
19 20 21 22 23 24 25 26
    focused: {
      type: Boolean,
      required: true,
    },
    searchText: {
      type: String,
      required: true,
    },
27 28 29 30
    index: {
      type: Number,
      required: true,
    },
31 32 33 34 35
  },
  methods: {
    clickRow() {
      this.$emit('click', this.file);
    },
36 37 38
    mouseOverRow() {
      this.$emit('mouseover', this.index);
    },
Phil Hughes's avatar
Phil Hughes committed
39
    highlightText(text, addEllipsis) {
Phil Hughes's avatar
Phil Hughes committed
40
      const escapedText = escape(text);
Phil Hughes's avatar
Phil Hughes committed
41
      const maxText =
Phil Hughes's avatar
Phil Hughes committed
42 43 44
        escapedText.length < MAX_PATH_LENGTH || !addEllipsis
          ? escapedText
          : `...${escapedText.substr(escapedText.length - MAX_PATH_LENGTH)}`;
Phil Hughes's avatar
Phil Hughes committed
45
      const occurrences = fuzzaldrinPlus.match(maxText, this.searchText);
46

Phil Hughes's avatar
Phil Hughes committed
47
      return maxText
48 49 50 51 52 53 54
        .split('')
        .map(
          (char, i) =>
            `<span class="${occurrences.indexOf(i) >= 0 ? 'highlighted' : ''}">${char}</span>`,
        )
        .join('');
    },
55 56 57 58 59 60
  },
};
</script>

<template>
  <a
61
    href="#"
62
    class="diff-changed-file"
63 64 65 66
    :class="{
      'is-focused': focused,
    }"
    @click.prevent="clickRow"
67
    @mouseover="mouseOverRow"
68 69 70 71 72 73 74
  >
    <file-icon
      :file-name="file.name"
      :size="16"
      css-classes="diff-file-changed-icon append-right-8"
    />
    <span class="diff-changed-file-content append-right-8">
75 76
      <strong
        class="diff-changed-file-name"
Phil Hughes's avatar
Phil Hughes committed
77
        v-html="highlightText(file.name, false)"
78
      >
79
      </strong>
80 81
      <span
        class="diff-changed-file-path prepend-top-5"
Phil Hughes's avatar
Phil Hughes committed
82
        v-html="highlightText(file.path, true)"
83
      >
84 85
      </span>
    </span>
86 87 88 89 90 91 92 93
    <span
      v-if="file.changed || file.tempFile"
      class="diff-changed-stats"
    >
      <changed-file-icon
        :file="file"
      />
    </span>
94 95
  </a>
</template>