summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/components/release_block_assets.vue
blob: 8824cbefd7e099868635c7e3e32bbdc2f61957bb (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
184
185
186
187
188
<script>
import { GlTooltipDirective, GlLink, GlButton, GlCollapse, GlIcon, GlBadge } from '@gitlab/ui';
import { difference, get } from 'lodash';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { ASSET_LINK_TYPE } from '../constants';
import { __, s__, sprintf } from '~/locale';

export default {
  name: 'ReleaseBlockAssets',
  components: {
    GlLink,
    GlButton,
    GlCollapse,
    GlIcon,
    GlBadge,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  mixins: [glFeatureFlagsMixin()],
  props: {
    assets: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isAssetsExpanded: true,
    };
  },
  computed: {
    hasAssets() {
      return Boolean(this.assets.count);
    },
    imageLinks() {
      return this.linksForType(ASSET_LINK_TYPE.IMAGE);
    },
    packageLinks() {
      return this.linksForType(ASSET_LINK_TYPE.PACKAGE);
    },
    runbookLinks() {
      return this.linksForType(ASSET_LINK_TYPE.RUNBOOK);
    },
    otherLinks() {
      return difference(this.assets.links, [
        ...this.imageLinks,
        ...this.packageLinks,
        ...this.runbookLinks,
      ]);
    },
    sections() {
      return [
        {
          links: get(this.assets, 'sources', []).map(s => ({
            url: s.url,
            name: sprintf(__('Source code (%{fileExtension})'), { fileExtension: s.format }),
          })),
          iconName: 'doc-code',
        },
        {
          title: s__('ReleaseAssetLinkType|Images'),
          links: this.imageLinks,
          iconName: 'container-image',
        },
        {
          title: s__('ReleaseAssetLinkType|Packages'),
          links: this.packageLinks,
          iconName: 'package',
        },
        {
          title: s__('ReleaseAssetLinkType|Runbooks'),
          links: this.runbookLinks,
          iconName: 'book',
        },
        {
          title: s__('ReleaseAssetLinkType|Other'),
          links: this.otherLinks,
          iconName: 'link',
        },
      ].filter(section => section.links.length > 0);
    },
  },
  methods: {
    toggleAssetsExpansion() {
      this.isAssetsExpanded = !this.isAssetsExpanded;
    },
    linksForType(type) {
      return this.assets.links.filter(l => l.linkType === type);
    },
  },
  externalLinkTooltipText: __('This link points to external content'),
};
</script>

<template>
  <div class="card-text gl-mt-3">
    <template v-if="glFeatures.releaseAssetLinkType">
      <gl-button
        data-testid="accordion-button"
        variant="link"
        class="gl-font-weight-bold"
        @click="toggleAssetsExpansion"
      >
        <gl-icon
          name="chevron-right"
          class="gl-transition-medium"
          :class="{ 'gl-rotate-90': isAssetsExpanded }"
        />
        {{ __('Assets') }}
        <gl-badge size="sm" variant="neutral" class="gl-display-inline-block">{{
          assets.count
        }}</gl-badge>
      </gl-button>
      <gl-collapse v-model="isAssetsExpanded">
        <div class="gl-pl-6 gl-pt-3 js-assets-list">
          <template v-for="(section, index) in sections">
            <h5 v-if="section.title" :key="`section-header-${index}`" class="gl-mb-2">
              {{ section.title }}
            </h5>
            <ul :key="`section-body-${index}`" class="list-unstyled gl-m-0">
              <li v-for="link in section.links" :key="link.url">
                <gl-link
                  :href="link.directAssetUrl || link.url"
                  class="gl-display-flex gl-align-items-center gl-line-height-24"
                >
                  <gl-icon
                    :name="section.iconName"
                    class="gl-mr-2 gl-flex-shrink-0 gl-flex-grow-0"
                  />
                  {{ link.name }}
                  <gl-icon
                    v-if="link.external"
                    v-gl-tooltip
                    name="external-link"
                    :aria-label="$options.externalLinkTooltipText"
                    :title="$options.externalLinkTooltipText"
                    data-testid="external-link-indicator"
                    class="gl-ml-2 gl-flex-shrink-0 gl-flex-grow-0 gl-text-gray-400"
                  />
                </gl-link>
              </li>
            </ul>
          </template>
        </div>
      </gl-collapse>
    </template>

    <template v-else>
      <b>
        {{ __('Assets') }}
        <span class="js-assets-count badge badge-pill">{{ assets.count }}</span>
      </b>

      <ul v-if="assets.links.length" class="pl-0 mb-0 gl-mt-3 list-unstyled js-assets-list">
        <li v-for="link in assets.links" :key="link.name" class="gl-mb-3">
          <gl-link v-gl-tooltip.bottom :title="__('Download asset')" :href="link.directAssetUrl">
            <gl-icon name="package" class="align-middle gl-mr-2 align-text-bottom" />
            {{ link.name }}
            <span v-if="link.external" data-testid="external-link-indicator">{{
              __('(external source)')
            }}</span>
          </gl-link>
        </li>
      </ul>

      <div v-if="hasAssets" class="dropdown">
        <button
          type="button"
          class="btn btn-link"
          data-toggle="dropdown"
          aria-haspopup="true"
          aria-expanded="false"
        >
          <gl-icon name="doc-code" class="align-top gl-mr-2" />
          {{ __('Source code') }}
          <gl-icon name="chevron-down" />
        </button>

        <div class="js-sources-dropdown dropdown-menu">
          <li v-for="asset in assets.sources" :key="asset.url">
            <gl-link :href="asset.url">{{ __('Download') }} {{ asset.format }}</gl-link>
          </li>
        </div>
      </div>
    </template>
  </div>
</template>