gitlab-org--gitlab-foss/app/assets/javascripts/issue_show/components/description.vue

159 lines
3.6 KiB
Vue
Raw Normal View History

<script>
2018-10-10 02:23:54 -04:00
import $ from 'jquery';
import { s__, sprintf } from '~/locale';
import createFlash from '~/flash';
2018-10-10 02:23:54 -04:00
import animateMixin from '../mixins/animate';
import TaskList from '../../task_list';
import recaptchaModalImplementor from '../../vue_shared/mixins/recaptcha_modal_implementor';
2018-10-10 02:23:54 -04:00
export default {
mixins: [animateMixin, recaptchaModalImplementor],
2018-10-10 02:23:54 -04:00
props: {
canUpdate: {
type: Boolean,
required: true,
},
2018-10-10 02:23:54 -04:00
descriptionHtml: {
type: String,
required: true,
},
2018-10-10 02:23:54 -04:00
descriptionText: {
type: String,
required: true,
},
taskStatus: {
type: String,
required: false,
default: '',
},
issuableType: {
type: String,
required: false,
default: 'issue',
},
updateUrl: {
type: String,
required: false,
default: null,
},
lockVersion: {
type: Number,
required: false,
2019-01-22 15:53:08 -05:00
default: 0,
},
2018-10-10 02:23:54 -04:00
},
data() {
return {
preAnimation: false,
pulseAnimation: false,
};
},
watch: {
descriptionHtml() {
this.animateChange();
2018-10-10 02:23:54 -04:00
this.$nextTick(() => {
this.renderGFM();
});
},
2018-10-10 02:23:54 -04:00
taskStatus() {
2018-01-05 09:31:01 -05:00
this.updateTaskStatusText();
},
2018-10-10 02:23:54 -04:00
},
mounted() {
this.renderGFM();
this.updateTaskStatusText();
},
methods: {
renderGFM() {
$(this.$refs['gfm-content']).renderGFM();
2018-10-10 02:23:54 -04:00
if (this.canUpdate) {
// eslint-disable-next-line no-new
new TaskList({
dataType: this.issuableType,
fieldName: 'description',
lockVersion: this.lockVersion,
2018-10-10 02:23:54 -04:00
selector: '.detail-page-description',
onSuccess: this.taskListUpdateSuccess.bind(this),
onError: this.taskListUpdateError.bind(this),
2018-10-10 02:23:54 -04:00
});
}
},
2018-10-10 02:23:54 -04:00
taskListUpdateSuccess(data) {
try {
this.checkForSpam(data);
this.closeRecaptcha();
} catch (error) {
if (error && error.name === 'SpamError') this.openRecaptcha();
}
},
2019-01-22 15:53:08 -05:00
taskListUpdateError() {
createFlash(
sprintf(
s__(
'Someone edited this %{issueType} at the same time you did. The description has been updated and you will need to make your changes again.',
),
{
issueType: this.issuableType,
},
2019-01-26 09:49:49 -05:00
),
2019-01-22 15:53:08 -05:00
);
this.$emit('taskListUpdateFailed');
},
2018-10-10 02:23:54 -04:00
updateTaskStatusText() {
const taskRegexMatches = this.taskStatus.match(/(\d+) of ((?!0)\d+)/);
const $issuableHeader = $('.issuable-meta');
const $tasks = $('#task_status', $issuableHeader);
const $tasksShort = $('#task_status_short', $issuableHeader);
2018-10-10 02:23:54 -04:00
if (taskRegexMatches) {
$tasks.text(this.taskStatus);
$tasksShort.text(
`${taskRegexMatches[1]}/${taskRegexMatches[2]} task${taskRegexMatches[2] > 1 ? 's' : ''}`,
);
} else {
$tasks.text('');
$tasksShort.text('');
}
},
2018-10-10 02:23:54 -04:00
},
};
</script>
<template>
<div
2017-05-16 05:42:17 -04:00
v-if="descriptionHtml"
:class="{
2018-11-16 15:07:38 -05:00
'js-task-list-container': canUpdate,
2018-01-05 09:31:01 -05:00
}"
2018-06-11 05:49:47 -04:00
class="description"
2018-01-05 09:31:01 -05:00
>
<div
2018-06-11 05:49:47 -04:00
ref="gfm-content"
:class="{
'issue-realtime-pre-pulse': preAnimation,
2018-11-16 15:07:38 -05:00
'issue-realtime-trigger-pulse': pulseAnimation,
}"
class="md"
2018-11-16 15:07:38 -05:00
v-html="descriptionHtml"
></div>
<textarea
v-if="descriptionText"
ref="textarea"
2017-11-29 04:29:06 -05:00
v-model="descriptionText"
:data-update-url="updateUrl"
2018-06-11 05:49:47 -04:00
class="hidden js-task-list-field"
dir="auto"
2017-11-29 04:29:06 -05:00
>
</textarea>
2018-11-16 15:07:38 -05:00
<recaptcha-modal v-show="showRecaptcha" :html="recaptchaHTML" @close="closeRecaptcha" />
</div>
</template>