BigW Consortium Gitlab

merge_requests.js.coffee 2.74 KB
Newer Older
1 2
#
# * Filter merge requests
Andrey Kumanyaev committed
3
#
4 5 6 7 8 9 10
@merge_requestsPage = ->
  $('#assignee_id').chosen()
  $('#milestone_id').chosen()
  $('#milestone_id, #assignee_id').on 'change', ->
    $(this).closest('form').submit()

class MergeRequest
Andrey Kumanyaev committed
11

12 13 14 15
  constructor: (@opts) ->
    this.$el = $('.merge-request')
    @diffs_loaded = false
    @commits_loaded = false
Andrey Kumanyaev committed
16

17
    this.activateTab(@opts.action)
Andrey Kumanyaev committed
18

19
    this.bindEvents()
Andrey Kumanyaev committed
20

21 22 23
    this.initMergeWidget()
    this.$('.show-all-commits').on 'click', =>
      this.showAllCommits()
24 25 26 27 28 29 30
    
    modal = $('#modal_merge_info').modal modal: true, show:false

    $('.how_to_merge_link').bind "click", ->
      modal.show()
    $('.modal-header .close').bind "click", ->
      modal.hide()
31 32 33 34 35 36

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

  initMergeWidget: ->
Andrew8xx8 committed
37
    this.showState( @opts.current_status )
Andrey Kumanyaev committed
38

39 40
    if this.$('.automerge_widget').length and @opts.check_enable
      $.get @opts.url_to_automerge_check, (data) =>
41
        this.showState( data.merge_status )
42 43 44
      , 'json'

    if @opts.ci_enable
45
      $.get @opts.url_to_ci_check, (data) =>
46 47 48 49 50 51
        this.showCiState data.status
      , 'json'

  bindEvents: ->
    this.$('.nav-tabs').on 'click', 'a', (event) =>
      a = $(event.currentTarget)
Andrey Kumanyaev committed
52

53 54
      href = a.attr('href')
      History.replaceState {path: href}, document.title, href
Andrey Kumanyaev committed
55

56
      event.preventDefault()
Andrey Kumanyaev committed
57

58 59 60
    this.$('.nav-tabs').on 'click', 'li', (event) =>
      this.activateTab($(event.currentTarget).data('action'))

61 62 63 64
    this.$('.accept_merge_request').on 'click', ->
      $('.automerge_widget.can_be_merged').hide()
      $('.merge-in-progress').show()

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
  activateTab: (action) ->
    this.$('.nav-tabs li').removeClass 'active'
    this.$('.tab-content').hide()
    switch action
      when 'diffs'
        this.$('.nav-tabs .diffs-tab').addClass 'active'
        this.loadDiff() unless @diffs_loaded
        this.$('.diffs').show()
      else
        this.$('.nav-tabs .notes-tab').addClass 'active'
        this.$('.notes').show()

  showState: (state) ->
    $('.automerge_widget').hide()
    $('.automerge_widget.' + state).show()

  showCiState: (state) ->
    $('.ci_widget').hide()
    $('.ci_widget.ci-' + state).show()

  loadDiff: (event) ->
    $.ajax
      type: 'GET'
      url: this.$('.nav-tabs .diffs-tab a').attr('href')
      beforeSend: =>
        this.$('.status').addClass 'loading'

      complete: =>
        @diffs_loaded = true
        this.$('.status').removeClass 'loading'

      dataType: 'script'

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

  alreadyOrCannotBeMerged: ->
    this.$('.automerge_widget').hide()
    this.$('.merge-in-progress').hide()
    this.$('.automerge_widget.already_cannot_be_merged').show()

107
this.MergeRequest = MergeRequest