Skip to content

Commit 31714d0

Browse files
committed
0.3.2
1 parent 0c55f2f commit 31714d0

File tree

4 files changed

+168
-154
lines changed

4 files changed

+168
-154
lines changed

dist/jquery.github.js

Lines changed: 164 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,205 @@
11
/*
2-
* jQuery Github - v0.3.1
2+
* jQuery Github - v0.3.2
33
* A jQuery plugin to display your Github Repositories.
44
* https://github.com/zenorocha/jquery-github/
55
*
66
* Copyright (c) 2013
77
* MIT License
88
*/
9-
;( function ( $, window, undefined ) {
10-
11-
var pluginName = "github",
12-
document = window.document,
13-
defaults = {
9+
// -- Github Repository --------------------------------------------------------
10+
11+
function GithubRepo( repo ) {
12+
this.description = repo.description;
13+
this.forks = repo.forks;
14+
this.name = repo.name;
15+
this.open_issues = repo.open_issues;
16+
this.pushed_at = repo.pushed_at;
17+
this.url = repo.url;
18+
this.watchers = repo.watchers;
19+
}
20+
21+
// Parses HTML template
22+
GithubRepo.prototype.toHTML = function () {
23+
var self = this;
24+
25+
self.pushed_at = self._parsePushedDate( self.pushed_at ),
26+
self.url = self._parseURL( self.url );
27+
28+
return $(
29+
"<div class='github-box'>" +
30+
"<div class='github-box-header'>" +
31+
"<h3>" +
32+
"<a href='" + self.url + "'>" + self.name + "</a>" +
33+
"</h3>" +
34+
"<div class='github-stats'>" +
35+
"<a class='repo-stars' title='Stars' data-icon='7' href='" + self.url + "/watchers'>" + self.watchers + "</a>" +
36+
"<a class='repo-forks' title='Forks' data-icon='f' href='" + self.url + "/network'>" + self.forks + "</a>" +
37+
"<a class='repo-issues' title='Issues' data-icon='i' href='" + self.url + "/issues'>" + self.open_issues + "</a>" +
38+
"</div>" +
39+
"</div>" +
40+
"<div class='github-box-content'>" +
41+
"<p>" + self.description + " &mdash; <a href='" + self.url + "#readme'>Read More</a></p>" +
42+
"</div>" +
43+
"<div class='github-box-download'>" +
44+
"<p class='repo-update'>Latest commit to <strong>master</strong> on " + self.pushed_at + "</p>" +
45+
"<a class='repo-download' title='Download as zip' data-icon='w' href='" + self.url + "/zipball/master'></a>" +
46+
"</div>" +
47+
"</div>");
48+
};
49+
50+
// Parses pushed_at with date format
51+
GithubRepo.prototype._parsePushedDate = function ( pushed_at ) {
52+
var self = this,
53+
date = new Date( pushed_at );
54+
55+
return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
56+
};
57+
58+
// Parses URL to be friendly
59+
GithubRepo.prototype._parseURL = function ( url ) {
60+
var self = this;
61+
62+
return url.replace( "api.", "" ).replace( "repos/", "" );
63+
};
64+
65+
// -- Github Plugin ------------------------------------------------------------
66+
67+
function Github( element, options ) {
68+
var self = this,
69+
defaults = {
1470
iconStars: true,
1571
iconForks: true,
1672
iconIssues: false
1773
};
1874

19-
function Plugin( element, options ) {
20-
var self = this;
75+
self.element = element;
76+
self.$container = $( element );
77+
self.repo = self.$container.attr( "data-repo" );
2178

22-
self.element = element;
23-
self.$container = $( element );
24-
self.repo = self.$container.attr( "data-repo" );
79+
self.options = $.extend( {}, defaults, options ) ;
2580

26-
self.options = $.extend( {}, defaults, options ) ;
81+
self._defaults = defaults;
2782

28-
self._defaults = defaults;
29-
self._name = pluginName;
83+
self.init();
84+
self.displayIcons();
85+
}
3086

31-
self.init();
32-
self.displayIcons();
33-
}
34-
35-
// Initializer
36-
Plugin.prototype.init = function () {
37-
var self = this,
38-
cached = self.getCache();
87+
// Initializer
88+
Github.prototype.init = function () {
89+
var self = this,
90+
cached = self.getCache();
3991

40-
if ( cached !== null ) {
41-
self.applyTemplate( JSON.parse( cached ) );
42-
}
43-
else {
44-
self.requestData( self.repo );
45-
}
46-
};
47-
48-
// Display or hide icons
49-
Plugin.prototype.displayIcons = function () {
50-
$iconStars = $( ".repo-stars" );
51-
$iconForks = $( ".repo-forks" );
52-
$iconIssues = $( ".repo-issues" );
53-
54-
if ( this.options.iconStars ) {
55-
$iconStars.css( "display", "inline-block" );
56-
} else {
57-
$iconStars.css( "display", "none" );
58-
}
59-
60-
if ( this.options.iconForks ) {
61-
$iconForks.css( "display", "inline-block" );
62-
} else {
63-
$iconForks.css( "display", "none" );
64-
}
92+
if ( cached !== null ) {
93+
self.applyTemplate( JSON.parse( cached ) );
94+
}
95+
else {
96+
self.requestData( self.repo );
97+
}
98+
};
99+
100+
// Display or hide icons
101+
Github.prototype.displayIcons = function () {
102+
$iconStars = $( ".repo-stars" );
103+
$iconForks = $( ".repo-forks" );
104+
$iconIssues = $( ".repo-issues" );
105+
106+
if ( this.options.iconStars ) {
107+
$iconStars.css( "display", "inline-block" );
108+
} else {
109+
$iconStars.css( "display", "none" );
110+
}
65111

66-
if ( this.options.iconIssues ) {
67-
$iconIssues.css( "display", "inline-block" );
68-
} else {
69-
$iconIssues.css( "display", "none" );
70-
}
71-
};
112+
if ( this.options.iconForks ) {
113+
$iconForks.css( "display", "inline-block" );
114+
} else {
115+
$iconForks.css( "display", "none" );
116+
}
72117

73-
// Apply results to HTML template
74-
Plugin.prototype.applyTemplate = function ( repo ) {
75-
var self = this,
76-
$widget = self.parseTemplate( repo );
118+
if ( this.options.iconIssues ) {
119+
$iconIssues.css( "display", "inline-block" );
120+
} else {
121+
$iconIssues.css( "display", "none" );
122+
}
123+
};
77124

78-
$widget.appendTo( self.$container );
79-
};
125+
// Request repositories from Github
126+
Github.prototype.requestData = function ( repo ) {
127+
var self = this;
80128

81-
// Stores repostories in sessionStorage if available
82-
Plugin.prototype.cacheResults = function ( result_data ) {
83-
var self = this;
129+
$.ajax({
130+
url: "https://api.github.com/repos/" + repo,
131+
dataType: "jsonp",
132+
success: function( results ) {
133+
var result_data = results.data;
84134

85-
// Cache data
86-
if ( window.sessionStorage ) {
87-
window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) );
135+
// Handle API failures
136+
if ( results.meta.status >= 400 && result_data.message ) {
137+
self.handleErrorRequest( result_data );
138+
}
139+
else {
140+
self.handleSuccessfulRequest( result_data );
141+
}
88142
}
89-
};
143+
});
144+
};
90145

91-
// Grab cached results
92-
Plugin.prototype.getCache = function() {
93-
var self = this;
146+
// Handle Errors requests
147+
Github.prototype.handleErrorRequest = function ( result_data ) {
148+
var self = this;
94149

95-
if ( window.sessionStorage ) {
96-
return window.sessionStorage.getItem( "gh-repos:" + self.repo );
97-
}
98-
else {
99-
return false;
100-
}
101-
};
150+
console.warn( result_data.message );
151+
return;
152+
};
102153

103-
// Handle Errors requests
104-
Plugin.prototype.handlerErrorRequests = function ( result_data ) {
105-
var self = this;
154+
// Handle Successful request
155+
Github.prototype.handleSuccessfulRequest = function ( result_data ) {
156+
var self = this;
106157

107-
console.warn( result_data.message );
108-
return;
109-
};
158+
self.applyTemplate( result_data );
159+
self.setCache( result_data );
160+
};
110161

111-
// Handle Successful request
112-
Plugin.prototype.handlerSuccessfulRequest = function ( result_data ) {
113-
var self = this;
162+
// Stores repostories in sessionStorage if available
163+
Github.prototype.setCache = function ( result_data ) {
164+
var self = this;
114165

115-
self.applyTemplate( result_data );
116-
self.cacheResults( result_data );
117-
};
166+
// Cache data
167+
if ( window.sessionStorage ) {
168+
window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) );
169+
}
170+
};
118171

119-
// Parses Pushed date with date format
120-
Plugin.prototype.parsePushedDate = function ( pushed_at ) {
121-
var self = this,
122-
date = new Date( pushed_at );
172+
// Grab cached results
173+
Github.prototype.getCache = function() {
174+
var self = this;
123175

124-
return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
125-
};
176+
if ( window.sessionStorage ) {
177+
return window.sessionStorage.getItem( "gh-repos:" + self.repo );
178+
}
179+
else {
180+
return false;
181+
}
182+
};
126183

127-
// Parses repository URL to be friendly
128-
Plugin.prototype.parseRepositoryURL = function ( url ) {
129-
var self = this;
184+
// Apply results to HTML template
185+
Github.prototype.applyTemplate = function ( repo ) {
186+
var self = this,
187+
githubRepo = new GithubRepo( repo ),
188+
$widget = githubRepo.toHTML();
130189

131-
return url.replace( "api.", "" ).replace( "repos/", "" );
132-
};
190+
$widget.appendTo( self.$container );
191+
};
133192

134-
// Parses HTML template
135-
Plugin.prototype.parseTemplate = function ( repo ) {
136-
var self = this,
137-
pushed_at = self.parsePushedDate( repo.pushed_at ),
138-
repo_url = self.parseRepositoryURL( repo.url );
139-
140-
return $(
141-
"<div class='github-box'>" +
142-
"<div class='github-box-header'>" +
143-
"<h3>" +
144-
"<a href='" + repo_url + "'>" + repo.name + "</a>" +
145-
"</h3>" +
146-
"<div class='github-stats'>" +
147-
"<a class='repo-stars' title='Stars' data-icon='7' href='" + repo_url + "/watchers'>" + repo.watchers + "</a>" +
148-
"<a class='repo-forks' title='Forks' data-icon='f' href='" + repo_url + "/network'>" + repo.forks + "</a>" +
149-
"<a class='repo-issues' title='Issues' data-icon='i' href='" + repo_url + "/issues'>" + repo.open_issues + "</a>" +
150-
"</div>" +
151-
"</div>" +
152-
"<div class='github-box-content'>" +
153-
"<p>" + repo.description + " &mdash; <a href='" + repo_url + "#readme'>Read More</a></p>" +
154-
"</div>" +
155-
"<div class='github-box-download'>" +
156-
"<p class='repo-update'>Latest commit to <strong>master</strong> on " + pushed_at + "</p>" +
157-
"<a class='repo-download' title='Download as zip' data-icon='w' href='" + repo_url + "/zipball/master'></a>" +
158-
"</div>" +
159-
"</div>");
160-
};
193+
// -- Attach plugin to jQuery's prototype --------------------------------------
161194

162-
// Request repositories from Github
163-
Plugin.prototype.requestData = function ( repo ) {
164-
var self = this;
165-
166-
$.ajax({
167-
url: "https://api.github.com/repos/" + repo,
168-
dataType: "jsonp",
169-
success: function( results ) {
170-
var result_data = results.data;
171-
172-
// Handle API failures
173-
if ( results.meta.status >= 400 && result_data.message ) {
174-
self.handlerErrorRequest();
175-
}
176-
else {
177-
self.handlerSuccessfulRequest( result_data );
178-
}
179-
}
180-
});
181-
};
195+
;( function ( $, window, undefined ) {
182196

183-
$.fn[pluginName] = function ( options ) {
197+
$.fn.github = function ( options ) {
184198
return this.each(function () {
185-
if ( !$( this ).data( "plugin_" + pluginName ) ) {
186-
$( this ).data( "plugin_" + pluginName, new Plugin( this, options ) );
199+
if ( !$( this ).data( "plugin_github" ) ) {
200+
$( this ).data( "plugin_github", new Github( this, options ) );
187201
}
188202
});
189203
};
190204

191-
}( window.jQuery || window.Zepto, window ));
205+
}( window.jQuery || window.Zepto, window ) );

dist/jquery.github.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"repositories",
88
"git"
99
],
10-
"version": "0.3.0",
10+
"version": "0.3.2",
1111
"author": {
1212
"name": "Zeno Rocha",
1313
"url": "https://github.com/zenorocha"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A jQuery plugin to display your Github Repositories.",
55
"author": "Zeno Rocha",
66
"homepage": "https://github.com/zenorocha/jquery-github/",
7-
"version": "0.3.1",
7+
"version": "0.3.2",
88
"devDependencies": {
99
"grunt": "~0.4.1",
1010
"grunt-cli": "~0.1.7",

0 commit comments

Comments
 (0)