files_comment_button.js.coffee 3.8 KB
Newer Older
1
class @FilesCommentButton
2 3 4 5 6 7 8 9
  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'
10
  NEW_CLASS = 'new'
11 12 13
  LINE_COLUMN_CLASSES = ".#{LINE_NUMBER_CLASS}, .line_content"
  TEXT_FILE_SELECTOR = '.text-file'
  DEBOUNCE_TIMEOUT_DURATION = 100
14

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

18
    debounce = _.debounce @render, DEBOUNCE_TIMEOUT_DURATION
19

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

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

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

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

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

  buildButton: (buttonAttributes) ->
49 50
    initializedButtonTemplate = COMMENT_BUTTON_TEMPLATE
      COMMENT_BUTTON_CLASS: COMMENT_BUTTON_CLASS.substr 1
L
Luke "Jared" Bennett 已提交
51
    $(initializedButtonTemplate).attr
52 53 54 55 56
      '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
57
      'data-position': buttonAttributes.position
58 59
      'data-discussion-id': buttonAttributes.discussionID
      'data-line-type': buttonAttributes.lineType
60

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

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

67
    $(".#{LINE_CONTENT_CLASS + @diffTypeClass hoveredElement}", hoveredElement.parent())
68

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

73
      $(".#{OLD_LINE_CLASS}", hoveredElement.parent())
74
    else
75
      return hoveredElement if hoveredElement.hasClass LINE_NUMBER_CLASS
76

77 78 79 80
      $(".#{LINE_NUMBER_CLASS + @diffTypeClass hoveredElement}", hoveredElement.parent())

  diffTypeClass: (hoveredElement) ->
    if hoveredElement.hasClass(NEW_CLASS) then '.new' else '.old'
81

82
  isMovingToSameType: (e) ->
83
    newButtonParent = @getButtonParent $(e.toElement)
84
    return false unless newButtonParent
85
    newButtonParent.is @getButtonParent $(e.currentTarget)
86 87

  shouldRender: (e, buttonParentElement) ->
88 89 90 91 92 93 94 95 96 97
    (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)