All files / app/assets/javascripts/releases/components asset_links_form.vue

0% Statements 0/16
0% Branches 0/8
0% Functions 0/14
0% Lines 0/16

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
<script>
import {
  GlSprintf,
  GlLink,
  GlFormGroup,
  GlButton,
  GlIcon,
  GlTooltipDirective,
  GlFormInput,
  GlFormSelect,
} from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapState, mapActions, mapGetters } from 'vuex';
import { s__ } from '~/locale';
import { DEFAULT_ASSET_LINK_TYPE, ASSET_LINK_TYPE } from '../constants';
 
export default {
  name: 'AssetLinksForm',
  components: {
    GlSprintf,
    GlLink,
    GlFormGroup,
    GlButton,
    GlIcon,
    GlFormInput,
    GlFormSelect,
  },
  directives: { GlTooltip: GlTooltipDirective },
  computed: {
    ...mapState('editNew', ['release', 'releaseAssetsDocsPath']),
    ...mapGetters('editNew', ['validationErrors']),
  },
  created() {
    this.ensureAtLeastOneLink();
  },
  methods: {
    ...mapActions('editNew', [
      'addEmptyAssetLink',
      'updateAssetLinkUrl',
      'updateAssetLinkName',
      'updateAssetLinkType',
      'removeAssetLink',
    ]),
    onAddAnotherClicked() {
      this.addEmptyAssetLink();
    },
    onRemoveClicked(linkId) {
      this.removeAssetLink(linkId);
      this.ensureAtLeastOneLink();
    },
    updateUrl(link, newUrl) {
      this.updateAssetLinkUrl({ linkIdToUpdate: link.id, newUrl });
    },
    updateName(link, newName) {
      this.updateAssetLinkName({ linkIdToUpdate: link.id, newName });
    },
    hasDuplicateUrl(link) {
      return Boolean(this.getLinkErrors(link).isDuplicate);
    },
    hasDuplicateName(link) {
      return Boolean(this.getLinkErrors(link).isTitleDuplicate);
    },
    hasBadFormat(link) {
      return Boolean(this.getLinkErrors(link).isBadFormat);
    },
    hasEmptyUrl(link) {
      return Boolean(this.getLinkErrors(link).isUrlEmpty);
    },
    hasEmptyName(link) {
      return Boolean(this.getLinkErrors(link).isNameEmpty);
    },
    getLinkErrors(link) {
      return this.validationErrors.assets.links[link.id] || {};
    },
    isUrlValid(link) {
      return !this.hasDuplicateUrl(link) && !this.hasBadFormat(link) && !this.hasEmptyUrl(link);
    },
    isNameValid(link) {
      return !this.hasEmptyName(link) && !this.hasDuplicateName(link);
    },
 
    /**
     * Make sure the form is never completely empty by adding an
     * empty row if the form contains 0 links
     */
    ensureAtLeastOneLink() {
      Iif (this.release.assets.links.length === 0) {
        this.addEmptyAssetLink();
      }
    },
  },
  typeOptions: [
    { value: ASSET_LINK_TYPE.IMAGE, text: s__('ReleaseAssetLinkType|Image') },
    { value: ASSET_LINK_TYPE.PACKAGE, text: s__('ReleaseAssetLinkType|Package') },
    { value: ASSET_LINK_TYPE.RUNBOOK, text: s__('ReleaseAssetLinkType|Runbook') },
    { value: ASSET_LINK_TYPE.OTHER, text: s__('ReleaseAssetLinkType|Other') },
  ],
  defaultTypeOptionValue: DEFAULT_ASSET_LINK_TYPE,
};
</script>
 
<template>
  <div class="d-flex flex-column release-assets-links-form">
    <h2 class="text-4">{{ __('Release assets') }}</h2>
    <p class="m-0">
      <gl-sprintf
        :message="
          __(
            'Add %{linkStart}assets%{linkEnd} to your Release. GitLab automatically includes read-only assets, like source code and release evidence.',
          )
        "
      >
        <template #link="{ content }">
          <gl-link
            :href="releaseAssetsDocsPath"
            target="_blank"
            :aria-label="__('Release assets documentation')"
          >
            {{ content }}
          </gl-link>
        </template>
      </gl-sprintf>
    </p>
    <h3 class="text-3">{{ __('Links') }}</h3>
    <p>
      {{
        __(
          'Point to any links you like: documentation, built binaries, or other related materials. These can be internal or external links from your GitLab instance. Each URL and link title must be unique.',
        )
      }}
    </p>
    <div
      v-for="(link, index) in release.assets.links"
      :key="link.id"
      class="gl-sm-display-flex flex-column flex-sm-row gl-gap-5 align-items-stretch align-items-sm-start no-gutters"
    >
      <gl-form-group
        class="url-field form-group col"
        :label="__('URL')"
        :label-for="`asset-url-${index}`"
      >
        <gl-form-input
          :id="`asset-url-${index}`"
          ref="urlInput"
          :value="link.url"
          type="text"
          class="form-control"
          name="asset-url"
          :state="isUrlValid(link)"
          @change="updateUrl(link, $event)"
          @keydown.ctrl.enter="updateUrl(link, $event.target.value)"
          @keydown.meta.enter="updateUrl(link, $event.target.value)"
        />
        <template #invalid-feedback>
          <span v-if="hasEmptyUrl(link)" class="invalid-feedback d-inline">
            {{ __('URL is required') }}
          </span>
          <span v-else-if="hasBadFormat(link)" class="invalid-feedback d-inline">
            <gl-sprintf
              :message="
                __(
                  'URL must start with %{codeStart}http://%{codeEnd}, %{codeStart}https://%{codeEnd}, or %{codeStart}ftp://%{codeEnd}',
                )
              "
            >
              <template #code="{ content }">
                <code>{{ content }}</code>
              </template>
            </gl-sprintf>
          </span>
          <span v-else-if="hasDuplicateUrl(link)" class="invalid-feedback d-inline">
            {{ __('This URL already exists.') }}
          </span>
        </template>
      </gl-form-group>
 
      <gl-form-group
        class="link-title-field col"
        :label="__('Link title')"
        :label-for="`asset-link-name-${index}`"
      >
        <gl-form-input
          :id="`asset-link-name-${index}`"
          ref="nameInput"
          :value="link.name"
          type="text"
          class="form-control"
          name="asset-link-name"
          :state="isNameValid(link)"
          @change="updateName(link, $event)"
          @keydown.ctrl.enter="updateName(link, $event.target.value)"
          @keydown.meta.enter="updateName(link, $event.target.value)"
        />
        <template #invalid-feedback>
          <span v-if="hasEmptyName(link)" class="invalid-feedback d-inline">
            {{ __('Link title is required') }}
          </span>
          <span v-else-if="hasDuplicateName(link)" class="invalid-feedback d-inline">
            {{ __('This title already exists.') }}
          </span>
        </template>
      </gl-form-group>
 
      <gl-form-group
        class="link-type-field col-auto"
        :label="__('Type')"
        :label-for="`asset-type-${index}`"
      >
        <gl-form-select
          :id="`asset-type-${index}`"
          ref="typeSelect"
          :value="link.linkType || $options.defaultTypeOptionValue"
          class="pr-4"
          name="asset-type"
          :options="$options.typeOptions"
          @change="updateAssetLinkType({ linkIdToUpdate: link.id, newType: $event })"
        />
      </gl-form-group>
 
      <div v-if="release.assets.links.length !== 1" class="mb-5 mb-sm-3 mt-sm-4 col col-sm-auto">
        <gl-button
          class="remove-button gl-w-full form-control"
          :aria-label="__('Remove asset link')"
          :title="__('Remove asset link')"
          @click="onRemoveClicked(link.id)"
        >
          <div class="d-flex">
            <gl-icon class="mr-1 mr-sm-0" :size="16" name="remove" />
            <span class="d-inline d-sm-none">{{ __('Remove asset link') }}</span>
          </div>
        </gl-button>
      </div>
    </div>
    <gl-button
      ref="addAnotherLinkButton"
      category="secondary"
      variant="confirm"
      class="gl-align-self-start gl-mb-5"
      @click="onAddAnotherClicked"
    >
      {{ __('Add another link') }}
    </gl-button>
  </div>
</template>