summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/files_comment_button.js.coffee
blob: 171778e5347bbef69eb287549a11c1c9ed56b7e5 (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
98
99
class @FilesCommentButton
  constructor: (@filesContainerElement) ->
    return unless @filesContainerElement
    return if _.isUndefined @filesContainerElement.data 'can-create-note'

    @COMMENT_BUTTON_CLASS = '.add-diff-note'
    @COMMENT_BUTTON_TEMPLATE = _.template '<button name="button" type="submit" class="btn <%- COMMENT_BUTTON_CLASS %> js-add-diff-note-button" title="Add a comment to this line"><i class="fa fa-comment-o"></i></button>'

    @LINE_HOLDER_CLASS = '.line_holder'
    @LINE_NUMBER_CLASS = 'diff-line-num'
    @LINE_CONTENT_CLASS = 'line_content'
    @UNFOLDABLE_LINE_CLASS = 'js-unfold'
    @EMPTY_CELL_CLASS = 'empty-cell'
    @OLD_LINE_CLASS = 'old_line'
    @LINE_COLUMN_CLASSES = ".#{@LINE_NUMBER_CLASS}, .line_content"
    @TEXT_FILE_SELECTOR = '.text-file'

    @DEBOUNCE_TIMEOUT_DURATION = 150

    @VIEW_TYPE = $('input#view[type=hidden]').val()

    $(document)
      .on 'mouseover', @LINE_COLUMN_CLASSES, @debounceRender
      .on 'mouseleave', @LINE_COLUMN_CLASSES, @destroy

  debounceRender: (e) =>
    clearTimeout @debounceTimeout if @debounceTimeout
    @debounceTimeout = setTimeout =>
      @render e
    , @DEBOUNCE_TIMEOUT_DURATION
    return

  render: (e) ->
    currentTarget = $(e.currentTarget)
    textFileElement = @getTextFileElement(currentTarget)
    lineHolderElement = @getLineHolder(currentTarget)
    lineContentElement = @getLineContent(currentTarget)
    buttonParentElement = @getButtonParent(currentTarget)

    return unless @shouldRender e, buttonParentElement

    buttonParentElement.append @buildButton
      commit_id: textFileElement.attr 'data-commit-id'
      discussion_id: lineContentElement.attr('data-discussion-id') or lineHolderElement.attr('data-discussion-id')
      line_code: lineContentElement.attr('data-line-code') or lineHolderElement.attr('id')
      line_type: lineContentElement.attr 'data-line-type'
      note_type: textFileElement.attr 'data-note-type'
      noteable_id: textFileElement.attr 'data-noteable-id'
      noteable_type: textFileElement.attr 'data-noteable-type'
    return

  destroy: (e) =>
    return if @isMovingToSameType e
    $(@COMMENT_BUTTON_CLASS, @getButtonParent $(e.currentTarget)).remove()
    return

  buildButton: (buttonAttributes) ->
    initializedButtonTemplate = @COMMENT_BUTTON_TEMPLATE
      COMMENT_BUTTON_CLASS: @COMMENT_BUTTON_CLASS.substr 1
    $(initializedButtonTemplate).attr
      'data-noteable-id': buttonAttributes.noteable_id
      'data-commit-id': buttonAttributes.commit_id
      'data-discussion-id': buttonAttributes.discussion_id
      'data-noteable-type': buttonAttributes.noteable_type
      'data-line-type': buttonAttributes.line_type
      'data-note-type': buttonAttributes.note_type
      'data-line-code': buttonAttributes.line_code

  getTextFileElement: (hoveredElement) ->
    $(hoveredElement.closest(@TEXT_FILE_SELECTOR))

  getLineHolder: (hoveredElement) ->
    return hoveredElement if hoveredElement.hasClass @LINE_HOLDER_CLASS
    $(hoveredElement.parent())

  getLineContent: (hoveredElement) ->
    return hoveredElement if hoveredElement.hasClass @LINE_CONTENT_CLASS

    $(hoveredElement).next ".#{@LINE_CONTENT_CLASS}"

  getButtonParent: (hoveredElement) ->
    if @VIEW_TYPE is 'inline'
      return hoveredElement if hoveredElement.hasClass @OLD_LINE_CLASS

      $(hoveredElement).parent().find ".#{@OLD_LINE_CLASS}"
    else
      return hoveredElement if hoveredElement.hasClass @LINE_NUMBER_CLASS

      $(hoveredElement).prev ".#{@LINE_NUMBER_CLASS}"

  isMovingToSameType: (e) ->
    newButtonParent = @getButtonParent($(e.toElement))
    return false unless newButtonParent
    (newButtonParent).is @getButtonParent($(e.currentTarget))

  shouldRender: (e, buttonParentElement) ->
    (!buttonParentElement.hasClass(@EMPTY_CELL_CLASS) and \
    !buttonParentElement.hasClass(@UNFOLDABLE_LINE_CLASS) and \
    $(@COMMENT_BUTTON_CLASS, buttonParentElement).length is 0)