11/*
2- * jQuery Github - v0.3.2
2+ * jquery-github - v0.3.4
33 * A jQuery plugin to display your Github Repositories.
4- * https://github.com/zenorocha/jquery-github/
4+ * https://github.com/zenorocha/jquery-github
55 *
66 * Copyright (c) 2014
77 * MIT License
1010
1111function GithubRepo ( repo ) {
1212 this . description = repo . description ;
13- this . forks = repo . forks ;
13+ this . forks = repo . forks_count ;
1414 this . name = repo . name ;
1515 this . open_issues = repo . open_issues ;
1616 this . pushed_at = repo . pushed_at ;
1717 this . url = repo . url ;
18- this . watchers = repo . watchers ;
18+ this . stargazers = repo . stargazers_count ;
1919}
2020
2121// Parses HTML template
2222GithubRepo . prototype . toHTML = function ( ) {
23- var self = this ;
24-
25- self . pushed_at = self . _parsePushedDate ( self . pushed_at ) ,
26- self . url = self . _parseURL ( self . url ) ;
23+ this . pushed_at = this . _parsePushedDate ( this . pushed_at ) ,
24+ this . url = this . _parseURL ( this . url ) ;
2725
2826 return $ (
2927 "<div class='github-box'>" +
3028 "<div class='github-box-header'>" +
3129 "<h3>" +
32- "<a href='" + self . url + "'>" + self . name + "</a>" +
30+ "<a href='" + this . url + "'>" + this . name + "</a>" +
3331 "</h3>" +
3432 "<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>" +
33+ "<a class='repo-stars' title='Stars' data-icon='7' href='" + this . url + "/stargazers '>" + this . stargazers + "</a>" +
34+ "<a class='repo-forks' title='Forks' data-icon='f' href='" + this . url + "/network'>" + this . forks + "</a>" +
35+ "<a class='repo-issues' title='Issues' data-icon='i' href='" + this . url + "/issues'>" + this . open_issues + "</a>" +
3836 "</div>" +
3937 "</div>" +
4038 "<div class='github-box-content'>" +
41- "<p>" + self . description + " — <a href='" + self . url + "#readme'>Read More</a></p>" +
39+ "<p>" + this . description + " — <a href='" + this . url + "#readme'>Read More</a></p>" +
4240 "</div>" +
4341 "<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>" +
42+ "<p class='repo-update'>Latest commit to <strong>master</strong> on " + this . pushed_at + "</p>" +
43+ "<a class='repo-download' title='Download as zip' data-icon='w' href='" + this . url + "/zipball/master'></a>" +
4644 "</div>" +
4745 "</div>" ) ;
4846} ;
4947
5048// Parses pushed_at with date format
5149GithubRepo . prototype . _parsePushedDate = function ( pushed_at ) {
52- var self = this ,
53- date = new Date ( pushed_at ) ;
50+ var date = new Date ( pushed_at ) ;
5451
5552 return date . getDate ( ) + "/" + ( date . getMonth ( ) + 1 ) + "/" + date . getFullYear ( ) ;
5653} ;
5754
5855// Parses URL to be friendly
5956GithubRepo . prototype . _parseURL = function ( url ) {
60- var self = this ;
61-
6257 return url . replace ( "api." , "" ) . replace ( "repos/" , "" ) ;
6358} ;
6459
6560// -- Github Plugin ------------------------------------------------------------
6661
6762function Github ( element , options ) {
68- var self = this ,
69- defaults = {
63+ var defaults = {
7064 iconStars : true ,
7165 iconForks : true ,
7266 iconIssues : false
7367 } ;
7468
75- self . element = element ;
76- self . $container = $ ( element ) ;
77- self . repo = self . $container . attr ( "data-repo" ) ;
69+ this . element = element ;
70+ this . $container = $ ( element ) ;
71+ this . repo = this . $container . attr ( "data-repo" ) ;
7872
79- self . options = $ . extend ( { } , defaults , options ) ;
73+ this . options = $ . extend ( { } , defaults , options ) ;
8074
81- self . _defaults = defaults ;
75+ this . _defaults = defaults ;
8276
83- self . init ( ) ;
84- self . displayIcons ( ) ;
77+ this . init ( ) ;
78+ this . displayIcons ( ) ;
8579}
8680
8781// Initializer
8882Github . prototype . init = function ( ) {
89- var self = this ,
90- cached = self . getCache ( ) ;
83+ var cached = this . getCache ( ) ;
9184
9285 if ( cached !== null ) {
93- self . applyTemplate ( JSON . parse ( cached ) ) ;
94- }
95- else {
96- self . requestData ( self . repo ) ;
86+ this . applyTemplate ( JSON . parse ( cached ) ) ;
87+ return ;
9788 }
89+
90+ this . requestData ( this . repo ) ;
9891} ;
9992
10093// Display or hide icons
@@ -111,57 +104,49 @@ Github.prototype.displayIcons = function () {
111104
112105// Request repositories from Github
113106Github . prototype . requestData = function ( repo ) {
114- var self = this ;
107+ var that = this ;
115108
116109 $ . ajax ( {
117110 url : "https://api.github.com/repos/" + repo ,
118111 dataType : "jsonp" ,
119112 success : function ( results ) {
120- var result_data = results . data ;
113+ var result_data = results . data ,
114+ isFailling = results . meta . status >= 400 && result_data . message ;
121115
122- // Handle API failures
123- if ( results . meta . status >= 400 && result_data . message ) {
124- self . handleErrorRequest ( result_data ) ;
125- }
126- else {
127- self . handleSuccessfulRequest ( result_data ) ;
116+ if ( isFailling ) {
117+ that . handleErrorRequest ( result_data ) ;
118+ return ;
128119 }
120+
121+ that . handleSuccessfulRequest ( result_data ) ;
129122 }
130123 } ) ;
131124} ;
132125
133126// Handle Errors requests
134127Github . prototype . handleErrorRequest = function ( result_data ) {
135- var self = this ;
136-
137128 console . warn ( result_data . message ) ;
138129 return ;
139130} ;
140131
141132// Handle Successful request
142133Github . prototype . handleSuccessfulRequest = function ( result_data ) {
143- var self = this ;
144-
145- self . applyTemplate ( result_data ) ;
146- self . setCache ( result_data ) ;
134+ this . applyTemplate ( result_data ) ;
135+ this . setCache ( result_data ) ;
147136} ;
148137
149138// Stores repostories in sessionStorage if available
150139Github . prototype . setCache = function ( result_data ) {
151- var self = this ;
152-
153140 // Cache data
154141 if ( window . sessionStorage ) {
155- window . sessionStorage . setItem ( "gh-repos:" + self . repo , JSON . stringify ( result_data ) ) ;
142+ window . sessionStorage . setItem ( "gh-repos:" + this . repo , JSON . stringify ( result_data ) ) ;
156143 }
157144} ;
158145
159146// Grab cached results
160147Github . prototype . getCache = function ( ) {
161- var self = this ;
162-
163148 if ( window . sessionStorage ) {
164- return window . sessionStorage . getItem ( "gh-repos:" + self . repo ) ;
149+ return window . sessionStorage . getItem ( "gh-repos:" + this . repo ) ;
165150 }
166151 else {
167152 return false ;
@@ -170,11 +155,10 @@ Github.prototype.getCache = function() {
170155
171156// Apply results to HTML template
172157Github . prototype . applyTemplate = function ( repo ) {
173- var self = this ,
174- githubRepo = new GithubRepo ( repo ) ,
175- $widget = githubRepo . toHTML ( ) ;
158+ var githubRepo = new GithubRepo ( repo ) ,
159+ $widget = githubRepo . toHTML ( ) ;
176160
177- $widget . appendTo ( self . $container ) ;
161+ $widget . appendTo ( this . $container ) ;
178162} ;
179163
180164// -- Attach plugin to jQuery's prototype --------------------------------------
0 commit comments