files_comment_button.js.coffee 3.8 KB
Newer Older
1 2
class @FilesCommentButton
  constructor: (@filesContainerElement) ->
3 4
    return if not @filesContainerElement
    return if not @filesContainerElement.data 'can-create-note'
5 6 7 8 9 10 11

    @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'
L
Luke "Jared" Bennett 已提交
12 13 14
    @UNFOLDABLE_LINE_CLASS = 'js-unfold'
    @EMPTY_CELL_CLASS = 'empty-cell'
    @OLD_LINE_CLASS = 'old_line'
15
    @LINE_COLUMN_CLASSES = ".#{@LINE_NUMBER_CLASS}, .line_content"
16
    @TEXT_FILE_SELECTOR = '.text-file'
17 18 19

    @DEBOUNCE_TIMEOUT_DURATION = 150

L
Luke "Jared" Bennett 已提交
20 21
    @VIEW_TYPE = $('input#view[type=hidden]').val()

22 23 24 25 26 27 28 29 30 31 32 33
    $(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) ->
34 35 36 37
    currentTarget = $(e.currentTarget)
    textFileElement = @getTextFileElement(currentTarget)
    lineHolderElement = @getLineHolder(currentTarget)
    lineContentElement = @getLineContent(currentTarget)
38
    buttonParentElement = @getButtonParent(currentTarget)
39 40 41 42 43

    return if not @shouldRender e, buttonParentElement

    buttonParentElement.append @buildButton
      id:
44 45
        noteable: textFileElement.attr 'data-noteable-id'
        commit: textFileElement.attr 'data-commit-id'
L
Luke "Jared" Bennett 已提交
46
        discussion: lineContentElement.attr('data-discussion-id') or lineHolderElement.attr('data-discussion-id')
47
      type:
48 49
        noteable: textFileElement.attr 'data-noteable-type'
        note: textFileElement.attr 'data-note-type'
50 51
        line: lineContentElement.attr 'data-line-type'
      code:
L
Luke "Jared" Bennett 已提交
52
        line: lineContentElement.attr('data-line-code') or lineHolderElement.attr('id')
53 54 55 56
    return

  destroy: (e) =>
    return if @isMovingToSameType e
57
    $(@COMMENT_BUTTON_CLASS, @getButtonParent $(e.currentTarget)).remove()
58 59 60 61 62 63 64 65 66 67 68 69
    return

  buildButton: (buttonAttributes) ->
    $(@COMMENT_BUTTON_TEMPLATE COMMENT_BUTTON_CLASS: @COMMENT_BUTTON_CLASS.substr 1).attr
      'data-noteable-id': buttonAttributes.id.noteable
      'data-commit-id': buttonAttributes.id.commit
      'data-discussion-id': buttonAttributes.id.discussion
      'data-noteable-type': buttonAttributes.type.noteable
      'data-line-type': buttonAttributes.type.line
      'data-note-type': buttonAttributes.type.note
      'data-line-code': buttonAttributes.code.line

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

73 74 75 76 77 78 79
  getLineHolder: (hoveredElement) ->
    return hoveredElement if hoveredElement.hasClass @LINE_HOLDER_CLASS
    $(hoveredElement.parent())

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

L
Luke "Jared" Bennett 已提交
80
    $(hoveredElement).next ".#{@LINE_CONTENT_CLASS}"
81

82 83 84 85 86 87 88 89 90 91
  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}"

92
  isMovingToSameType: (e) ->
93 94 95
    newButtonParent = @getButtonParent($(e.toElement))
    return false unless newButtonParent
    (newButtonParent).is @getButtonParent($(e.currentTarget))
96 97

  shouldRender: (e, buttonParentElement) ->
L
Luke "Jared" Bennett 已提交
98 99 100
    (!buttonParentElement.hasClass(@EMPTY_CELL_CLASS) and \
    !buttonParentElement.hasClass(@UNFOLDABLE_LINE_CLASS) and \
    $(@COMMENT_BUTTON_CLASS, buttonParentElement).length is 0)