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

207 lines
5.6 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
2018-05-09 12:01:36 +05:30
import { mapGetters, mapActions } from 'vuex';
import { getLocationHash } from '../../lib/utils/url_utility';
import Flash from '../../flash';
import * as constants from '../constants';
2018-11-08 19:23:39 +05:30
import eventHub from '../event_hub';
2018-05-09 12:01:36 +05:30
import noteableNote from './noteable_note.vue';
import noteableDiscussion from './noteable_discussion.vue';
import systemNote from '../../vue_shared/components/notes/system_note.vue';
import commentForm from './comment_form.vue';
import placeholderNote from '../../vue_shared/components/notes/placeholder_note.vue';
import placeholderSystemNote from '../../vue_shared/components/notes/placeholder_system_note.vue';
import skeletonLoadingContainer from '../../vue_shared/components/notes/skeleton_note.vue';
2018-12-05 23:21:45 +05:30
import highlightCurrentUser from '~/behaviors/markdown/highlight_current_user';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export default {
name: 'NotesApp',
components: {
noteableNote,
noteableDiscussion,
systemNote,
commentForm,
placeholderNote,
placeholderSystemNote,
},
props: {
noteableData: {
type: Object,
required: true,
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
notesData: {
type: Object,
required: true,
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
userData: {
type: Object,
required: false,
default: () => ({}),
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
shouldShow: {
type: Boolean,
required: false,
default: true,
},
markdownVersion: {
type: Number,
required: false,
default: 0,
},
2018-05-09 12:01:36 +05:30
},
data() {
return {
isLoading: true,
};
},
computed: {
2018-11-08 19:23:39 +05:30
...mapGetters(['isNotesFetched', 'discussions', 'getNotesDataByProp', 'discussionCount']),
2018-05-09 12:01:36 +05:30
noteableType() {
return this.noteableData.noteableType;
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
allDiscussions() {
2018-05-09 12:01:36 +05:30
if (this.isLoading) {
const totalNotes = parseInt(this.notesData.totalNotes, 10) || 0;
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
return new Array(totalNotes).fill({
isSkeletonNote: true,
2018-03-17 18:26:18 +05:30
});
}
2018-11-08 19:23:39 +05:30
return this.discussions;
},
},
watch: {
shouldShow() {
if (!this.isNotesFetched) {
this.fetchNotes();
}
2018-03-27 19:54:05 +05:30
},
2018-05-09 12:01:36 +05:30
},
created() {
this.setNotesData(this.notesData);
this.setNoteableData(this.noteableData);
this.setUserData(this.userData);
2018-11-08 19:23:39 +05:30
this.setTargetNoteHash(getLocationHash());
eventHub.$once('fetchNotesData', this.fetchNotes);
2018-05-09 12:01:36 +05:30
},
mounted() {
2018-11-08 19:23:39 +05:30
if (this.shouldShow) {
this.fetchNotes();
}
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
const { parentElement } = this.$el;
if (parentElement && parentElement.classList.contains('js-vue-notes-event')) {
2018-05-09 12:01:36 +05:30
parentElement.addEventListener('toggleAward', event => {
const { awardName, noteId } = event.detail;
this.actionToggleAward({ awardName, noteId });
});
}
},
2018-12-05 23:21:45 +05:30
updated() {
this.$nextTick(() => highlightCurrentUser(this.$el.querySelectorAll('.gfm-project_member')));
},
2018-05-09 12:01:36 +05:30
methods: {
...mapActions({
2018-11-08 19:23:39 +05:30
fetchDiscussions: 'fetchDiscussions',
2018-05-09 12:01:36 +05:30
poll: 'poll',
actionToggleAward: 'toggleAward',
scrollToNoteIfNeeded: 'scrollToNoteIfNeeded',
setNotesData: 'setNotesData',
setNoteableData: 'setNoteableData',
setUserData: 'setUserData',
setLastFetchedAt: 'setLastFetchedAt',
setTargetNoteHash: 'setTargetNoteHash',
2018-11-08 19:23:39 +05:30
toggleDiscussion: 'toggleDiscussion',
setNotesFetchedState: 'setNotesFetchedState',
2018-05-09 12:01:36 +05:30
}),
2018-11-08 19:23:39 +05:30
getComponentName(discussion) {
if (discussion.isSkeletonNote) {
2018-05-09 12:01:36 +05:30
return skeletonLoadingContainer;
}
2018-11-08 19:23:39 +05:30
if (discussion.isPlaceholderNote) {
if (discussion.placeholderType === constants.SYSTEM_NOTE) {
2018-05-09 12:01:36 +05:30
return placeholderSystemNote;
2018-03-27 19:54:05 +05:30
}
2018-05-09 12:01:36 +05:30
return placeholderNote;
2018-11-08 19:23:39 +05:30
} else if (discussion.individual_note) {
return discussion.notes[0].system ? systemNote : noteableNote;
2018-05-09 12:01:36 +05:30
}
return noteableDiscussion;
},
2018-11-08 19:23:39 +05:30
getComponentData(discussion) {
return discussion.individual_note ? { note: discussion.notes[0] } : { discussion };
2018-05-09 12:01:36 +05:30
},
fetchNotes() {
2018-11-08 19:23:39 +05:30
return this.fetchDiscussions(this.getNotesDataByProp('discussionsPath'))
.then(() => {
this.initPolling();
})
2018-05-09 12:01:36 +05:30
.then(() => {
this.isLoading = false;
2018-11-08 19:23:39 +05:30
this.setNotesFetchedState(true);
2018-11-20 20:47:30 +05:30
eventHub.$emit('fetchedNotesData');
2018-05-09 12:01:36 +05:30
})
.then(() => this.$nextTick())
.then(() => this.checkLocationHash())
.catch(() => {
this.isLoading = false;
2018-11-08 19:23:39 +05:30
this.setNotesFetchedState(true);
Flash('Something went wrong while fetching comments. Please try again.');
2018-05-09 12:01:36 +05:30
});
},
initPolling() {
if (this.isPollingInitialized) {
return;
}
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt'));
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.poll();
this.isPollingInitialized = true;
},
checkLocationHash() {
const hash = getLocationHash();
2018-11-08 19:23:39 +05:30
const noteId = hash && hash.replace(/^note_/, '');
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
if (noteId) {
this.discussions.forEach(discussion => {
if (discussion.notes) {
discussion.notes.forEach(note => {
if (`${note.id}` === `${noteId}`) {
// FIXME: this modifies the store state without using a mutation/action
Object.assign(discussion, { expanded: true });
}
});
}
});
2018-05-09 12:01:36 +05:30
}
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>
2018-11-08 19:23:39 +05:30
<div
v-show="shouldShow"
id="notes"
>
2018-03-17 18:26:18 +05:30
<ul
id="notes-list"
2018-11-08 19:23:39 +05:30
class="notes main-notes-list timeline"
>
2018-03-17 18:26:18 +05:30
<component
2018-11-08 19:23:39 +05:30
:is="getComponentName(discussion)"
2018-12-05 23:21:45 +05:30
v-for="discussion in allDiscussions"
2018-11-08 19:23:39 +05:30
:key="discussion.id"
2018-12-05 23:21:45 +05:30
v-bind="getComponentData(discussion)"
2018-03-17 18:26:18 +05:30
/>
</ul>
2018-03-27 19:54:05 +05:30
<comment-form
:noteable-type="noteableType"
2018-11-08 19:23:39 +05:30
:markdown-version="markdownVersion"
2018-03-27 19:54:05 +05:30
/>
2018-03-17 18:26:18 +05:30
</div>
</template>