debian-mirror-gitlab/app/assets/javascripts/single_file_diff.js

105 lines
3.6 KiB
JavaScript
Raw Normal View History

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