BigW Consortium Gitlab

milestone.js.coffee 4.65 KB
Newer Older
1
class @Milestone
2 3 4 5 6 7 8
  @updateIssue: (li, issue_url, data) ->
    $.ajax
      type: "PUT"
      url: issue_url
      data: data
      success: (data) ->
        if data.saved == true
9 10 11 12 13 14 15
          if data.assignee_avatar_url
            img_tag = $('<img/>')
            img_tag.attr('src', data.assignee_avatar_url)
            img_tag.addClass('avatar s16')
            $(li).find('.assignee-icon').html(img_tag)
          else
            $(li).find('.assignee-icon').html('')
16 17 18 19 20
          $(li).effect 'highlight'
        else
          new Flash("Issue update failed", 'alert')
      dataType: "json"

21 22 23 24 25 26 27 28 29 30 31 32
  @sortIssues: (data) ->
    sort_issues_url = location.href + "/sort_issues"

    $.ajax
      type: "PUT"
      url: sort_issues_url
      data: data
      success: (data) ->
        if data.saved != true
          new Flash("Issues update failed", 'alert')
      dataType: "json"

33 34 35 36 37 38 39 40 41 42 43 44
  @sortMergeRequests: (data) ->
    sort_mr_url = location.href + "/sort_merge_requests"

    $.ajax
      type: "PUT"
      url: sort_mr_url
      data: data
      success: (data) ->
        if data.saved != true
          new Flash("MR update failed", 'alert')
      dataType: "json"

45 46 47 48 49 50 51
  @updateMergeRequest: (li, merge_request_url, data) ->
    $.ajax
      type: "PUT"
      url: merge_request_url
      data: data
      success: (data) ->
        if data.saved == true
52 53 54 55 56 57 58
          if data.assignee_avatar_url
            img_tag = $('<img/>')
            img_tag.attr('src', data.assignee_avatar_url)
            img_tag.addClass('avatar s16')
            $(li).find('.assignee-icon').html(img_tag)
          else
            $(li).find('.assignee-icon').html('')
59 60 61 62 63
          $(li).effect 'highlight'
        else
          new Flash("Issue update failed", 'alert')
      dataType: "json"

64
  constructor: ->
65 66 67 68 69
    oldMouseStart = $.ui.sortable.prototype._mouseStart
    $.ui.sortable.prototype._mouseStart = (event, overrideHandle, noActivation) ->
      this._trigger "beforeStart", event, this._uiHash()
      oldMouseStart.apply this, [event, overrideHandle, noActivation]

70 71
    @bindIssuesSorting()
    @bindMergeRequestSorting()
72
    @bindTabsSwitching()
73

74
  bindIssuesSorting: ->
75
    $("#issues-list-unassigned, #issues-list-ongoing, #issues-list-closed").sortable(
76 77
      connectWith: ".issues-sortable-list",
      dropOnEmpty: true,
78
      items: "li:not(.ui-sort-disabled)",
79 80 81 82
      beforeStart: (event, ui) ->
        $(".issues-sortable-list").css "min-height", ui.item.outerHeight()
      stop: (event, ui) ->
        $(".issues-sortable-list").css "min-height", "0px"
83 84 85 86
      update: (event, ui) ->
        data = $(this).sortable("serialize")
        Milestone.sortIssues(data)

87 88 89 90 91 92 93 94 95 96 97 98
      receive: (event, ui) ->
        new_state = $(this).data('state')
        issue_id = ui.item.data('iid')
        issue_url = ui.item.data('url')

        data = switch new_state
          when 'ongoing'
            "issue[assignee_id]=" + gon.current_user_id
          when 'unassigned'
            "issue[assignee_id]="
          when 'closed'
            "issue[state_event]=close"
99 100 101

        if $(ui.sender).data('state') == "closed"
          data += "&issue[state_event]=reopen"
102 103 104 105 106

        Milestone.updateIssue(ui.item, issue_url, data)

    ).disableSelection()

107
  bindTabsSwitching: ->
108 109 110 111 112 113 114
    $('a[data-toggle="tab"]').on 'show.bs.tab', (e) ->
      currentTabClass  = $(e.target).data('show')
      previousTabClass =  $(e.relatedTarget).data('show')

      $(previousTabClass).hide()
      $(currentTabClass).removeClass('hidden')
      $(currentTabClass).show()
115 116

  bindMergeRequestSorting: ->
117 118 119
    $("#merge_requests-list-unassigned, #merge_requests-list-ongoing, #merge_requests-list-closed").sortable(
      connectWith: ".merge_requests-sortable-list",
      dropOnEmpty: true,
120
      items: "li:not(.ui-sort-disabled)",
121 122 123 124
      beforeStart: (event, ui) ->
        $(".merge_requests-sortable-list").css "min-height", ui.item.outerHeight()
      stop: (event, ui) ->
        $(".merge_requests-sortable-list").css "min-height", "0px"
125 126 127 128
      update: (event, ui) ->
        data = $(this).sortable("serialize")
        Milestone.sortMergeRequests(data)

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      receive: (event, ui) ->
        new_state = $(this).data('state')
        merge_request_id = ui.item.data('iid')
        merge_request_url = ui.item.data('url')

        data = switch new_state
          when 'ongoing'
            "merge_request[assignee_id]=" + gon.current_user_id
          when 'unassigned'
            "merge_request[assignee_id]="
          when 'closed'
            "merge_request[state_event]=close"

        if $(ui.sender).data('state') == "closed"
          data += "&merge_request[state_event]=reopen"

        Milestone.updateMergeRequest(ui.item, merge_request_url, data)

    ).disableSelection()