gitlab-org--gitlab-foss/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.

263 lines
7.1 KiB
Vue
Raw Normal View History

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