gitlab-org--gitlab-foss/app/assets/javascripts/single_file_diff.js

101 lines
3.7 KiB
JavaScript
Raw Normal View History

/* 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 */
import FilesCommentButton from './files_comment_button';
2016-07-24 20:45:11 +00:00
(function() {
window.SingleFileDiff = (function() {
2016-07-24 20:45:11 +00:00
var COLLAPSED_HTML, ERROR_HTML, LOADING_HTML, WRAPPER;
2017-05-15 17:18:19 +00:00
WRAPPER = '<div class="diff-content"></div>';
2016-07-24 20:45:11 +00:00
LOADING_HTML = '<i class="fa fa-spinner fa-spin"></i>';
ERROR_HTML = '<div class="nothing-here-block"><i class="fa fa-warning"></i> Could not load diff</div>';
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
function SingleFileDiff(file) {
2016-07-24 20:45:11 +00:00
this.file = file;
this.toggleDiff = this.toggleDiff.bind(this);
2016-07-24 20:45:11 +00:00
this.content = $('.diff-content', this.file);
this.$toggleIcon = $('.diff-toggle-caret', this.file);
2016-07-24 20:45:11 +00:00
this.diffForPath = this.content.find('[data-diff-for-path]').data('diff-for-path');
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');
2016-07-24 20:45:11 +00:00
} else {
this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
this.content.after(this.collapsedContent);
this.$toggleIcon.addClass('fa-caret-down');
2016-07-24 20:45:11 +00:00
}
2017-02-05 22:28:48 +00:00
$('.js-file-title, .click-to-expand', this.file).on('click', (function (e) {
this.toggleDiff($(e.target));
}).bind(this));
2016-07-24 20:45:11 +00:00
}
SingleFileDiff.prototype.toggleDiff = function($target, cb) {
2017-02-05 22:28:48 +00:00
if (!$target.hasClass('js-file-title') && !$target.hasClass('click-to-expand') && !$target.hasClass('diff-toggle-caret')) return;
2016-07-24 20:45:11 +00:00
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();
}
2016-07-24 20:45:11 +00:00
} 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();
}
2016-07-24 20:45:11 +00:00
} else {
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
return this.getContentHTML(cb);
2016-07-24 20:45:11 +00:00
}
};
SingleFileDiff.prototype.getContentHTML = function(cb) {
2016-07-24 20:45:11 +00:00
this.collapsedContent.hide();
this.loadingContent.show();
$.get(this.diffForPath, (function(_this) {
return function(data) {
_this.loadingContent.hide();
if (data.html) {
_this.content = $(data.html);
_this.content.syntaxHighlight();
} else {
_this.hasError = true;
_this.content = $(ERROR_HTML);
}
_this.collapsedContent.after(_this.content);
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
gl.diffNotesCompileComponents();
}
FilesCommentButton.init($(_this.file));
if (cb) cb();
2016-07-24 20:45:11 +00:00
};
})(this));
};
return SingleFileDiff;
})();
$.fn.singleFileDiff = function() {
2016-07-24 20:45:11 +00:00
return this.each(function() {
if (!$.data(this, 'singleFileDiff')) {
return $.data(this, 'singleFileDiff', new window.SingleFileDiff(this));
2016-07-24 20:45:11 +00:00
}
});
};
}).call(window);