debian-mirror-gitlab/app/assets/javascripts/notes/mixins/discussion_navigation.js

116 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-03-09 13:42:32 +05:30
import { mapGetters, mapActions, mapState } from 'vuex';
2018-11-18 11:00:15 +05:30
import { scrollToElement } from '~/lib/utils/common_utils';
2019-02-15 15:39:39 +05:30
import eventHub from '../../notes/event_hub';
2018-11-18 11:00:15 +05:30
2020-04-08 14:13:33 +05:30
/**
* @param {string} selector
* @returns {boolean}
*/
function scrollTo(selector) {
const el = document.querySelector(selector);
if (el) {
scrollToElement(el);
return true;
}
return false;
}
/**
* @param {object} self Component instance with mixin applied
* @param {string} id Discussion id we are jumping to
*/
function diffsJump({ expandDiscussion }, id) {
const selector = `ul.notes[data-discussion-id="${id}"]`;
eventHub.$once('scrollToDiscussion', () => scrollTo(selector));
expandDiscussion({ discussionId: id });
}
/**
* @param {object} self Component instance with mixin applied
* @param {string} id Discussion id we are jumping to
* @returns {boolean}
*/
function discussionJump({ expandDiscussion }, id) {
const selector = `div.discussion[data-discussion-id="${id}"]`;
expandDiscussion({ discussionId: id });
return scrollTo(selector);
}
/**
* @param {object} self Component instance with mixin applied
* @param {string} id Discussion id we are jumping to
*/
function switchToDiscussionsTabAndJumpTo(self, id) {
window.mrTabs.eventHub.$once('MergeRequestTabChange', () => {
setTimeout(() => discussionJump(self, id), 0);
});
window.mrTabs.tabShown('show');
}
/**
* @param {object} self Component instance with mixin applied
* @param {object} discussion Discussion we are jumping to
*/
function jumpToDiscussion(self, discussion) {
const { id, diff_discussion: isDiffDiscussion } = discussion;
if (id) {
const activeTab = window.mrTabs.currentAction;
if (activeTab === 'diffs' && isDiffDiscussion) {
diffsJump(self, id);
} else if (activeTab === 'show') {
discussionJump(self, id);
} else {
switchToDiscussionsTabAndJumpTo(self, id);
}
}
}
/**
* @param {object} self Component instance with mixin applied
* @param {function} fn Which function used to get the target discussion's id
* @param {string} [discussionId=this.currentDiscussionId] Current discussion id, will be null if discussions have not been traversed yet
*/
function handleDiscussionJump(self, fn, discussionId = self.currentDiscussionId) {
const isDiffView = window.mrTabs.currentAction === 'diffs';
const targetId = fn(discussionId, isDiffView);
const discussion = self.getDiscussion(targetId);
jumpToDiscussion(self, discussion);
self.setCurrentDiscussionId(targetId);
}
2018-11-18 11:00:15 +05:30
export default {
2020-03-09 13:42:32 +05:30
computed: {
...mapGetters([
'nextUnresolvedDiscussionId',
'previousUnresolvedDiscussionId',
'getDiscussion',
]),
...mapState({
currentDiscussionId: state => state.notes.currentDiscussionId,
}),
},
2018-11-18 11:00:15 +05:30
methods: {
2020-03-09 13:42:32 +05:30
...mapActions(['expandDiscussion', 'setCurrentDiscussionId']),
jumpToNextDiscussion() {
2020-04-08 14:13:33 +05:30
handleDiscussionJump(this, this.nextUnresolvedDiscussionId);
2020-03-09 13:42:32 +05:30
},
jumpToPreviousDiscussion() {
2020-04-08 14:13:33 +05:30
handleDiscussionJump(this, this.previousUnresolvedDiscussionId);
2020-03-09 13:42:32 +05:30
},
2020-04-08 14:13:33 +05:30
/**
* Go to the next discussion from the given discussionId
* @param {String} discussionId The id we are jumping from
*/
jumpToNextRelativeDiscussion(discussionId) {
handleDiscussionJump(this, this.nextUnresolvedDiscussionId, discussionId);
2020-03-09 13:42:32 +05:30
},
2018-11-18 11:00:15 +05:30
},
};