summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff.js
blob: 23eb470503e8a51ce429c75517fdafad8d335301 (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
import $ from 'jquery';
import { merge } from 'lodash';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import FilesCommentButton from './files_comment_button';
import initImageDiffHelper from './image_diff/helpers/init_image_diff';
import { getLocationHash } from './lib/utils/url_utility';
import SingleFileDiff from './single_file_diff';

const UNFOLD_COUNT = 20;
let isBound = false;

export default class Diff {
  constructor() {
    const $diffFile = $('.files .diff-file');

    $diffFile.each((index, file) => {
      if (!$.data(file, 'singleFileDiff')) {
        $.data(file, 'singleFileDiff', new SingleFileDiff(file));
      }
    });

    const tab = document.getElementById('diffs');
    if (!tab || (tab && tab.dataset && tab.dataset.isLocked !== ''))
      FilesCommentButton.init($diffFile);

    const firstFile = $('.files').first().get(0);
    const canCreateNote =
      firstFile && Object.prototype.hasOwnProperty.call(firstFile.dataset, 'canCreateNote');
    $diffFile.each((index, file) => initImageDiffHelper.initImageDiff(file, canCreateNote));

    if (!isBound) {
      $(document)
        .on('click', '.js-unfold', this.handleClickUnfold.bind(this))
        .on('click', '.diff-line-num a', this.handleClickLineNum.bind(this))
        .on('mousedown', 'td.line_content.parallel', this.handleParallelLineDown.bind(this));
      isBound = true;
    }

    if (getLocationHash()) {
      this.highlightSelectedLine();
    }

    this.openAnchoredDiff();

    this.prepareRenderedDiff();
  }

  handleClickUnfold(e) {
    const $target = $(e.target);
    const [oldLineNumber, newLineNumber] = this.lineNumbers($target.parent());
    const offset = newLineNumber - oldLineNumber;
    const bottom = $target.hasClass('js-unfold-bottom');
    let since;
    let to;
    let unfold = true;

    if (bottom) {
      const lineNumber = newLineNumber + 1;
      since = lineNumber;
      to = lineNumber + UNFOLD_COUNT;
    } else {
      const lineNumber = newLineNumber - 1;
      since = lineNumber - UNFOLD_COUNT;
      to = lineNumber;

      // make sure we aren't loading more than we need
      const prevNewLine = this.lineNumbers($target.parent().prev())[1];
      if (since <= prevNewLine + 1) {
        since = prevNewLine + 1;
        unfold = false;
      }
    }

    const file = $target.parents('.diff-file');
    const link = file.data('blobDiffPath');
    const view = file.data('view');

    const params = { since, to, bottom, offset, unfold, view };
    axios
      .get(link, { params })
      .then(({ data }) => $target.parent().replaceWith(data))
      .catch(() =>
        createAlert({
          message: __('An error occurred while loading diff'),
        }),
      );
  }

  openAnchoredDiff(cb) {
    const locationHash = getLocationHash();
    const anchoredDiff = locationHash && locationHash.split('_')[0];

    if (!anchoredDiff) return;

    const diffTitle = $(`#${anchoredDiff}`);
    const diffFile = diffTitle.closest('.diff-file');
    const nothingHereBlock = $('.nothing-here-block:visible', diffFile);
    if (nothingHereBlock.length) {
      const clickTarget = $('.js-file-title, .click-to-expand', diffFile);
      diffFile.data('singleFileDiff').toggleDiff(clickTarget, () => {
        this.highlightSelectedLine();
        this.prepareRenderedDiff();
        if (cb) cb();
      });
    } else if (cb) {
      cb();
    }
  }

  handleClickLineNum(e) {
    const hash = $(e.currentTarget).attr('href');
    e.preventDefault();
    if (window.history.pushState) {
      window.history.pushState(null, null, hash);
    } else {
      window.location.hash = hash;
    }
    this.highlightSelectedLine();
  }
  // eslint-disable-next-line class-methods-use-this
  handleParallelLineDown(e) {
    const line = $(e.currentTarget);
    const table = line.closest('table');

    table.removeClass('left-side-selected right-side-selected');

    const lineClass = ['left-side', 'right-side'].filter((name) => line.hasClass(name))[0];
    if (lineClass) {
      table.addClass(`${lineClass}-selected`);
    }
  }
  // eslint-disable-next-line class-methods-use-this
  diffViewType() {
    return $('.inline-parallel-buttons a.active').data('viewType');
  }
  // eslint-disable-next-line class-methods-use-this
  lineNumbers(line) {
    const children = line.find('.diff-line-num').toArray();
    if (children.length !== 2) {
      return [0, 0];
    }
    return children.map((elm) => parseInt($(elm).data('linenumber'), 10) || 0);
  }
  // eslint-disable-next-line class-methods-use-this
  highlightSelectedLine() {
    const hash = getLocationHash();
    const $diffFiles = $('.diff-file');
    $diffFiles.find('.hll').removeClass('hll');

    if (hash) {
      $diffFiles
        .find(`tr#${hash}:not(.match) td, td#${hash}, td[data-line-code="${hash}"]`)
        .addClass('hll');
    }
  }

  prepareRenderedDiff() {
    const allElements = this.elementsForRenderedDiff();
    const diff = this;

    for (const [fileHash, fileElements] of Object.entries(allElements)) {
      // eslint-disable no-param-reassign
      fileElements.rawButton.onclick = () => {
        diff.showRawViewer(fileHash, diff.elementsForRenderedDiff()[fileHash]);
      };

      fileElements.renderedButton.onclick = () => {
        diff.showRenderedViewer(fileHash, diff.elementsForRenderedDiff()[fileHash]);
      };
      // eslint-enable no-param-reassign

      diff.showRenderedViewer(fileHash, fileElements);
    }
  }

  // eslint-disable-next-line class-methods-use-this
  formatElementToObject = (element) => {
    const key = element.attributes['data-file-hash'].value;
    const name = element.attributes['data-diff-toggle-entity'].value;

    return { [key]: { [name]: element } };
  };

  elementsForRenderedDiff = () => {
    const $elements = $('[data-diff-toggle-entity]');

    if ($elements.length === 0) return {};

    const diff = this;

    return $elements.toArray().map(diff.formatElementToObject).reduce(merge);
  };

  // eslint-disable-next-line class-methods-use-this
  showRawViewer = (fileHash, elements) => {
    if (elements === undefined) return;

    elements.rawButton.classList.add('selected');
    elements.renderedButton.classList.remove('selected');

    elements.renderedViewer.classList.add('hidden');
    elements.rawViewer.classList.remove('hidden');
  };

  // eslint-disable-next-line class-methods-use-this
  showRenderedViewer = (fileHash, elements) => {
    if (elements === undefined) return;

    elements.rawButton.classList.remove('selected');
    elements.rawViewer.classList.add('hidden');

    elements.renderedButton.classList.add('selected');
    elements.renderedViewer.classList.remove('hidden');
  };
}