BigW Consortium Gitlab

merge_request.js.coffee 2.35 KB
Newer Older
1
#= require jquery.waitforimages
2 3
#= require task_list

4 5
#= require merge_request_tabs

6
class @MergeRequest
7 8 9
  # Initialize MergeRequest behavior
  #
  # Options:
10
  #   action - String, current controller action
11
  #
12 13 14 15 16 17
  constructor: (@opts) ->
    this.$el = $('.merge-request')

    this.$('.show-all-commits').on 'click', =>
      this.showAllCommits()

18
    @initTabs()
19

20 21
    # Prevent duplicate event bindings
    @disableTaskList()
22
    @initMRBtnListeners()
23

24
    if $("a.btn-close").length
25
      @initTaskList()
26 27 28 29 30

  # Local jQuery finder
  $: (selector) ->
    this.$el.find(selector)

31 32 33 34 35 36 37 38
  initTabs: ->
    if @opts.action != 'new'
      # `MergeRequests#new` has no tab-persisting or lazy-loading behavior
      new MergeRequestTabs(@opts)
    else
      # Show the first tab (Commits)
      $('.merge-request-tabs a[data-toggle="tab"]:first').tab('show')

39 40 41 42
  showAllCommits: ->
    this.$('.first-commits').remove()
    this.$('.all-commits').removeClass 'hide'

43
  initTaskList: ->
44 45
    $('.detail-page-description .js-task-list-container').taskList('enable')
    $(document).on 'tasklist:changed', '.detail-page-description .js-task-list-container', @updateTaskList
46

47 48 49 50 51
  initMRBtnListeners: ->
    _this = @
    $('a.btn-close, a.btn-reopen').on 'click', (e) ->
      $this = $(this)
      shouldSubmit = $this.hasClass('btn-comment')
52 53
      if shouldSubmit && $this.data('submitted')
        return
54
      if shouldSubmit
55 56 57 58 59
        if $this.hasClass('btn-comment-and-close') || $this.hasClass('btn-comment-and-reopen')
          e.preventDefault()
          e.stopImmediatePropagation()
          _this.submitNoteForm($this.closest('form'),$this)

60 61 62 63 64 65 66 67 68

  submitNoteForm: (form, $button) =>
    noteText = form.find("textarea.js-note-text").val()
    if noteText.trim().length > 0
      form.submit()
      $button.data('submitted',true)
      $button.trigger('click')


69
  disableTaskList: ->
70 71
    $('.detail-page-description .js-task-list-container').taskList('disable')
    $(document).off 'tasklist:changed', '.detail-page-description .js-task-list-container'
72 73 74 75 76 77 78 79 80

  # TODO (rspeicher): Make the merge request description inline-editable like a
  # note so that we can re-use its form here
  updateTaskList: ->
    patchData = {}
    patchData['merge_request'] = {'description': $('.js-task-list-field', this).val()}

    $.ajax
      type: 'PATCH'
81
      url: $('form.js-issuable-update').attr('action')
82
      data: patchData