BigW Consortium Gitlab

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

3
(() => {
4
  const UNFOLD_COUNT = 20;
Fatih Acet committed
5

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

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

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

18
      $(document)
19 20 21
        .off('click', '.js-unfold, .diff-line-num a')
        .on('click', '.js-unfold', this.handleClickUnfold.bind(this))
        .on('click', '.diff-line-num a', this.handleClickLineNum.bind(this));
22 23

      this.highlighSelectedLine();
Fatih Acet committed
24 25
    }

26 27
    handleClickUnfold(e) {
      const $target = $(e.target);
28 29 30 31 32
      // current babel config relies on iterators implementation, so we cannot simply do:
      // const [oldLineNumber, newLineNumber] = this.lineNumbers($target.parent());
      const ref = this.lineNumbers($target.parent());
      const oldLineNumber = ref[0];
      const newLineNumber = ref[1];
33 34
      const offset = newLineNumber - oldLineNumber;
      const bottom = $target.hasClass('js-unfold-bottom');
35 36
      let since;
      let to;
37 38 39 40 41 42 43 44 45 46 47 48
      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
49
        const prevNewLine = this.lineNumbers($target.parent().prev())[1];
50 51 52 53 54 55 56 57 58 59 60
        if (since <= prevNewLine + 1) {
          since = prevNewLine + 1;
          unfold = false;
        }
      }

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

      const params = { since, to, bottom, offset, unfold, view };
61
      $.get(link, params, response => $target.parent().replaceWith(response));
62 63
    }

64 65 66 67 68 69 70 71 72 73 74
    openAnchoredDiff(anchoredDiff, cb) {
      const diffTitle = $(`#file-path-${anchoredDiff}`);
      const diffFile = diffTitle.closest('.diff-file');
      const nothingHereBlock = $('.nothing-here-block:visible', diffFile);
      if (nothingHereBlock.length) {
        diffFile.singleFileDiff(true, cb);
      } else {
        cb();
      }
    }

75 76 77
    handleClickLineNum(e) {
      const hash = $(e.currentTarget).attr('href');
      e.preventDefault();
78 79
      if (window.history.pushState) {
        window.history.pushState(null, null, hash);
80 81 82 83
      } else {
        window.location.hash = hash;
      }
      this.highlighSelectedLine();
84
    }
85

86
    diffViewType() {
87 88 89
      return $('.inline-parallel-buttons a.active').data('view-type');
    }

90
    lineNumbers(line) {
Fatih Acet committed
91 92 93
      if (!line.children().length) {
        return [0, 0];
      }
94
      return line.find('.diff-line-num').map((i, elm) => parseInt($(elm).data('linenumber'), 10));
95
    }
Fatih Acet committed
96

97 98 99
    highlighSelectedLine() {
      const $diffFiles = $('.diff-file');
      $diffFiles.find('.hll').removeClass('hll');
100

101 102 103 104 105 106 107 108
      if (window.location.hash !== '') {
        const hash = window.location.hash.replace('#', '');
        $diffFiles
          .find(`tr#${hash}:not(.match) td, td#${hash}, td[data-line-code="${hash}"]`)
          .addClass('hll');
      }
    }
  }
Fatih Acet committed
109

110 111 112
  window.gl = window.gl || {};
  window.gl.Diff = Diff;
})();