BigW Consortium Gitlab

diff.js 3.39 KB
Newer Older
1
/* eslint-disable class-methods-use-this */
Fatih Acet committed
2

3
require('./lib/utils/url_utility');
4

5 6
const UNFOLD_COUNT = 20;
let isBound = false;
Fatih Acet committed
7

8 9 10 11 12
class Diff {
  constructor() {
    const $diffFile = $('.files .diff-file');
    $diffFile.singleFileDiff();
    $diffFile.filesCommentButton();
13

14
    $diffFile.each((index, file) => new gl.ImageFile(file));
15

16 17 18
    if (this.diffViewType() === 'parallel') {
      $('.content-wrapper .container-fluid').removeClass('container-limited');
    }
19

20 21 22 23 24 25
    if (!isBound) {
      $(document)
        .on('click', '.js-unfold', this.handleClickUnfold.bind(this))
        .on('click', '.diff-line-num a', this.handleClickLineNum.bind(this));
      isBound = true;
    }
26

27 28
    if (gl.utils.getLocationHash()) {
      this.highlightSelectedLine();
Fatih Acet committed
29 30
    }

31 32 33 34 35
    this.openAnchoredDiff();
  }

  handleClickUnfold(e) {
    const $target = $(e.target);
36
    const [oldLineNumber, newLineNumber] = this.lineNumbers($target.parent());
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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;
57
      }
58
    }
59

60 61 62
    const file = $target.parents('.diff-file');
    const link = file.data('blob-diff-path');
    const view = file.data('view');
63

64 65 66
    const params = { since, to, bottom, offset, unfold, view };
    $.get(link, params, response => $target.parent().replaceWith(response));
  }
67

68 69 70
  openAnchoredDiff(cb) {
    const locationHash = gl.utils.getLocationHash();
    const anchoredDiff = locationHash && locationHash.split('_')[0];
71

72 73 74 75 76 77 78 79 80 81 82 83 84
    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();
        if (cb) cb();
      });
    } else if (cb) {
      cb();
85
    }
86
  }
87

88 89 90 91 92 93 94
  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;
95
    }
96 97
    this.highlightSelectedLine();
  }
98

99 100 101 102 103
  diffViewType() {
    return $('.inline-parallel-buttons a.active').data('view-type');
  }

  lineNumbers(line) {
104 105
    const children = line.find('.diff-line-num').toArray();
    if (children.length !== 2) {
106
      return [0, 0];
107
    }
108
    return children.map(elm => parseInt($(elm).data('linenumber'), 10) || 0);
109
  }
Fatih Acet committed
110

111 112 113 114
  highlightSelectedLine() {
    const hash = gl.utils.getLocationHash();
    const $diffFiles = $('.diff-file');
    $diffFiles.find('.hll').removeClass('hll');
115

116 117 118 119
    if (hash) {
      $diffFiles
        .find(`tr#${hash}:not(.match) td, td#${hash}, td[data-line-code="${hash}"]`)
        .addClass('hll');
120 121
    }
  }
122
}
Fatih Acet committed
123

124 125
window.gl = window.gl || {};
window.gl.Diff = Diff;