summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/merge_request.js
blob: 3b42a154af8504da4fa187fa61bb7b209729fed4 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable func-names, no-var, no-underscore-dangle, one-var, consistent-return, prefer-arrow-callback */

import $ from 'jquery';
import { __ } from '~/locale';
import createFlash from '~/flash';
import TaskList from './task_list';
import MergeRequestTabs from './merge_request_tabs';
import IssuablesHelper from './helpers/issuables_helper';
import { addDelimiter } from './lib/utils/text_utility';

function MergeRequest(opts) {
  // Initialize MergeRequest behavior
  //
  // Options:
  //   action - String, current controller action
  //
  this.opts = opts != null ? opts : {};
  this.submitNoteForm = this.submitNoteForm.bind(this);
  this.$el = $('.merge-request');
  this.$('.show-all-commits').on(
    'click',
    (function(_this) {
      return function() {
        return _this.showAllCommits();
      };
    })(this),
  );

  this.initTabs();
  this.initMRBtnListeners();
  this.initCommitMessageListeners();
  this.closeReopenReportToggle = IssuablesHelper.initCloseReopenReport();

  if ($('a.btn-close').length) {
    this.taskList = new TaskList({
      dataType: 'merge_request',
      fieldName: 'description',
      selector: '.detail-page-description',
      lockVersion: this.$el.data('lockVersion'),
      onSuccess: result => {
        document.querySelector('#task_status').innerText = result.task_status;
        document.querySelector('#task_status_short').innerText = result.task_status_short;
      },
      onError: () => {
        createFlash(
          __(
            'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
          ),
        );
      },
    });
  }
}

// Local jQuery finder
MergeRequest.prototype.$ = function(selector) {
  return this.$el.find(selector);
};

MergeRequest.prototype.initTabs = function() {
  if (window.mrTabs) {
    window.mrTabs.unbindEvents();
  }

  window.mrTabs = new MergeRequestTabs(this.opts);
};

MergeRequest.prototype.showAllCommits = function() {
  this.$('.first-commits').remove();
  return this.$('.all-commits').removeClass('hide');
};

MergeRequest.prototype.initMRBtnListeners = function() {
  var _this;
  _this = this;
  return $('a.btn-close, a.btn-reopen').on('click', function(e) {
    var $this, shouldSubmit;
    $this = $(this);
    shouldSubmit = $this.hasClass('btn-comment');
    if (shouldSubmit && $this.data('submitted')) {
      return;
    }

    if (this.closeReopenReportToggle) this.closeReopenReportToggle.setDisable();

    if (shouldSubmit) {
      if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
        e.preventDefault();
        e.stopImmediatePropagation();

        _this.submitNoteForm($this.closest('form'), $this);
      }
    }
  });
};

MergeRequest.prototype.submitNoteForm = function(form, $button) {
  var noteText;
  noteText = form.find('textarea.js-note-text').val();
  if (noteText.trim().length > 0) {
    form.submit();
    $button.data('submitted', true);
    return $button.trigger('click');
  }
};

MergeRequest.prototype.initCommitMessageListeners = function() {
  $(document).on('click', 'a.js-with-description-link', function(e) {
    var textarea = $('textarea.js-commit-message');
    e.preventDefault();

    textarea.val(textarea.data('messageWithDescription'));
    $('.js-with-description-hint').hide();
    $('.js-without-description-hint').show();
  });

  $(document).on('click', 'a.js-without-description-link', function(e) {
    var textarea = $('textarea.js-commit-message');
    e.preventDefault();

    textarea.val(textarea.data('messageWithoutDescription'));
    $('.js-with-description-hint').show();
    $('.js-without-description-hint').hide();
  });
};

MergeRequest.setStatusBoxToMerged = function() {
  $('.detail-page-header .status-box')
    .removeClass('status-box-open')
    .addClass('status-box-mr-merged')
    .find('span')
    .text(__('Merged'));
};

MergeRequest.decreaseCounter = function(by = 1) {
  const $el = $('.js-merge-counter');
  const count = Math.max(parseInt($el.text().replace(/[^\d]/, ''), 10) - by, 0);

  $el.text(addDelimiter(count));
};

MergeRequest.hideCloseButton = function() {
  const el = document.querySelector('.merge-request .js-issuable-actions');
  const closeDropdownItem = el.querySelector('li.close-item');
  if (closeDropdownItem) {
    closeDropdownItem.classList.add('hidden');
    // Selects the next dropdown item
    el.querySelector('li.report-item').click();
  } else {
    // No dropdown just hide the Close button
    el.querySelector('.btn-close').classList.add('hidden');
  }
  // Dropdown for mobile screen
  el.querySelector('li.js-close-item').classList.add('hidden');
};

export default MergeRequest;