debian-mirror-gitlab/app/assets/javascripts/badges/components/badge_form.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

265 lines
7.1 KiB
Vue
Raw Normal View History

2018-05-09 12:01:36 +05:30
<script>
2023-03-04 22:38:38 +05:30
import { GlLoadingIcon, GlFormInput, GlFormGroup, GlButton } from '@gitlab/ui';
2020-04-08 14:13:33 +05:30
import { escape, debounce } from 'lodash';
2018-05-09 12:01:36 +05:30
import { mapActions, mapState } from 'vuex';
2023-03-04 22:38:38 +05:30
import SafeHtml from '~/vue_shared/directives/safe_html';
2022-11-25 23:54:43 +05:30
import { createAlert, VARIANT_INFO } from '~/flash';
2018-05-09 12:01:36 +05:30
import { s__, sprintf } from '~/locale';
import createEmptyBadge from '../empty_badge';
2023-03-04 22:38:38 +05:30
import { PLACEHOLDERS } from '../constants';
2018-05-09 12:01:36 +05:30
import Badge from './badge.vue';
const badgePreviewDelayInMilliseconds = 1500;
export default {
name: 'BadgeForm',
components: {
Badge,
2020-11-24 15:15:51 +05:30
GlButton,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2020-01-01 13:55:28 +05:30
GlFormInput,
GlFormGroup,
2018-05-09 12:01:36 +05:30
},
2021-11-11 11:23:49 +05:30
directives: {
2023-03-04 22:38:38 +05:30
SafeHtml,
2021-11-11 11:23:49 +05:30
},
2018-05-09 12:01:36 +05:30
props: {
isEditing: {
type: Boolean,
required: true,
},
},
2018-11-20 20:47:30 +05:30
data() {
return {
wasValidated: false,
};
},
2018-05-09 12:01:36 +05:30
computed: {
...mapState([
'badgeInAddForm',
'badgeInEditForm',
'docsUrl',
'isRendering',
'isSaving',
'renderedBadge',
]),
badge() {
if (this.isEditing) {
return this.badgeInEditForm;
}
return this.badgeInAddForm;
},
helpText() {
2023-03-04 22:38:38 +05:30
const placeholders = PLACEHOLDERS.map((placeholder) => `<code>%{${placeholder}}</code>`).join(
', ',
);
2018-05-09 12:01:36 +05:30
return sprintf(
2021-03-08 18:12:59 +05:30
s__('Badges|Supported %{docsLinkStart}variables%{docsLinkEnd}: %{placeholders}'),
2018-05-09 12:01:36 +05:30
{
docsLinkEnd: '</a>',
2020-04-08 14:13:33 +05:30
docsLinkStart: `<a href="${escape(this.docsUrl)}">`,
2018-05-09 12:01:36 +05:30
placeholders,
},
false,
);
},
renderedImageUrl() {
return this.renderedBadge ? this.renderedBadge.renderedImageUrl : '';
},
renderedLinkUrl() {
return this.renderedBadge ? this.renderedBadge.renderedLinkUrl : '';
},
2020-01-01 13:55:28 +05:30
name: {
get() {
return this.badge ? this.badge.name : '';
},
set(name) {
const badge = this.badge || createEmptyBadge();
this.updateBadgeInForm({
...badge,
name,
});
},
},
2018-05-09 12:01:36 +05:30
imageUrl: {
get() {
return this.badge ? this.badge.imageUrl : '';
},
set(imageUrl) {
const badge = this.badge || createEmptyBadge();
this.updateBadgeInForm({
...badge,
imageUrl,
});
},
},
linkUrl: {
get() {
return this.badge ? this.badge.linkUrl : '';
},
set(linkUrl) {
const badge = this.badge || createEmptyBadge();
this.updateBadgeInForm({
...badge,
linkUrl,
});
},
},
2018-11-20 20:47:30 +05:30
badgeImageUrlExample() {
const exampleUrl =
2019-07-07 11:18:12 +05:30
'https://example.gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg';
2021-03-08 18:12:59 +05:30
return sprintf(s__('Badges|Example: %{exampleUrl}'), {
2018-11-20 20:47:30 +05:30
exampleUrl,
});
},
badgeLinkUrlExample() {
const exampleUrl = 'https://example.gitlab.com/%{project_path}';
2021-03-08 18:12:59 +05:30
return sprintf(s__('Badges|Example: %{exampleUrl}'), {
2018-11-20 20:47:30 +05:30
exampleUrl,
});
2018-05-09 12:01:36 +05:30
},
},
methods: {
...mapActions(['addBadge', 'renderBadge', 'saveBadge', 'stopEditing', 'updateBadgeInForm']),
2020-04-08 14:13:33 +05:30
debouncedPreview: debounce(function preview() {
2018-05-09 12:01:36 +05:30
this.renderBadge();
}, badgePreviewDelayInMilliseconds),
onCancel() {
this.stopEditing();
},
onSubmit() {
2018-11-20 20:47:30 +05:30
const form = this.$el;
if (!form.checkValidity()) {
this.wasValidated = true;
2018-05-09 12:01:36 +05:30
return Promise.resolve();
}
if (this.isEditing) {
return this.saveBadge()
.then(() => {
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-04 01:27:46 +05:30
message: s__('Badges|Badge saved.'),
2022-11-25 23:54:43 +05:30
variant: VARIANT_INFO,
2021-09-04 01:27:46 +05:30
});
2018-11-20 20:47:30 +05:30
this.wasValidated = false;
2018-05-09 12:01:36 +05:30
})
2021-03-08 18:12:59 +05:30
.catch((error) => {
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-04 01:27:46 +05:30
message: s__(
'Badges|Saving the badge failed, please check the entered URLs and try again.',
),
});
2018-05-09 12:01:36 +05:30
throw error;
});
}
return this.addBadge()
.then(() => {
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-04 01:27:46 +05:30
message: s__('Badges|New badge added.'),
2022-11-25 23:54:43 +05:30
variant: VARIANT_INFO,
2021-09-04 01:27:46 +05:30
});
2018-11-20 20:47:30 +05:30
this.wasValidated = false;
2018-05-09 12:01:36 +05:30
})
2021-03-08 18:12:59 +05:30
.catch((error) => {
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-04 01:27:46 +05:30
message: s__(
'Badges|Adding the badge failed, please check the entered URLs and try again.',
),
});
2018-05-09 12:01:36 +05:30
throw error;
});
},
},
2021-11-11 11:23:49 +05:30
safeHtmlConfig: { ALLOW_TAGS: ['a', 'code'] },
2018-05-09 12:01:36 +05:30
};
</script>
<template>
<form
2018-11-20 20:47:30 +05:30
:class="{ 'was-validated': wasValidated }"
2020-07-28 23:09:34 +05:30
class="gl-mt-3 gl-mb-3 needs-validation"
2018-11-20 20:47:30 +05:30
novalidate
2018-05-09 12:01:36 +05:30
@submit.prevent.stop="onSubmit"
>
2020-01-01 13:55:28 +05:30
<gl-form-group :label="s__('Badges|Name')" label-for="badge-name">
2022-04-04 11:22:00 +05:30
<gl-form-input id="badge-name" v-model="name" data-qa-selector="badge_name_field" />
2020-01-01 13:55:28 +05:30
</gl-form-group>
2018-05-09 12:01:36 +05:30
<div class="form-group">
2019-02-15 15:39:39 +05:30
<label for="badge-link-url" class="label-bold">{{ s__('Badges|Link') }}</label>
2021-11-11 11:23:49 +05:30
<p v-safe-html:[$options.safeHtmlConfig]="helpText"></p>
2018-05-09 12:01:36 +05:30
<input
id="badge-link-url"
v-model="linkUrl"
2022-04-04 11:22:00 +05:30
data-qa-selector="badge_link_url_field"
2018-11-20 20:47:30 +05:30
type="URL"
2021-03-11 19:13:27 +05:30
class="form-control gl-form-input"
2018-11-20 20:47:30 +05:30
required
2018-05-09 12:01:36 +05:30
@input="debouncedPreview"
/>
2021-03-08 18:12:59 +05:30
<div class="invalid-feedback">{{ s__('Badges|Enter a valid URL') }}</div>
2020-10-24 23:57:45 +05:30
<span class="form-text text-muted">{{ badgeLinkUrlExample }}</span>
2018-05-09 12:01:36 +05:30
</div>
<div class="form-group">
2019-02-15 15:39:39 +05:30
<label for="badge-image-url" class="label-bold">{{ s__('Badges|Badge image URL') }}</label>
2021-11-11 11:23:49 +05:30
<p v-safe-html:[$options.safeHtmlConfig]="helpText"></p>
2018-05-09 12:01:36 +05:30
<input
id="badge-image-url"
v-model="imageUrl"
2022-04-04 11:22:00 +05:30
data-qa-selector="badge_image_url_field"
2018-11-20 20:47:30 +05:30
type="URL"
2021-03-11 19:13:27 +05:30
class="form-control gl-form-input"
2018-11-20 20:47:30 +05:30
required
2018-05-09 12:01:36 +05:30
@input="debouncedPreview"
/>
2021-03-08 18:12:59 +05:30
<div class="invalid-feedback">{{ s__('Badges|Enter a valid URL') }}</div>
2020-10-24 23:57:45 +05:30
<span class="form-text text-muted">{{ badgeImageUrlExample }}</span>
2018-05-09 12:01:36 +05:30
</div>
<div class="form-group">
<label for="badge-preview">{{ s__('Badges|Badge image preview') }}</label>
<badge
v-show="renderedBadge && !isRendering"
2018-11-08 19:23:39 +05:30
id="badge-preview"
2018-05-09 12:01:36 +05:30
:image-url="renderedImageUrl"
:link-url="renderedLinkUrl"
/>
2020-10-24 23:57:45 +05:30
<p v-show="isRendering">
2021-09-30 23:02:18 +05:30
<gl-loading-icon size="sm" :inline="true" />
2020-10-24 23:57:45 +05:30
</p>
2019-02-15 15:39:39 +05:30
<p v-show="!renderedBadge && !isRendering" class="disabled-content">
{{ s__('Badges|No image to preview') }}
2018-05-09 12:01:36 +05:30
</p>
</div>
2021-01-03 14:25:43 +05:30
<div v-if="isEditing" class="row-content-block">
2020-11-24 15:15:51 +05:30
<gl-button class="btn-cancel gl-mr-4" data-testid="cancelEditing" @click="onCancel">
2020-10-24 23:57:45 +05:30
{{ __('Cancel') }}
2020-11-24 15:15:51 +05:30
</gl-button>
<gl-button
2018-05-09 12:01:36 +05:30
:loading="isSaving"
2018-11-08 19:23:39 +05:30
type="submit"
2021-04-17 20:07:23 +05:30
variant="confirm"
2020-11-24 15:15:51 +05:30
category="primary"
data-testid="saveEditing"
>
{{ s__('Badges|Save changes') }}
</gl-button>
2018-05-09 12:01:36 +05:30
</div>
2021-01-03 14:25:43 +05:30
<div v-else class="form-group">
2022-04-04 11:22:00 +05:30
<gl-button
:loading="isSaving"
type="submit"
variant="confirm"
category="primary"
data-qa-selector="add_badge_button"
>
2020-11-24 15:15:51 +05:30
{{ s__('Badges|Add badge') }}
</gl-button>
2018-11-20 20:47:30 +05:30
</div>
2018-05-09 12:01:36 +05:30
</form>
</template>