1
1
/*
2
- * jQuery Github - v0.3.2
2
+ * jquery-github - v0.3.4
3
3
* A jQuery plugin to display your Github Repositories.
4
- * https://github.com/zenorocha/jquery-github/
4
+ * https://github.com/zenorocha/jquery-github
5
5
*
6
6
* Copyright (c) 2014
7
7
* MIT License
10
10
11
11
function GithubRepo ( repo ) {
12
12
this . description = repo . description ;
13
- this . forks = repo . forks ;
13
+ this . forks = repo . forks_count ;
14
14
this . name = repo . name ;
15
15
this . open_issues = repo . open_issues ;
16
16
this . pushed_at = repo . pushed_at ;
17
17
this . url = repo . url ;
18
- this . watchers = repo . watchers ;
18
+ this . stargazers = repo . stargazers_count ;
19
19
}
20
20
21
21
// Parses HTML template
22
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 ) ;
23
+ this . pushed_at = this . _parsePushedDate ( this . pushed_at ) ,
24
+ this . url = this . _parseURL ( this . url ) ;
27
25
28
26
return $ (
29
27
"<div class='github-box'>" +
30
28
"<div class='github-box-header'>" +
31
29
"<h3>" +
32
- "<a href='" + self . url + "'>" + self . name + "</a>" +
30
+ "<a href='" + this . url + "'>" + this . name + "</a>" +
33
31
"</h3>" +
34
32
"<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>" +
38
36
"</div>" +
39
37
"</div>" +
40
38
"<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>" +
42
40
"</div>" +
43
41
"<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>" +
46
44
"</div>" +
47
45
"</div>" ) ;
48
46
} ;
49
47
50
48
// Parses pushed_at with date format
51
49
GithubRepo . prototype . _parsePushedDate = function ( pushed_at ) {
52
- var self = this ,
53
- date = new Date ( pushed_at ) ;
50
+ var date = new Date ( pushed_at ) ;
54
51
55
52
return date . getDate ( ) + "/" + ( date . getMonth ( ) + 1 ) + "/" + date . getFullYear ( ) ;
56
53
} ;
57
54
58
55
// Parses URL to be friendly
59
56
GithubRepo . prototype . _parseURL = function ( url ) {
60
- var self = this ;
61
-
62
57
return url . replace ( "api." , "" ) . replace ( "repos/" , "" ) ;
63
58
} ;
64
59
65
60
// -- Github Plugin ------------------------------------------------------------
66
61
67
62
function Github ( element , options ) {
68
- var self = this ,
69
- defaults = {
63
+ var defaults = {
70
64
iconStars : true ,
71
65
iconForks : true ,
72
66
iconIssues : false
73
67
} ;
74
68
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" ) ;
78
72
79
- self . options = $ . extend ( { } , defaults , options ) ;
73
+ this . options = $ . extend ( { } , defaults , options ) ;
80
74
81
- self . _defaults = defaults ;
75
+ this . _defaults = defaults ;
82
76
83
- self . init ( ) ;
84
- self . displayIcons ( ) ;
77
+ this . init ( ) ;
78
+ this . displayIcons ( ) ;
85
79
}
86
80
87
81
// Initializer
88
82
Github . prototype . init = function ( ) {
89
- var self = this ,
90
- cached = self . getCache ( ) ;
83
+ var cached = this . getCache ( ) ;
91
84
92
85
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 ;
97
88
}
89
+
90
+ this . requestData ( this . repo ) ;
98
91
} ;
99
92
100
93
// Display or hide icons
@@ -111,57 +104,49 @@ Github.prototype.displayIcons = function () {
111
104
112
105
// Request repositories from Github
113
106
Github . prototype . requestData = function ( repo ) {
114
- var self = this ;
107
+ var that = this ;
115
108
116
109
$ . ajax ( {
117
110
url : "https://api.github.com/repos/" + repo ,
118
111
dataType : "jsonp" ,
119
112
success : function ( results ) {
120
- var result_data = results . data ;
113
+ var result_data = results . data ,
114
+ isFailling = results . meta . status >= 400 && result_data . message ;
121
115
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 ;
128
119
}
120
+
121
+ that . handleSuccessfulRequest ( result_data ) ;
129
122
}
130
123
} ) ;
131
124
} ;
132
125
133
126
// Handle Errors requests
134
127
Github . prototype . handleErrorRequest = function ( result_data ) {
135
- var self = this ;
136
-
137
128
console . warn ( result_data . message ) ;
138
129
return ;
139
130
} ;
140
131
141
132
// Handle Successful request
142
133
Github . 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 ) ;
147
136
} ;
148
137
149
138
// Stores repostories in sessionStorage if available
150
139
Github . prototype . setCache = function ( result_data ) {
151
- var self = this ;
152
-
153
140
// Cache data
154
141
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 ) ) ;
156
143
}
157
144
} ;
158
145
159
146
// Grab cached results
160
147
Github . prototype . getCache = function ( ) {
161
- var self = this ;
162
-
163
148
if ( window . sessionStorage ) {
164
- return window . sessionStorage . getItem ( "gh-repos:" + self . repo ) ;
149
+ return window . sessionStorage . getItem ( "gh-repos:" + this . repo ) ;
165
150
}
166
151
else {
167
152
return false ;
@@ -170,11 +155,10 @@ Github.prototype.getCache = function() {
170
155
171
156
// Apply results to HTML template
172
157
Github . 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 ( ) ;
176
160
177
- $widget . appendTo ( self . $container ) ;
161
+ $widget . appendTo ( this . $container ) ;
178
162
} ;
179
163
180
164
// -- Attach plugin to jQuery's prototype --------------------------------------
0 commit comments