summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/compare.js
blob: d4243baadb5bc8f44035d56e97bcdd51d8663667 (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
88
89
90
91
92
93
/* eslint-disable func-names, space-before-function-paren, wrap-iife, quotes, no-var, object-shorthand, consistent-return, no-unused-vars, comma-dangle, vars-on-top, prefer-template, padded-blocks, max-len */
(function() {
  this.Compare = (function() {
    function Compare(opts) {
      this.opts = opts;
      this.source_loading = $(".js-source-loading");
      this.target_loading = $(".js-target-loading");
      $('.js-compare-dropdown').each((function(_this) {
        return function(i, dropdown) {
          var $dropdown;
          $dropdown = $(dropdown);
          return $dropdown.glDropdown({
            selectable: true,
            fieldName: $dropdown.data('field-name'),
            filterable: true,
            id: function(obj, $el) {
              return $el.data('id');
            },
            toggleLabel: function(obj, $el) {
              return $el.text().trim();
            },
            clicked: function(e, el) {
              if ($dropdown.is('.js-target-branch')) {
                return _this.getTargetHtml();
              } else if ($dropdown.is('.js-source-branch')) {
                return _this.getSourceHtml();
              } else if ($dropdown.is('.js-target-project')) {
                return _this.getTargetProject();
              }
            }
          });
        };
      })(this));
      this.initialState();
    }

    Compare.prototype.initialState = function() {
      this.getSourceHtml();
      return this.getTargetHtml();
    };

    Compare.prototype.getTargetProject = function() {
      return $.ajax({
        url: this.opts.targetProjectUrl,
        data: {
          target_project_id: $("input[name='merge_request[target_project_id]']").val()
        },
        beforeSend: function() {
          return $('.mr_target_commit').empty();
        },
        success: function(html) {
          return $('.js-target-branch-dropdown .dropdown-content').html(html);
        }
      });
    };

    Compare.prototype.getSourceHtml = function() {
      return this.sendAjax(this.opts.sourceBranchUrl, this.source_loading, '.mr_source_commit', {
        ref: $("input[name='merge_request[source_branch]']").val()
      });
    };

    Compare.prototype.getTargetHtml = function() {
      return this.sendAjax(this.opts.targetBranchUrl, this.target_loading, '.mr_target_commit', {
        target_project_id: $("input[name='merge_request[target_project_id]']").val(),
        ref: $("input[name='merge_request[target_branch]']").val()
      });
    };

    Compare.prototype.sendAjax = function(url, loading, target, data) {
      var $target;
      $target = $(target);
      return $.ajax({
        url: url,
        data: data,
        beforeSend: function() {
          loading.show();
          return $target.empty();
        },
        success: function(html) {
          loading.hide();
          $target.html(html);
          var className = '.' + $target[0].className.replace(' ', '.');
          gl.utils.localTimeAgo($('.js-timeago', className));
        }
      });
    };

    return Compare;

  })();

}).call(this);