files_comment_button.js.coffee 3.6 KB
Newer Older
1
class @FilesCommentButton
2 3 4 5 6 7 8 9 10 11 12
  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 = 100
13

14
  constructor: (@filesContainerElement) ->
L
Luke "Jared" Bennett 已提交
15 16
    @VIEW_TYPE = $('input#view[type=hidden]').val()

17
    debounce = _.debounce @render, DEBOUNCE_TIMEOUT_DURATION
18

19 20 21
    $(document)
      .on 'mouseover', LINE_COLUMN_CLASSES, debounce
      .on 'mouseleave', LINE_COLUMN_CLASSES, @destroy
22

23 24 25
  render: (e) =>
    $currentTarget = $(e.currentTarget)
    buttonParentElement = @getButtonParent $currentTarget
L
Luke "Jared" Bennett 已提交
26
    return unless @shouldRender e, buttonParentElement
27

28 29 30
    textFileElement = @getTextFileElement $currentTarget
    lineContentElement = @getLineContent $currentTarget

31
    buttonParentElement.append @buildButton
32 33 34 35
      noteableType: textFileElement.attr 'data-noteable-type'
      noteableID: textFileElement.attr 'data-noteable-id'
      commitID: textFileElement.attr 'data-commit-id'
      noteType: lineContentElement.attr 'data-note-type'
36
      position: lineContentElement.attr 'data-position'
37 38 39
      lineType: lineContentElement.attr 'data-line-type'
      discussionID: lineContentElement.attr 'data-discussion-id'
      lineCode: lineContentElement.attr 'data-line-code'
40 41 42 43
    return

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

  buildButton: (buttonAttributes) ->
48 49
    initializedButtonTemplate = COMMENT_BUTTON_TEMPLATE
      COMMENT_BUTTON_CLASS: COMMENT_BUTTON_CLASS.substr 1
L
Luke "Jared" Bennett 已提交
50
    $(initializedButtonTemplate).attr
51 52 53 54 55
      'data-noteable-type': buttonAttributes.noteableType
      'data-noteable-id': buttonAttributes.noteableID
      'data-commit-id': buttonAttributes.commitID
      'data-note-type': buttonAttributes.noteType
      'data-line-code': buttonAttributes.lineCode
56
      'data-position': buttonAttributes.position
57 58
      'data-discussion-id': buttonAttributes.discussionID
      'data-line-type': buttonAttributes.lineType
59

60
  getTextFileElement: (hoveredElement) ->
61
    $(hoveredElement.closest TEXT_FILE_SELECTOR)
62

63
  getLineContent: (hoveredElement) ->
64
    return hoveredElement if hoveredElement.hasClass LINE_CONTENT_CLASS
65

66
    $(hoveredElement).next ".#{LINE_CONTENT_CLASS}"
67

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

72
      hoveredElement.parent().find ".#{OLD_LINE_CLASS}"
73
    else
74
      return hoveredElement if hoveredElement.hasClass LINE_NUMBER_CLASS
75

76
      $(hoveredElement).prev ".#{LINE_NUMBER_CLASS}"
77

78
  isMovingToSameType: (e) ->
79
    newButtonParent = @getButtonParent $(e.toElement)
80
    return false unless newButtonParent
81
    newButtonParent.is @getButtonParent $(e.currentTarget)
82 83

  shouldRender: (e, buttonParentElement) ->
84 85 86 87 88 89 90 91 92 93
    (not buttonParentElement.hasClass(EMPTY_CELL_CLASS) and \
    not buttonParentElement.hasClass(UNFOLDABLE_LINE_CLASS) and \
    $(COMMENT_BUTTON_CLASS, buttonParentElement).length is 0)

$.fn.filesCommentButton = ->
  return unless this and @parent().data('can-create-note')?

  @each ->
    unless $.data this, 'filesCommentButton'
      $.data this, 'filesCommentButton', new FilesCommentButton $(this)