2019-09-24 05:06:04 -04:00
|
|
|
/* eslint-disable consistent-return */
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2021-02-01 10:08:56 -05:00
|
|
|
import { spriteIcon } from '~/lib/utils/common_utils';
|
2018-01-30 07:19:50 -05:00
|
|
|
import { __ } from './locale';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as createFlash } from './flash';
|
2017-06-12 14:43:21 -04:00
|
|
|
import FilesCommentButton from './files_comment_button';
|
2019-09-18 10:02:45 -04:00
|
|
|
import initImageDiffHelper from './image_diff/helpers/init_image_diff';
|
2017-12-13 04:26:44 -05:00
|
|
|
import syntaxHighlight from './syntax_highlight';
|
2017-06-12 14:43:21 -04:00
|
|
|
|
2017-06-30 17:27:30 -04:00
|
|
|
const WRAPPER = '<div class="diff-content"></div>';
|
2020-02-17 01:08:55 -05:00
|
|
|
const LOADING_HTML = '<span class="spinner"></span>';
|
2020-10-29 11:09:12 -04:00
|
|
|
const ERROR_HTML = `<div class="nothing-here-block">${spriteIcon(
|
|
|
|
'warning-solid',
|
|
|
|
's16',
|
|
|
|
)} Could not load diff</div>`;
|
2018-10-10 03:13:34 -04:00
|
|
|
const COLLAPSED_HTML =
|
|
|
|
'<div class="nothing-here-block diff-collapsed">This diff is collapsed. <button class="click-to-expand btn btn-link">Click to expand it.</button></div>';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-07-06 14:39:28 -04:00
|
|
|
export default class SingleFileDiff {
|
2017-06-30 17:27:30 -04:00
|
|
|
constructor(file) {
|
2017-07-05 13:20:41 -04:00
|
|
|
this.file = file;
|
|
|
|
this.toggleDiff = this.toggleDiff.bind(this);
|
|
|
|
this.content = $('.diff-content', this.file);
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronRightIcon = $('.diff-toggle-caret .chevron-right', this.file);
|
|
|
|
this.$chevronDownIcon = $('.diff-toggle-caret .chevron-down', this.file);
|
2018-02-20 17:20:48 -05:00
|
|
|
this.diffForPath = this.content.find('[data-diff-for-path]').data('diffForPath');
|
2017-07-05 13:20:41 -04:00
|
|
|
this.isOpen = !this.diffForPath;
|
|
|
|
if (this.diffForPath) {
|
|
|
|
this.collapsedContent = this.content;
|
2020-12-23 07:10:26 -05:00
|
|
|
this.loadingContent = $(WRAPPER).addClass('loading').html(LOADING_HTML).hide();
|
2017-07-05 13:20:41 -04:00
|
|
|
this.content = null;
|
|
|
|
this.collapsedContent.after(this.loadingContent);
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronRightIcon.removeClass('gl-display-none');
|
2017-07-05 13:20:41 -04:00
|
|
|
} else {
|
2020-12-23 07:10:26 -05:00
|
|
|
this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
|
2017-07-05 13:20:41 -04:00
|
|
|
this.content.after(this.collapsedContent);
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronDownIcon.removeClass('gl-display-none');
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
$('.js-file-title, .click-to-expand', this.file).on('click', (e) => {
|
2019-09-24 05:06:04 -04:00
|
|
|
this.toggleDiff($(e.target));
|
|
|
|
});
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2017-01-07 16:58:25 -05:00
|
|
|
|
2017-06-30 17:27:30 -04:00
|
|
|
toggleDiff($target, cb) {
|
2018-10-10 03:13:34 -04:00
|
|
|
if (
|
|
|
|
!$target.hasClass('js-file-title') &&
|
|
|
|
!$target.hasClass('click-to-expand') &&
|
2020-11-27 07:09:14 -05:00
|
|
|
!$target.closest('.diff-toggle-caret').length > 0
|
2018-10-10 03:13:34 -04:00
|
|
|
)
|
|
|
|
return;
|
2017-07-05 13:20:41 -04:00
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
if (!this.isOpen && !this.hasError) {
|
|
|
|
this.content.hide();
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronRightIcon.removeClass('gl-display-none');
|
|
|
|
this.$chevronDownIcon.addClass('gl-display-none');
|
2017-07-05 13:20:41 -04:00
|
|
|
this.collapsedContent.show();
|
|
|
|
} else if (this.content) {
|
|
|
|
this.collapsedContent.hide();
|
|
|
|
this.content.show();
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronDownIcon.removeClass('gl-display-none');
|
|
|
|
this.$chevronRightIcon.addClass('gl-display-none');
|
2017-07-05 13:20:41 -04:00
|
|
|
} else {
|
2020-11-27 07:09:14 -05:00
|
|
|
this.$chevronDownIcon.removeClass('gl-display-none');
|
|
|
|
this.$chevronRightIcon.addClass('gl-display-none');
|
2017-07-05 13:20:41 -04:00
|
|
|
return this.getContentHTML(cb);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-06-30 17:27:30 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-30 17:27:30 -04:00
|
|
|
getContentHTML(cb) {
|
2017-07-05 13:20:41 -04:00
|
|
|
this.collapsedContent.hide();
|
|
|
|
this.loadingContent.show();
|
2018-01-30 07:19:50 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.get(this.diffForPath)
|
2018-01-30 07:19:50 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
this.loadingContent.hide();
|
2017-07-05 13:20:41 -04:00
|
|
|
if (data.html) {
|
2018-01-30 07:19:50 -05:00
|
|
|
this.content = $(data.html);
|
|
|
|
syntaxHighlight(this.content);
|
2017-07-05 13:20:41 -04:00
|
|
|
} else {
|
2018-01-30 07:19:50 -05:00
|
|
|
this.hasError = true;
|
|
|
|
this.content = $(ERROR_HTML);
|
2016-08-01 05:21:57 -04:00
|
|
|
}
|
2018-01-30 07:19:50 -05:00
|
|
|
this.collapsedContent.after(this.content);
|
2017-07-05 13:20:41 -04:00
|
|
|
|
2018-01-30 07:19:50 -05:00
|
|
|
const $file = $(this.file);
|
2017-10-07 00:25:17 -04:00
|
|
|
FilesCommentButton.init($file);
|
|
|
|
|
|
|
|
const canCreateNote = $file.closest('.files').is('[data-can-create-note]');
|
2019-09-18 10:02:45 -04:00
|
|
|
initImageDiffHelper.initImageDiff($file[0], canCreateNote);
|
2017-06-12 14:43:21 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
if (cb) cb();
|
2018-01-30 07:19:50 -05:00
|
|
|
})
|
2018-02-20 01:32:25 -05:00
|
|
|
.catch(() => {
|
|
|
|
createFlash(__('An error occurred while retrieving diff'));
|
|
|
|
});
|
2017-06-30 17:27:30 -04:00
|
|
|
}
|
|
|
|
}
|