debian-mirror-gitlab/app/assets/javascripts/notes/components/comment_form.vue

445 lines
14 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
import { mapActions, mapGetters, mapState } from 'vuex';
import _ from 'underscore';
import Autosize from 'autosize';
import { __, sprintf } from '~/locale';
2019-02-15 15:39:39 +05:30
import TimelineEntryItem from '~/vue_shared/components/notes/timeline_entry_item.vue';
2018-05-09 12:01:36 +05:30
import Flash from '../../flash';
import Autosave from '../../autosave';
2018-12-05 23:21:45 +05:30
import {
capitalizeFirstCharacter,
convertToCamelCase,
splitCamelCase,
2019-07-31 22:56:46 +05:30
slugifyWithUnderscore,
2018-12-05 23:21:45 +05:30
} from '../../lib/utils/text_utility';
2018-05-09 12:01:36 +05:30
import * as constants from '../constants';
import eventHub from '../event_hub';
import issueWarning from '../../vue_shared/components/issue/issue_warning.vue';
import markdownField from '../../vue_shared/components/markdown/field.vue';
import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
import loadingButton from '../../vue_shared/components/loading_button.vue';
import noteSignedOutWidget from './note_signed_out_widget.vue';
import discussionLockedWidget from './discussion_locked_widget.vue';
import issuableStateMixin from '../mixins/issuable_state';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export default {
name: 'CommentForm',
components: {
issueWarning,
noteSignedOutWidget,
discussionLockedWidget,
markdownField,
userAvatarLink,
loadingButton,
2019-02-15 15:39:39 +05:30
TimelineEntryItem,
2018-05-09 12:01:36 +05:30
},
mixins: [issuableStateMixin],
props: {
noteableType: {
type: String,
required: true,
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
},
data() {
return {
note: '',
noteType: constants.COMMENT,
isSubmitting: false,
isSubmitButtonDisabled: true,
};
},
computed: {
...mapGetters([
'getCurrentUserLastNote',
'getUserData',
'getNoteableData',
'getNotesData',
'openState',
]),
...mapState(['isToggleStateButtonLoading']),
noteableDisplayName() {
2018-11-08 19:23:39 +05:30
return splitCamelCase(this.noteableType).toLowerCase();
2018-03-27 19:54:05 +05:30
},
2018-05-09 12:01:36 +05:30
isLoggedIn() {
return this.getUserData.id;
},
commentButtonTitle() {
2018-11-08 19:23:39 +05:30
return this.noteType === constants.COMMENT ? 'Comment' : 'Start discussion';
},
startDiscussionDescription() {
let text = 'Discuss a specific suggestion or question';
if (this.getNoteableData.noteableType === constants.MERGE_REQUEST_NOTEABLE_TYPE) {
text += ' that needs to be resolved';
}
return `${text}.`;
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
isOpen() {
2018-11-08 19:23:39 +05:30
return this.openState === constants.OPENED || this.openState === constants.REOPENED;
2018-05-09 12:01:36 +05:30
},
canCreateNote() {
return this.getNoteableData.current_user.can_create_note;
},
issueActionButtonTitle() {
const openOrClose = this.isOpen ? 'close' : 'reopen';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (this.note.length) {
return sprintf(__('%{actionText} & %{openOrClose} %{noteable}'), {
actionText: this.commentButtonTitle,
openOrClose,
noteable: this.noteableDisplayName,
});
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
return sprintf(__('%{openOrClose} %{noteable}'), {
openOrClose: capitalizeFirstCharacter(openOrClose),
noteable: this.noteableDisplayName,
});
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
actionButtonClassNames() {
return {
'btn-reopen': !this.isOpen,
'btn-close': this.isOpen,
'js-note-target-close': this.isOpen,
'js-note-target-reopen': !this.isOpen,
};
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
markdownDocsPath() {
return this.getNotesData.markdownDocsPath;
},
quickActionsDocsPath() {
return this.getNotesData.quickActionsDocsPath;
},
markdownPreviewPath() {
return this.getNoteableData.preview_note_path;
},
author() {
return this.getUserData;
},
2019-07-31 22:56:46 +05:30
canToggleIssueState() {
return (
this.getNoteableData.current_user.can_update &&
this.getNoteableData.state !== constants.MERGED
);
2018-05-09 12:01:36 +05:30
},
endpoint() {
return this.getNoteableData.create_note_path;
},
2018-11-08 19:23:39 +05:30
issuableTypeTitle() {
2018-12-05 23:21:45 +05:30
return this.noteableType === constants.MERGE_REQUEST_NOTEABLE_TYPE
? 'merge request'
: 'issue';
2018-11-08 19:23:39 +05:30
},
2019-07-31 22:56:46 +05:30
trackingLabel() {
return slugifyWithUnderscore(`${this.commentButtonTitle} button`);
},
2018-05-09 12:01:36 +05:30
},
watch: {
note(newNote) {
this.setIsSubmitButtonDisabled(newNote, this.isSubmitting);
},
isSubmitting(newValue) {
this.setIsSubmitButtonDisabled(this.note, newValue);
},
},
mounted() {
// jQuery is needed here because it is a custom event being dispatched with jQuery.
$(document).on('issuable:change', (e, isClosed) => {
2018-11-08 19:23:39 +05:30
this.toggleIssueLocalState(isClosed ? constants.CLOSED : constants.REOPENED);
2018-05-09 12:01:36 +05:30
});
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.initAutoSave();
},
methods: {
...mapActions([
'saveNote',
'stopPolling',
'restartPolling',
'removePlaceholderNotes',
'closeIssue',
'reopenIssue',
'toggleIssueLocalState',
'toggleStateButtonLoading',
]),
setIsSubmitButtonDisabled(note, isSubmitting) {
if (!_.isEmpty(note) && !isSubmitting) {
this.isSubmitButtonDisabled = false;
} else {
this.isSubmitButtonDisabled = true;
}
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
handleSave(withIssueAction) {
this.isSubmitting = true;
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
if (this.note.length) {
const noteData = {
endpoint: this.endpoint,
flashContainer: this.$el,
data: {
note: {
noteable_type: this.noteableType,
noteable_id: this.getNoteableData.id,
note: this.note,
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
merge_request_diff_head_sha: this.getNoteableData.diff_head_sha,
2018-05-09 12:01:36 +05:30
},
};
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (this.noteType === constants.DISCUSSION) {
noteData.data.note.type = constants.DISCUSSION_NOTE;
}
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
this.note = ''; // Empty textarea while being requested. Repopulate in catch
this.resizeTextarea();
this.stopPolling();
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.saveNote(noteData)
.then(res => {
this.enableButton();
this.restartPolling();
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (res.errors) {
if (res.errors.commands_only) {
2018-03-17 18:26:18 +05:30
this.discard();
2018-05-09 12:01:36 +05:30
} else {
Flash(
'Something went wrong while adding your comment. Please try again.',
'alert',
this.$refs.commentForm,
);
2018-03-17 18:26:18 +05:30
}
2018-05-09 12:01:36 +05:30
} else {
this.discard();
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (withIssueAction) {
this.toggleIssueState();
}
})
.catch(() => {
this.enableButton();
this.discard(false);
const msg = `Your comment could not be submitted!
2018-03-17 18:26:18 +05:30
Please check your network connection and try again.`;
2018-05-09 12:01:36 +05:30
Flash(msg, 'alert', this.$el);
this.note = noteData.data.note.note; // Restore textarea content.
this.removePlaceholderNotes();
});
} else {
this.toggleIssueState();
}
},
enableButton() {
this.isSubmitting = false;
},
toggleIssueState() {
if (this.isOpen) {
this.closeIssue()
.then(() => this.enableButton())
.catch(() => {
this.enableButton();
this.toggleStateButtonLoading(false);
Flash(
sprintf(
2018-11-08 19:23:39 +05:30
__('Something went wrong while closing the %{issuable}. Please try again later'),
2018-05-09 12:01:36 +05:30
{ issuable: this.noteableDisplayName },
),
);
});
} else {
this.reopenIssue()
.then(() => this.enableButton())
2019-02-15 15:39:39 +05:30
.catch(({ data }) => {
2018-05-09 12:01:36 +05:30
this.enableButton();
this.toggleStateButtonLoading(false);
2019-02-15 15:39:39 +05:30
let errorMessage = sprintf(
__('Something went wrong while reopening the %{issuable}. Please try again later'),
{ issuable: this.noteableDisplayName },
2018-05-09 12:01:36 +05:30
);
2019-02-15 15:39:39 +05:30
if (data) {
errorMessage = Object.values(data).join('\n');
}
Flash(errorMessage);
2018-05-09 12:01:36 +05:30
});
}
},
discard(shouldClear = true) {
// `blur` is needed to clear slash commands autocomplete cache if event fired.
// `focus` is needed to remain cursor in the textarea.
this.$refs.textarea.blur();
this.$refs.textarea.focus();
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (shouldClear) {
this.note = '';
this.resizeTextarea();
this.$refs.markdownField.previewMarkdown = false;
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.autosave.reset();
},
setNoteType(type) {
this.noteType = type;
},
editCurrentUserLastNote() {
if (this.note === '') {
const lastNote = this.getCurrentUserLastNote;
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (lastNote) {
eventHub.$emit('enterEditMode', {
noteId: lastNote.id,
});
2018-03-17 18:26:18 +05:30
}
2018-05-09 12:01:36 +05:30
}
},
initAutoSave() {
if (this.isLoggedIn) {
2018-11-08 19:23:39 +05:30
const noteableType = capitalizeFirstCharacter(convertToCamelCase(this.noteableType));
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
this.autosave = new Autosave($(this.$refs.textarea), [
'Note',
noteableType,
this.getNoteableData.id,
]);
}
},
resizeTextarea() {
this.$nextTick(() => {
Autosize.update(this.$refs.textarea);
});
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
},
};
2018-03-17 18:26:18 +05:30
</script>
<template>
<div>
<note-signed-out-widget v-if="!isLoggedIn" />
2019-02-15 15:39:39 +05:30
<discussion-locked-widget v-else-if="!canCreateNote" :issuable-type="issuableTypeTitle" />
<ul v-else-if="canCreateNote" class="notes notes-form timeline">
<timeline-entry-item class="note-form">
<div class="flash-container error-alert timeline-content"></div>
<div class="timeline-icon d-none d-sm-none d-md-block">
<user-avatar-link
v-if="author"
:link-href="author.path"
:img-src="author.avatar_url"
:img-alt="author.name"
:img-size="40"
/>
</div>
<div class="timeline-content timeline-content-form">
<form ref="commentForm" class="new-note common-note-form gfm-form js-main-target-form">
<div class="error-alert"></div>
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
<issue-warning
v-if="hasWarning(getNoteableData)"
:is-locked="isLocked(getNoteableData)"
:is-confidential="isConfidential(getNoteableData)"
/>
2019-01-03 12:48:30 +05:30
2019-02-15 15:39:39 +05:30
<markdown-field
ref="markdownField"
:markdown-preview-path="markdownPreviewPath"
:markdown-docs-path="markdownDocsPath"
:quick-actions-docs-path="quickActionsDocsPath"
:add-spacing-classes="false"
>
<textarea
id="note-body"
ref="textarea"
slot="textarea"
v-model="note"
2019-07-31 22:56:46 +05:30
dir="auto"
2019-02-15 15:39:39 +05:30
:disabled="isSubmitting"
name="note[note]"
class="note-textarea js-vue-comment-form js-note-text
2018-12-05 23:21:45 +05:30
js-gfm-input js-autosize markdown-area js-vue-textarea qa-comment-input"
2019-02-15 15:39:39 +05:30
data-supports-quick-actions="true"
aria-label="Description"
placeholder="Write a comment or drag your files here…"
2019-03-02 22:35:43 +05:30
@keydown.up="editCurrentUserLastNote()"
@keydown.meta.enter="handleSave()"
@keydown.ctrl.enter="handleSave()"
2019-02-15 15:39:39 +05:30
>
</textarea>
</markdown-field>
<div class="note-form-actions">
<div
class="float-left btn-group
append-right-10 comment-type-dropdown js-comment-type-dropdown droplab-dropdown"
>
<button
:disabled="isSubmitButtonDisabled"
2019-03-02 22:35:43 +05:30
class="btn btn-success js-comment-button js-comment-submit-button
2018-12-05 23:21:45 +05:30
qa-comment-button"
2019-02-15 15:39:39 +05:30
type="submit"
2019-07-31 22:56:46 +05:30
:data-track-label="trackingLabel"
data-track-event="click_button"
2019-03-02 22:35:43 +05:30
@click.prevent="handleSave()"
2019-02-15 15:39:39 +05:30
>
{{ __(commentButtonTitle) }}
</button>
2018-03-17 18:26:18 +05:30
<button
2019-02-15 15:39:39 +05:30
:disabled="isSubmitButtonDisabled"
name="button"
2018-11-08 19:23:39 +05:30
type="button"
2019-03-02 22:35:43 +05:30
class="btn btn-success note-type-toggle js-note-new-discussion dropdown-toggle qa-note-dropdown"
2019-02-15 15:39:39 +05:30
data-display="static"
data-toggle="dropdown"
aria-label="Open comment type dropdown"
>
<i aria-hidden="true" class="fa fa-caret-down toggle-icon"> </i>
2018-03-17 18:26:18 +05:30
</button>
2019-02-15 15:39:39 +05:30
<ul class="note-type-dropdown dropdown-open-top dropdown-menu">
<li :class="{ 'droplab-item-selected': noteType === 'comment' }">
<button
type="button"
class="btn btn-transparent"
2019-03-02 22:35:43 +05:30
@click.prevent="setNoteType('comment')"
2019-02-15 15:39:39 +05:30
>
<i aria-hidden="true" class="fa fa-check icon"> </i>
<div class="description">
<strong>Comment</strong>
<p>Add a general comment to this {{ noteableDisplayName }}.</p>
</div>
</button>
</li>
<li class="divider droplab-item-ignore"></li>
<li :class="{ 'droplab-item-selected': noteType === 'discussion' }">
<button
type="button"
class="btn btn-transparent qa-discussion-option"
2019-03-02 22:35:43 +05:30
@click.prevent="setNoteType('discussion')"
2019-02-15 15:39:39 +05:30
>
<i aria-hidden="true" class="fa fa-check icon"> </i>
<div class="description">
<strong>Start discussion</strong>
<p>{{ startDiscussionDescription }}</p>
</div>
</button>
</li>
</ul>
2018-03-17 18:26:18 +05:30
</div>
2019-02-15 15:39:39 +05:30
<loading-button
2019-07-31 22:56:46 +05:30
v-if="canToggleIssueState"
2019-02-15 15:39:39 +05:30
:loading="isToggleStateButtonLoading"
:container-class="[
actionButtonClassNames,
'btn btn-comment btn-comment-and-close js-action-button',
]"
:disabled="isToggleStateButtonLoading || isSubmitting"
:label="issueActionButtonTitle"
2019-03-02 22:35:43 +05:30
@click="handleSave(true)"
2019-02-15 15:39:39 +05:30
/>
</div>
</form>
2018-03-17 18:26:18 +05:30
</div>
2019-02-15 15:39:39 +05:30
</timeline-entry-item>
</ul>
2018-03-17 18:26:18 +05:30
</div>
</template>