single_file_diff.js 3.61 KB
Newer Older
1
/* 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 */
2

Fatih Acet's avatar
Fatih Acet committed
3
(function() {
4
  window.SingleFileDiff = (function() {
Fatih Acet's avatar
Fatih Acet committed
5 6
    var COLLAPSED_HTML, ERROR_HTML, LOADING_HTML, WRAPPER;

Douwe Maan's avatar
Douwe Maan committed
7
    WRAPPER = '<div class="diff-content"></div>';
Fatih Acet's avatar
Fatih Acet committed
8 9 10 11 12

    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>';

13
    COLLAPSED_HTML = '<div class="nothing-here-block diff-collapsed">This diff is collapsed. <a class="click-to-expand">Click to expand it.</a></div>';
Fatih Acet's avatar
Fatih Acet committed
14

15
    function SingleFileDiff(file) {
Fatih Acet's avatar
Fatih Acet committed
16
      this.file = file;
17
      this.toggleDiff = this.toggleDiff.bind(this);
Fatih Acet's avatar
Fatih Acet committed
18
      this.content = $('.diff-content', this.file);
19
      this.$toggleIcon = $('.diff-toggle-caret', this.file);
Fatih Acet's avatar
Fatih Acet committed
20 21 22 23 24 25 26
      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);
27
        this.$toggleIcon.addClass('fa-caret-right');
Fatih Acet's avatar
Fatih Acet committed
28 29 30
      } else {
        this.collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide();
        this.content.after(this.collapsedContent);
31
        this.$toggleIcon.addClass('fa-caret-down');
Fatih Acet's avatar
Fatih Acet committed
32
      }
33

34
      $('.js-file-title, .click-to-expand', this.file).on('click', (function (e) {
35 36
        this.toggleDiff($(e.target));
      }).bind(this));
Fatih Acet's avatar
Fatih Acet committed
37 38
    }

39
    SingleFileDiff.prototype.toggleDiff = function($target, cb) {
40
      if (!$target.hasClass('js-file-title') && !$target.hasClass('click-to-expand') && !$target.hasClass('diff-toggle-caret')) return;
Fatih Acet's avatar
Fatih Acet committed
41 42 43
      this.isOpen = !this.isOpen;
      if (!this.isOpen && !this.hasError) {
        this.content.hide();
44
        this.$toggleIcon.addClass('fa-caret-right').removeClass('fa-caret-down');
45
        this.collapsedContent.show();
46 47
        if (typeof gl.diffNotesCompileComponents !== 'undefined') {
          gl.diffNotesCompileComponents();
48
        }
Fatih Acet's avatar
Fatih Acet committed
49 50
      } else if (this.content) {
        this.collapsedContent.hide();
51
        this.content.show();
52
        this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
53 54
        if (typeof gl.diffNotesCompileComponents !== 'undefined') {
          gl.diffNotesCompileComponents();
55
        }
Fatih Acet's avatar
Fatih Acet committed
56
      } else {
57
        this.$toggleIcon.addClass('fa-caret-down').removeClass('fa-caret-right');
58
        return this.getContentHTML(cb);
Fatih Acet's avatar
Fatih Acet committed
59 60 61
      }
    };

62
    SingleFileDiff.prototype.getContentHTML = function(cb) {
Fatih Acet's avatar
Fatih Acet committed
63 64 65 66 67 68 69 70 71 72 73 74
      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);
          }
75 76
          _this.collapsedContent.after(_this.content);

77 78
          if (typeof gl.diffNotesCompileComponents !== 'undefined') {
            gl.diffNotesCompileComponents();
79
          }
80 81

          if (cb) cb();
Fatih Acet's avatar
Fatih Acet committed
82 83 84 85 86 87 88
        };
      })(this));
    };

    return SingleFileDiff;
  })();

89
  $.fn.singleFileDiff = function() {
Fatih Acet's avatar
Fatih Acet committed
90
    return this.each(function() {
91 92
      if (!$.data(this, 'singleFileDiff')) {
        return $.data(this, 'singleFileDiff', new window.SingleFileDiff(this));
Fatih Acet's avatar
Fatih Acet committed
93 94 95
      }
    });
  };
96
}).call(window);