Read about our upcoming Code of Conduct on this issue

breadcrumbs.vue 6.14 KB
Newer Older
1
<script>
2
3
4
import { GlDropdown, GlDropdownDivider, GlDropdownHeader, GlDropdownItem } from '@gitlab/ui';
import { __ } from '../../locale';
import Icon from '../../vue_shared/components/icon.vue';
5
import getRefMixin from '../mixins/get_ref';
6
import getProjectShortPath from '../queries/getProjectShortPath.query.graphql';
7
8
9
10
11
12
13
import getProjectPath from '../queries/getProjectPath.query.graphql';
import getPermissions from '../queries/getPermissions.query.graphql';

const ROW_TYPES = {
  header: 'header',
  divider: 'divider',
};
14
15

export default {
16
17
18
19
20
21
22
  components: {
    GlDropdown,
    GlDropdownDivider,
    GlDropdownHeader,
    GlDropdownItem,
    Icon,
  },
23
24
25
26
  apollo: {
    projectShortPath: {
      query: getProjectShortPath,
    },
27
28
29
30
31
32
33
34
35
36
    projectPath: {
      query: getProjectPath,
    },
    userPermissions: {
      query: getPermissions,
      variables() {
        return {
          projectPath: this.projectPath,
        };
      },
37
38
39
40
      update: data => data.project?.userPermissions,
      error(error) {
        throw error;
      },
41
    },
42
43
44
45
46
47
  },
  mixins: [getRefMixin],
  props: {
    currentPath: {
      type: String,
      required: false,
48
      default: '',
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
    canCollaborate: {
      type: Boolean,
      required: false,
      default: false,
    },
    canEditTree: {
      type: Boolean,
      required: false,
      default: false,
    },
    newBranchPath: {
      type: String,
      required: false,
      default: null,
    },
    newTagPath: {
      type: String,
      required: false,
      default: null,
    },
    newBlobPath: {
      type: String,
      required: false,
      default: null,
    },
    forkNewBlobPath: {
      type: String,
      required: false,
      default: null,
    },
    forkNewDirectoryPath: {
      type: String,
      required: false,
      default: null,
    },
    forkUploadBlobPath: {
      type: String,
      required: false,
      default: null,
    },
90
91
92
93
  },
  data() {
    return {
      projectShortPath: '',
94
95
      projectPath: '',
      userPermissions: {},
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    };
  },
  computed: {
    pathLinks() {
      return this.currentPath
        .split('/')
        .filter(p => p !== '')
        .reduce(
          (acc, name, i) => {
            const path = `${i > 0 ? acc[i].path : ''}/${name}`;

            return acc.concat({
              name,
              path,
110
              to: `/-/tree/${escape(this.ref)}${escape(path)}`,
111
112
            });
          },
113
114
115
116
117
118
119
          [
            {
              name: this.projectShortPath,
              path: '/',
              to: `/-/tree/${escape(this.ref)}/`,
            },
          ],
120
121
        );
    },
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    canCreateMrFromFork() {
      return this.userPermissions.forkProject && this.userPermissions.createMergeRequestIn;
    },
    dropdownItems() {
      const items = [];

      if (this.canEditTree) {
        items.push(
          {
            type: ROW_TYPES.header,
            text: __('This directory'),
          },
          {
            attrs: {
136
              href: `${this.newBlobPath}/${this.currentPath ? escape(this.currentPath) : ''}`,
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
              class: 'qa-new-file-option',
            },
            text: __('New file'),
          },
          {
            attrs: {
              href: '#modal-upload-blob',
              'data-target': '#modal-upload-blob',
              'data-toggle': 'modal',
            },
            text: __('Upload file'),
          },
          {
            attrs: {
              href: '#modal-create-new-dir',
              'data-target': '#modal-create-new-dir',
              'data-toggle': 'modal',
            },
            text: __('New directory'),
          },
        );
      } else if (this.canCreateMrFromFork) {
        items.push(
          {
            attrs: {
              href: this.forkNewBlobPath,
              'data-method': 'post',
            },
            text: __('New file'),
          },
          {
            attrs: {
              href: this.forkUploadBlobPath,
              'data-method': 'post',
            },
            text: __('Upload file'),
          },
          {
            attrs: {
              href: this.forkNewDirectoryPath,
              'data-method': 'post',
            },
            text: __('New directory'),
          },
        );
      }

184
      if (this.userPermissions?.pushCode) {
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
        items.push(
          {
            type: ROW_TYPES.divider,
          },
          {
            type: ROW_TYPES.header,
            text: __('This repository'),
          },
          {
            attrs: {
              href: this.newBranchPath,
            },
            text: __('New branch'),
          },
          {
            attrs: {
              href: this.newTagPath,
            },
            text: __('New tag'),
          },
        );
      }

      return items;
    },
    renderAddToTreeDropdown() {
      return this.canCollaborate || this.canCreateMrFromFork;
    },
213
214
215
216
217
  },
  methods: {
    isLast(i) {
      return i === this.pathLinks.length - 1;
    },
218
219
220
221
222
223
224
225
226
227
    getComponent(type) {
      switch (type) {
        case ROW_TYPES.divider:
          return 'gl-dropdown-divider';
        case ROW_TYPES.header:
          return 'gl-dropdown-header';
        default:
          return 'gl-dropdown-item';
      }
    },
228
229
230
231
232
233
234
235
236
237
238
239
  },
};
</script>

<template>
  <nav :aria-label="__('Files breadcrumb')">
    <ol class="breadcrumb repo-breadcrumb">
      <li v-for="(link, i) in pathLinks" :key="i" class="breadcrumb-item">
        <router-link :to="link.to" :aria-current="isLast(i) ? 'page' : null">
          {{ link.name }}
        </router-link>
      </li>
240
241
242
243
244
      <li v-if="renderAddToTreeDropdown" class="breadcrumb-item">
        <gl-dropdown toggle-class="add-to-tree qa-add-to-tree ml-1">
          <template slot="button-content">
            <span class="sr-only">{{ __('Add to tree') }}</span>
            <icon name="plus" :size="16" class="float-left" />
245
            <icon name="chevron-down" :size="16" class="float-left" />
246
247
248
249
250
251
252
253
          </template>
          <template v-for="(item, i) in dropdownItems">
            <component :is="getComponent(item.type)" :key="i" v-bind="item.attrs">
              {{ item.text }}
            </component>
          </template>
        </gl-dropdown>
      </li>
254
255
256
    </ol>
  </nav>
</template>