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

branch_switcher.vue « file_nav « components « pipeline_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1acf3a03e7330c022de7cd46c8abb1f42f0c5cc0 (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
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
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlDropdownSectionHeader,
  GlInfiniteScroll,
  GlLoadingIcon,
  GlSearchBoxByType,
} from '@gitlab/ui';
import { historyPushState } from '~/lib/utils/common_utils';
import { setUrlParams } from '~/lib/utils/url_utility';
import { s__ } from '~/locale';
import {
  BRANCH_PAGINATION_LIMIT,
  BRANCH_SEARCH_DEBOUNCE,
  DEFAULT_FAILURE,
} from '~/pipeline_editor/constants';
import getAvailableBranches from '~/pipeline_editor/graphql/queries/available_branches.graphql';
import getCurrentBranch from '~/pipeline_editor/graphql/queries/client/current_branch.graphql';

export default {
  i18n: {
    dropdownHeader: s__('Switch Branch'),
    title: s__('Branches'),
    fetchError: s__('Unable to fetch branch list for this project.'),
  },
  inputDebounce: BRANCH_SEARCH_DEBOUNCE,
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownSectionHeader,
    GlInfiniteScroll,
    GlLoadingIcon,
    GlSearchBoxByType,
  },
  inject: ['projectFullPath', 'totalBranches'],
  props: {
    paginationLimit: {
      type: Number,
      required: false,
      default: BRANCH_PAGINATION_LIMIT,
    },
  },
  data() {
    return {
      branches: [],
      page: {
        limit: this.paginationLimit,
        offset: 0,
        searchTerm: '',
      },
    };
  },
  apollo: {
    availableBranches: {
      query: getAvailableBranches,
      variables() {
        return {
          limit: this.page.limit,
          offset: this.page.offset,
          projectFullPath: this.projectFullPath,
          searchPattern: this.searchPattern,
        };
      },
      update(data) {
        return data.project?.repository?.branchNames || [];
      },
      result({ data }) {
        const newBranches = data.project?.repository?.branchNames || [];

        // check that we're not re-concatenating existing fetch results
        if (!this.branches.includes(newBranches[0])) {
          this.branches = this.branches.concat(newBranches);
        }
      },
      error() {
        this.$emit('showError', {
          type: DEFAULT_FAILURE,
          reasons: [this.$options.i18n.fetchError],
        });
      },
    },
    currentBranch: {
      query: getCurrentBranch,
    },
  },
  computed: {
    isBranchesLoading() {
      return this.$apollo.queries.availableBranches.loading;
    },
    showBranchSwitcher() {
      return this.branches.length > 0 || this.page.searchTerm.length > 0;
    },
    searchPattern() {
      if (this.page.searchTerm === '') {
        return '*';
      }

      return `*${this.page.searchTerm}*`;
    },
  },
  methods: {
    // if there is no searchPattern, paginate by {paginationLimit} branches
    fetchNextBranches() {
      if (
        this.isBranchesLoading ||
        this.page.searchTerm.length > 0 ||
        this.branches.length === this.totalBranches
      ) {
        return;
      }

      this.page = {
        ...this.page,
        limit: this.paginationLimit,
        offset: this.page.offset + this.paginationLimit,
      };
    },
    async selectBranch(newBranch) {
      if (newBranch === this.currentBranch) {
        return;
      }

      await this.$apollo.getClient().writeQuery({
        query: getCurrentBranch,
        data: { currentBranch: newBranch },
      });

      const updatedPath = setUrlParams({ branch_name: newBranch });
      historyPushState(updatedPath);

      this.$emit('refetchContent');
    },
    setSearchTerm(newSearchTerm) {
      this.branches = [];
      this.page = {
        limit: newSearchTerm.trim() === '' ? this.paginationLimit : this.totalBranches,
        offset: 0,
        searchTerm: newSearchTerm.trim(),
      };
    },
  },
};
</script>

<template>
  <gl-dropdown
    v-if="showBranchSwitcher"
    class="gl-ml-2"
    :header-text="$options.i18n.dropdownHeader"
    :text="currentBranch"
    icon="branch"
  >
    <gl-search-box-by-type :debounce="$options.inputDebounce" @input="setSearchTerm" />
    <gl-dropdown-section-header>
      {{ $options.i18n.title }}
    </gl-dropdown-section-header>

    <gl-infinite-scroll
      :fetched-items="branches.length"
      :total-items="totalBranches"
      :max-list-height="250"
      @bottomReached="fetchNextBranches"
    >
      <template #items>
        <gl-dropdown-item
          v-for="branch in branches"
          :key="branch"
          :is-checked="currentBranch === branch"
          :is-check-item="true"
          @click="selectBranch(branch)"
        >
          {{ branch }}
        </gl-dropdown-item>
      </template>
      <template #default>
        <gl-dropdown-item v-if="isBranchesLoading" key="loading">
          <gl-loading-icon size="md" />
        </gl-dropdown-item>
      </template>
    </gl-infinite-scroll>
  </gl-dropdown>
</template>