merge_request.js 4.38 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, quotes, no-underscore-dangle, one-var, one-var-declaration-per-line, consistent-return, dot-notation, quote-props, comma-dangle, object-shorthand, max-len, prefer-arrow-callback */
2
/* global MergeRequestTabs */
Fatih Acet's avatar
Fatih Acet committed
3

4
import 'vendor/jquery.waitforimages';
5
import TaskList from './task_list';
6
import './merge_request_tabs';
7
import IssuablesHelper from './helpers/issuables_helper';
Fatih Acet's avatar
Fatih Acet committed
8 9 10 11

(function() {
  this.MergeRequest = (function() {
    function MergeRequest(opts) {
12 13 14 15 16
      // Initialize MergeRequest behavior
      //
      // Options:
      //   action - String, current controller action
      //
Fatih Acet's avatar
Fatih Acet committed
17
      this.opts = opts != null ? opts : {};
18
      this.submitNoteForm = this.submitNoteForm.bind(this);
Fatih Acet's avatar
Fatih Acet committed
19 20 21 22 23 24
      this.$el = $('.merge-request');
      this.$('.show-all-commits').on('click', (function(_this) {
        return function() {
          return _this.showAllCommits();
        };
      })(this));
25

Fatih Acet's avatar
Fatih Acet committed
26 27
      this.initTabs();
      this.initMRBtnListeners();
28
      this.initCommitMessageListeners();
29
      this.closeReopenReportToggle = IssuablesHelper.initCloseReopenReport();
30

Fatih Acet's avatar
Fatih Acet committed
31
      if ($("a.btn-close").length) {
32
        this.taskList = new TaskList({
33
          dataType: 'merge_request',
34
          fieldName: 'description',
35 36 37 38 39
          selector: '.detail-page-description',
          onSuccess: (result) => {
            document.querySelector('#task_status').innerText = result.task_status;
            document.querySelector('#task_status_short').innerText = result.task_status_short;
          }
40
        });
Fatih Acet's avatar
Fatih Acet committed
41 42 43
      }
    }

44
    // Local jQuery finder
Fatih Acet's avatar
Fatih Acet committed
45 46 47 48 49
    MergeRequest.prototype.$ = function(selector) {
      return this.$el.find(selector);
    };

    MergeRequest.prototype.initTabs = function() {
50 51
      if (window.mrTabs) {
        window.mrTabs.unbindEvents();
Fatih Acet's avatar
Fatih Acet committed
52
      }
53
      window.mrTabs = new gl.MergeRequestTabs(this.opts);
Fatih Acet's avatar
Fatih Acet committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    };

    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;
        }
71

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

Fatih Acet's avatar
Fatih Acet committed
74 75 76 77
        if (shouldSubmit) {
          if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
            e.preventDefault();
            e.stopImmediatePropagation();
78 79

            _this.submitNoteForm($this.closest('form'), $this);
Fatih Acet's avatar
Fatih Acet committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
          }
        }
      });
    };

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

95
    MergeRequest.prototype.initCommitMessageListeners = function() {
Mike Greiling's avatar
Mike Greiling committed
96 97
      $(document).on('click', 'a.js-with-description-link', function(e) {
        var textarea = $('textarea.js-commit-message');
98
        e.preventDefault();
99 100

        textarea.val(textarea.data('messageWithDescription'));
Nur Rony's avatar
Nur Rony committed
101 102
        $('.js-with-description-hint').hide();
        $('.js-without-description-hint').show();
103 104
      });

Mike Greiling's avatar
Mike Greiling committed
105 106
      $(document).on('click', 'a.js-without-description-link', function(e) {
        var textarea = $('textarea.js-commit-message');
107 108 109
        e.preventDefault();

        textarea.val(textarea.data('messageWithoutDescription'));
Nur Rony's avatar
Nur Rony committed
110 111
        $('.js-with-description-hint').show();
        $('.js-without-description-hint').hide();
112 113 114
      });
    };

Fatih Acet's avatar
Fatih Acet committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    MergeRequest.prototype.updateStatusText = function(classToRemove, classToAdd, newStatusText) {
      $('.detail-page-header .status-box')
        .removeClass(classToRemove)
        .addClass(classToAdd)
        .find('span')
        .text(newStatusText);
    };

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

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

Fatih Acet's avatar
Fatih Acet committed
130 131
    return MergeRequest;
  })();
132
}).call(window);