BigW Consortium Gitlab

diff.js.coffee 1.49 KB
Newer Older
1
class @Diff
skv committed
2 3
  UNFOLD_COUNT = 20
  constructor: ->
4
    $(document).off('click', '.js-unfold')
skv committed
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
    $(document).on('click', '.js-unfold', (event) =>
      target = $(event.target)
      unfoldBottom = target.hasClass('js-unfold-bottom')
      unfold = true

      [old_line, line_number] = @lineNumbers(target.parent())
      offset = line_number - old_line

      if unfoldBottom
        line_number += 1
        since = line_number
        to = line_number + UNFOLD_COUNT
      else
        [prev_old_line, prev_new_line] = @lineNumbers(target.parent().prev())
        line_number -= 1
        to = line_number
        if line_number - UNFOLD_COUNT > prev_new_line + 1
          since = line_number - UNFOLD_COUNT
        else
          since = prev_new_line + 1
          unfold = false

      link = target.parents('.diff-file').attr('data-blob-diff-path')
      params =
        since: since
        to: to
        bottom: unfoldBottom
        offset: offset
        unfold: unfold
34 35 36 37
        # indent is used to compensate for single space indent to fit
        # '+' and '-' prepended to diff lines,
        # see https://gitlab.com/gitlab-org/gitlab-ce/issues/707
        indent: 1
skv committed
38 39 40 41

      $.get(link, params, (response) =>
        target.parent().replaceWith(response)
      )
42
    )
skv committed
43 44 45 46 47 48

  lineNumbers: (line) ->
    return ([0, 0]) unless line.children().length
    lines = line.children().slice(0, 2)
    line_numbers = ($(l).attr('data-linenumber') for l in lines)
    (parseInt(line_number) for line_number in line_numbers)