BigW Consortium Gitlab

milestone.js.coffee 4.51 KB
Newer Older
1
class @Milestone
2 3 4 5 6
  @updateIssue: (li, issue_url, data) ->
    $.ajax
      type: "PUT"
      url: issue_url
      data: data
7 8 9 10
      success: (_data) =>
        @successCallback(_data, li)
      error: (data) ->
        new Flash("Issue update failed", 'alert')
11 12
      dataType: "json"

13 14 15 16 17 18 19
  @sortIssues: (data) ->
    sort_issues_url = location.href + "/sort_issues"

    $.ajax
      type: "PUT"
      url: sort_issues_url
      data: data
20 21 22 23
      success: (_data) =>
        @successCallback(_data)
      error: ->
        new Flash("Issues update failed", 'alert')
24 25
      dataType: "json"

26 27 28 29 30 31 32
  @sortMergeRequests: (data) ->
    sort_mr_url = location.href + "/sort_merge_requests"

    $.ajax
      type: "PUT"
      url: sort_mr_url
      data: data
33 34 35 36
      success: (_data) =>
        @successCallback(_data)
      error: (data) ->
        new Flash("Issue update failed", 'alert')
37 38
      dataType: "json"

39 40 41 42 43
  @updateMergeRequest: (li, merge_request_url, data) ->
    $.ajax
      type: "PUT"
      url: merge_request_url
      data: data
44 45 46 47
      success: (_data) =>
        @successCallback(_data, li)
      error: (data) ->
        new Flash("Issue update failed", 'alert')
48 49
      dataType: "json"

50 51 52 53 54 55 56 57 58 59 60
  @successCallback: (data, element) =>
    if data.assignee
      img_tag = $('<img/>')
      img_tag.attr('src', data.assignee.avatar_url)
      img_tag.addClass('avatar s16')
      $(element).find('.assignee-icon').html(img_tag)
    else
      $(element).find('.assignee-icon').html('')

    $(element).effect 'highlight'

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

67 68
    @bindIssuesSorting()
    @bindMergeRequestSorting()
69
    @bindTabsSwitching()
70

71
  bindIssuesSorting: ->
72
    $("#issues-list-unassigned, #issues-list-ongoing, #issues-list-closed").sortable(
73 74
      connectWith: ".issues-sortable-list",
      dropOnEmpty: true,
75
      items: "li:not(.ui-sort-disabled)",
76 77 78 79
      beforeStart: (event, ui) ->
        $(".issues-sortable-list").css "min-height", ui.item.outerHeight()
      stop: (event, ui) ->
        $(".issues-sortable-list").css "min-height", "0px"
80
      update: (event, ui) ->
81 82
        # Prevents sorting from container which element has been removed.
        if $(this).find(ui.item).length > 0
83 84
          data = $(this).sortable("serialize")
          Milestone.sortIssues(data)
85

86 87 88 89 90 91 92 93 94 95 96 97
      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"
98 99 100

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

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

    ).disableSelection()

106
  bindTabsSwitching: ->
107 108 109 110 111 112 113
    $('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()
114 115

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

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      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()