summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/single_file_diff.js
blob: 156b9b8abec32199ca3fbce26e5d3758670fb02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(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();
        this.collapsedContent.show();
        if (typeof DiffNotesApp !== 'undefined') {
          DiffNotesApp.compileComponents();
        }
      } else if (this.content) {
        this.collapsedContent.hide();
        this.content.show();
        if (typeof DiffNotesApp !== 'undefined') {
          DiffNotesApp.compileComponents();
        }
      } 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);
          }
          _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);