2017-01-12 04:27:41 +00:00
|
|
|
/* eslint-disable func-names, prefer-arrow-callback, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, one-var-declaration-per-line, consistent-return, no-param-reassign, max-len */
|
2016-12-14 05:26:26 +00:00
|
|
|
|
2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2018-01-30 12:19:50 +00:00
|
|
|
import { __ } from './locale';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import createFlash from './flash';
|
2017-06-12 18:43:21 +00:00
|
|
|
import FilesCommentButton from './files_comment_button';
|
2017-10-07 04:25:17 +00:00
|
|
|
import imageDiffHelper from './image_diff/helpers/index';
|
2017-12-13 09:26:44 +00:00
|
|
|
import syntaxHighlight from './syntax_highlight';
|
2017-06-12 18:43:21 +00:00
|
|
|
|
2017-06-30 21:27:30 +00:00
|
|
|
const WRAPPER = '<div class="diff-content"></div>';
|
|
|
|
const LOADING_HTML = '<i class="fa fa-spinner fa-spin"></i>';
|
|
|
|
const ERROR_HTML = '<div class="nothing-here-block"><i class="fa fa-warning"></i> Could not load diff</div>';
|
|
|
|
const COLLAPSED_HTML = '<div class="nothing-here-block diff-collapsed">This diff is collapsed. <a class="click-to-expand">Click to expand it.</a></div>';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-07-06 18:39:28 +00:00
|
|
|
export default class SingleFileDiff {
|
2017-06-30 21:27:30 +00:00
|
|
|
constructor(file) {
|
2017-07-05 17:20:41 +00:00
|
|
|
this.file = file;
|
|
|
|
this.toggleDiff = this.toggleDiff.bind(this);
|
|
|
|
this.content = $('.diff-content', this.file);
|
|
|
|
this.$toggleIcon = $('.diff-toggle-caret', this.file);
|
2018-02-20 22:20:48 +00:00
|
|
|
this.diffForPath = this.content.find('[data-diff-for-path]').data('diffForPath');
|
2017-07-05 17:20:41 +00:00
|
|
|
this.isOpen = !this.diffForPath;
|
|
|
|
if (this.diffForPath) {
|
|
|
|
this.collapsedContent = this.content;
|
|
|
|
this.loadingContent = $(WRAPPER).addClass('loading').html(LOADING_HTML).hide();
|
|
|
|
this.content = null;
|
|
|
|
this.collapsedContent.after(this.loadingContent);
|
|
|
|
this.$toggleIcon.addClass('fa-caret-right');
|
|
|
|
} else {
|
|
|
|
this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
|
|
|
|
this.content.after(this.collapsedContent);
|
|
|
|
this.$toggleIcon.addClass('fa-caret-down');
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.js-file-title, .click-to-expand', this.file).on('click', (function (e) {
|
|
|
|
this.toggleDiff($(e.target));
|
|
|
|
}).bind(this));
|
|
|
|
}
|
2017-01-07 21:58:25 +00:00
|
|
|
|
2017-06-30 21:27:30 +00:00
|
|
|
toggleDiff($target, cb) {
|
2017-07-05 17:20:41 +00:00
|
|
|
if (!$target.hasClass('js-file-title') && !$target.hasClass('click-to-expand') && !$target.hasClass('diff-toggle-caret')) return;
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
if (!this.isOpen && !this.hasError) {
|
|
|
|
this.content.hide();
|
|
|
|
this.$toggleIcon.addClass('fa-caret-right').removeClass('fa-caret-down');
|
|
|
|
this.collapsedContent.show();
|
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
|
|
|
}
|
|
|
|
} else if (this.content) {
|
|
|
|
this.collapsedContent.hide();
|
|
|
|
this.content.show();
|
|
|
|
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
|
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
|
|
|
|
return this.getContentHTML(cb);
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
2017-06-30 21:27:30 +00:00
|
|
|
}
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-06-30 21:27:30 +00:00
|
|
|
getContentHTML(cb) {
|
2017-07-05 17:20:41 +00:00
|
|
|
this.collapsedContent.hide();
|
|
|
|
this.loadingContent.show();
|
2018-01-30 12:19:50 +00:00
|
|
|
|
|
|
|
axios.get(this.diffForPath)
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.loadingContent.hide();
|
2017-07-05 17:20:41 +00:00
|
|
|
if (data.html) {
|
2018-01-30 12:19:50 +00:00
|
|
|
this.content = $(data.html);
|
|
|
|
syntaxHighlight(this.content);
|
2017-07-05 17:20:41 +00:00
|
|
|
} else {
|
2018-01-30 12:19:50 +00:00
|
|
|
this.hasError = true;
|
|
|
|
this.content = $(ERROR_HTML);
|
2016-08-01 09:21:57 +00:00
|
|
|
}
|
2018-01-30 12:19:50 +00:00
|
|
|
this.collapsedContent.after(this.content);
|
2017-07-05 17:20:41 +00:00
|
|
|
|
2016-11-08 16:54:18 +00:00
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
2016-08-01 09:21:57 +00:00
|
|
|
}
|
2016-09-09 15:47:43 +00:00
|
|
|
|
2018-01-30 12:19:50 +00:00
|
|
|
const $file = $(this.file);
|
2017-10-07 04:25:17 +00:00
|
|
|
FilesCommentButton.init($file);
|
|
|
|
|
|
|
|
const canCreateNote = $file.closest('.files').is('[data-can-create-note]');
|
|
|
|
imageDiffHelper.initImageDiff($file[0], canCreateNote);
|
2017-06-12 18:43:21 +00:00
|
|
|
|
2017-07-05 17:20:41 +00:00
|
|
|
if (cb) cb();
|
2018-01-30 12:19:50 +00:00
|
|
|
})
|
2018-02-20 06:32:25 +00:00
|
|
|
.catch(() => {
|
|
|
|
createFlash(__('An error occurred while retrieving diff'));
|
|
|
|
});
|
2017-06-30 21:27:30 +00:00
|
|
|
}
|
|
|
|
}
|