2022-01-29 00:00:11 +03:00
import $ from 'jquery' ;
2021-10-16 20:28:04 +03:00
import { htmlEscape } from 'escape-goat' ;
import attachTribute from './tribute.js' ;
2022-01-05 15:17:25 +03:00
import { createCommentEasyMDE , getAttachedEasyMDE } from './comp/EasyMDE.js' ;
2021-10-16 20:28:04 +03:00
import { initCompImagePaste } from './comp/ImagePaste.js' ;
import { initCompMarkupContentPreviewTab } from './comp/MarkupContentPreview.js' ;
2021-10-21 10:37:43 +03:00
const { appSubUrl , csrfToken } = window . config ;
2021-10-16 20:28:04 +03:00
export function initRepoIssueTimeTracking ( ) {
$ ( document ) . on ( 'click' , '.issue-add-time' , ( ) => {
$ ( '.issue-start-time-modal' ) . modal ( {
duration : 200 ,
onApprove ( ) {
$ ( '#add_time_manual_form' ) . trigger ( 'submit' ) ;
} ,
} ) . modal ( 'show' ) ;
$ ( '.issue-start-time-modal input' ) . on ( 'keydown' , ( e ) => {
if ( ( e . keyCode || e . key ) === 13 ) {
$ ( '#add_time_manual_form' ) . trigger ( 'submit' ) ;
}
} ) ;
} ) ;
$ ( document ) . on ( 'click' , '.issue-start-time, .issue-stop-time' , ( ) => {
$ ( '#toggle_stopwatch_form' ) . trigger ( 'submit' ) ;
} ) ;
$ ( document ) . on ( 'click' , '.issue-cancel-time' , ( ) => {
$ ( '#cancel_stopwatch_form' ) . trigger ( 'submit' ) ;
} ) ;
$ ( document ) . on ( 'click' , 'button.issue-delete-time' , function ( ) {
const sel = ` .issue-delete-time-modal[data-id=" ${ $ ( this ) . data ( 'id' ) } "] ` ;
2022-01-16 14:19:26 +03:00
$ ( sel ) . modal ( {
2021-10-16 20:28:04 +03:00
duration : 200 ,
onApprove ( ) {
$ ( ` ${ sel } form ` ) . trigger ( 'submit' ) ;
} ,
} ) . modal ( 'show' ) ;
} ) ;
}
function updateDeadline ( deadlineString ) {
$ ( '#deadline-err-invalid-date' ) . hide ( ) ;
$ ( '#deadline-loader' ) . addClass ( 'loading' ) ;
let realDeadline = null ;
if ( deadlineString !== '' ) {
const newDate = Date . parse ( deadlineString ) ;
if ( Number . isNaN ( newDate ) ) {
$ ( '#deadline-loader' ) . removeClass ( 'loading' ) ;
$ ( '#deadline-err-invalid-date' ) . show ( ) ;
return false ;
}
realDeadline = new Date ( newDate ) ;
}
2022-04-07 21:59:56 +03:00
$ . ajax ( ` ${ $ ( '#update-issue-deadline-form' ) . attr ( 'action' ) } ` , {
2021-10-16 20:28:04 +03:00
data : JSON . stringify ( {
due _date : realDeadline ,
} ) ,
headers : {
2021-10-21 10:37:43 +03:00
'X-Csrf-Token' : csrfToken ,
2021-10-16 20:28:04 +03:00
} ,
contentType : 'application/json' ,
type : 'POST' ,
success ( ) {
window . location . reload ( ) ;
} ,
error ( ) {
$ ( '#deadline-loader' ) . removeClass ( 'loading' ) ;
$ ( '#deadline-err-invalid-date' ) . show ( ) ;
} ,
} ) ;
}
export function initRepoIssueDue ( ) {
$ ( document ) . on ( 'click' , '.issue-due-edit' , ( ) => {
$ ( '#deadlineForm' ) . fadeToggle ( 150 ) ;
} ) ;
$ ( document ) . on ( 'click' , '.issue-due-remove' , ( ) => {
updateDeadline ( '' ) ;
} ) ;
$ ( document ) . on ( 'submit' , '.issue-due-form' , ( ) => {
updateDeadline ( $ ( '#deadlineDate' ) . val ( ) ) ;
return false ;
} ) ;
}
export function initRepoIssueList ( ) {
const repolink = $ ( '#repolink' ) . val ( ) ;
const repoId = $ ( '#repoId' ) . val ( ) ;
const crossRepoSearch = $ ( '#crossRepoSearch' ) . val ( ) ;
const tp = $ ( '#type' ) . val ( ) ;
2022-04-07 21:59:56 +03:00
let issueSearchUrl = ` ${ appSubUrl } / ${ repolink } /issues/search?q={query}&type= ${ tp } ` ;
2021-10-16 20:28:04 +03:00
if ( crossRepoSearch === 'true' ) {
2022-04-07 21:59:56 +03:00
issueSearchUrl = ` ${ appSubUrl } /issues/search?q={query}&priority_repo_id= ${ repoId } &type= ${ tp } ` ;
2021-10-16 20:28:04 +03:00
}
$ ( '#new-dependency-drop-list' )
. dropdown ( {
apiSettings : {
url : issueSearchUrl ,
onResponse ( response ) {
const filteredResponse = { success : true , results : [ ] } ;
const currIssueId = $ ( '#new-dependency-drop-list' ) . data ( 'issue-id' ) ;
// Parse the response from the api to work with our dropdown
$ . each ( response , ( _i , issue ) => {
// Don't list current issue in the dependency list.
if ( issue . id === currIssueId ) {
return ;
}
filteredResponse . results . push ( {
name : ` # ${ issue . number } ${ htmlEscape ( issue . title )
} < div class = "text small dont-break-out" > $ { htmlEscape ( issue . repository . full _name ) } < / d i v > ` ,
value : issue . id ,
} ) ;
} ) ;
return filteredResponse ;
} ,
cache : false ,
} ,
fullTextSearch : true ,
} ) ;
function excludeLabel ( item ) {
const href = $ ( item ) . attr ( 'href' ) ;
const id = $ ( item ) . data ( 'label-id' ) ;
const regStr = ` labels=((?:-?[0-9]+%2c)*)( ${ id } )((?:%2c-?[0-9]+)*)& ` ;
const newStr = 'labels=$1-$2$3&' ;
window . location = href . replace ( new RegExp ( regStr ) , newStr ) ;
}
$ ( '.menu a.label-filter-item' ) . each ( function ( ) {
$ ( this ) . on ( 'click' , function ( e ) {
if ( e . altKey ) {
e . preventDefault ( ) ;
excludeLabel ( this ) ;
}
} ) ;
} ) ;
$ ( '.menu .ui.dropdown.label-filter' ) . on ( 'keydown' , ( e ) => {
if ( e . altKey && e . keyCode === 13 ) {
const selectedItems = $ ( '.menu .ui.dropdown.label-filter .menu .item.selected' ) ;
if ( selectedItems . length > 0 ) {
excludeLabel ( $ ( selectedItems [ 0 ] ) ) ;
}
}
} ) ;
}
export function initRepoIssueCommentDelete ( ) {
// Delete comment
$ ( document ) . on ( 'click' , '.delete-comment' , function ( ) {
const $this = $ ( this ) ;
if ( window . confirm ( $this . data ( 'locale' ) ) ) {
$ . post ( $this . data ( 'url' ) , {
2021-10-21 10:37:43 +03:00
_csrf : csrfToken ,
2021-10-16 20:28:04 +03:00
} ) . done ( ( ) => {
const $conversationHolder = $this . closest ( '.conversation-holder' ) ;
2022-05-07 08:35:12 +03:00
// Check if this was a pending comment.
if ( $conversationHolder . find ( '.pending-label' ) . length ) {
const $counter = $ ( '#review-box .review-comments-counter' ) ;
let num = parseInt ( $counter . attr ( 'data-pending-comment-number' ) ) - 1 || 0 ;
num = Math . max ( num , 0 ) ;
$counter . attr ( 'data-pending-comment-number' , num ) ;
$counter . text ( num ) ;
}
2021-10-16 20:28:04 +03:00
$ ( ` # ${ $this . data ( 'comment-id' ) } ` ) . remove ( ) ;
if ( $conversationHolder . length && ! $conversationHolder . find ( '.comment' ) . length ) {
const path = $conversationHolder . data ( 'path' ) ;
const side = $conversationHolder . data ( 'side' ) ;
const idx = $conversationHolder . data ( 'idx' ) ;
const lineType = $conversationHolder . closest ( 'tr' ) . data ( 'line-type' ) ;
if ( lineType === 'same' ) {
2021-11-19 05:28:27 +03:00
$ ( ` [data-path=" ${ path } "] a.add-code-comment[data-idx=" ${ idx } "] ` ) . removeClass ( 'invisible' ) ;
2021-10-16 20:28:04 +03:00
} else {
2021-11-19 05:28:27 +03:00
$ ( ` [data-path=" ${ path } "] a.add-code-comment[data-side=" ${ side } "][data-idx=" ${ idx } "] ` ) . removeClass ( 'invisible' ) ;
2021-10-16 20:28:04 +03:00
}
$conversationHolder . remove ( ) ;
}
} ) ;
}
return false ;
} ) ;
}
export function initRepoIssueDependencyDelete ( ) {
// Delete Issue dependency
$ ( document ) . on ( 'click' , '.delete-dependency-button' , ( e ) => {
2021-11-22 11:19:01 +03:00
const id = e . currentTarget . getAttribute ( 'data-id' ) ;
const type = e . currentTarget . getAttribute ( 'data-type' ) ;
2021-10-16 20:28:04 +03:00
$ ( '.remove-dependency' ) . modal ( {
closable : false ,
duration : 200 ,
onApprove : ( ) => {
$ ( '#removeDependencyID' ) . val ( id ) ;
$ ( '#dependencyType' ) . val ( type ) ;
$ ( '#removeDependencyForm' ) . trigger ( 'submit' ) ;
} ,
} ) . modal ( 'show' ) ;
} ) ;
}
export function initRepoIssueCodeCommentCancel ( ) {
// Cancel inline code comment
$ ( document ) . on ( 'click' , '.cancel-code-comment' , ( e ) => {
const form = $ ( e . currentTarget ) . closest ( 'form' ) ;
if ( form . length > 0 && form . hasClass ( 'comment-form' ) ) {
form . addClass ( 'hide' ) ;
form . closest ( '.comment-code-cloud' ) . find ( 'button.comment-form-reply' ) . show ( ) ;
} else {
form . closest ( '.comment-code-cloud' ) . remove ( ) ;
}
} ) ;
}
export function initRepoIssueStatusButton ( ) {
// Change status
const $statusButton = $ ( '#status-button' ) ;
$ ( '#comment-form textarea' ) . on ( 'keyup' , function ( ) {
2021-12-10 05:51:27 +03:00
const easyMDE = getAttachedEasyMDE ( this ) ;
const value = easyMDE ? . value ( ) || $ ( this ) . val ( ) ;
2021-10-16 20:28:04 +03:00
$statusButton . text ( $statusButton . data ( value . length === 0 ? 'status' : 'status-and-comment' ) ) ;
} ) ;
$statusButton . on ( 'click' , ( ) => {
$ ( '#status' ) . val ( $statusButton . data ( 'status-val' ) ) ;
$ ( '#comment-form' ) . trigger ( 'submit' ) ;
} ) ;
}
export function initRepoPullRequestUpdate ( ) {
// Pull Request update button
const $pullUpdateButton = $ ( '.update-button > button' ) ;
$pullUpdateButton . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
const $this = $ ( this ) ;
const redirect = $this . data ( 'redirect' ) ;
$this . addClass ( 'loading' ) ;
$ . post ( $this . data ( 'do' ) , {
2021-10-21 10:37:43 +03:00
_csrf : csrfToken
2021-10-16 20:28:04 +03:00
} ) . done ( ( data ) => {
if ( data . redirect ) {
window . location . href = data . redirect ;
} else if ( redirect ) {
window . location . href = redirect ;
} else {
window . location . reload ( ) ;
}
} ) ;
} ) ;
$ ( '.update-button > .dropdown' ) . dropdown ( {
onChange ( _text , _value , $choice ) {
const $url = $choice . data ( 'do' ) ;
if ( $url ) {
$pullUpdateButton . find ( '.button-text' ) . text ( $choice . text ( ) ) ;
$pullUpdateButton . data ( 'do' , $url ) ;
}
}
} ) ;
}
export function initRepoPullRequestMergeInstruction ( ) {
$ ( '.show-instruction' ) . on ( 'click' , ( ) => {
$ ( '.instruct-content' ) . toggle ( ) ;
} ) ;
}
2022-04-28 18:45:33 +03:00
export function initRepoPullRequestAllowMaintainerEdit ( ) {
const $checkbox = $ ( '#allow-edits-from-maintainers' ) ;
if ( ! $checkbox . length ) return ;
const promptTip = $checkbox . attr ( 'data-prompt-tip' ) ;
const promptError = $checkbox . attr ( 'data-prompt-error' ) ;
$checkbox . popup ( { content : promptTip } ) ;
$checkbox . checkbox ( {
'onChange' : ( ) => {
const checked = $checkbox . checkbox ( 'is checked' ) ;
let url = $checkbox . attr ( 'data-url' ) ;
url += '/set_allow_maintainer_edit' ;
$checkbox . checkbox ( 'set disabled' ) ;
$ . ajax ( { url , type : 'POST' ,
data : { _csrf : csrfToken , allow _maintainer _edit : checked } ,
error : ( ) => {
$checkbox . popup ( {
content : promptError ,
onHidden : ( ) => {
// the error popup should be shown only once, then we restore the popup to the default message
$checkbox . popup ( { content : promptTip } ) ;
} ,
} ) ;
$checkbox . popup ( 'show' ) ;
} ,
complete : ( ) => {
$checkbox . checkbox ( 'set enabled' ) ;
} ,
} ) ;
} ,
} ) ;
}
2021-10-16 20:28:04 +03:00
export function initRepoIssueReferenceRepositorySearch ( ) {
$ ( '.issue_reference_repository_search' )
. dropdown ( {
apiSettings : {
2022-04-07 21:59:56 +03:00
url : ` ${ appSubUrl } /repo/search?q={query}&limit=20 ` ,
2021-10-16 20:28:04 +03:00
onResponse ( response ) {
const filteredResponse = { success : true , results : [ ] } ;
$ . each ( response . data , ( _r , repo ) => {
filteredResponse . results . push ( {
name : htmlEscape ( repo . full _name ) ,
value : repo . full _name
} ) ;
} ) ;
return filteredResponse ;
} ,
cache : false ,
} ,
onChange ( _value , _text , $choice ) {
const $form = $choice . closest ( 'form' ) ;
2021-10-21 10:37:43 +03:00
$form . attr ( 'action' , ` ${ appSubUrl } / ${ _text } /issues/new ` ) ;
2021-10-16 20:28:04 +03:00
} ,
fullTextSearch : true
} ) ;
}
export function initRepoIssueWipTitle ( ) {
$ ( '.title_wip_desc > a' ) . on ( 'click' , ( e ) => {
e . preventDefault ( ) ;
const $issueTitle = $ ( '#issue_title' ) ;
$issueTitle . focus ( ) ;
const value = $issueTitle . val ( ) . trim ( ) . toUpperCase ( ) ;
const wipPrefixes = $ ( '.title_wip_desc' ) . data ( 'wip-prefixes' ) ;
for ( const prefix of wipPrefixes ) {
if ( value . startsWith ( prefix . toUpperCase ( ) ) ) {
return ;
}
}
$issueTitle . val ( ` ${ wipPrefixes [ 0 ] } ${ $issueTitle . val ( ) } ` ) ;
} ) ;
}
2021-11-16 05:21:13 +03:00
export async function updateIssuesMeta ( url , action , issueIds , elementId ) {
return $ . ajax ( {
type : 'POST' ,
url ,
data : {
_csrf : csrfToken ,
action ,
issue _ids : issueIds ,
id : elementId ,
} ,
2021-10-16 20:28:04 +03:00
} ) ;
}
export function initRepoIssueComments ( ) {
if ( $ ( '.repository.view.issue .timeline' ) . length === 0 ) return ;
2021-11-22 11:19:01 +03:00
$ ( '.re-request-review' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
2021-10-16 20:28:04 +03:00
const url = $ ( this ) . data ( 'update-url' ) ;
const issueId = $ ( this ) . data ( 'issue-id' ) ;
const id = $ ( this ) . data ( 'id' ) ;
const isChecked = $ ( this ) . hasClass ( 'checked' ) ;
updateIssuesMeta (
url ,
isChecked ? 'detach' : 'attach' ,
issueId ,
id ,
2021-12-04 09:43:14 +03:00
) . then ( ( ) => window . location . reload ( ) ) ;
2021-10-16 20:28:04 +03:00
} ) ;
$ ( '.dismiss-review-btn' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
const $this = $ ( this ) ;
const $dismissReviewModal = $this . next ( ) ;
$dismissReviewModal . modal ( 'show' ) ;
} ) ;
$ ( document ) . on ( 'click' , ( event ) => {
const urlTarget = $ ( ':target' ) ;
if ( urlTarget . length === 0 ) return ;
const urlTargetId = urlTarget . attr ( 'id' ) ;
if ( ! urlTargetId ) return ;
if ( ! /^(issue|pull)(comment)?-\d+$/ . test ( urlTargetId ) ) return ;
const $target = $ ( event . target ) ;
if ( $target . closest ( ` # ${ urlTargetId } ` ) . length === 0 ) {
const scrollPosition = $ ( window ) . scrollTop ( ) ;
window . location . hash = '' ;
$ ( window ) . scrollTop ( scrollPosition ) ;
window . history . pushState ( null , null , ' ' ) ;
}
} ) ;
}
function assignMenuAttributes ( menu ) {
const id = Math . floor ( Math . random ( ) * Math . floor ( 1000000 ) ) ;
menu . attr ( 'data-write' , menu . attr ( 'data-write' ) + id ) ;
menu . attr ( 'data-preview' , menu . attr ( 'data-preview' ) + id ) ;
menu . find ( '.item' ) . each ( function ( ) {
const tab = $ ( this ) . attr ( 'data-tab' ) + id ;
$ ( this ) . attr ( 'data-tab' , tab ) ;
} ) ;
menu . parent ( ) . find ( "*[data-tab='write']" ) . attr ( 'data-tab' , ` write ${ id } ` ) ;
menu . parent ( ) . find ( "*[data-tab='preview']" ) . attr ( 'data-tab' , ` preview ${ id } ` ) ;
initCompMarkupContentPreviewTab ( menu . parent ( '.form' ) ) ;
return id ;
}
export function initRepoPullRequestReview ( ) {
if ( window . location . hash && window . location . hash . startsWith ( '#issuecomment-' ) ) {
const commentDiv = $ ( window . location . hash ) ;
if ( commentDiv ) {
// get the name of the parent id
const groupID = commentDiv . closest ( 'div[id^="code-comments-"]' ) . attr ( 'id' ) ;
if ( groupID && groupID . startsWith ( 'code-comments-' ) ) {
2022-02-18 09:50:36 +03:00
const id = groupID . slice ( 14 ) ;
2021-10-16 20:28:04 +03:00
$ ( ` #show-outdated- ${ id } ` ) . addClass ( 'hide' ) ;
$ ( ` #code-comments- ${ id } ` ) . removeClass ( 'hide' ) ;
$ ( ` #code-preview- ${ id } ` ) . removeClass ( 'hide' ) ;
$ ( ` #hide-outdated- ${ id } ` ) . removeClass ( 'hide' ) ;
commentDiv [ 0 ] . scrollIntoView ( ) ;
}
}
}
$ ( document ) . on ( 'click' , '.show-outdated' , function ( e ) {
e . preventDefault ( ) ;
const id = $ ( this ) . data ( 'comment' ) ;
$ ( this ) . addClass ( 'hide' ) ;
$ ( ` #code-comments- ${ id } ` ) . removeClass ( 'hide' ) ;
$ ( ` #code-preview- ${ id } ` ) . removeClass ( 'hide' ) ;
$ ( ` #hide-outdated- ${ id } ` ) . removeClass ( 'hide' ) ;
} ) ;
$ ( document ) . on ( 'click' , '.hide-outdated' , function ( e ) {
e . preventDefault ( ) ;
const id = $ ( this ) . data ( 'comment' ) ;
$ ( this ) . addClass ( 'hide' ) ;
$ ( ` #code-comments- ${ id } ` ) . addClass ( 'hide' ) ;
$ ( ` #code-preview- ${ id } ` ) . addClass ( 'hide' ) ;
$ ( ` #show-outdated- ${ id } ` ) . removeClass ( 'hide' ) ;
} ) ;
2022-01-05 15:17:25 +03:00
$ ( document ) . on ( 'click' , 'button.comment-form-reply' , async function ( e ) {
2021-10-16 20:28:04 +03:00
e . preventDefault ( ) ;
2022-01-05 15:17:25 +03:00
2021-10-16 20:28:04 +03:00
$ ( this ) . hide ( ) ;
const form = $ ( this ) . closest ( '.comment-code-cloud' ) . find ( '.comment-form' ) ;
form . removeClass ( 'hide' ) ;
const $textarea = form . find ( 'textarea' ) ;
2021-12-10 05:51:27 +03:00
let easyMDE = getAttachedEasyMDE ( $textarea ) ;
if ( ! easyMDE ) {
2022-01-05 15:17:25 +03:00
await attachTribute ( $textarea . get ( ) , { mentions : true , emoji : true } ) ;
easyMDE = await createCommentEasyMDE ( $textarea ) ;
2021-10-16 20:28:04 +03:00
}
$textarea . focus ( ) ;
2021-12-10 05:51:27 +03:00
easyMDE . codemirror . focus ( ) ;
2021-10-16 20:28:04 +03:00
assignMenuAttributes ( form . find ( '.menu' ) ) ;
} ) ;
const $reviewBox = $ ( '.review-box' ) ;
if ( $reviewBox . length === 1 ) {
2022-01-08 16:33:32 +03:00
( async ( ) => {
2022-01-18 19:57:57 +03:00
// the editor's height is too large in some cases, and the panel cannot be scrolled with page now because there is `.repository .diff-detail-box.sticky { position: sticky; }`
// the temporary solution is to make the editor's height smaller (about 4 lines). GitHub also only show 4 lines for default. We can improve the UI (including Dropzone area) in future
2022-03-04 23:57:26 +03:00
// EasyMDE's options can not handle minHeight & maxHeight together correctly, we have to set max-height for .CodeMirror-scroll in CSS.
await createCommentEasyMDE ( $reviewBox . find ( 'textarea' ) , { minHeight : '80px' } ) ;
2022-01-08 16:33:32 +03:00
initCompImagePaste ( $reviewBox ) ;
} ) ( ) ;
2021-10-16 20:28:04 +03:00
}
// The following part is only for diff views
if ( $ ( '.repository.pull.diff' ) . length === 0 ) {
return ;
}
$ ( '.btn-review' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
$ ( this ) . closest ( '.dropdown' ) . find ( '.menu' ) . toggle ( 'visible' ) ;
} ) . closest ( '.dropdown' ) . find ( '.close' ) . on ( 'click' , function ( e ) {
e . preventDefault ( ) ;
$ ( this ) . closest ( '.menu' ) . toggle ( 'visible' ) ;
} ) ;
2021-11-20 15:12:59 +03:00
$ ( document ) . on ( 'click' , 'a.add-code-comment' , async function ( e ) {
2021-10-16 20:28:04 +03:00
if ( $ ( e . target ) . hasClass ( 'btn-add-single' ) ) return ; // https://github.com/go-gitea/gitea/issues/4745
e . preventDefault ( ) ;
const isSplit = $ ( this ) . closest ( '.code-diff' ) . hasClass ( 'code-diff-split' ) ;
const side = $ ( this ) . data ( 'side' ) ;
const idx = $ ( this ) . data ( 'idx' ) ;
2021-11-19 05:28:27 +03:00
const path = $ ( this ) . closest ( '[data-path]' ) . data ( 'path' ) ;
2021-10-16 20:28:04 +03:00
const tr = $ ( this ) . closest ( 'tr' ) ;
const lineType = tr . data ( 'line-type' ) ;
let ntr = tr . next ( ) ;
if ( ! ntr . hasClass ( 'add-comment' ) ) {
ntr = $ ( `
< tr class = "add-comment" data - line - type = "${lineType}" >
$ { isSplit ? `
< td class = "lines-num" > < / t d >
2022-01-25 15:18:30 +03:00
< td class = "lines-escape" > < / t d >
2021-10-16 20:28:04 +03:00
< td class = "lines-type-marker" > < / t d >
< td class = "add-comment-left" > < / t d >
< td class = "lines-num" > < / t d >
2022-01-25 15:18:30 +03:00
< td class = "lines-escape" > < / t d >
2021-10-16 20:28:04 +03:00
< td class = "lines-type-marker" > < / t d >
< td class = "add-comment-right" > < / t d >
` : `
2022-02-01 22:08:33 +03:00
< td class = "lines-num" > < / t d >
< td class = "lines-num" > < / t d >
< td class = "lines-escape" > < / t d >
2021-10-16 20:28:04 +03:00
< td class = "add-comment-left add-comment-right" colspan = "2" > < / t d >
` }
< / t r > ` ) ;
tr . after ( ntr ) ;
}
const td = ntr . find ( ` .add-comment- ${ side } ` ) ;
let commentCloud = td . find ( '.comment-code-cloud' ) ;
if ( commentCloud . length === 0 && ! ntr . find ( 'button[name="is_review"]' ) . length ) {
2021-11-14 08:28:57 +03:00
const data = await $ . get ( $ ( this ) . closest ( '[data-new-comment-url]' ) . data ( 'new-comment-url' ) ) ;
2021-10-16 20:28:04 +03:00
td . html ( data ) ;
commentCloud = td . find ( '.comment-code-cloud' ) ;
assignMenuAttributes ( commentCloud . find ( '.menu' ) ) ;
td . find ( "input[name='line']" ) . val ( idx ) ;
td . find ( "input[name='side']" ) . val ( side === 'left' ? 'previous' : 'proposed' ) ;
td . find ( "input[name='path']" ) . val ( path ) ;
const $textarea = commentCloud . find ( 'textarea' ) ;
2022-01-05 15:17:25 +03:00
await attachTribute ( $textarea . get ( ) , { mentions : true , emoji : true } ) ;
const easyMDE = await createCommentEasyMDE ( $textarea ) ;
2021-10-16 20:28:04 +03:00
$textarea . focus ( ) ;
2021-12-10 05:51:27 +03:00
easyMDE . codemirror . focus ( ) ;
2021-10-16 20:28:04 +03:00
}
} ) ;
}
export function initRepoIssueReferenceIssue ( ) {
// Reference issue
$ ( document ) . on ( 'click' , '.reference-issue' , function ( event ) {
const $this = $ ( this ) ;
$this . closest ( '.dropdown' ) . find ( '.menu' ) . toggle ( 'visible' ) ;
const content = $ ( ` #comment- ${ $this . data ( 'target' ) } ` ) . text ( ) ;
const poster = $this . data ( 'poster-username' ) ;
const reference = $this . data ( 'reference' ) ;
2022-01-16 14:19:26 +03:00
const $modal = $ ( $this . data ( 'modal' ) ) ;
2021-10-16 20:28:04 +03:00
$modal . find ( 'textarea[name="content"]' ) . val ( ` ${ content } \n \n _Originally posted by @ ${ poster } in ${ reference } _ ` ) ;
$modal . modal ( 'show' ) ;
event . preventDefault ( ) ;
} ) ;
}
export function initRepoIssueWipToggle ( ) {
// Toggle WIP
$ ( '.toggle-wip a, .toggle-wip button' ) . on ( 'click' , async ( e ) => {
e . preventDefault ( ) ;
2021-11-22 11:19:01 +03:00
const toggleWip = e . currentTarget . closest ( '.toggle-wip' ) ;
const title = toggleWip . getAttribute ( 'data-title' ) ;
const wipPrefix = toggleWip . getAttribute ( 'data-wip-prefix' ) ;
const updateUrl = toggleWip . getAttribute ( 'data-update-url' ) ;
2021-10-16 20:28:04 +03:00
await $ . post ( updateUrl , {
2021-10-21 10:37:43 +03:00
_csrf : csrfToken ,
2022-02-18 09:50:36 +03:00
title : title ? . startsWith ( wipPrefix ) ? title . slice ( wipPrefix . length ) . trim ( ) : ` ${ wipPrefix . trim ( ) } ${ title } ` ,
2021-10-16 20:28:04 +03:00
} ) ;
window . location . reload ( ) ;
} ) ;
}
export function initRepoIssueTitleEdit ( ) {
// Edit issue title
const $issueTitle = $ ( '#issue-title' ) ;
const $editInput = $ ( '#edit-title-input input' ) ;
const editTitleToggle = function ( ) {
$issueTitle . toggle ( ) ;
$ ( '.not-in-edit' ) . toggle ( ) ;
$ ( '#edit-title-input' ) . toggle ( ) ;
$ ( '#pull-desc' ) . toggle ( ) ;
$ ( '#pull-desc-edit' ) . toggle ( ) ;
$ ( '.in-edit' ) . toggle ( ) ;
$ ( '#issue-title-wrapper' ) . toggleClass ( 'edit-active' ) ;
$editInput . focus ( ) ;
return false ;
} ;
$ ( '#edit-title' ) . on ( 'click' , editTitleToggle ) ;
$ ( '#cancel-edit-title' ) . on ( 'click' , editTitleToggle ) ;
$ ( '#save-edit-title' ) . on ( 'click' , editTitleToggle ) . on ( 'click' , function ( ) {
const pullrequest _targetbranch _change = function ( update _url ) {
const targetBranch = $ ( '#pull-target-branch' ) . data ( 'branch' ) ;
const $branchTarget = $ ( '#branch_target' ) ;
if ( targetBranch === $branchTarget . text ( ) ) {
return false ;
}
$ . post ( update _url , {
2021-10-21 10:37:43 +03:00
_csrf : csrfToken ,
2021-10-16 20:28:04 +03:00
target _branch : targetBranch
} ) . done ( ( data ) => {
$branchTarget . text ( data . base _branch ) ;
} ) . always ( ( ) => {
window . location . reload ( ) ;
} ) ;
} ;
const pullrequest _target _update _url = $ ( this ) . data ( 'target-update-url' ) ;
if ( $editInput . val ( ) . length === 0 || $editInput . val ( ) === $issueTitle . text ( ) ) {
$editInput . val ( $issueTitle . text ( ) ) ;
pullrequest _targetbranch _change ( pullrequest _target _update _url ) ;
} else {
$ . post ( $ ( this ) . data ( 'update-url' ) , {
2021-10-21 10:37:43 +03:00
_csrf : csrfToken ,
2021-10-16 20:28:04 +03:00
title : $editInput . val ( )
} , ( data ) => {
$editInput . val ( data . title ) ;
$issueTitle . text ( data . title ) ;
pullrequest _targetbranch _change ( pullrequest _target _update _url ) ;
window . location . reload ( ) ;
} ) ;
}
return false ;
} ) ;
}
export function initRepoIssueBranchSelect ( ) {
const changeBranchSelect = function ( ) {
const selectionTextField = $ ( '#pull-target-branch' ) ;
const baseName = selectionTextField . data ( 'basename' ) ;
const branchNameNew = $ ( this ) . data ( 'branch' ) ;
const branchNameOld = selectionTextField . data ( 'branch' ) ;
// Replace branch name to keep translation from HTML template
selectionTextField . html ( selectionTextField . html ( ) . replace (
` ${ baseName } : ${ branchNameOld } ` ,
` ${ baseName } : ${ branchNameNew } `
) ) ;
selectionTextField . data ( 'branch' , branchNameNew ) ; // update branch name in setting
} ;
$ ( '#branch-select > .item' ) . on ( 'click' , changeBranchSelect ) ;
}