BigW Consortium Gitlab

merge_request.js 3.74 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 committed
3

4
require('vendor/jquery.waitforimages');
5
require('./task_list');
6
require('./merge_request_tabs');
Fatih Acet committed
7 8

(function() {
9
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
Fatih Acet committed
10 11 12

  this.MergeRequest = (function() {
    function MergeRequest(opts) {
13 14 15 16 17
      // Initialize MergeRequest behavior
      //
      // Options:
      //   action - String, current controller action
      //
Fatih Acet committed
18 19 20 21 22 23 24 25 26 27
      this.opts = opts != null ? opts : {};
      this.submitNoteForm = bind(this.submitNoteForm, this);
      this.$el = $('.merge-request');
      this.$('.show-all-commits').on('click', (function(_this) {
        return function() {
          return _this.showAllCommits();
        };
      })(this));
      this.initTabs();
      this.initMRBtnListeners();
28
      this.initCommitMessageListeners();
Fatih Acet committed
29
      if ($("a.btn-close").length) {
30 31
        this.taskList = new gl.TaskList({
          dataType: 'merge_request',
32
          fieldName: 'description',
33 34 35 36 37
          selector: '.detail-page-description',
          onSuccess: (result) => {
            document.querySelector('#task_status').innerText = result.task_status;
            document.querySelector('#task_status_short').innerText = result.task_status_short;
          }
38
        });
Fatih Acet committed
39 40 41
      }
    }

42
    // Local jQuery finder
Fatih Acet committed
43 44 45 46 47
    MergeRequest.prototype.$ = function(selector) {
      return this.$el.find(selector);
    };

    MergeRequest.prototype.initTabs = function() {
48 49
      if (window.mrTabs) {
        window.mrTabs.unbindEvents();
Fatih Acet committed
50
      }
51
      window.mrTabs = new gl.MergeRequestTabs(this.opts);
Fatih Acet committed
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
    };

    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 (shouldSubmit) {
          if ($this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return _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');
      }
    };

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

        textarea.val(textarea.data('messageWithDescription'));
Nur Rony committed
95 96
        $('.js-with-description-hint').hide();
        $('.js-without-description-hint').show();
97 98
      });

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

        textarea.val(textarea.data('messageWithoutDescription'));
Nur Rony committed
104 105
        $('.js-with-description-hint').show();
        $('.js-without-description-hint').hide();
106 107 108
      });
    };

Fatih Acet committed
109 110
    return MergeRequest;
  })();
111
}).call(window);