summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/mixins/discussion_navigation.js
blob: db5f9ebf3f0b320e382783561403d1de78d803c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { mapGetters, mapActions, mapState } from 'vuex';
import { scrollToElementWithContext, scrollToElement, contentTop } from '~/lib/utils/common_utils';
import { updateHistory } from '~/lib/utils/url_utility';
import eventHub from '../event_hub';

/**
 * @param {string} selector
 * @returns {boolean}
 */
function scrollTo(selector, { withoutContext = false, offset = 0 } = {}) {
  const el = document.querySelector(selector);
  const scrollFunction = withoutContext ? scrollToElement : scrollToElementWithContext;

  if (el) {
    scrollFunction(el, {
      behavior: 'auto',
      offset,
    });
    return true;
  }

  return false;
}

function updateUrlWithNoteId(noteId) {
  const newHistoryEntry = {
    state: null,
    title: window.title,
    url: `#note_${noteId}`,
    replace: true,
  };

  if (noteId) {
    // Temporarily mask the ID to avoid the browser default
    //    scrolling taking over which is broken with virtual
    //    scrolling enabled.
    const note = document.querySelector(`#note_${noteId}`);
    note?.setAttribute('id', `masked::${note.id}`);

    // Update the hash now that the ID "doesn't exist" in the page
    updateHistory(newHistoryEntry);

    // Unmask the note's ID
    note?.setAttribute('id', `note_${noteId}`);
  }
}

/**
 * @param {object} self Component instance with mixin applied
 * @param {string} id Discussion id we are jumping to
 */
function diffsJump({ expandDiscussion }, id, firstNoteId) {
  const selector = `ul.notes[data-discussion-id="${id}"]`;

  eventHub.$once('scrollToDiscussion', () => {
    scrollTo(selector);
    // Wait for the discussion scroll before updating to the more specific ID
    setTimeout(() => updateUrlWithNoteId(firstNoteId), 0);
  });
  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, {
    withoutContext: true,
    offset: window.gon?.features?.movedMrSidebar ? -28 : 0,
  });
}

/**
 * @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, notes } = discussion;
  const firstNoteId = notes?.[0]?.id;
  if (id) {
    const activeTab = window.mrTabs.currentAction;

    if (activeTab === 'diffs' && isDiffDiscussion) {
      diffsJump(self, id, firstNoteId);
    } 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
 */
function handleDiscussionJump(self, fn) {
  const isDiffView = window.mrTabs.currentAction === 'diffs';
  const targetId = fn(self.currentDiscussionId, isDiffView);
  const discussion = self.getDiscussion(targetId);
  const discussionFilePath = discussion?.diff_file?.file_path;

  window.location.hash = '';

  if (discussionFilePath) {
    self.scrollToFile({
      path: discussionFilePath,
    });
  }

  self.$nextTick(() => {
    jumpToDiscussion(self, discussion);
    self.setCurrentDiscussionId(targetId);
  });
}

function getAllDiscussionElements() {
  return Array.from(
    document.querySelectorAll('[data-discussion-id]:not([data-discussion-resolved])'),
  );
}

function hasReachedPageEnd() {
  return document.body.scrollHeight <= Math.ceil(window.scrollY + window.innerHeight);
}

function findNextClosestVisibleDiscussion(discussionElements) {
  const offsetHeight = contentTop();
  let isActive;
  const index = discussionElements.findIndex((element) => {
    const { y } = element.getBoundingClientRect();
    const visibleHorizontalOffset = Math.ceil(y) - offsetHeight;
    // handle rect rounding errors
    isActive = visibleHorizontalOffset < 2;
    return visibleHorizontalOffset >= 0;
  });
  return [discussionElements[index], index, isActive];
}

function getNextDiscussion() {
  const discussionElements = getAllDiscussionElements();
  const firstDiscussion = discussionElements[0];
  if (hasReachedPageEnd()) {
    return firstDiscussion;
  }
  const [nextClosestDiscussion, index, isActive] = findNextClosestVisibleDiscussion(
    discussionElements,
  );
  if (nextClosestDiscussion && !isActive) {
    return nextClosestDiscussion;
  }
  const nextDiscussion = discussionElements[index + 1];
  if (!nextClosestDiscussion || !nextDiscussion) {
    return firstDiscussion;
  }
  return nextDiscussion;
}

function getPreviousDiscussion() {
  const discussionElements = getAllDiscussionElements();
  const lastDiscussion = discussionElements[discussionElements.length - 1];
  const [, index] = findNextClosestVisibleDiscussion(discussionElements);
  const previousDiscussion = discussionElements[index - 1];
  if (previousDiscussion) {
    return previousDiscussion;
  }
  return lastDiscussion;
}

function handleJumpForBothPages(getDiscussion, ctx, fn, scrollOptions) {
  if (window.mrTabs.currentAction !== 'show') {
    handleDiscussionJump(ctx, fn);
  } else {
    const discussion = getDiscussion();
    const id = discussion.dataset.discussionId;
    ctx.expandDiscussion({ discussionId: id });
    scrollToElement(discussion, scrollOptions);
  }
}

export default {
  computed: {
    ...mapGetters([
      'nextUnresolvedDiscussionId',
      'previousUnresolvedDiscussionId',
      'getDiscussion',
    ]),
    ...mapState({
      currentDiscussionId: (state) => state.notes.currentDiscussionId,
    }),
  },
  methods: {
    ...mapActions(['expandDiscussion', 'setCurrentDiscussionId']),
    ...mapActions('diffs', ['scrollToFile']),

    jumpToNextDiscussion(scrollOptions) {
      handleJumpForBothPages(
        getNextDiscussion,
        this,
        this.nextUnresolvedDiscussionId,
        scrollOptions,
      );
    },

    jumpToPreviousDiscussion(scrollOptions) {
      handleJumpForBothPages(
        getPreviousDiscussion,
        this,
        this.previousUnresolvedDiscussionId,
        scrollOptions,
      );
    },

    jumpToFirstUnresolvedDiscussion() {
      this.setCurrentDiscussionId(null)
        .then(() => {
          this.jumpToNextDiscussion();
        })
        .catch(() => {});
    },
  },
};