summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/merge_requests.js.coffee
blob: cdc685f2c0906ed3b2ab782aaf6bd989bcd61eef (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
#
# * Filter merge requests
# 
@merge_requestsPage = ->
  $('#assignee_id').chosen()
  $('#milestone_id').chosen()
  $('#milestone_id, #assignee_id').on 'change', ->
    $(this).closest('form').submit()

class MergeRequest
  
  constructor: (@opts) ->
    this.$el = $('.merge-request')
    @diffs_loaded = false
    @commits_loaded = false
    
    this.activateTab(@opts.action)
    
    this.bindEvents()
    
    this.initMergeWidget()
    this.$('.show-all-commits').on 'click', =>
      this.showAllCommits()

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

  initMergeWidget: ->
    this.showState( @opts.current_state )
    
    if this.$('.automerge_widget').length and @opts.check_enable
      $.get @opts.url_to_automerge_check, (data) =>
        this.showState( data.state )
      , 'json'

    if @opts.ci_enable
      $.get @opts.url_to_ci_check, (data) =>
        this.showCiState data.status
      , 'json'

  bindEvents: ->
    this.$('.nav-tabs').on 'click', 'a', (event) =>
      a = $(event.currentTarget)
      
      href = a.attr('href')
      History.replaceState {path: href}, document.title, href
      
      event.preventDefault()
      
    this.$('.nav-tabs').on 'click', 'li', (event) =>
      this.activateTab($(event.currentTarget).data('action'))

  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) ->
    $('.dashboard-loader').show()
    $.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()

this.MergeRequest = MergeRequest