/* eslint-disable func-names, space-before-function-paren, no-var, space-before-blocks, 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, padded-blocks, max-len */
/* global MergeRequestTabs */

/*= require jquery.waitforimages */
/*= require task_list */
/*= require merge_request_tabs */

(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.MergeRequest = (function() {
    function MergeRequest(opts) {
      // Initialize MergeRequest behavior
      //
      // Options:
      //   action - String, current controller action
      //
      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();
      // Prevent duplicate event bindings
      this.disableTaskList();
      this.initMRBtnListeners();
      this.initMessageWithDescriptionListener();
      this.initMessageWithoutDescriptionListener();
      if ($("a.btn-close").length) {
        this.initTaskList();
      }
    }

    // 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 gl.MergeRequestTabs(this.opts);
    };

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

    MergeRequest.prototype.initTaskList = function() {
      $('.detail-page-description .js-task-list-container').taskList('enable');
      return $(document).on('tasklist:changed', '.detail-page-description .js-task-list-container', this.updateTaskList);
    };

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

    MergeRequest.prototype.disableTaskList = function() {
      $('.detail-page-description .js-task-list-container').taskList('disable');
      return $(document).off('tasklist:changed', '.detail-page-description .js-task-list-container');
    };

    MergeRequest.prototype.updateTaskList = function() {
      var patchData;
      patchData = {};
      patchData['merge_request'] = {
        'description': $('.js-task-list-field', this).val()
      };
      return $.ajax({
        type: 'PATCH',
        url: $('form.js-issuable-update').attr('action'),
        data: patchData,
        success: function(mergeRequest) {
          document.querySelector('#task_status').innerText = mergeRequest.task_status;
          document.querySelector('#task_status_short').innerText = mergeRequest.task_status_short;
        }
      });
    // TODO (rspeicher): Make the merge request description inline-editable like a
    // note so that we can re-use its form here
    };

    MergeRequest.prototype.initMessageWithDescriptionListener = function() {
      return $('a.with-description-link').on('click', function(e) {
        e.preventDefault();

        var textarea = $('textarea.js-commit-message');

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

    MergeRequest.prototype.initMessageWithoutDescriptionListener = function() {
      return $('a.without-description-link').on('click', function(e) {
        e.preventDefault();

        var textarea = $('textarea.js-commit-message');

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

    return MergeRequest;

  })();

}).call(this);

@@ -1169,90 +1169,29 @@
 init
+Commit
 Message
-WithDescriptionListener();%0A      this.initMessageWithoutDescription
 Listener
 ();%0A
@@ -1186,16 +1186,17 @@
 Listener
+s
 ();%0A    
@@ -4007,38 +4007,29 @@
 init
+Commit
 Message
-WithDescription
 Listener
  = f
@@ -4016,32 +4016,33 @@
 tMessageListener
+s
  = function() %7B%0A
@@ -4039,38 +4039,86 @@
 ction() %7B%0A      
-return
+var textarea = $('textarea.js-commit-message');%0A%0A     
  $('a.with-descr
@@ -4190,65 +4190,8 @@
 );%0A%0A
-        var textarea = $('textarea.js-commit-message');%0A%0A
     
@@ -4356,108 +4356,14 @@
 %7D);%0A
+%0A 
     
-%7D;%0A%0A    MergeRequest.prototype.initMessageWithoutDescriptionListener = function() %7B%0A      return
  $('
@@ -4450,65 +4450,8 @@
 );%0A%0A
-        var textarea = $('textarea.js-commit-message');%0A%0A
     

Unify commit message listeners in one function