2017-01-11 23:27:41 -05:00
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, max-len, one-var, one-var-declaration-per-line, quotes, prefer-template, newline-per-chained-call, comma-dangle, new-cap, no-else-return, consistent-return */
2016-12-14 00:26:26 -05:00
/* global FilesCommentButton */
2017-02-22 12:59:07 -05:00
/* global notes */
2016-12-14 00:26:26 -05:00
2016-07-24 16:45:11 -04:00
( function ( ) {
2017-02-22 12:59:07 -05:00
let $commentButtonTemplate ;
2017-01-11 23:27:41 -05:00
var bind = function ( fn , me ) { return function ( ) { return fn . apply ( me , arguments ) ; } ; } ;
2016-07-24 16:45:11 -04:00
this . FilesCommentButton = ( function ( ) {
2017-02-22 12:30:17 -05:00
var COMMENT _BUTTON _CLASS , EMPTY _CELL _CLASS , LINE _COLUMN _CLASSES , LINE _CONTENT _CLASS , LINE _HOLDER _CLASS , LINE _NUMBER _CLASS , OLD _LINE _CLASS , TEXT _FILE _SELECTOR , UNFOLDABLE _LINE _CLASS ;
2016-07-24 16:45:11 -04:00
COMMENT _BUTTON _CLASS = '.add-diff-note' ;
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' ;
function FilesCommentButton ( filesContainerElement ) {
this . render = bind ( this . render , this ) ;
2017-02-22 12:30:17 -05:00
this . hideButton = bind ( this . hideButton , this ) ;
2017-02-22 12:59:07 -05:00
this . isParallelView = notes . isParallelView ( ) ;
filesContainerElement . on ( 'mouseover' , LINE _COLUMN _CLASSES , this . render )
2017-02-22 12:30:17 -05:00
. on ( 'mouseleave' , LINE _COLUMN _CLASSES , this . hideButton ) ;
2016-07-24 16:45:11 -04:00
}
FilesCommentButton . prototype . render = function ( e ) {
2017-02-22 12:30:17 -05:00
var $currentTarget , buttonParentElement , lineContentElement , textFileElement , $button ;
2016-07-24 16:45:11 -04:00
$currentTarget = $ ( e . currentTarget ) ;
2017-03-08 11:07:26 -05:00
if ( $currentTarget . hasClass ( 'js-no-comment-btn' ) ) return ;
2016-07-24 16:45:11 -04:00
lineContentElement = this . getLineContent ( $currentTarget ) ;
2017-02-22 12:59:07 -05:00
buttonParentElement = this . getButtonParent ( $currentTarget ) ;
if ( ! this . validateButtonParent ( buttonParentElement ) || ! this . validateLineContent ( lineContentElement ) ) return ;
2016-08-05 08:02:08 -04:00
2017-02-22 12:30:17 -05:00
$button = $ ( COMMENT _BUTTON _CLASS , buttonParentElement ) ;
buttonParentElement . addClass ( 'is-over' )
2017-02-22 12:59:07 -05:00
. nextUntil ( ` . ${ LINE _CONTENT _CLASS } ` ) . addClass ( 'is-over' ) ;
2017-02-22 12:30:17 -05:00
if ( $button . length ) {
return ;
}
2016-08-05 08:02:08 -04:00
textFileElement = this . getTextFileElement ( $currentTarget ) ;
2016-07-24 16:45:11 -04:00
buttonParentElement . append ( this . buildButton ( {
noteableType : textFileElement . attr ( 'data-noteable-type' ) ,
noteableID : textFileElement . attr ( 'data-noteable-id' ) ,
commitID : textFileElement . attr ( 'data-commit-id' ) ,
noteType : lineContentElement . attr ( 'data-note-type' ) ,
position : lineContentElement . attr ( 'data-position' ) ,
lineType : lineContentElement . attr ( 'data-line-type' ) ,
discussionID : lineContentElement . attr ( 'data-discussion-id' ) ,
lineCode : lineContentElement . attr ( 'data-line-code' )
} ) ) ;
} ;
2017-02-22 12:30:17 -05:00
FilesCommentButton . prototype . hideButton = function ( e ) {
var $currentTarget = $ ( e . currentTarget ) ;
var buttonParentElement = this . getButtonParent ( $currentTarget ) ;
buttonParentElement . removeClass ( 'is-over' )
2017-02-22 12:59:07 -05:00
. nextUntil ( ` . ${ LINE _CONTENT _CLASS } ` ) . removeClass ( 'is-over' ) ;
2016-07-24 16:45:11 -04:00
} ;
FilesCommentButton . prototype . buildButton = function ( buttonAttributes ) {
2017-02-22 12:30:17 -05:00
return $commentButtonTemplate . clone ( ) . attr ( {
2016-07-24 16:45:11 -04:00
'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 ,
'data-position' : buttonAttributes . position ,
'data-discussion-id' : buttonAttributes . discussionID ,
'data-line-type' : buttonAttributes . lineType
} ) ;
} ;
FilesCommentButton . prototype . getTextFileElement = function ( hoveredElement ) {
2017-02-22 12:59:07 -05:00
return hoveredElement . closest ( TEXT _FILE _SELECTOR ) ;
2016-07-24 16:45:11 -04:00
} ;
FilesCommentButton . prototype . getLineContent = function ( hoveredElement ) {
if ( hoveredElement . hasClass ( LINE _CONTENT _CLASS ) ) {
return hoveredElement ;
}
2017-02-22 12:59:07 -05:00
if ( ! this . isParallelView ) {
2016-07-24 16:45:11 -04:00
return $ ( hoveredElement ) . closest ( LINE _HOLDER _CLASS ) . find ( "." + LINE _CONTENT _CLASS ) ;
} else {
return $ ( hoveredElement ) . next ( "." + LINE _CONTENT _CLASS ) ;
}
} ;
FilesCommentButton . prototype . getButtonParent = function ( hoveredElement ) {
2017-02-22 12:59:07 -05:00
if ( ! this . isParallelView ) {
2016-07-24 16:45:11 -04:00
if ( hoveredElement . hasClass ( OLD _LINE _CLASS ) ) {
return hoveredElement ;
}
return hoveredElement . parent ( ) . find ( "." + OLD _LINE _CLASS ) ;
} else {
if ( hoveredElement . hasClass ( LINE _NUMBER _CLASS ) ) {
return hoveredElement ;
}
return $ ( hoveredElement ) . prev ( "." + LINE _NUMBER _CLASS ) ;
}
} ;
2016-08-05 08:02:08 -04:00
FilesCommentButton . prototype . validateButtonParent = function ( buttonParentElement ) {
2017-02-22 12:30:17 -05:00
return ! buttonParentElement . hasClass ( EMPTY _CELL _CLASS ) && ! buttonParentElement . hasClass ( UNFOLDABLE _LINE _CLASS ) ;
2016-07-24 16:45:11 -04:00
} ;
2016-08-05 08:02:08 -04:00
FilesCommentButton . prototype . validateLineContent = function ( lineContentElement ) {
2016-08-23 15:08:18 -04:00
return lineContentElement . attr ( 'data-discussion-id' ) && lineContentElement . attr ( 'data-discussion-id' ) !== '' ;
2016-08-05 08:02:08 -04:00
} ;
2016-07-24 16:45:11 -04:00
return FilesCommentButton ;
} ) ( ) ;
$ . fn . filesCommentButton = function ( ) {
2017-02-22 12:30:17 -05:00
$commentButtonTemplate = $ ( '<button name="button" type="submit" class="add-diff-note js-add-diff-note-button" title="Add a comment to this line"><i class="fa fa-comment-o"></i></button>' ) ;
2016-07-24 16:45:11 -04:00
if ( ! ( this && ( this . parent ( ) . data ( 'can-create-note' ) != null ) ) ) {
return ;
}
return this . each ( function ( ) {
if ( ! $ . data ( this , 'filesCommentButton' ) ) {
return $ . data ( this , 'filesCommentButton' , new FilesCommentButton ( $ ( this ) ) ) ;
}
} ) ;
} ;
2017-02-10 01:50:50 -05:00
} ) . call ( window ) ;