summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/compare.js
blob: 303a5bf4a53f726bdb4edf0609d252a88fd0cfce (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
/* 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, max-len */

import $ from 'jquery';
import { localTimeAgo } from './lib/utils/datetime_utility';
import axios from './lib/utils/axios_utils';

export default class Compare {
  constructor(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('fieldName'),
          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();
  }

  initialState() {
    this.getSourceHtml();
    this.getTargetHtml();
  }

  getTargetProject() {
    $('.mr_target_commit').empty();

    return axios.get(this.opts.targetProjectUrl, {
      params: {
        target_project_id: $("input[name='merge_request[target_project_id]']").val(),
      },
    }).then(({ data }) => {
      $('.js-target-branch-dropdown .dropdown-content').html(data);
    });
  }

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

  getTargetHtml() {
    return this.constructor.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()
    });
  }

  static sendAjax(url, loading, target, params) {
    const $target = $(target);

    loading.show();
    $target.empty();

    return axios.get(url, {
      params,
    }).then(({ data }) => {
      loading.hide();
      $target.html(data);
      const className = '.' + $target[0].className.replace(' ', '.');
      localTimeAgo($('.js-timeago', className));
    });
  }
}