All files / app/assets/javascripts/vue_shared/components diff_stats_dropdown.vue

14.28% Statements 2/14
0% Branches 0/7
0% Functions 0/9
16.66% Lines 2/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          7x             7x                                                                                                                                                                                                                                                                                                                          
<script>
import { GlDisclosureDropdown, GlIcon, GlSearchBoxByType, GlSprintf } from '@gitlab/ui';
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { __, n__, s__, sprintf } from '~/locale';
 
export const i18n = {
  messageAdditionsDeletions: s__('Diffs|with %{additions} and %{deletions}'),
  noFilesFound: __('No files found.'),
  noFileNameAvailable: s__('Diffs|No file name available'),
  searchFiles: __('Search files'),
};
 
const variantCssColorMap = {
  success: 'gl-text-green-500',
  danger: 'gl-text-red-500',
};
 
export default {
  i18n,
  components: {
    GlDisclosureDropdown,
    GlIcon,
    GlSearchBoxByType,
    GlSprintf,
  },
  props: {
    changed: {
      type: Number,
      required: true,
    },
    added: {
      type: Number,
      required: true,
    },
    deleted: {
      type: Number,
      required: true,
    },
    files: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      search: '',
    };
  },
  computed: {
    filteredFiles() {
      return this.search.length > 0
        ? fuzzaldrinPlus.filter(this.files, this.search, { key: 'name' })
        : this.files;
    },
    dropdownItems() {
      return this.filteredFiles.map((file) => {
        return {
          ...file,
          text: file.name || this.$options.i18n.noFileNameAvailable,
          iconColor: variantCssColorMap[file.iconColor],
        };
      });
    },
    messageChanged() {
      return sprintf(
        n__(
          'Diffs|Showing %{dropdownStart}%{count} changed file%{dropdownEnd}',
          'Diffs|Showing %{dropdownStart}%{count} changed files%{dropdownEnd}',
          this.changed,
        ),
        { count: this.changed },
      );
    },
  },
  methods: {
    focusInput() {
      this.$refs.search.focusInput();
    },
    focusFirstItem() {
      Iif (!this.filteredFiles.length) return;
      this.$el.querySelector('.gl-new-dropdown-item:first-child').focus();
    },
    additionsText(numberOfChanges = this.added) {
      return n__('Diffs|%d addition', 'Diffs|%d additions', numberOfChanges);
    },
    deletionsText(numberOfChanges = this.deleted) {
      return n__('Diffs|%d deletion', 'Diffs|%d deletions', numberOfChanges);
    },
  },
};
</script>
 
<template>
  <div>
    <gl-sprintf :message="messageChanged">
      <template #dropdown="{ content: dropdownText }">
        <gl-disclosure-dropdown
          :toggle-text="dropdownText"
          :items="dropdownItems"
          category="tertiary"
          variant="confirm"
          data-testid="diff-stats-dropdown"
          class="gl-vertical-align-baseline"
          toggle-class="gl-px-0! gl-font-weight-bold!"
          fluid-width
          @shown="focusInput"
        >
          <template #header>
            <gl-search-box-by-type
              ref="search"
              v-model.trim="search"
              :placeholder="$options.i18n.searchFiles"
              class="gl-mx-3 gl-my-4"
              @keydown.down="focusFirstItem"
            />
            <span v-if="!filteredFiles.length" class="gl-mx-3">
              {{ $options.i18n.noFilesFound }}
            </span>
          </template>
          <template #list-item="{ item }">
            <div class="gl-display-flex gl-gap-3 gl-align-items-center gl-overflow-hidden">
              <gl-icon :name="item.icon" :class="item.iconColor" class="gl-flex-shrink-0" />
              <div class="gl-flex-grow-1 gl-overflow-hidden">
                <div class="gl-display-flex">
                  <span
                    class="gl-font-weight-bold gl-mr-3 gl-flex-grow-1"
                    :class="item.name ? 'gl-text-truncate' : 'gl-italic gl-gray-400'"
                    >{{ item.text }}</span
                  >
                  <span class="gl-ml-auto gl-white-space-nowrap" aria-hidden="true">
                    <span class="gl-text-green-600">+{{ item.added }}</span>
                    <span class="gl-text-red-500">-{{ item.removed }}</span>
                  </span>
                  <span class="gl-sr-only"
                    >{{ additionsText(item.added) }}, {{ deletionsText(item.removed) }}</span
                  >
                </div>
                <div class="gl-text-gray-700 gl-overflow-hidden gl-text-overflow-ellipsis">
                  {{ item.path }}
                </div>
              </div>
            </div>
          </template>
        </gl-disclosure-dropdown>
      </template>
    </gl-sprintf>
    <span
      class="diff-stats-additions-deletions-expanded"
      data-testid="diff-stats-additions-deletions-expanded"
    >
      <gl-sprintf :message="$options.i18n.messageAdditionsDeletions">
        <template #additions>
          <span class="gl-text-green-600 gl-font-weight-bold">{{ additionsText() }}</span>
        </template>
        <template #deletions>
          <span class="gl-text-red-500 gl-font-weight-bold">{{ deletionsText() }}</span>
        </template>
      </gl-sprintf>
    </span>
  </div>
</template>
 
<style scoped>
/* TODO: Use max-height prop when gitlab-ui got updated.
See https://gitlab.com/gitlab-org/gitlab-ui/-/issues/2374 */
::v-deep .gl-new-dropdown-inner {
  max-height: 310px;
}
</style>