2020-07-12 11:10:56 +02:00
import { svg } from '../svg.js' ;
2020-02-11 19:53:18 -06:00
2020-03-11 20:34:54 +01:00
const { AppSubUrl } = window . config ;
2020-02-11 19:53:18 -06:00
export default function initContextPopups ( ) {
2020-01-19 22:39:21 -06:00
const refIssues = $ ( '.ref-issue' ) ;
if ( ! refIssues . length ) return ;
refIssues . each ( function ( ) {
const [ index , _issues , repo , owner ] = $ ( this ) . attr ( 'href' ) . replace ( /[#?].*$/ , '' ) . split ( '/' ) . reverse ( ) ;
2020-02-11 19:53:18 -06:00
issuePopup ( owner , repo , index , $ ( this ) ) ;
2020-01-19 22:39:21 -06:00
} ) ;
}
2020-02-11 19:53:18 -06:00
function issuePopup ( owner , repo , index , $element ) {
$ . get ( ` ${ AppSubUrl } /api/v1/repos/ ${ owner } / ${ repo } /issues/ ${ index } ` , ( issue ) => {
2020-03-11 20:34:54 +01:00
const createdAt = new Date ( issue . created _at ) . toLocaleDateString ( undefined , { year : 'numeric' , month : 'short' , day : 'numeric' } ) ;
2020-01-19 22:39:21 -06:00
let body = issue . body . replace ( /\n+/g , ' ' ) ;
if ( body . length > 85 ) {
body = ` ${ body . substring ( 0 , 85 ) } ... ` ;
}
let labels = '' ;
for ( let i = 0 ; i < issue . labels . length ; i ++ ) {
const label = issue . labels [ i ] ;
const red = parseInt ( label . color . substring ( 0 , 2 ) , 16 ) ;
const green = parseInt ( label . color . substring ( 2 , 4 ) , 16 ) ;
const blue = parseInt ( label . color . substring ( 4 , 6 ) , 16 ) ;
let color = '#ffffff' ;
if ( ( red * 0.299 + green * 0.587 + blue * 0.114 ) > 125 ) {
color = '#000000' ;
}
2020-04-28 14:05:39 -04:00
labels += ` <div class="ui label" style="color: ${ color } ; background-color:# ${ label . color } ;"> ${ label . name } </div> ` ;
2020-01-19 22:39:21 -06:00
}
if ( labels . length > 0 ) {
labels = ` <p> ${ labels } </p> ` ;
}
2020-02-11 19:53:18 -06:00
let octicon , color ;
2020-01-19 22:39:21 -06:00
if ( issue . pull _request !== null ) {
if ( issue . state === 'open' ) {
2020-02-11 19:53:18 -06:00
color = 'green' ;
octicon = 'octicon-git-pull-request' ; // Open PR
2020-01-19 22:39:21 -06:00
} else if ( issue . pull _request . merged === true ) {
2020-02-11 19:53:18 -06:00
color = 'purple' ;
octicon = 'octicon-git-merge' ; // Merged PR
2020-01-19 22:39:21 -06:00
} else {
2020-02-11 19:53:18 -06:00
color = 'red' ;
octicon = 'octicon-git-pull-request' ; // Closed PR
2020-01-19 22:39:21 -06:00
}
} else if ( issue . state === 'open' ) {
2020-02-11 19:53:18 -06:00
color = 'green' ;
octicon = 'octicon-issue-opened' ; // Open Issue
2020-01-19 22:39:21 -06:00
} else {
2020-02-11 19:53:18 -06:00
color = 'red' ;
octicon = 'octicon-issue-closed' ; // Closed Issue
2020-01-19 22:39:21 -06:00
}
$element . popup ( {
variation : 'wide' ,
delay : {
show : 250
} ,
html : `
< div >
< p > < small > $ { issue . repository . full _name } on $ { createdAt } < / s m a l l > < / p >
2020-09-11 22:19:00 +02:00
< p > < span class = "${color}" > $ { svg ( octicon ) } < / s p a n > < s t r o n g > $ { i s s u e . t i t l e } < / s t r o n g > # $ { i n d e x } < / p >
2020-01-19 22:39:21 -06:00
< p > $ { body } < / p >
$ { labels }
< / d i v >
`
} ) ;
} ) ;
}