2016-12-14 00:26:26 -05:00
|
|
|
/* eslint-disable func-names, space-before-function-paren, no-var, space-before-blocks, prefer-rest-params, wrap-iife, one-var, one-var-declaration-per-line, consistent-return, no-param-reassign, padded-blocks, max-len */
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
(function() {
|
|
|
|
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
|
|
|
|
2016-12-14 00:26:26 -05:00
|
|
|
window.SingleFileDiff = (function() {
|
2016-07-24 16:45:11 -04:00
|
|
|
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-09-02 10:26:45 -04:00
|
|
|
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 16:45:11 -04:00
|
|
|
|
2016-09-09 11:47:43 -04:00
|
|
|
function SingleFileDiff(file, forceLoad, cb) {
|
|
|
|
var clickTarget;
|
2016-07-24 16:45:11 -04:00
|
|
|
this.file = file;
|
|
|
|
this.toggleDiff = bind(this.toggleDiff, this);
|
|
|
|
this.content = $('.diff-content', this.file);
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon = $('.diff-toggle-caret', this.file);
|
2016-07-24 16:45:11 -04: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);
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon.addClass('fa-caret-right');
|
2016-07-24 16:45:11 -04:00
|
|
|
} else {
|
|
|
|
this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
|
|
|
|
this.content.after(this.collapsedContent);
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon.addClass('fa-caret-down');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2016-09-09 11:47:43 -04:00
|
|
|
clickTarget = $('.file-title, .click-to-expand', this.file).on('click', this.toggleDiff);
|
|
|
|
if (forceLoad) {
|
|
|
|
this.toggleDiff({ target: clickTarget }, cb);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2016-09-09 11:47:43 -04:00
|
|
|
SingleFileDiff.prototype.toggleDiff = function(e, cb) {
|
2016-09-02 10:26:45 -04:00
|
|
|
var $target = $(e.target);
|
|
|
|
if (!$target.hasClass('file-title') && !$target.hasClass('click-to-expand') && !$target.hasClass('diff-toggle-caret')) return;
|
2016-07-24 16:45:11 -04:00
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
if (!this.isOpen && !this.hasError) {
|
|
|
|
this.content.hide();
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon.addClass('fa-caret-right').removeClass('fa-caret-down');
|
2016-08-01 05:06:31 -04:00
|
|
|
this.collapsedContent.show();
|
2016-11-08 11:54:18 -05:00
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
2016-08-01 05:21:57 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
} else if (this.content) {
|
|
|
|
this.collapsedContent.hide();
|
2016-08-01 05:06:31 -04:00
|
|
|
this.content.show();
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
|
2016-11-08 11:54:18 -05:00
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
2016-08-01 05:21:57 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
} else {
|
2016-09-02 10:26:45 -04:00
|
|
|
this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
|
2016-09-09 11:47:43 -04:00
|
|
|
return this.getContentHTML(cb);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-09 11:47:43 -04:00
|
|
|
SingleFileDiff.prototype.getContentHTML = function(cb) {
|
2016-07-24 16:45:11 -04: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);
|
|
|
|
}
|
2016-08-04 06:27:11 -04:00
|
|
|
_this.collapsedContent.after(_this.content);
|
|
|
|
|
2016-11-08 11:54:18 -05:00
|
|
|
if (typeof gl.diffNotesCompileComponents !== 'undefined') {
|
|
|
|
gl.diffNotesCompileComponents();
|
2016-08-04 06:27:11 -04:00
|
|
|
}
|
2016-09-09 11:47:43 -04:00
|
|
|
|
|
|
|
if (cb) cb();
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
return SingleFileDiff;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
2016-09-09 11:47:43 -04:00
|
|
|
$.fn.singleFileDiff = function(forceLoad, cb) {
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.each(function() {
|
2016-09-09 11:47:43 -04:00
|
|
|
if (!$.data(this, 'singleFileDiff') || forceLoad) {
|
2016-12-14 00:26:26 -05:00
|
|
|
return $.data(this, 'singleFileDiff', new window.SingleFileDiff(this, forceLoad, cb));
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|