2016-12-02 23:43:20 +00:00
|
|
|
/* eslint-disable class-methods-use-this */
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-01-28 01:33:58 +00:00
|
|
|
require('./lib/utils/url_utility');
|
2017-01-25 20:34:49 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
const UNFOLD_COUNT = 20;
|
|
|
|
let isBound = false;
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
class Diff {
|
|
|
|
constructor() {
|
|
|
|
const $diffFile = $('.files .diff-file');
|
|
|
|
$diffFile.singleFileDiff();
|
|
|
|
$diffFile.filesCommentButton();
|
2016-12-04 11:12:23 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
$diffFile.each((index, file) => new gl.ImageFile(file));
|
2016-10-25 19:25:46 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
if (this.diffViewType() === 'parallel') {
|
|
|
|
$('.content-wrapper .container-fluid').removeClass('container-limited');
|
|
|
|
}
|
2016-10-25 06:27:21 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
if (!isBound) {
|
|
|
|
$(document)
|
|
|
|
.on('click', '.js-unfold', this.handleClickUnfold.bind(this))
|
|
|
|
.on('click', '.diff-line-num a', this.handleClickLineNum.bind(this));
|
|
|
|
isBound = true;
|
|
|
|
}
|
2017-03-02 13:43:52 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
if (gl.utils.getLocationHash()) {
|
|
|
|
this.highlightSelectedLine();
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
this.openAnchoredDiff();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickUnfold(e) {
|
|
|
|
const $target = $(e.target);
|
2017-03-21 17:37:25 +00:00
|
|
|
const [oldLineNumber, newLineNumber] = this.lineNumbers($target.parent());
|
2017-03-11 07:30:44 +00:00
|
|
|
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;
|
2016-10-26 01:22:09 +00:00
|
|
|
}
|
2017-03-11 07:30:44 +00:00
|
|
|
}
|
2016-10-26 01:22:09 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
const file = $target.parents('.diff-file');
|
|
|
|
const link = file.data('blob-diff-path');
|
|
|
|
const view = file.data('view');
|
2016-10-26 01:22:09 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
const params = { since, to, bottom, offset, unfold, view };
|
|
|
|
$.get(link, params, response => $target.parent().replaceWith(response));
|
|
|
|
}
|
2016-10-26 01:22:09 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
openAnchoredDiff(cb) {
|
|
|
|
const locationHash = gl.utils.getLocationHash();
|
|
|
|
const anchoredDiff = locationHash && locationHash.split('_')[0];
|
2016-09-09 15:47:43 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
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();
|
2016-10-27 20:32:30 +00:00
|
|
|
}
|
2017-03-11 07:30:44 +00:00
|
|
|
}
|
2016-10-26 01:22:09 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
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;
|
2016-09-19 23:02:52 +00:00
|
|
|
}
|
2017-03-11 07:30:44 +00:00
|
|
|
this.highlightSelectedLine();
|
|
|
|
}
|
2016-09-19 23:02:52 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
diffViewType() {
|
|
|
|
return $('.inline-parallel-buttons a.active').data('view-type');
|
|
|
|
}
|
|
|
|
|
|
|
|
lineNumbers(line) {
|
2017-03-22 07:57:31 +00:00
|
|
|
const children = line.find('.diff-line-num').toArray();
|
|
|
|
if (children.length !== 2) {
|
2017-03-11 07:30:44 +00:00
|
|
|
return [0, 0];
|
2016-10-25 19:25:46 +00:00
|
|
|
}
|
2017-03-22 07:57:31 +00:00
|
|
|
return children.map(elm => parseInt($(elm).data('linenumber'), 10) || 0);
|
2017-03-11 07:30:44 +00:00
|
|
|
}
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
highlightSelectedLine() {
|
|
|
|
const hash = gl.utils.getLocationHash();
|
|
|
|
const $diffFiles = $('.diff-file');
|
|
|
|
$diffFiles.find('.hll').removeClass('hll');
|
2016-10-25 06:27:21 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
if (hash) {
|
|
|
|
$diffFiles
|
|
|
|
.find(`tr#${hash}:not(.match) td, td#${hash}, td[data-line-code="${hash}"]`)
|
|
|
|
.addClass('hll');
|
2016-10-25 19:25:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 07:30:44 +00:00
|
|
|
}
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-03-11 07:30:44 +00:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.Diff = Diff;
|