Skip to content

Commit 71b99bc

Browse files
committed
Added online sample
1 parent 0a95d8f commit 71b99bc

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

jQuery.GitHubBadge/Sample/Sample.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.GithubBadge
2+
{
3+
border-left: 1px solid #DFDFDF;
4+
border-top: 1px solid #DFDFDF;
5+
border-right: 1px solid #AFAFAF;
6+
border-bottom: 1px solid #AFAFAF;
7+
-webkit-border-radius:3px;
8+
-moz-border-radius:3px;
9+
border-radius:3px;
10+
background-color : #F6F6F6;
11+
padding : 5px;
12+
}
13+
14+
.GithubBadgeTitle
15+
{
16+
font-weight:bold;
17+
}
18+
19+
.GithubBadgeRepo
20+
{
21+
background : url('http://github.com/images/icons/public.png') no-repeat left center;
22+
padding-left : 22px;
23+
}
24+
25+
.GithubBadgeFork
26+
{
27+
font-style : italic;
28+
}
29+
30+
.GithubBadgeError
31+
{
32+
color:red;
33+
}
34+

jQuery.GitHubBadge/Sample/Sample.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<html>
2+
<head>
3+
<title>jQuery GitHubBadge Tester</title>
4+
5+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
6+
7+
<script type="text/javascript" src="../jquery.GitHubBadge.min.js"></script>
8+
9+
<link rel="Stylesheet" type="text/css" href="sample.css" />
10+
11+
<script type="text/javascript">
12+
$(document).ready(function() {
13+
$("#GetGithHubBadge").click(function() {
14+
$('#toBadge').GitHubBadge({ user: $("#GithHubUser").val(), showErrors: true, validateUser: true });
15+
});
16+
$("#GetGithHubBadgeSimple").click(function() {
17+
$('#toBadge').GitHubBadge($("#GithHubUser").val());
18+
});
19+
});
20+
</script>
21+
22+
</head>
23+
<body>
24+
<h2>
25+
jQuery GitHubBadge Tester</h2>
26+
<div>
27+
<label>
28+
Enter User<input type="text" id="GithHubUser" /></label>
29+
<input type="button" value="Get GithHub Badge" id="GetGithHubBadge" />
30+
<input type="button" value="Get GithHub Badge (SimpleMode)" id="GetGithHubBadgeSimple" />
31+
<hr />
32+
<div id="toBadge" style="width : 350px;">
33+
This is where the badge will be.<br />
34+
Typical usage : loading image/text...</div>
35+
</div>
36+
</body>
37+
</html>

jQuery.GitHubBadge/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head>
3+
<title>Erik Zaadi @ GitHub -> jQuery Plugins -> jQuery GitHub Badge 1.0</title>
4+
</head>
5+
<body>
6+
<h2>jQuery GitHub Badge 1.0</h2>
7+
<h4><a href="Sample/Sample.html">Sample</a></h4>
8+
<div>
9+
Copyright (c) 2009 Erik Zaadi
10+
</div>
11+
<div>
12+
Inspired by <a href="http://drnicjavascript.rubyforge.org/github_badge" target="_blank">Dr Nic</a> and
13+
<a href="http://mattn.github.com/jquery-github-badge/" target="_blank">Mattn</a>
14+
</div>
15+
<div>
16+
<a href="http://erikzaadi.github.com/jQueryPlugins/jQuery.GitHubBadge" target="_blank">HomePage</a>
17+
<a href="http://plugins.jquery.com/project/GitHubBadge" target="_blank">jQuery Plugin home page</a>
18+
<a href="http://wiki.github.com/erikzaadi/jQueryPlugins/jQuery.GitHubBadge" target="_blank">Wiki</a>
19+
</div>
20+
<div>
21+
Dual licensed under the MIT and GPL licenses:
22+
http://www.opensource.org/licenses/mit-license.php
23+
http://www.gnu.org/licenses/gpl.html
24+
</div>
25+
</body>
26+
</html>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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($){$.fn.GitHubBadge=function(options){if(typeof(options)=="string"){options={user:options};
18+
}var mainOptions=$.extend({},$.fn.GitHubBadge.defaults,options);return this.each(function(){var opts=$.meta?$.extend({},mainOptions,$this.data()):mainOptions;
19+
_GitHubBadge($(this),opts);});};$.fn.GitHubBadge.defaults={user:"",showErrors:false,showForks:true,validateUser:false};
20+
function _GitHubBadge($gitHubBadgeElement,options){if(options.validateUser){_GitHubBadgeValidateUser($gitHubBadgeElement,options);
21+
}else{_getGitHubBadge($gitHubBadgeElement,options);}}function _GitHubBadgeValidateUser($gitHubBadgeElement,options){$.ajax({url:"http://github.com/api/v2/json/user/search/"+options.user,data:{},success:function(data){if(data&&data.users&&data.users.length>0){_getGitHubBadge($gitHubBadgeElement,options);
22+
}else{_GitHubBadgeError($gitHubBadgeElement,options);}},dataType:"jsonp",error:function(XMLHttpRequest,textStatus,errorThrown){if(console&&console.log){console.log('Error occured while getting the GitHub repositories for : "'+options.user+'"');
23+
console.log(XMLHttpRequest);console.log(textStatus);console.log(errorThrown);}_GitHubBadgeError($gitHubBadgeElement,options);
24+
}});}function _GitHubBadgeError($gitHubBadgeElement,options){if(options.showErrors){$gitHubBadgeElement.html('<div class="GitHubBadgeError">Error occured while getting the GitHub repositories for : <strong>"'+options.user+'"</strong></div>');
25+
}}function _getGitHubBadge($gitHubBadgeElement,options){$.ajax({url:"http://github.com/api/v2/json/repos/show/"+options.user,data:{},success:function(data){var forks=new Array();
26+
var html=new Array();var repositories=data.repositories;html.push('<div class="GithubBadge"><div class="GithubBadgeTitle"><a href="http://github.com/'+options.user+'" target="_blank">My Github Homepage</a></div>');
27+
if(repositories.length>1){html.push('<div class="GithubBadgeTitle">My Repositories</div>');
28+
for(var repo in repositories){var current=repositories[repo];if(typeof(current.description)=="undefined"){continue;
29+
}if(current.fork){forks.push(current);continue;}if($.trim(current.name.toString().toLowerCase())==options.user.toString().toLowerCase()+".github.com"){continue;
30+
}html.push('<div class="GithubBadgeRepo"><a title="'+current.description+'" href="'+current.url+'" target="_blank">'+current.name+"</a></div>");
31+
}if(options.showForks&&forks.length>0){html.push('<div class="GithubBadgeTitle">Forked Repositories</div>');
32+
for(var forkRep in forks){var currentFork=forks[forkRep];if(typeof(currentFork.description)=="undefined"){continue;
33+
}html.push('<div class="GithubBadgeRepo GithubBadgeFork"><a title="'+currentFork.description+'" href="'+currentFork.url+'" target="_blank">'+currentFork.name+"</a></div>");
34+
}}}else{html.push('<div class="GithubBadgeTitle">No repositories found..</div>');
35+
}html.push("</div>");$gitHubBadgeElement.fadeOut("slow",function(){$(this).html(html.join("")).fadeIn("slow");
36+
});},dataType:"jsonp",error:function(XMLHttpRequest,textStatus,errorThrown){if(console&&console.log){console.log('Error occured while getting the GitHub repositories for : "'+options.user+'"');
37+
console.log(XMLHttpRequest);console.log(textStatus);console.log(errorThrown);}_GitHubBadgeError($gitHubBadgeElement,options);
38+
}});}})(jQuery);

0 commit comments

Comments
 (0)