Commit 1f73afd0 authored by Luke "Jared" Bennett's avatar Luke "Jared" Bennett

Registered FilesCommentButton as a jQuery plugin and made review changes

parent 71e4175f
class @Diff class @Diff
UNFOLD_COUNT = 20 UNFOLD_COUNT = 20
constructor: -> constructor: ->
@filesCommentButton = new FilesCommentButton($('.files')) @filesCommentButton = $('.files .diff-file').filesCommentButton()
$(document).off('click', '.js-unfold') $(document).off('click', '.js-unfold')
$(document).on('click', '.js-unfold', (event) => $(document).on('click', '.js-unfold', (event) =>
......
class @FilesCommentButton class @FilesCommentButton
constructor: (@filesContainerElement) -> COMMENT_BUTTON_CLASS = '.add-diff-note'
return unless @filesContainerElement 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>'
return if _.isUndefined @filesContainerElement.data 'can-create-note' LINE_HOLDER_CLASS = '.line_holder'
LINE_NUMBER_CLASS = 'diff-line-num'
@COMMENT_BUTTON_CLASS = '.add-diff-note' LINE_CONTENT_CLASS = 'line_content'
@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>' UNFOLDABLE_LINE_CLASS = 'js-unfold'
EMPTY_CELL_CLASS = 'empty-cell'
@LINE_NUMBER_CLASS = 'diff-line-num' OLD_LINE_CLASS = 'old_line'
@LINE_CONTENT_CLASS = 'line_content' LINE_COLUMN_CLASSES = ".#{LINE_NUMBER_CLASS}, .line_content"
@UNFOLDABLE_LINE_CLASS = 'js-unfold' TEXT_FILE_SELECTOR = '.text-file'
@EMPTY_CELL_CLASS = 'empty-cell' DEBOUNCE_TIMEOUT_DURATION = 100
@OLD_LINE_CLASS = 'old_line'
@LINE_COLUMN_CLASSES = ".#{@LINE_NUMBER_CLASS}, .line_content"
@TEXT_FILE_SELECTOR = '.text-file'
@DEBOUNCE_TIMEOUT_DURATION = 150
constructor: (@filesContainerElement) ->
@VIEW_TYPE = $('input#view[type=hidden]').val() @VIEW_TYPE = $('input#view[type=hidden]').val()
$(document) debounce = _.debounce @render, DEBOUNCE_TIMEOUT_DURATION
.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) -> $(document)
currentTarget = $(e.currentTarget) .on 'mouseover', LINE_COLUMN_CLASSES, debounce
textFileElement = @getTextFileElement(currentTarget) .on 'mouseleave', LINE_COLUMN_CLASSES, @destroy
lineContentElement = @getLineContent(currentTarget)
buttonParentElement = @getButtonParent(currentTarget)
render: (e) =>
$currentTarget = $(e.currentTarget)
buttonParentElement = @getButtonParent $currentTarget
return unless @shouldRender e, buttonParentElement return unless @shouldRender e, buttonParentElement
textFileElement = @getTextFileElement $currentTarget
lineContentElement = @getLineContent $currentTarget
buttonParentElement.append @buildButton buttonParentElement.append @buildButton
noteable_type: textFileElement.attr 'data-noteable-type' noteableType: textFileElement.attr 'data-noteable-type'
noteable_id: textFileElement.attr 'data-noteable-id' noteableID: textFileElement.attr 'data-noteable-id'
commit_id: textFileElement.attr 'data-commit-id' commitID: textFileElement.attr 'data-commit-id'
note_type: lineContentElement.attr 'data-note-type' noteType: lineContentElement.attr 'data-note-type'
position: lineContentElement.attr 'data-position' position: lineContentElement.attr 'data-position'
line_type: lineContentElement.attr 'data-line-type' lineType: lineContentElement.attr 'data-line-type'
discussion_id: lineContentElement.attr 'data-discussion-id' discussionID: lineContentElement.attr 'data-discussion-id'
line_code: lineContentElement.attr 'data-line-code' lineCode: lineContentElement.attr 'data-line-code'
return return
destroy: (e) => destroy: (e) =>
return if @isMovingToSameType e return if @isMovingToSameType e
$(@COMMENT_BUTTON_CLASS, @getButtonParent $(e.currentTarget)).remove() $(COMMENT_BUTTON_CLASS, @getButtonParent $(e.currentTarget)).remove()
return return
buildButton: (buttonAttributes) -> buildButton: (buttonAttributes) ->
initializedButtonTemplate = @COMMENT_BUTTON_TEMPLATE initializedButtonTemplate = COMMENT_BUTTON_TEMPLATE
COMMENT_BUTTON_CLASS: @COMMENT_BUTTON_CLASS.substr 1 COMMENT_BUTTON_CLASS: COMMENT_BUTTON_CLASS.substr 1
$(initializedButtonTemplate).attr $(initializedButtonTemplate).attr
'data-noteable-type': buttonAttributes.noteable_type 'data-noteable-type': buttonAttributes.noteableType
'data-noteable-id': buttonAttributes.noteable_id 'data-noteable-id': buttonAttributes.noteableID
'data-commit-id': buttonAttributes.commit_id 'data-commit-id': buttonAttributes.commitID
'data-note-type': buttonAttributes.note_type 'data-note-type': buttonAttributes.noteType
'data-line-code': buttonAttributes.line_code 'data-line-code': buttonAttributes.lineCode
'data-position': buttonAttributes.position 'data-position': buttonAttributes.position
'data-discussion-id': buttonAttributes.discussion_id 'data-discussion-id': buttonAttributes.discussionID
'data-line-type': buttonAttributes.line_type 'data-line-type': buttonAttributes.lineType
getTextFileElement: (hoveredElement) -> getTextFileElement: (hoveredElement) ->
$(hoveredElement.closest(@TEXT_FILE_SELECTOR)) $(hoveredElement.closest TEXT_FILE_SELECTOR)
getLineContent: (hoveredElement) -> getLineContent: (hoveredElement) ->
return hoveredElement if hoveredElement.hasClass @LINE_CONTENT_CLASS return hoveredElement if hoveredElement.hasClass LINE_CONTENT_CLASS
$(hoveredElement).next ".#{@LINE_CONTENT_CLASS}" $(hoveredElement).next ".#{LINE_CONTENT_CLASS}"
getButtonParent: (hoveredElement) -> getButtonParent: (hoveredElement) ->
if @VIEW_TYPE is 'inline' if @VIEW_TYPE is 'inline'
return hoveredElement if hoveredElement.hasClass @OLD_LINE_CLASS return hoveredElement if hoveredElement.hasClass OLD_LINE_CLASS
$(hoveredElement).parent().find ".#{@OLD_LINE_CLASS}" hoveredElement.parent().find ".#{OLD_LINE_CLASS}"
else else
return hoveredElement if hoveredElement.hasClass @LINE_NUMBER_CLASS return hoveredElement if hoveredElement.hasClass LINE_NUMBER_CLASS
$(hoveredElement).prev ".#{@LINE_NUMBER_CLASS}" $(hoveredElement).prev ".#{LINE_NUMBER_CLASS}"
isMovingToSameType: (e) -> isMovingToSameType: (e) ->
newButtonParent = @getButtonParent($(e.toElement)) newButtonParent = @getButtonParent $(e.toElement)
return false unless newButtonParent return false unless newButtonParent
(newButtonParent).is @getButtonParent($(e.currentTarget)) newButtonParent.is @getButtonParent $(e.currentTarget)
shouldRender: (e, buttonParentElement) -> shouldRender: (e, buttonParentElement) ->
(!buttonParentElement.hasClass(@EMPTY_CELL_CLASS) and \ (not buttonParentElement.hasClass(EMPTY_CELL_CLASS) and \
!buttonParentElement.hasClass(@UNFOLDABLE_LINE_CLASS) and \ not buttonParentElement.hasClass(UNFOLDABLE_LINE_CLASS) and \
$(@COMMENT_BUTTON_CLASS, buttonParentElement).length is 0) $(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)
...@@ -163,7 +163,7 @@ class @MergeRequestTabs ...@@ -163,7 +163,7 @@ class @MergeRequestTabs
@diffsLoaded = true @diffsLoaded = true
@scrollToElement("#diffs") @scrollToElement("#diffs")
@highlighSelectedLine() @highlighSelectedLine()
@filesCommentButton = new FilesCommentButton($('.files')) @filesCommentButton = $('.files .diff-file').filesCommentButton()
$(document) $(document)
.off 'click', '.diff-line-num a' .off 'click', '.diff-line-num a'
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
- else - else
%td.old_line.diff-line-num{id: left[:line_code], class: [left[:type], ('empty-cell' unless left[:number])], data: { linenumber: left[:number] }} %td.old_line.diff-line-num{id: left[:line_code], class: [left[:type], ('empty-cell' unless left[:number])], data: { linenumber: left[:number] }}
%a{href: "##{left[:line_code]}" }= raw(left[:number]) %a{href: "##{left[:line_code]}" }= raw(left[:number])
%td.line_content.parallel.noteable_line{class: [left[:type], ('empty-cell' if left[:text].empty?)], data: diff_view_line_data(left[:line_code], left[:position], left[:type])}= diff_line_content(left[:text]) %td.line_content.parallel.noteable_line{class: [left[:type], ('empty-cell' if left[:text].empty?)], data: diff_view_line_data(left[:line_code], left[:position], 'old')}= diff_line_content(left[:text])
- if right[:type] == 'new' - if right[:type] == 'new'
- new_line_type = 'new' - new_line_type = 'new'
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
%td.new_line.diff-line-num{id: new_line_code, class: [new_line_type, ('empty-cell' unless right[:number])], data: { linenumber: right[:number] }} %td.new_line.diff-line-num{id: new_line_code, class: [new_line_type, ('empty-cell' unless right[:number])], data: { linenumber: right[:number] }}
%a{href: "##{new_line_code}" }= raw(right[:number]) %a{href: "##{new_line_code}" }= raw(right[:number])
%td.line_content.parallel.noteable_line{class: [new_line_type, ('empty-cell' if right[:text].empty?)], data: diff_view_line_data(new_line_code, new_position, new_line_type)}= diff_line_content(right[:text]) %td.line_content.parallel.noteable_line{class: [new_line_type, ('empty-cell' if right[:text].empty?)], data: diff_view_line_data(new_line_code, new_position, 'new')}= diff_line_content(right[:text])
- unless @diff_notes_disabled - unless @diff_notes_disabled
- notes_left, notes_right = organize_comments(left, right) - notes_left, notes_right = organize_comments(left, right)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment