debian-mirror-gitlab/app/assets/javascripts/releases/components/tag_field_new.vue

167 lines
5.1 KiB
Vue
Raw Normal View History

2020-10-24 23:57:45 +05:30
<script>
2021-04-17 20:07:23 +05:30
import { GlFormGroup, GlDropdownItem, GlSprintf } from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import { uniqueId } from 'lodash';
2021-03-11 19:13:27 +05:30
import { mapState, mapActions, mapGetters } from 'vuex';
2020-10-24 23:57:45 +05:30
import { __ } from '~/locale';
import RefSelector from '~/ref/components/ref_selector.vue';
2021-04-17 20:07:23 +05:30
import { REF_TYPE_TAGS } from '~/ref/constants';
2020-10-24 23:57:45 +05:30
import FormFieldContainer from './form_field_container.vue';
export default {
name: 'TagFieldNew',
2021-04-17 20:07:23 +05:30
components: {
GlFormGroup,
RefSelector,
FormFieldContainer,
GlDropdownItem,
GlSprintf,
},
2020-10-24 23:57:45 +05:30
data() {
return {
// Keeps track of whether or not the user has interacted with
// the input field. This is used to avoid showing validation
// errors immediately when the page loads.
isInputDirty: false,
2021-04-17 20:07:23 +05:30
showCreateFrom: true,
2020-10-24 23:57:45 +05:30
};
},
computed: {
2021-04-29 21:17:54 +05:30
...mapState('editNew', ['projectId', 'release', 'createFrom']),
...mapGetters('editNew', ['validationErrors']),
2020-10-24 23:57:45 +05:30
tagName: {
get() {
return this.release.tagName;
},
set(tagName) {
this.updateReleaseTagName(tagName);
2021-04-17 20:07:23 +05:30
// This setter is used by the `v-model` on the `RefSelector`.
// When this is called, the selection originated from the
// dropdown list of existing tag names, so we know the tag
// already exists and don't need to show the "create from" input
this.showCreateFrom = false;
2020-10-24 23:57:45 +05:30
},
},
createFromModel: {
get() {
return this.createFrom;
},
set(createFrom) {
this.updateCreateFrom(createFrom);
},
},
showTagNameValidationError() {
return this.isInputDirty && this.validationErrors.isTagNameEmpty;
},
tagNameInputId() {
return uniqueId('tag-name-input-');
},
createFromSelectorId() {
return uniqueId('create-from-selector-');
},
},
methods: {
2021-04-29 21:17:54 +05:30
...mapActions('editNew', ['updateReleaseTagName', 'updateCreateFrom']),
2020-10-24 23:57:45 +05:30
markInputAsDirty() {
this.isInputDirty = true;
},
2021-04-17 20:07:23 +05:30
createTagClicked(newTagName) {
this.updateReleaseTagName(newTagName);
// This method is called when the user selects the "create tag"
// option, so the tag does not already exist. Because of this,
// we need to show the "create from" input.
this.showCreateFrom = true;
},
2021-06-08 01:23:25 +05:30
shouldShowCreateTagOption(isLoading, matches, query) {
// Show the "create tag" option if:
return (
// we're not currently loading any results, and
!isLoading &&
// the search query isn't just whitespace, and
query.trim() &&
// the `matches` object is non-null, and
matches &&
// the tag name doesn't already exist
!matches.tags.list.some(
(tagInfo) => tagInfo.name.toUpperCase() === query.toUpperCase().trim(),
)
);
},
2020-10-24 23:57:45 +05:30
},
translations: {
2021-04-17 20:07:23 +05:30
tagName: {
noRefSelected: __('No tag selected'),
dropdownHeader: __('Tag name'),
searchPlaceholder: __('Search or create tag'),
2021-12-11 22:18:48 +05:30
label: __('Tag name'),
labelDescription: __('*Required'),
2021-04-17 20:07:23 +05:30
},
createFrom: {
noRefSelected: __('No source selected'),
searchPlaceholder: __('Search branches, tags, and commits'),
dropdownHeader: __('Select source'),
},
2020-10-24 23:57:45 +05:30
},
2021-04-17 20:07:23 +05:30
tagNameEnabledRefTypes: [REF_TYPE_TAGS],
2020-10-24 23:57:45 +05:30
};
</script>
<template>
<div>
<gl-form-group
data-testid="tag-name-field"
:state="!showTagNameValidationError"
:invalid-feedback="__('Tag name is required')"
2021-12-11 22:18:48 +05:30
:label="$options.translations.tagName.label"
:label-for="tagNameInputId"
:label-description="$options.translations.tagName.labelDescription"
2020-10-24 23:57:45 +05:30
>
<form-field-container>
2021-04-17 20:07:23 +05:30
<ref-selector
2020-10-24 23:57:45 +05:30
:id="tagNameInputId"
v-model="tagName"
2021-04-17 20:07:23 +05:30
:project-id="projectId"
:translations="$options.translations.tagName"
:enabled-ref-types="$options.tagNameEnabledRefTypes"
2020-10-24 23:57:45 +05:30
:state="!showTagNameValidationError"
2021-04-17 20:07:23 +05:30
@hide.once="markInputAsDirty"
>
<template #footer="{ isLoading, matches, query }">
<gl-dropdown-item
2021-06-08 01:23:25 +05:30
v-if="shouldShowCreateTagOption(isLoading, matches, query)"
2021-04-17 20:07:23 +05:30
is-check-item
:is-checked="tagName === query"
@click="createTagClicked(query)"
>
<gl-sprintf :message="__('Create tag %{tagName}')">
<template #tagName>
<b>{{ query }}</b>
</template>
</gl-sprintf>
</gl-dropdown-item>
</template>
</ref-selector>
2020-10-24 23:57:45 +05:30
</form-field-container>
</gl-form-group>
<gl-form-group
2021-04-17 20:07:23 +05:30
v-if="showCreateFrom"
2020-10-24 23:57:45 +05:30
:label="__('Create from')"
:label-for="createFromSelectorId"
data-testid="create-from-field"
>
<form-field-container>
<ref-selector
:id="createFromSelectorId"
v-model="createFromModel"
:project-id="projectId"
2021-04-17 20:07:23 +05:30
:translations="$options.translations.createFrom"
2020-10-24 23:57:45 +05:30
/>
</form-field-container>
<template #description>
{{ __('Existing branch name, tag, or commit SHA') }}
</template>
</gl-form-group>
</div>
</template>