2017-08-17 22:00:37 +05:30
/* 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 */
/* global FilesCommentButton */
/* global notes */
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
let $commentButtonTemplate ;
var bind = function ( fn , me ) { return function ( ) { return fn . apply ( me , arguments ) ; } ; } ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
window . FilesCommentButton = ( function ( ) {
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-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
COMMENT _BUTTON _CLASS = '.add-diff-note' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
LINE _HOLDER _CLASS = '.line_holder' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
LINE _NUMBER _CLASS = 'diff-line-num' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
LINE _CONTENT _CLASS = 'line_content' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
UNFOLDABLE _LINE _CLASS = 'js-unfold' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
EMPTY _CELL _CLASS = 'empty-cell' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
OLD _LINE _CLASS = 'old_line' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
LINE _COLUMN _CLASSES = "." + LINE _NUMBER _CLASS + ", .line_content" ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
TEXT _FILE _SELECTOR = '.text-file' ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
function FilesCommentButton ( filesContainerElement ) {
this . render = bind ( this . render , this ) ;
this . hideButton = bind ( this . hideButton , this ) ;
this . isParallelView = notes . isParallelView ( ) ;
filesContainerElement . on ( 'mouseover' , LINE _COLUMN _CLASSES , this . render )
. on ( 'mouseleave' , LINE _COLUMN _CLASSES , this . hideButton ) ;
}
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
FilesCommentButton . prototype . render = function ( e ) {
var $currentTarget , buttonParentElement , lineContentElement , textFileElement , $button ;
$currentTarget = $ ( e . currentTarget ) ;
if ( $currentTarget . hasClass ( 'js-no-comment-btn' ) ) return ;
lineContentElement = this . getLineContent ( $currentTarget ) ;
buttonParentElement = this . getButtonParent ( $currentTarget ) ;
if ( ! this . validateButtonParent ( buttonParentElement ) || ! this . validateLineContent ( lineContentElement ) ) return ;
$button = $ ( COMMENT _BUTTON _CLASS , buttonParentElement ) ;
buttonParentElement . addClass ( 'is-over' )
. nextUntil ( ` . ${ LINE _CONTENT _CLASS } ` ) . addClass ( 'is-over' ) ;
if ( $button . length ) {
return ;
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
textFileElement = this . getTextFileElement ( $currentTarget ) ;
buttonParentElement . append ( this . buildButton ( {
discussionID : lineContentElement . attr ( 'data-discussion-id' ) ,
lineType : lineContentElement . attr ( 'data-line-type' ) ,
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
noteableType : textFileElement . attr ( 'data-noteable-type' ) ,
noteableID : textFileElement . attr ( 'data-noteable-id' ) ,
commitID : textFileElement . attr ( 'data-commit-id' ) ,
noteType : lineContentElement . attr ( 'data-note-type' ) ,
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
// LegacyDiffNote
lineCode : lineContentElement . attr ( 'data-line-code' ) ,
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
// DiffNote
position : lineContentElement . attr ( 'data-position' )
} ) ) ;
} ;
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
FilesCommentButton . prototype . hideButton = function ( e ) {
var $currentTarget = $ ( e . currentTarget ) ;
var buttonParentElement = this . getButtonParent ( $currentTarget ) ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
buttonParentElement . removeClass ( 'is-over' )
. nextUntil ( ` . ${ LINE _CONTENT _CLASS } ` ) . removeClass ( 'is-over' ) ;
} ;
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
FilesCommentButton . prototype . buildButton = function ( buttonAttributes ) {
return $commentButtonTemplate . clone ( ) . attr ( {
'data-discussion-id' : buttonAttributes . discussionID ,
'data-line-type' : buttonAttributes . lineType ,
'data-noteable-type' : buttonAttributes . noteableType ,
'data-noteable-id' : buttonAttributes . noteableID ,
'data-commit-id' : buttonAttributes . commitID ,
'data-note-type' : buttonAttributes . noteType ,
// LegacyDiffNote
'data-line-code' : buttonAttributes . lineCode ,
// DiffNote
'data-position' : buttonAttributes . position
} ) ;
} ;
FilesCommentButton . prototype . getTextFileElement = function ( hoveredElement ) {
return hoveredElement . closest ( TEXT _FILE _SELECTOR ) ;
} ;
FilesCommentButton . prototype . getLineContent = function ( hoveredElement ) {
if ( hoveredElement . hasClass ( LINE _CONTENT _CLASS ) ) {
return hoveredElement ;
}
if ( ! this . isParallelView ) {
return $ ( hoveredElement ) . closest ( LINE _HOLDER _CLASS ) . find ( "." + LINE _CONTENT _CLASS ) ;
} else {
return $ ( hoveredElement ) . next ( "." + LINE _CONTENT _CLASS ) ;
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
} ;
FilesCommentButton . prototype . getButtonParent = function ( hoveredElement ) {
if ( ! this . isParallelView ) {
if ( hoveredElement . hasClass ( OLD _LINE _CLASS ) ) {
return hoveredElement ;
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
return hoveredElement . parent ( ) . find ( "." + OLD _LINE _CLASS ) ;
} else {
if ( hoveredElement . hasClass ( LINE _NUMBER _CLASS ) ) {
return hoveredElement ;
}
return $ ( hoveredElement ) . prev ( "." + LINE _NUMBER _CLASS ) ;
}
2016-09-13 17:45:13 +05:30
} ;
2017-08-17 22:00:37 +05:30
FilesCommentButton . prototype . validateButtonParent = function ( buttonParentElement ) {
return ! buttonParentElement . hasClass ( EMPTY _CELL _CLASS ) && ! buttonParentElement . hasClass ( UNFOLDABLE _LINE _CLASS ) ;
} ;
FilesCommentButton . prototype . validateLineContent = function ( lineContentElement ) {
return lineContentElement . attr ( 'data-note-type' ) && lineContentElement . attr ( 'data-note-type' ) !== '' ;
} ;
return FilesCommentButton ;
} ) ( ) ;
$ . fn . filesCommentButton = function ( ) {
$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>' ) ;
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 ) ) ) ;
}
} ) ;
} ;