Welcome to mirror list, hosted at ThFree Co, Russian Federation.

author_select.vue « components « commits « projects « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 752bb5947944839edba59c5b4144be7dbced6dbb (plain)
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
<script>
import { debounce } from 'lodash';
import { mapState, mapActions } from 'vuex';
import {
  GlDropdown,
  GlDropdownSectionHeader,
  GlDropdownItem,
  GlSearchBoxByType,
  GlDropdownDivider,
  GlTooltipDirective,
} from '@gitlab/ui';
import { redirectTo } from '~/lib/utils/url_utility';
import { urlParamsToObject } from '~/lib/utils/common_utils';
import { __ } from '~/locale';

const tooltipMessage = __('Searching by both author and message is currently not supported.');

export default {
  name: 'AuthorSelect',
  components: {
    GlDropdown,
    GlDropdownSectionHeader,
    GlDropdownItem,
    GlSearchBoxByType,
    GlDropdownDivider,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    projectCommitsEl: {
      type: HTMLDivElement,
      required: true,
    },
  },
  data() {
    return {
      hasSearchParam: false,
      searchTerm: '',
      authorInput: '',
      currentAuthor: '',
    };
  },
  computed: {
    ...mapState(['commitsPath', 'commitsAuthors']),
    dropdownText() {
      return this.currentAuthor || __('Author');
    },
    tooltipTitle() {
      return this.hasSearchParam && tooltipMessage;
    },
  },
  mounted() {
    this.fetchAuthors();
    const params = urlParamsToObject(window.location.search);
    const { search: searchParam, author: authorParam } = params;
    const commitsSearchInput = this.projectCommitsEl.querySelector('#commits-search');

    if (authorParam) {
      commitsSearchInput.setAttribute('disabled', true);
      commitsSearchInput.setAttribute('data-toggle', 'tooltip');
      commitsSearchInput.setAttribute('title', tooltipMessage);
      this.currentAuthor = authorParam;
    }

    if (searchParam) {
      this.hasSearchParam = true;
    }

    commitsSearchInput.addEventListener(
      'keyup',
      debounce((event) => this.setSearchParam(event.target.value), 500), // keyup & time is to match effect of "filter by commit message"
    );
  },
  methods: {
    ...mapActions(['fetchAuthors']),
    selectAuthor(author) {
      const { name: user } = author || {};

      // Follow up issue "Remove usage of $.fadeIn from the codebase"
      // > https://gitlab.com/gitlab-org/gitlab/-/issues/214395

      // Follow up issue "Refactor commit list to a Vue Component"
      // To resolving mixing Vue + Vanilla JS
      // > https://gitlab.com/gitlab-org/gitlab/-/issues/214010
      const commitListElement = this.projectCommitsEl.querySelector('#commits-list');

      // To mimick effect of "filter by commit message"
      commitListElement.style.opacity = 0.5;
      commitListElement.style.transition = 'opacity 200ms';

      if (!user) {
        return redirectTo(this.commitsPath);
      }

      return redirectTo(`${this.commitsPath}?author=${user}`);
    },
    searchAuthors() {
      this.fetchAuthors(this.authorInput);
    },
    setSearchParam(value) {
      this.hasSearchParam = Boolean(value);
    },
  },
};
</script>

<template>
  <div ref="dropdownContainer" v-gl-tooltip :title="tooltipTitle" :disabled="!hasSearchParam">
    <gl-dropdown
      :text="dropdownText"
      :disabled="hasSearchParam"
      toggle-class="gl-py-3 gl-border-0"
      class="w-100 mt-2 mt-sm-0"
    >
      <gl-dropdown-section-header>
        {{ __('Search by author') }}
      </gl-dropdown-section-header>
      <gl-dropdown-divider />
      <gl-search-box-by-type
        v-model.trim="authorInput"
        :placeholder="__('Search')"
        @input="searchAuthors"
      />
      <gl-dropdown-item :is-checked="!currentAuthor" @click="selectAuthor(null)">
        {{ __('Any Author') }}
      </gl-dropdown-item>
      <gl-dropdown-divider />
      <gl-dropdown-item
        v-for="author in commitsAuthors"
        :key="author.id"
        :is-checked="author.name === currentAuthor"
        :avatar-url="author.avatar_url"
        :secondary-text="author.username"
        @click="selectAuthor(author)"
      >
        {{ author.name }}
      </gl-dropdown-item>
    </gl-dropdown>
  </div>
</template>