summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/single_file_diff.js
diff options
context:
space:
mode:
authorFatih Acet <acetfatih@gmail.com>2016-07-24 23:45:11 +0300
committerFatih Acet <acetfatih@gmail.com>2016-07-24 23:45:11 +0300
commitaaa9509d120524573085e94af9de5cdde83e3271 (patch)
tree3824cffd4cdd132ee9cf75a00a7624f5ccc0dabd /app/assets/javascripts/single_file_diff.js
parent56b79181adc0bd6e9abef97ea075c14be971a01a (diff)
downloadgitlab-ce-aaa9509d120524573085e94af9de5cdde83e3271.tar.gz
ES6ify all the things!
Diffstat (limited to 'app/assets/javascripts/single_file_diff.js')
-rw-r--r--app/assets/javascripts/single_file_diff.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/app/assets/javascripts/single_file_diff.js b/app/assets/javascripts/single_file_diff.js
new file mode 100644
index 00000000000..b9ae497b0e5
--- /dev/null
+++ b/app/assets/javascripts/single_file_diff.js
@@ -0,0 +1,77 @@
+(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>';
+
+ COLLAPSED_HTML = '<div class="nothing-here-block diff-collapsed">This diff is collapsed. Click to expand it.</div>';
+
+ function SingleFileDiff(file) {
+ this.file = file;
+ this.toggleDiff = bind(this.toggleDiff, this);
+ this.content = $('.diff-content', this.file);
+ 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);
+ } else {
+ this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
+ this.content.after(this.collapsedContent);
+ }
+ this.collapsedContent.on('click', this.toggleDiff);
+ $('.file-title > a', this.file).on('click', this.toggleDiff);
+ }
+
+ SingleFileDiff.prototype.toggleDiff = function(e) {
+ this.isOpen = !this.isOpen;
+ if (!this.isOpen && !this.hasError) {
+ this.content.hide();
+ return this.collapsedContent.show();
+ } else if (this.content) {
+ this.collapsedContent.hide();
+ return this.content.show();
+ } else {
+ 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);
+ }
+ return _this.collapsedContent.after(_this.content);
+ };
+ })(this));
+ };
+
+ return SingleFileDiff;
+
+ })();
+
+ $.fn.singleFileDiff = function() {
+ return this.each(function() {
+ if (!$.data(this, 'singleFileDiff')) {
+ return $.data(this, 'singleFileDiff', new SingleFileDiff(this));
+ }
+ });
+ };
+
+}).call(this);