2014-03-06 18:55:32 +04:00
var Gogits = {
"PageIsSignup" : false
} ;
2014-03-02 17:47:55 +04:00
2014-03-24 17:27:19 +04:00
( function ( $ ) {
2014-03-22 21:44:02 +04:00
// extend jQuery ajax, set csrf token value
var ajax = $ . ajax ;
$ . extend ( {
2014-03-24 17:27:19 +04:00
ajax : function ( url , options ) {
2014-03-22 21:44:02 +04:00
if ( typeof url === 'object' ) {
options = url ;
url = undefined ;
}
options = options || { } ;
url = options . url ;
var csrftoken = $ ( 'meta[name=_csrf]' ) . attr ( 'content' ) ;
var headers = options . headers || { } ;
var domain = document . domain . replace ( /\./ig , '\\.' ) ;
if ( ! /^(http:|https:).*/ . test ( url ) || eval ( '/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/' ) . test ( url ) ) {
2014-03-24 17:27:19 +04:00
headers = $ . extend ( headers , { 'X-Csrf-Token' : csrftoken } ) ;
2014-03-22 21:44:02 +04:00
}
options . headers = headers ;
var callback = options . success ;
2014-03-24 17:27:19 +04:00
options . success = function ( data ) {
if ( data . once ) {
2014-03-22 21:44:02 +04:00
// change all _once value if ajax data.once exist
$ ( '[name=_once]' ) . val ( data . once ) ;
}
2014-03-24 17:27:19 +04:00
if ( callback ) {
2014-03-22 21:44:02 +04:00
callback . apply ( this , arguments ) ;
}
} ;
return ajax ( url , options ) ;
2014-03-23 18:14:29 +04:00
} ,
2014-03-24 17:27:19 +04:00
changeHash : function ( hash ) {
if ( history . pushState ) {
2014-03-23 18:14:29 +04:00
history . pushState ( null , null , hash ) ;
}
else {
location . hash = hash ;
}
} ,
2014-03-24 17:27:19 +04:00
deSelect : function ( ) {
if ( window . getSelection ) {
2014-03-23 18:14:29 +04:00
window . getSelection ( ) . removeAllRanges ( ) ;
} else {
document . selection . empty ( ) ;
}
2014-03-22 21:44:02 +04:00
}
} ) ;
} ( jQuery ) ) ;
2014-03-06 18:55:32 +04:00
( function ( $ ) {
2014-03-10 12:54:52 +04:00
2014-03-02 17:47:55 +04:00
Gogits . showTab = function ( selector , index ) {
if ( ! index ) {
index = 0 ;
}
$ ( selector ) . tab ( "show" ) ;
$ ( selector ) . find ( "li:eq(" + index + ") a" ) . tab ( "show" ) ;
2014-03-06 18:55:32 +04:00
} ;
Gogits . validateForm = function ( selector , options ) {
var $form = $ ( selector ) ;
options = options || { } ;
options . showErrors = function ( map , list ) {
var $error = $form . find ( '.form-error' ) . addClass ( 'hidden' ) ;
$ ( '.has-error' ) . removeClass ( "has-error" ) ;
$error . text ( list [ 0 ] . message ) . show ( ) . removeClass ( "hidden" ) ;
$ ( list [ 0 ] . element ) . parents ( ".form-group" ) . addClass ( "has-error" ) ;
} ;
$form . validate ( options ) ;
} ;
2014-03-10 12:54:52 +04:00
// ----- init elements
Gogits . initModals = function ( ) {
var modals = $ ( "[data-toggle=modal]" ) ;
if ( modals . length < 1 ) {
return ;
}
$ . each ( modals , function ( i , item ) {
2014-03-10 17:12:49 +04:00
var hide = $ ( item ) . data ( 'modal' ) ;
$ ( item ) . modal ( hide ? hide : "hide" ) ;
2014-03-10 12:54:52 +04:00
} ) ;
} ;
Gogits . initTooltips = function ( ) {
$ ( "body" ) . tooltip ( {
selector : "[data-toggle=tooltip]"
//container: "body"
} ) ;
} ;
2014-03-17 14:13:07 +04:00
Gogits . initPopovers = function ( ) {
2014-03-20 16:12:31 +04:00
var hideAllPopovers = function ( ) {
$ ( '[data-toggle=popover]' ) . each ( function ( ) {
2014-03-17 14:13:07 +04:00
$ ( this ) . popover ( 'hide' ) ;
2014-03-20 16:12:31 +04:00
} ) ;
2014-03-17 14:13:07 +04:00
} ;
2014-03-20 16:12:31 +04:00
$ ( document ) . on ( 'click' , function ( e ) {
2014-03-17 14:13:07 +04:00
var $e = $ ( e . target ) ;
2014-03-20 16:12:31 +04:00
if ( $e . data ( 'toggle' ) == 'popover' || $e . parents ( "[data-toggle=popover], .popover" ) . length > 0 ) {
2014-03-17 14:13:07 +04:00
return ;
}
hideAllPopovers ( ) ;
} ) ;
$ ( "body" ) . popover ( {
selector : "[data-toggle=popover]"
} ) ;
} ;
2014-03-10 17:12:49 +04:00
Gogits . initTabs = function ( ) {
2014-03-13 10:08:49 +04:00
var $tabs = $ ( '[data-init=tabs]' ) ;
2014-03-25 17:14:05 +04:00
$tabs . tab ( "show" ) ;
2014-03-13 10:08:49 +04:00
$tabs . find ( "li:eq(0) a" ) . tab ( "show" ) ;
2014-03-17 11:17:44 +04:00
} ;
2014-03-21 19:27:33 +04:00
// fix dropdown inside click
2014-03-24 17:27:19 +04:00
Gogits . initDropDown = function ( ) {
$ ( '.dropdown-menu.no-propagation' ) . on ( 'click' , function ( e ) {
2014-03-21 19:27:33 +04:00
e . stopPropagation ( ) ;
} ) ;
} ;
2014-03-17 11:17:44 +04:00
// render markdown
Gogits . renderMarkdown = function ( ) {
2014-03-20 13:25:48 +04:00
var $md = $ ( '.markdown' ) ;
var $pre = $md . find ( 'pre > code' ) . parent ( ) ;
2014-03-20 19:55:27 +04:00
$pre . addClass ( 'prettyprint linenums' ) ;
2014-03-17 11:17:44 +04:00
prettyPrint ( ) ;
2014-03-20 13:25:48 +04:00
// Set anchor.
var headers = { } ;
$md . find ( 'h1, h2, h3, h4, h5, h6' ) . each ( function ( ) {
var node = $ ( this ) ;
var val = encodeURIComponent ( node . text ( ) . toLowerCase ( ) . replace ( /[^\w\- ]/g , '' ) . replace ( /[ ]/g , '-' ) ) ;
var name = val ;
2014-03-20 17:32:08 +04:00
if ( headers [ val ] > 0 ) {
2014-03-20 13:25:48 +04:00
name = val + '-' + headers [ val ] ;
}
2014-03-20 17:32:08 +04:00
if ( headers [ val ] == undefined ) {
2014-03-20 13:25:48 +04:00
headers [ val ] = 1 ;
2014-03-20 17:32:08 +04:00
} else {
2014-03-20 13:25:48 +04:00
headers [ val ] += 1 ;
}
node = node . wrap ( '<div id="' + name + '" class="anchor-wrap" ></div>' ) ;
node . append ( '<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>' ) ;
} ) ;
2014-03-24 17:27:19 +04:00
} ;
2014-03-17 11:17:44 +04:00
2014-03-23 16:58:12 +04:00
Gogits . renderCodeView = function ( ) {
2014-03-24 17:27:19 +04:00
function selectRange ( $list , $select , $from ) {
2014-03-23 18:14:29 +04:00
$list . removeClass ( 'active' ) ;
2014-03-24 17:27:19 +04:00
if ( $from ) {
2014-03-23 18:14:29 +04:00
var a = parseInt ( $select . attr ( 'rel' ) . substr ( 1 ) ) ;
var b = parseInt ( $from . attr ( 'rel' ) . substr ( 1 ) ) ;
var c ;
2014-03-24 17:27:19 +04:00
if ( a != b ) {
if ( a > b ) {
2014-03-23 18:14:29 +04:00
c = a ;
a = b ;
b = c ;
}
var classes = [ ] ;
2014-03-24 17:27:19 +04:00
for ( i = a ; i <= b ; i ++ ) {
classes . push ( '.L' + i ) ;
2014-03-23 18:14:29 +04:00
}
$list . filter ( classes . join ( ',' ) ) . addClass ( 'active' ) ;
$ . changeHash ( '#L' + a + '-' + 'L' + b ) ;
return
}
}
$select . addClass ( 'active' ) ;
$ . changeHash ( '#' + $select . attr ( 'rel' ) ) ;
}
$ ( document ) . on ( 'click' , '.lines-num span' , function ( e ) {
var $select = $ ( this ) ;
var $list = $select . parent ( ) . siblings ( '.lines-code' ) . find ( 'ol.linenums > li' ) ;
2014-03-24 17:27:19 +04:00
selectRange ( $list , $list . filter ( '[rel=' + $select . attr ( 'rel' ) + ']' ) , ( e . shiftKey ? $list . filter ( '.active' ) . eq ( 0 ) : null ) ) ;
2014-03-23 18:14:29 +04:00
$ . deSelect ( ) ;
} ) ;
2014-03-24 17:27:19 +04:00
$ ( '.code-view .lines-code > pre' ) . each ( function ( ) {
2014-03-23 16:58:12 +04:00
var $pre = $ ( this ) ;
2014-03-23 18:14:29 +04:00
var $lineCode = $pre . parent ( ) ;
var $lineNums = $lineCode . siblings ( '.lines-num' ) ;
2014-03-23 16:58:12 +04:00
if ( $lineNums . length > 0 ) {
var nums = $pre . find ( 'ol.linenums > li' ) . length ;
for ( var i = 1 ; i <= nums ; i ++ ) {
2014-03-23 18:14:29 +04:00
$lineNums . append ( '<span id="L' + i + '" rel="L' + i + '">' + i + '</span>' ) ;
2014-03-23 16:58:12 +04:00
}
}
} ) ;
2014-03-23 18:14:29 +04:00
2014-03-24 17:27:19 +04:00
$ ( window ) . on ( 'hashchange' , function ( e ) {
2014-03-23 18:14:29 +04:00
var m = window . location . hash . match ( /^#(L\d+)\-(L\d+)$/ ) ;
var $list = $ ( '.code-view ol.linenums > li' ) ;
2014-03-24 17:27:19 +04:00
if ( m ) {
var $first = $list . filter ( '.' + m [ 1 ] ) ;
selectRange ( $list , $first , $list . filter ( '.' + m [ 2 ] ) ) ;
$ ( "html, body" ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
2014-03-23 18:14:29 +04:00
return ;
}
m = window . location . hash . match ( /^#(L\d+)$/ ) ;
2014-03-24 17:27:19 +04:00
if ( m ) {
var $first = $list . filter ( '.' + m [ 1 ] ) ;
2014-03-23 18:14:29 +04:00
selectRange ( $list , $first ) ;
2014-03-24 17:27:19 +04:00
$ ( "html, body" ) . scrollTop ( $first . offset ( ) . top - 200 ) ;
2014-03-23 18:14:29 +04:00
}
} ) . trigger ( 'hashchange' ) ;
2014-03-23 16:58:12 +04:00
} ;
2014-03-10 17:12:49 +04:00
} ) ( jQuery ) ;
// ajax utils
( function ( $ ) {
Gogits . ajaxDelete = function ( url , data , success ) {
data = data || { } ;
data . _method = "DELETE" ;
$ . ajax ( {
url : url ,
data : data ,
method : "POST" ,
dataType : "json" ,
success : function ( json ) {
if ( success ) {
success ( json ) ;
}
}
} )
}
2014-03-03 18:40:22 +04:00
} ) ( jQuery ) ;
2014-03-06 18:55:32 +04:00
function initCore ( ) {
2014-03-10 12:54:52 +04:00
Gogits . initTooltips ( ) ;
2014-03-17 14:13:07 +04:00
Gogits . initPopovers ( ) ;
2014-03-13 10:08:49 +04:00
Gogits . initTabs ( ) ;
2014-03-10 12:54:52 +04:00
Gogits . initModals ( ) ;
2014-03-21 19:27:33 +04:00
Gogits . initDropDown ( ) ;
2014-03-17 11:17:44 +04:00
Gogits . renderMarkdown ( ) ;
2014-03-23 16:58:12 +04:00
Gogits . renderCodeView ( ) ;
2014-03-06 18:55:32 +04:00
}
function initRegister ( ) {
$ . getScript ( "/js/jquery.validate.min.js" , function ( ) {
2014-03-25 14:44:37 +04:00
Gogits . validateForm ( "#login-card" , {
2014-03-06 18:55:32 +04:00
rules : {
"username" : {
required : true ,
maxlength : 30
} ,
"email" : {
required : true ,
email : true
} ,
"passwd" : {
required : true ,
minlength : 6 ,
maxlength : 30
} ,
"re-passwd" : {
required : true ,
equalTo : "input[name=passwd]"
}
}
} ) ;
} ) ;
2014-03-10 17:12:49 +04:00
}
2014-03-17 11:17:44 +04:00
function initUserSetting ( ) {
2014-03-25 14:44:37 +04:00
$ ( '#ssh-keys .delete' ) . confirmation ( {
2014-03-16 17:07:50 +04:00
singleton : true ,
2014-03-17 11:17:44 +04:00
onConfirm : function ( e , $this ) {
Gogits . ajaxDelete ( "" , { "id" : $this . data ( "del" ) } , function ( json ) {
if ( json . ok ) {
2014-03-16 17:07:50 +04:00
window . location . reload ( ) ;
2014-03-17 11:17:44 +04:00
} else {
2014-03-16 17:07:50 +04:00
alert ( json . err ) ;
}
} ) ;
}
2014-03-10 17:12:49 +04:00
} ) ;
2014-03-16 19:25:01 +04:00
}
2014-03-20 16:12:31 +04:00
function initRepository ( ) {
2014-03-21 19:27:33 +04:00
// clone group button script
2014-03-20 17:32:08 +04:00
( function ( ) {
2014-03-21 19:27:33 +04:00
var $clone = $ ( '.clone-group-btn' ) ;
if ( $clone . length ) {
var $url = $ ( '.clone-group-url' ) ;
$clone . find ( 'button[data-link]' ) . on ( "click" , function ( e ) {
2014-03-20 17:32:08 +04:00
var $this = $ ( this ) ;
if ( ! $this . hasClass ( 'btn-primary' ) ) {
2014-03-24 19:53:26 +04:00
$clone . find ( '.input-group-btn .btn-primary' ) . removeClass ( 'btn-primary' ) . addClass ( "btn-default" ) ;
2014-03-20 17:32:08 +04:00
$ ( this ) . addClass ( 'btn-primary' ) . removeClass ( 'btn-default' ) ;
$url . val ( $this . data ( "link" ) ) ;
2014-03-21 19:27:33 +04:00
$clone . find ( 'span.clone-url' ) . text ( $this . data ( 'link' ) ) ;
2014-03-20 17:32:08 +04:00
}
} ) . eq ( 0 ) . trigger ( "click" ) ;
// todo copy to clipboard
}
} ) ( ) ;
2014-03-20 18:39:10 +04:00
// watching script
( function ( ) {
2014-03-25 14:44:37 +04:00
var $watch = $ ( '#repo-watching' ) ,
2014-03-20 18:39:10 +04:00
watchLink = $watch . data ( "watch" ) ,
unwatchLink = $watch . data ( "unwatch" ) ;
$watch . on ( 'click' , '.to-watch' , function ( ) {
if ( $watch . hasClass ( "watching" ) ) {
return false ;
}
$ . get ( watchLink , function ( json ) {
if ( json . ok ) {
$watch . find ( '.text-primary' ) . removeClass ( 'text-primary' ) ;
$watch . find ( '.to-watch h4' ) . addClass ( 'text-primary' ) ;
$watch . find ( '.fa-eye-slash' ) . removeClass ( 'fa-eye-slash' ) . addClass ( 'fa-eye' ) ;
$watch . removeClass ( "no-watching" ) . addClass ( "watching" ) ;
}
} ) ;
return false ;
} ) . on ( 'click' , '.to-unwatch' , function ( ) {
if ( $watch . hasClass ( "no-watching" ) ) {
return false ;
}
$ . get ( unwatchLink , function ( json ) {
if ( json . ok ) {
$watch . find ( '.text-primary' ) . removeClass ( 'text-primary' ) ;
$watch . find ( '.to-unwatch h4' ) . addClass ( 'text-primary' ) ;
$watch . find ( '.fa-eye' ) . removeClass ( 'fa-eye' ) . addClass ( 'fa-eye-slash' ) ;
$watch . removeClass ( "watching" ) . addClass ( "no-watching" ) ;
}
} ) ;
return false ;
} ) ;
} ) ( ) ;
2014-03-24 17:27:19 +04:00
// repo diff counter
( function ( ) {
var $counter = $ ( '.diff-counter' ) ;
if ( $counter . length < 1 ) {
return ;
}
$counter . each ( function ( i , item ) {
var $item = $ ( item ) ;
var addLine = $item . find ( 'span[data-line].add' ) . data ( "line" ) ;
var delLine = $item . find ( 'span[data-line].del' ) . data ( "line" ) ;
var addPercent = parseFloat ( addLine ) / ( parseFloat ( addLine ) + parseFloat ( delLine ) ) * 100 ;
$item . find ( ".bar .add" ) . css ( "width" , addPercent + "%" ) ;
} ) ;
} ( ) ) ;
2014-03-20 16:12:31 +04:00
}
2014-03-17 11:17:44 +04:00
( function ( $ ) {
$ ( function ( ) {
initCore ( ) ;
2014-03-25 14:44:37 +04:00
var body = $ ( "#body" ) ;
2014-03-17 11:17:44 +04:00
if ( body . data ( "page" ) == "user-signup" ) {
initRegister ( ) ;
}
if ( body . data ( "page" ) == "user" ) {
initUserSetting ( ) ;
}
2014-03-25 14:44:37 +04:00
if ( $ ( '.repo-nav' ) . length ) {
2014-03-20 16:12:31 +04:00
initRepository ( ) ;
}
2014-03-16 19:25:01 +04:00
} ) ;
} ) ( jQuery ) ;