2016-12-02 18:43:20 -05:00
|
|
|
/* eslint-disable class-methods-use-this */
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-12-02 18:43:20 -05:00
|
|
|
(() => {
|
2016-10-25 15:25:46 -04:00
|
|
|
const UNFOLD_COUNT = 20;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-10-25 15:25:46 -04:00
|
|
|
class Diff {
|
|
|
|
constructor() {
|
2016-12-04 06:12:23 -05:00
|
|
|
const $diffFile = $('.files .diff-file');
|
|
|
|
$diffFile.singleFileDiff();
|
|
|
|
$diffFile.filesCommentButton();
|
|
|
|
|
|
|
|
$diffFile.each((index, file) => new gl.ImageFile(file));
|
2016-10-25 15:25:46 -04:00
|
|
|
|
2016-09-19 19:02:52 -04:00
|
|
|
if (this.diffViewType() === 'parallel') {
|
|
|
|
$('.content-wrapper .container-fluid').removeClass('container-limited');
|
|
|
|
}
|
2016-10-25 21:22:09 -04:00
|
|
|
|
2016-10-25 02:27:21 -04:00
|
|
|
$(document)
|
2016-10-25 21:22:09 -04:00
|
|
|
.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));
|
2016-10-25 02:27:21 -04:00
|
|
|
|
|
|
|
this.highlighSelectedLine();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 12:35:26 -05:00
|
|
|
handleClickUnfold(e) {
|
|
|
|
const $target = $(e.target);
|
2016-10-26 10:37:17 -04:00
|
|
|
// 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];
|
2016-10-25 21:22:09 -04:00
|
|
|
const offset = newLineNumber - oldLineNumber;
|
|
|
|
const bottom = $target.hasClass('js-unfold-bottom');
|
2016-10-27 16:32:30 -04:00
|
|
|
let since;
|
|
|
|
let to;
|
2016-10-25 21:22:09 -04:00
|
|
|
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
|
2016-10-26 10:37:17 -04:00
|
|
|
const prevNewLine = this.lineNumbers($target.parent().prev())[1];
|
2016-10-25 21:22:09 -04:00
|
|
|
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 };
|
2016-10-27 16:32:30 -04:00
|
|
|
$.get(link, params, response => $target.parent().replaceWith(response));
|
2016-10-25 21:22:09 -04:00
|
|
|
}
|
|
|
|
|
2016-09-09 11:47:43 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 12:35:26 -05:00
|
|
|
handleClickLineNum(e) {
|
|
|
|
const hash = $(e.currentTarget).attr('href');
|
|
|
|
e.preventDefault();
|
2016-10-27 16:32:30 -04:00
|
|
|
if (window.history.pushState) {
|
|
|
|
window.history.pushState(null, null, hash);
|
2016-10-25 21:22:09 -04:00
|
|
|
} else {
|
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
|
|
|
this.highlighSelectedLine();
|
2016-10-27 16:32:30 -04:00
|
|
|
}
|
2016-10-25 21:22:09 -04:00
|
|
|
|
2016-10-25 15:25:46 -04:00
|
|
|
diffViewType() {
|
2016-09-19 19:02:52 -04:00
|
|
|
return $('.inline-parallel-buttons a.active').data('view-type');
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:25:46 -04:00
|
|
|
lineNumbers(line) {
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!line.children().length) {
|
|
|
|
return [0, 0];
|
|
|
|
}
|
2016-10-27 16:32:30 -04:00
|
|
|
return line.find('.diff-line-num').map((i, elm) => parseInt($(elm).data('linenumber'), 10));
|
2016-10-25 15:25:46 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-10-25 15:25:46 -04:00
|
|
|
highlighSelectedLine() {
|
|
|
|
const $diffFiles = $('.diff-file');
|
|
|
|
$diffFiles.find('.hll').removeClass('hll');
|
2016-10-25 02:27:21 -04:00
|
|
|
|
2016-10-25 15:25:46 -04:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-12-02 18:43:20 -05:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.Diff = Diff;
|
|
|
|
})();
|