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

95 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-09-13 17:45:13 +05:30
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.SingleFileDiff = (function() {
var COLLAPSED_HTML, ERROR_HTML, LOADING_HTML, WRAPPER;
WRAPPER = '<div class="diff-content diff-wrap-lines"></div>';
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>';
2016-11-03 12:29:30 +05:30
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-09-13 17:45:13 +05:30
function SingleFileDiff(file) {
this.file = file;
this.toggleDiff = bind(this.toggleDiff, this);
this.content = $('.diff-content', this.file);
2016-11-03 12:29:30 +05:30
this.$toggleIcon = $('.diff-toggle-caret', this.file);
2016-09-13 17:45:13 +05:30
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);
2016-11-03 12:29:30 +05:30
this.$toggleIcon.addClass('fa-caret-right');
2016-09-13 17:45:13 +05:30
} else {
this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
this.content.after(this.collapsedContent);
2016-11-03 12:29:30 +05:30
this.$toggleIcon.addClass('fa-caret-down');
2016-09-13 17:45:13 +05:30
}
2016-11-03 12:29:30 +05:30
$('.file-title, .click-to-expand', this.file).on('click', this.toggleDiff);
2016-09-13 17:45:13 +05:30
}
SingleFileDiff.prototype.toggleDiff = function(e) {
2016-11-03 12:29:30 +05:30
var $target = $(e.target);
if (!$target.hasClass('file-title') && !$target.hasClass('click-to-expand') && !$target.hasClass('diff-toggle-caret')) return;
2016-09-13 17:45:13 +05:30
this.isOpen = !this.isOpen;
if (!this.isOpen && !this.hasError) {
this.content.hide();
2016-11-03 12:29:30 +05:30
this.$toggleIcon.addClass('fa-caret-right').removeClass('fa-caret-down');
2016-09-13 17:45:13 +05:30
this.collapsedContent.show();
if (typeof DiffNotesApp !== 'undefined') {
DiffNotesApp.compileComponents();
}
} else if (this.content) {
this.collapsedContent.hide();
this.content.show();
2016-11-03 12:29:30 +05:30
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
2016-09-13 17:45:13 +05:30
if (typeof DiffNotesApp !== 'undefined') {
DiffNotesApp.compileComponents();
}
} else {
2016-11-03 12:29:30 +05:30
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
2016-09-13 17:45:13 +05:30
return this.getContentHTML();
}
};
SingleFileDiff.prototype.getContentHTML = function() {
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 DiffNotesApp !== 'undefined') {
DiffNotesApp.compileComponents();
}
};
})(this));
};
return SingleFileDiff;
})();
$.fn.singleFileDiff = function() {
return this.each(function() {
if (!$.data(this, 'singleFileDiff')) {
return $.data(this, 'singleFileDiff', new SingleFileDiff(this));
}
});
};
}).call(this);