All files / app/assets/javascripts/vue_shared/components/sidebar/labels_select_vue dropdown_contents_labels_view.vue

86.67% Statements 39/45
84.21% Branches 32/38
100% Functions 15/15
86.67% Lines 39/45

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 171 172 173 174 175 176 177 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  21x             21x 21x   21x   21x                       40x                                   51x 6x       45x     41x           46x               5x 3x                           2x               4x   4x                     1x     1x 1x               1x     1x 1x           6x 6x 1x 5x       2x 3x 2x 2x     2x 1x 1x     6x     5x 5x         2x 3x           440x                                                                                                                                          
<script>
import {
  GlIntersectionObserver,
  GlLoadingIcon,
  GlButton,
  GlSearchBoxByType,
  GlLink,
} from '@gitlab/ui';
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { mapState, mapGetters, mapActions } from 'vuex';
 
import { UP_KEY_CODE, DOWN_KEY_CODE, ENTER_KEY_CODE, ESC_KEY_CODE } from '~/lib/utils/keycodes';
 
import LabelItem from './label_item.vue';
 
export default {
  components: {
    GlIntersectionObserver,
    GlLoadingIcon,
    GlButton,
    GlSearchBoxByType,
    GlLink,
    LabelItem,
  },
  data() {
    return {
      searchKey: '',
      currentHighlightItem: -1,
    };
  },
  computed: {
    ...mapState([
      'allowLabelCreate',
      'allowMultiselect',
      'labelsManagePath',
      'labels',
      'labelsFetchInProgress',
      'labelsListTitle',
      'footerCreateLabelTitle',
      'footerManageLabelTitle',
    ]),
    ...mapGetters(['selectedLabelsList', 'isDropdownVariantSidebar', 'isDropdownVariantEmbedded']),
    visibleLabels() {
      if (this.searchKey) {
        return fuzzaldrinPlus.filter(this.labels, this.searchKey, {
          key: ['title'],
        });
      }
      return this.labels;
    },
    showDropdownFooter() {
      return (
        (this.isDropdownVariantSidebar || this.isDropdownVariantEmbedded) &&
        (this.allowLabelCreate || this.labelsManagePath)
      );
    },
    showNoMatchingResultsMessage() {
      return Boolean(this.searchKey) && this.visibleLabels.length === 0;
    },
  },
  watch: {
    searchKey(value) {
      // When there is search string present
      // and there are matching results,
      // highlight first item by default.
      if (value && this.visibleLabels.length) {
        this.currentHighlightItem = 0;
      }
    },
  },
  methods: {
    ...mapActions([
      'toggleDropdownContents',
      'toggleDropdownContentsCreateView',
      'fetchLabels',
      'receiveLabelsSuccess',
      'updateSelectedLabels',
      'toggleDropdownContents',
    ]),
    isLabelSelected(label) {
      return this.selectedLabelsList.includes(label.id);
    },
    /**
     * This method scrolls item from dropdown into
     * the view if it is off the viewable area of the
     * container.
     */
    scrollIntoViewIfNeeded() {
      const highlightedLabel = this.$refs.labelsListContainer.querySelector('.is-focused');
 
I      if (highlightedLabel) {
        const container = this.$refs.labelsListContainer.getBoundingClientRect();
        const label = highlightedLabel.getBoundingClientRect();
 
        if (label.bottom > container.bottom) {
          this.$refs.labelsListContainer.scrollTop += label.bottom - container.bottom;
        } else if (label.top < container.top) {
          this.$refs.labelsListContainer.scrollTop -= container.top - label.top;
        }
      }
    },
    handleComponentAppear() {
      // We can avoid putting `catch` block here
      // as failure is handled within actions.js already.
      return this.fetchLabels().then(() => {
        this.$refs.searchInput.focusInput();
      });
    },
    /**
     * We want to remove loaded labels to ensure component
     * fetches fresh set of labels every time when shown.
     */
    handleComponentDisappear() {
      this.receiveLabelsSuccess([]);
    },
    handleCreateLabelClick() {
      this.receiveLabelsSuccess([]);
      this.toggleDropdownContentsCreateView();
    },
    /**
     * This method enables keyboard navigation support for
     * the dropdown.
     */
    handleKeyDown(e) {
      if (e.keyCode === UP_KEY_CODE && this.currentHighlightItem > 0) {
        this.currentHighlightItem -= 1;
      } else if (
        e.keyCode === DOWN_KEY_CODE &&
        this.currentHighlightItem < this.visibleLabels.length - 1
      ) {
        this.currentHighlightItem += 1;
      } else if (e.keyCode === ENTER_KEY_CODE && this.currentHighlightItem > -1) {
        this.updateSelectedLabels([this.visibleLabels[this.currentHighlightItem]]);
        this.searchKey = '';
 
        // Prevent parent form submission upon hitting enter.
        e.preventDefault();
E      } else if (e.keyCode === ESC_KEY_CODE) {
        this.toggleDropdownContents();
      }
 
      if (e.keyCode !== ESC_KEY_CODE) {
        // Scroll the list only after highlighting
        // styles are rendered completely.
        this.$nextTick(() => {
          this.scrollIntoViewIfNeeded();
        });
      }
    },
    handleLabelClick(label) {
      this.updateSelectedLabels([label]);
      if (!this.allowMultiselect) this.toggleDropdownContents();
    },
  },
};
</script>
 
<template>
  <gl-intersection-observer @appear="handleComponentAppear" @disappear="handleComponentDisappear">
    <div class="labels-select-contents-list js-labels-list" @keydown="handleKeyDown">
      <div
        v-if="isDropdownVariantSidebar || isDropdownVariantEmbedded"
        class="dropdown-title gl-display-flex gl-align-items-center gl-pt-0 gl-pb-3!"
        data-testid="dropdown-title"
      >
        <span class="flex-grow-1">{{ labelsListTitle }}</span>
        <gl-button
          :aria-label="__('Close')"
          variant="link"
          size="small"
          class="dropdown-header-button gl-p-0!"
          icon="close"
          @click="toggleDropdownContents"
        />
      </div>
      <div class="dropdown-input" @click.stop="() => {}">
        <gl-search-box-by-type
          ref="searchInput"
          v-model="searchKey"
          :disabled="labelsFetchInProgress"
          data-qa-selector="dropdown_input_field"
        />
      </div>
      <div ref="labelsListContainer" class="dropdown-content" data-testid="dropdown-content">
        <gl-loading-icon
          v-if="labelsFetchInProgress"
          class="labels-fetch-loading gl-align-items-center w-100 h-100"
          size="md"
        />
        <ul v-else class="list-unstyled gl-mb-0 gl-word-break-word">
          <label-item
            v-for="(label, index) in visibleLabels"
            :key="label.id"
            :label="label"
            :is-label-set="label.set"
            :highlight="index === currentHighlightItem"
            @clickLabel="handleLabelClick(label)"
          />
          <li v-show="showNoMatchingResultsMessage" class="gl-p-3 gl-text-center">
            {{ __('No matching results') }}
          </li>
        </ul>
      </div>
      <div v-if="showDropdownFooter" class="dropdown-footer" data-testid="dropdown-footer">
        <ul class="list-unstyled">
          <li v-if="allowLabelCreate">
            <gl-link
              class="gl-display-flex w-100 flex-row text-break-word label-item"
              @click="handleCreateLabelClick"
            >
              {{ footerCreateLabelTitle }}
            </gl-link>
          </li>
          <li v-if="labelsManagePath">
            <gl-link
              :href="labelsManagePath"
              class="gl-display-flex flex-row text-break-word label-item"
            >
              {{ footerManageLabelTitle }}
            </gl-link>
          </li>
        </ul>
      </div>
    </div>
  </gl-intersection-observer>
</template>