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

212 lines
6.1 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 { mapGetters, mapActions } from 'vuex';
import { escape } from 'underscore';
import Flash from '../../flash';
import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
import noteHeader from './note_header.vue';
import noteActions from './note_actions.vue';
import noteBody from './note_body.vue';
import eventHub from '../event_hub';
import noteable from '../mixins/noteable';
import resolvable from '../mixins/resolvable';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export default {
2018-11-08 19:23:39 +05:30
name: 'NoteableNote',
2018-05-09 12:01:36 +05:30
components: {
userAvatarLink,
noteHeader,
noteActions,
noteBody,
},
mixins: [noteable, resolvable],
props: {
note: {
type: Object,
required: true,
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
},
data() {
return {
isEditing: false,
isDeleting: false,
isRequesting: false,
isResolving: false,
};
},
computed: {
2018-11-08 19:23:39 +05:30
...mapGetters(['targetNoteHash', 'getNoteableData', 'getUserData']),
2018-05-09 12:01:36 +05:30
author() {
return this.note.author;
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
classNameBindings() {
2018-03-17 18:26:18 +05:30
return {
2018-11-08 19:23:39 +05:30
[`note-row-${this.note.id}`]: true,
2018-05-09 12:01:36 +05:30
'is-editing': this.isEditing && !this.isRequesting,
'is-requesting being-posted': this.isRequesting,
'disabled-content': this.isDeleting,
2018-11-08 19:23:39 +05:30
target: this.isTarget,
2018-03-17 18:26:18 +05:30
};
},
2018-11-08 19:23:39 +05:30
canResolve() {
return this.note.resolvable && !!this.getUserData.id;
},
2018-05-09 12:01:36 +05:30
canReportAsAbuse() {
2018-11-08 19:23:39 +05:30
return this.note.report_abuse_path && this.author.id !== this.getUserData.id;
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
noteAnchorId() {
return `note_${this.note.id}`;
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
isTarget() {
return this.targetNoteHash === this.noteAnchorId;
},
2018-05-09 12:01:36 +05:30
},
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
created() {
eventHub.$on('enterEditMode', ({ noteId }) => {
if (noteId === this.note.id) {
2018-03-17 18:26:18 +05:30
this.isEditing = true;
2018-05-09 12:01:36 +05:30
this.scrollToNoteIfNeeded($(this.$el));
}
});
},
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
mounted() {
if (this.isTarget) {
this.scrollToNoteIfNeeded($(this.$el));
}
},
2018-05-09 12:01:36 +05:30
methods: {
2018-11-08 19:23:39 +05:30
...mapActions(['deleteNote', 'updateNote', 'toggleResolveNote', 'scrollToNoteIfNeeded']),
2018-05-09 12:01:36 +05:30
editHandler() {
this.isEditing = true;
},
deleteHandler() {
// eslint-disable-next-line no-alert
2018-11-08 19:23:39 +05:30
if (window.confirm('Are you sure you want to delete this comment?')) {
2018-05-09 12:01:36 +05:30
this.isDeleting = true;
2018-11-20 20:47:30 +05:30
this.$emit('handleDeleteNote', this.note);
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.deleteNote(this.note)
2018-03-17 18:26:18 +05:30
.then(() => {
2018-05-09 12:01:36 +05:30
this.isDeleting = false;
2018-03-17 18:26:18 +05:30
})
.catch(() => {
2018-11-08 19:23:39 +05:30
Flash('Something went wrong while deleting your note. Please try again.');
2018-05-09 12:01:36 +05:30
this.isDeleting = false;
2018-03-17 18:26:18 +05:30
});
2018-05-09 12:01:36 +05:30
}
},
formUpdateHandler(noteText, parentElement, callback) {
const data = {
endpoint: this.note.path,
note: {
2018-11-08 19:23:39 +05:30
target_type: this.getNoteableData.targetType,
2018-05-09 12:01:36 +05:30
target_id: this.note.noteable_id,
note: { note: noteText },
},
};
this.isRequesting = true;
this.oldContent = this.note.note_html;
this.note.note_html = escape(noteText);
this.updateNote(data)
.then(() => {
this.isEditing = false;
this.isRequesting = false;
2018-03-17 18:26:18 +05:30
this.oldContent = null;
2018-05-09 12:01:36 +05:30
$(this.$refs.noteBody.$el).renderGFM();
this.$refs.noteBody.resetAutoSave();
callback();
})
.catch(() => {
this.isRequesting = false;
this.isEditing = true;
this.$nextTick(() => {
2018-11-08 19:23:39 +05:30
const msg = 'Something went wrong while editing your comment. Please try again.';
2018-05-09 12:01:36 +05:30
Flash(msg, 'alert', this.$el);
this.recoverNoteContent(noteText);
callback();
});
});
},
formCancelHandler(shouldConfirm, isDirty) {
if (shouldConfirm && isDirty) {
// eslint-disable-next-line no-alert
2018-11-08 19:23:39 +05:30
if (!window.confirm('Are you sure you want to cancel editing this comment?')) return;
2018-05-09 12:01:36 +05:30
}
this.$refs.noteBody.resetAutoSave();
if (this.oldContent) {
this.note.note_html = this.oldContent;
this.oldContent = null;
}
this.isEditing = false;
},
recoverNoteContent(noteText) {
// we need to do this to prevent noteForm inconsistent content warning
// this is something we intentionally do so we need to recover the content
this.note.note = noteText;
2018-11-08 19:23:39 +05:30
this.$refs.noteBody.note.note = noteText;
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>
<li
:id="noteAnchorId"
:class="classNameBindings"
2018-11-08 19:23:39 +05:30
:data-award-url="note.toggle_award_path"
:data-note-id="note.id"
class="note timeline-entry"
>
2018-03-17 18:26:18 +05:30
<div class="timeline-entry-inner">
<div class="timeline-icon">
<user-avatar-link
:link-href="author.path"
:img-src="author.avatar_url"
:img-alt="author.name"
:img-size="40"
/>
</div>
<div class="timeline-content">
<div class="note-header">
<note-header
:author="author"
:created-at="note.created_at"
:note-id="note.id"
/>
<note-actions
:author-id="author.id"
:note-id="note.id"
2018-11-08 19:23:39 +05:30
:note-url="note.noteable_note_url"
2018-03-17 18:26:18 +05:30
:access-level="note.human_access"
:can-edit="note.current_user.can_edit"
2018-05-09 12:01:36 +05:30
:can-award-emoji="note.current_user.can_award_emoji"
2018-03-17 18:26:18 +05:30
:can-delete="note.current_user.can_edit"
:can-report-as-abuse="canReportAsAbuse"
2018-11-08 19:23:39 +05:30
:can-resolve="note.current_user.can_resolve"
2018-03-17 18:26:18 +05:30
:report-abuse-path="note.report_abuse_path"
2018-03-27 19:54:05 +05:30
:resolvable="note.resolvable"
:is-resolved="note.resolved"
:is-resolving="isResolving"
:resolved-by="note.resolved_by"
2018-03-17 18:26:18 +05:30
@handleEdit="editHandler"
@handleDelete="deleteHandler"
2018-03-27 19:54:05 +05:30
@handleResolve="resolveHandler"
2018-03-17 18:26:18 +05:30
/>
</div>
<note-body
2018-11-08 19:23:39 +05:30
ref="noteBody"
2018-03-17 18:26:18 +05:30
:note="note"
:can-edit="note.current_user.can_edit"
:is-editing="isEditing"
@handleFormUpdate="formUpdateHandler"
2018-11-08 19:23:39 +05:30
@cancelForm="formCancelHandler"
2018-03-17 18:26:18 +05:30
/>
</div>
</div>
</li>
</template>