merge_request_tabs.js.coffee 7.7 KB
Newer Older
R
Robert Speicher 已提交
1 2 3 4 5
# MergeRequestTabs
#
# Handles persisting and restoring the current tab selection and lazily-loading
# content on the MergeRequests#show page.
#
6 7
#= require jquery.cookie
#
R
Robert Speicher 已提交
8 9
# ### Example Markup
#
10
#   <ul class="nav-links merge-request-tabs">
R
Robert Speicher 已提交
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
#     <li class="notes-tab active">
#       <a data-action="notes" data-target="#notes" data-toggle="tab" href="/foo/bar/merge_requests/1">
#         Discussion
#       </a>
#     </li>
#     <li class="commits-tab">
#       <a data-action="commits" data-target="#commits" data-toggle="tab" href="/foo/bar/merge_requests/1/commits">
#         Commits
#       </a>
#     </li>
#     <li class="diffs-tab">
#       <a data-action="diffs" data-target="#diffs" data-toggle="tab" href="/foo/bar/merge_requests/1/diffs">
#         Diffs
#       </a>
#     </li>
#   </ul>
#
#   <div class="tab-content">
#     <div class="notes tab-pane active" id="notes">
#       Notes Content
#     </div>
#     <div class="commits tab-pane" id="commits">
#       Commits Content
#     </div>
#     <div class="diffs tab-pane" id="diffs">
#       Diffs Content
#     </div>
#   </div>
#
#   <div class="mr-loading-status">
#     <div class="loading">
#       Loading Animation
#     </div>
#   </div>
#
46 47
class @MergeRequestTabs
  diffsLoaded: false
D
Douwe Maan 已提交
48
  buildsLoaded: false
49 50
  commitsLoaded: false

R
Robert Speicher 已提交
51 52 53 54
  constructor: (@opts = {}) ->
    # Store the `location` object, allowing for easier stubbing in tests
    @_location = location

55 56 57
    @bindEvents()
    @activateTab(@opts.action)

58
  bindEvents: ->
R
Robert Speicher 已提交
59
    $(document).on 'shown.bs.tab', '.merge-request-tabs a[data-toggle="tab"]', @tabShown
D
Douwe Maan 已提交
60 61 62 63 64 65
    $(document).on 'click', '.js-show-tab', @showTab

  showTab: (event) =>
    event.preventDefault()

    @activateTab $(event.target).data('action')
66

R
Robert Speicher 已提交
67
  tabShown: (event) =>
68 69 70
    $target = $(event.target)
    action = $target.data('action')

R
Robert Speicher 已提交
71 72
    if action == 'commits'
      @loadCommits($target.attr('href'))
73
      @expandView()
R
Robert Speicher 已提交
74 75
    else if action == 'diffs'
      @loadDiff($target.attr('href'))
A
Alfredo Sumaran 已提交
76
      if bp? and bp.getBreakpointSize() isnt 'lg'
77
        @shrinkView()
78 79 80

      navBarHeight = $('.navbar-gitlab').outerHeight()
      $.scrollTo(".merge-request-details .merge-request-tabs", offset: -navBarHeight)
D
Douwe Maan 已提交
81 82
    else if action == 'builds'
      @loadBuilds($target.attr('href'))
83 84 85
      @expandView()
    else
      @expandView()
86 87 88

    @setCurrentAction(action)

V
Valery Sizov 已提交
89 90
  scrollToElement: (container) ->
    if window.location.hash
91
      navBarHeight = $('.navbar-gitlab').outerHeight() + $('.layout-nav').outerHeight()
92

93 94
      $el = $("#{container} #{window.location.hash}:not(.match)")
      $.scrollTo("#{container} #{window.location.hash}:not(.match)", offset: -navBarHeight) if $el.length
V
Valery Sizov 已提交
95

R
Robert Speicher 已提交
96 97 98 99
  # Activate a tab based on the current action
  activateTab: (action) ->
    action = 'notes' if action == 'show'
    $(".merge-request-tabs a[data-action='#{action}']").tab('show')
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

  # Replaces the current Merge Request-specific action in the URL with a new one
  #
  # If the action is "notes", the URL is reset to the standard
  # `MergeRequests#show` route.
  #
  # Examples:
  #
  #   location.pathname # => "/namespace/project/merge_requests/1"
  #   setCurrentAction('diffs')
  #   location.pathname # => "/namespace/project/merge_requests/1/diffs"
  #
  #   location.pathname # => "/namespace/project/merge_requests/1/diffs"
  #   setCurrentAction('notes')
  #   location.pathname # => "/namespace/project/merge_requests/1"
  #
  #   location.pathname # => "/namespace/project/merge_requests/1/diffs"
  #   setCurrentAction('commits')
  #   location.pathname # => "/namespace/project/merge_requests/1/commits"
R
Robert Speicher 已提交
119 120 121
  #
  # Returns the new URL String
  setCurrentAction: (action) =>
122 123 124 125
    # Normalize action, just to be safe
    action = 'notes' if action == 'show'

    # Remove a trailing '/commits' or '/diffs'
D
Douwe Maan 已提交
126
    new_state = @_location.pathname.replace(/\/(commits|diffs|builds)(\.html)?\/?$/, '')
127 128 129 130 131 132

    # Append the new action if we're on a tab other than 'notes'
    unless action == 'notes'
      new_state += "/#{action}"

    # Ensure parameters and hash come along for the ride
R
Robert Speicher 已提交
133
    new_state += @_location.search + @_location.hash
134 135 136 137 138 139 140

    # Replace the current history state with the new one without breaking
    # Turbolinks' history.
    #
    # See https://github.com/rails/turbolinks/issues/363
    history.replaceState {turbolinks: true, url: new_state}, document.title, new_state

R
Robert Speicher 已提交
141 142 143 144 145 146 147
    new_state

  loadCommits: (source) ->
    return if @commitsLoaded

    @_get
      url: "#{source}.json"
148
      success: (data) =>
149
        document.querySelector("div#commits").innerHTML = data.html
150
        gl.utils.localTimeAgo($('.js-timeago', 'div#commits'))
R
Robert Speicher 已提交
151
        @commitsLoaded = true
152
        @scrollToElement("#commits")
153

D
Douwe Maan 已提交
154 155 156 157 158
  loadDiff: (source) ->
    return if @diffsLoaded
    @_get
      url: "#{source}.json" + @_location.search
      success: (data) =>
159
        $('#diffs').html data.html
160 161 162 163 164

        if $('resolve-btn, resolve-all').length and DiffNotesApp?
          $('resolve-btn, resolve-all').each ->
            DiffNotesApp.$compile $(this).get(0)

165
        gl.utils.localTimeAgo($('.js-timeago', 'div#diffs'))
166
        $('#diffs .js-syntax-highlight').syntaxHighlight()
L
Luke "Jared" Bennett 已提交
167
        $('#diffs .diff-file').singleFileDiff()
168
        @expandViewContainer() if @diffViewType() is 'parallel'
D
Douwe Maan 已提交
169 170
        @diffsLoaded = true
        @scrollToElement("#diffs")
171
        @highlighSelectedLine()
172
        @filesCommentButton = $('.files .diff-file').filesCommentButton()
173

174
        $(document)
175 176
          .off 'click', '.diff-line-num a'
          .on 'click', '.diff-line-num a', (e) =>
177 178 179 180 181
            e.preventDefault()
            window.location.hash = $(e.currentTarget).attr 'href'
            @highlighSelectedLine()
            @scrollToElement("#diffs")

182
  highlighSelectedLine: ->
183 184
    $('.hll').removeClass 'hll'
    locationHash = window.location.hash
185 186 187

    if locationHash isnt ''
      hashClassString = ".#{locationHash.replace('#', '')}"
188
      $diffLine = $("#{locationHash}:not(.match)", $('#diffs'))
189

190 191
      if not $diffLine.is 'tr'
        $diffLine = $('#diffs').find("td#{locationHash}, td#{hashClassString}")
192
      else
193
        $diffLine = $diffLine.find('td')
194

195 196 197 198
      if $diffLine.length
        $diffLine.addClass 'hll'
        diffLineTop = $diffLine.offset().top
        navBarHeight = $('.navbar-gitlab').outerHeight()
D
Douwe Maan 已提交
199

D
Douwe Maan 已提交
200 201 202 203 204 205
  loadBuilds: (source) ->
    return if @buildsLoaded

    @_get
      url: "#{source}.json"
      success: (data) =>
206
        document.querySelector("div#builds").innerHTML = data.html
207
        gl.utils.localTimeAgo($('.js-timeago', 'div#builds'))
D
Douwe Maan 已提交
208 209 210
        @buildsLoaded = true
        @scrollToElement("#builds")

211 212 213 214 215
  # Show or hide the loading spinner
  #
  # status - Boolean, true to show, false to hide
  toggleLoading: (status) ->
    $('.mr-loading-status .loading').toggle(status)
R
Robert Speicher 已提交
216 217 218

  _get: (options) ->
    defaults = {
219 220
      beforeSend: => @toggleLoading(true)
      complete:   => @toggleLoading(false)
R
Robert Speicher 已提交
221 222 223 224 225 226 227
      dataType: 'json'
      type: 'GET'
    }

    options = $.extend({}, defaults, options)

    $.ajax(options)
228

229
  # Returns diff view type
230
  diffViewType: ->
A
Alfredo Sumaran 已提交
231
    $('.inline-parallel-buttons a.active').data('view-type')
232 233 234

  expandViewContainer: ->
    $('.container-fluid').removeClass('container-limited')
235 236

  shrinkView: ->
237
    $gutterIcon = $('.js-sidebar-toggle i:visible')
238

239 240
    # Wait until listeners are set
    setTimeout( ->
241
      # Only when sidebar is expanded
242
      if $gutterIcon.is('.fa-angle-double-right')
243 244 245 246 247 248 249 250 251 252 253 254 255 256
        $gutterIcon.closest('a').trigger('click', [true])
    , 0)

  # Expand the issuable sidebar unless the user explicitly collapsed it
  expandView: ->
    return if $.cookie('collapsed_gutter') == 'true'

    $gutterIcon = $('.js-sidebar-toggle i:visible')

    # Wait until listeners are set
    setTimeout( ->
      # Only when sidebar is collapsed
      if $gutterIcon.is('.fa-angle-double-left')
        $gutterIcon.closest('a').trigger('click', [true])
257
    , 0)