BigW Consortium Gitlab

issue.js.coffee 3.83 KB
Newer Older
1
#= require flash
2
#= require jquery.waitforimages
3
#= require task_list
4

5
class @Issue
6
  constructor: ->
7 8
    # Prevent duplicate event bindings
    @disableTaskList()
9
    if $('a.btn-close').length
10
      @initTaskList()
11
      @initIssueBtnEventListeners()
12

13 14
    @initMergeRequests()
    @initRelatedBranches()
15
    @initCanCreateBranch()
16

17
  initTaskList: ->
18 19
    $('.detail-page-description .js-task-list-container').taskList('enable')
    $(document).on 'tasklist:changed', '.detail-page-description .js-task-list-container', @updateTaskList
20

21
  initIssueBtnEventListeners: ->
22
    _this = @
23
    issueFailMessage = 'Unable to update this issue at this time.'
24
    $('a.btn-close, a.btn-reopen').on 'click', (e) ->
25 26 27 28
      e.preventDefault()
      e.stopImmediatePropagation()
      $this = $(this)
      isClose = $this.hasClass('btn-close')
29 30 31
      shouldSubmit = $this.hasClass('btn-comment')
      if shouldSubmit
        _this.submitNoteForm($this.closest('form'))
32
      $this.prop('disabled', true)
33
      url = $this.attr('href')
34 35 36 37 38
      $.ajax
        type: 'PUT'
        url: url,
        error: (jqXHR, textStatus, errorThrown) ->
          issueStatus = if isClose then 'close' else 'open'
39
          new Flash(issueFailMessage, 'alert')
40
        success: (data, textStatus, jqXHR) ->
41
          if 'id' of data
42
            $(document).trigger('issuable:change');
43
            if isClose
44
              $('a.btn-close').addClass('hidden')
45
              $('a.btn-reopen').removeClass('hidden')
46 47
              $('div.status-box-closed').removeClass('hidden')
              $('div.status-box-open').addClass('hidden')
48
            else
49
              $('a.btn-reopen').addClass('hidden')
50
              $('a.btn-close').removeClass('hidden')
51 52
              $('div.status-box-closed').addClass('hidden')
              $('div.status-box-open').removeClass('hidden')
53
          else
54
            new Flash(issueFailMessage, 'alert')
55 56
          $this.prop('disabled', false)

57 58 59 60 61
  submitNoteForm: (form) =>
    noteText = form.find("textarea.js-note-text").val()
    if noteText.trim().length > 0
      form.submit()

62
  disableTaskList: ->
63 64
    $('.detail-page-description .js-task-list-container').taskList('disable')
    $(document).off 'tasklist:changed', '.detail-page-description .js-task-list-container'
65 66 67 68 69 70 71 72 73

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

    $.ajax
      type: 'PATCH'
74
      url: $('form.js-issuable-update').attr('action')
75
      data: patchData
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  initMergeRequests: ->
    $container = $('#merge-requests')

    $.getJSON($container.data('url'))
      .error ->
        new Flash('Failed to load referenced merge requests', 'alert')
      .success (data) ->
        if 'html' of data
          $container.html(data.html)

  initRelatedBranches: ->
    $container = $('#related-branches')

    $.getJSON($container.data('url'))
      .error ->
        new Flash('Failed to load related branches', 'alert')
      .success (data) ->
        if 'html' of data
          $container.html(data.html)
96 97 98 99 100 101

  initCanCreateBranch: ->
    $container = $('div#new-branch')

    # If the user doesn't have the required permissions the container isn't
    # rendered at all.
102
    return if $container.length is 0
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    $.getJSON($container.data('path'))
      .error ->
        $container.find('.checking').hide()
        $container.find('.unavailable').show()

        new Flash('Failed to check if a new branch can be created.', 'alert')
      .success (data) ->
        if data.can_create_branch
          $container.find('.checking').hide()
          $container.find('.available').show()
          $container.find('a').attr('disabled', false)
        else
          $container.find('.checking').hide()
          $container.find('.unavailable').show()