summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issue.js.coffee
blob: f50df1f5ea3f1da8f8d07baa5bf43a702d8e612e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#= require flash
#= require jquery.waitforimages
#= require task_list

class @Issue
  constructor: ->
    # Prevent duplicate event bindings
    @disableTaskList()
    @fixAffixScroll()
    @initParticipants()
    if $('a.btn-close').length
      @initTaskList()
      @initIssueBtnEventListeners()

  fixAffixScroll: ->
    fixAffix = ->
      $discussion = $('.issuable-discussion')
      $sidebar = $('.issuable-sidebar')
      if $sidebar.hasClass('no-affix')
        $sidebar.removeClass(['affix-top','affix'])
      discussionHeight = $discussion.height()
      sidebarHeight = $sidebar.height()
      if sidebarHeight > discussionHeight
        $discussion.height(sidebarHeight + 50)
        $sidebar.addClass('no-affix')
    $(window).on('resize', fixAffix)
    fixAffix()

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

  initIssueBtnEventListeners: ->
    _this = @
    issueFailMessage = 'Unable to update this issue at this time.'
    $('a.btn-close, a.btn-reopen').on 'click', (e) ->
      e.preventDefault()
      e.stopImmediatePropagation()
      $this = $(this)
      isClose = $this.hasClass('btn-close')
      shouldSubmit = $this.hasClass('btn-comment')
      if shouldSubmit
        _this.submitNoteForm($this.closest('form'))
      $this.prop('disabled', true)
      url = $this.attr('href')
      $.ajax
        type: 'PUT'
        url: url,
        error: (jqXHR, textStatus, errorThrown) ->
          issueStatus = if isClose then 'close' else 'open'
          new Flash(issueFailMessage, 'alert')
        success: (data, textStatus, jqXHR) ->
          if data.saved
            $(document).trigger('issuable:change');
            if isClose
              $('a.btn-close').addClass('hidden')
              $('a.btn-reopen').removeClass('hidden')
              $('div.status-box-closed').removeClass('hidden')
              $('div.status-box-open').addClass('hidden')
            else
              $('a.btn-reopen').addClass('hidden')
              $('a.btn-close').removeClass('hidden')
              $('div.status-box-closed').addClass('hidden')
              $('div.status-box-open').removeClass('hidden')
          else
            new Flash(issueFailMessage, 'alert')
          $this.prop('disabled', false)

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

  disableTaskList: ->
    $('.detail-page-description .js-task-list-container').taskList('disable')
    $(document).off 'tasklist:changed', '.detail-page-description .js-task-list-container'

  # 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'
      url: $('form.js-issuable-update').attr('action')
      data: patchData

  initParticipants: ->
    _this = @
    $(document).on "click", ".js-participants-more", @toggleHiddenParticipants

    $(".js-participants-author").each (i) ->
      if i >= _this.PARTICIPANTS_ROW_COUNT
        $(@)
          .addClass "js-participants-hidden"
          .hide()

  toggleHiddenParticipants: (e) ->
    e.preventDefault()

    currentText = $(this).text().trim()
    lessText = $(this).data("less-text")
    originalText = $(this).data("original-text")

    if currentText is originalText
      $(this).text(lessText)
    else
      $(this).text(originalText)

    $(".js-participants-hidden").toggle()