1
+ /*
2
+ * jQuery GitHub Badge 1.0
3
+ *
4
+ * Copyright (c) 2009 Erik Zaadi
5
+ *
6
+ * Inspired by http://drnicjavascript.rubyforge.org/github_badge and
7
+ * http://mattn.github.com/jquery-github-badge/
8
+ *
9
+ * Home Page : http://erikzaadi.github.com/jQueryPlugins/jQueryGitHubBadge
10
+ * jQuery Plugin home page : http://plugins.jquery.com/project/GitHubBadge
11
+ * Wiki : http://wiki.github.com/erikzaadi/jQueryPlugins/jQueryGitHubBadge
12
+ *
13
+ * Dual licensed under the MIT and GPL licenses:
14
+ * http://www.opensource.org/licenses/mit-license.php
15
+ * http://www.gnu.org/licenses/gpl.html
16
+ */
17
+ ; ( function ( $ ) {
18
+ $ . fn . GitHubBadge = function ( options ) {
19
+ if ( typeof ( options ) == 'string' )
20
+ options = { user : options } ;
21
+ var mainOptions = $ . extend ( { } , $ . fn . GitHubBadge . defaults , options ) ;
22
+ return this . each ( function ( ) {
23
+ //Support Metadata Plug-in if available
24
+ var opts = $ . meta ? $ . extend ( { } , mainOptions , $this . data ( ) ) : mainOptions ;
25
+ _GitHubBadge ( $ ( this ) , opts ) ;
26
+ } ) ;
27
+ } ;
28
+ $ . fn . GitHubBadge . defaults = // Default options, can either be object, or simply the user name as string
29
+ {
30
+ user : '' , // Mandatory (Duh!)
31
+ showErrors : false , //Display error messages
32
+ showForks : true , //Shows not only personal public repositories, but forks as well
33
+ validateUser : false //Validates that user exists (404 can not be detecteted, due to JSONP..), NOTE: Generates an extra request
34
+ } ;
35
+ /*
36
+ CSS Classes used:
37
+ GithubBadge -> Main container
38
+ GithubBadgeTitle
39
+ GithubBadgeRepo
40
+ GithubBadgeFork
41
+ GithubBadgeError
42
+ */
43
+ function _GitHubBadge ( $gitHubBadgeElement , options ) {
44
+ if ( options . validateUser ) {
45
+ _GitHubBadgeValidateUser ( $gitHubBadgeElement , options ) ;
46
+ }
47
+ else {
48
+ _getGitHubBadge ( $gitHubBadgeElement , options ) ;
49
+ }
50
+ }
51
+
52
+ function _GitHubBadgeValidateUser ( $gitHubBadgeElement , options ) {
53
+ $ . ajax ( {
54
+ url : 'http://github.com/api/v2/json/user/search/' + options . user ,
55
+ data : { } ,
56
+ success : function ( data ) {
57
+ if ( data && data . users && data . users . length > 0 )
58
+ _getGitHubBadge ( $gitHubBadgeElement , options ) ;
59
+ else _GitHubBadgeError ( $gitHubBadgeElement , options ) ;
60
+ } ,
61
+ dataType : 'jsonp' ,
62
+ error : function ( XMLHttpRequest , textStatus , errorThrown ) {
63
+ if ( console && console . log ) {
64
+ console . log ( 'Error occured while getting the GitHub repositories for : "' + options . user + '"' ) ;
65
+ console . log ( XMLHttpRequest ) ;
66
+ console . log ( textStatus ) ;
67
+ console . log ( errorThrown ) ;
68
+ }
69
+ _GitHubBadgeError ( $gitHubBadgeElement , options ) ;
70
+ }
71
+ } ) ;
72
+ }
73
+
74
+ function _GitHubBadgeError ( $gitHubBadgeElement , options ) {
75
+ if ( options . showErrors ) {
76
+ $gitHubBadgeElement . html ( '<div class="GitHubBadgeError">Error occured while getting the GitHub repositories for : <strong>"' + options . user + '"</strong></div>' ) ;
77
+ }
78
+ }
79
+
80
+ function _getGitHubBadge ( $gitHubBadgeElement , options ) {
81
+ $ . ajax ( {
82
+ url : 'http://github.com/api/v2/json/repos/show/' + options . user ,
83
+ data : { } ,
84
+ success : function ( data ) {
85
+ var forks = new Array ( ) ;
86
+ var html = new Array ( ) ;
87
+ var repositories = data . repositories ;
88
+ html . push ( '<span class="GithubBadge"><div class="GithubBadgeTitle"><a href="http://github.com/' + options . user + '" target="_blank">My Github Homepage</a></div>' ) ;
89
+ if ( repositories . length > 1 ) {
90
+ html . push ( '<div class="GithubBadgeTitle">My Repositories</div>' ) ;
91
+ for ( var repo in repositories ) {
92
+ var current = repositories [ repo ] ;
93
+ //there's one function object that doesn't have the description
94
+ if ( typeof ( current . description ) == 'undefined' )
95
+ continue ;
96
+ //separate forks
97
+ if ( current . fork ) {
98
+ forks . push ( current ) ;
99
+ continue ;
100
+ }
101
+ //Don't show Github Pages Projects
102
+ if ( $ . trim ( current . name . toString ( ) . toLowerCase ( ) ) == options . user . toString ( ) . toLowerCase ( ) + '.github.com' ) {
103
+ continue ;
104
+ }
105
+ html . push ( '<div class="GithubBadgeRepo"><a title="' + current . description + '" href="' + current . url + '" target="_blank">' + current . name + '</a></div>' ) ;
106
+ }
107
+ if ( options . showForks && forks . length > 0 ) {
108
+ html . push ( '<div class="GithubBadgeTitle">Forked Repositories</div>' ) ;
109
+ for ( var forkRep in forks ) {
110
+ var currentFork = forks [ forkRep ] ;
111
+ //IE Bug :(
112
+ if ( typeof ( currentFork . description ) == 'undefined' )
113
+ continue ;
114
+ html . push ( '<div class="GithubBadgeRepo GithubBadgeFork"><a title="' + currentFork . description + '" href="' + currentFork . url + '" target="_blank">' + currentFork . name + '</a></div>' ) ;
115
+ }
116
+ }
117
+ }
118
+ else {
119
+ html . push ( '<div class="GithubBadgeTitle">No repositories found..</div>' ) ;
120
+ }
121
+ html . push ( '</span>' ) ;
122
+ $gitHubBadgeElement . fadeOut ( 'slow' , function ( ) { $ ( this ) . html ( html . join ( '' ) ) . fadeIn ( 'slow' ) ; } ) ;
123
+ } ,
124
+ dataType : 'jsonp' ,
125
+ error : function ( XMLHttpRequest , textStatus , errorThrown ) {
126
+ if ( console && console . log ) {
127
+ console . log ( 'Error occured while getting the GitHub repositories for : "' + options . user + '"' ) ;
128
+ console . log ( XMLHttpRequest ) ;
129
+ console . log ( textStatus ) ;
130
+ console . log ( errorThrown ) ;
131
+ }
132
+ _GitHubBadgeError ( $gitHubBadgeElement , options ) ;
133
+ }
134
+ } ) ;
135
+ }
136
+ } ) ( jQuery ) ;
0 commit comments