1
1
/*
2
- * jQuery Github - v0.2.5
2
+ * jQuery Github - v0.2.6
3
3
* A jQuery plugin to display your Github Repositories.
4
4
* https://github.com/zenorocha/jquery-github/
5
5
6
6
* Copyright (c) 2013
7
7
* MIT License
8
8
*/
9
9
10
- // the semi-colon before function invocation is a safety net against concatenated
11
- // scripts and/or other plugins which may not be closed properly.
12
10
; ( function ( $ , window , undefined ) {
13
-
14
- // undefined is used here as the undefined global variable in ECMAScript 3 is
15
- // mutable (ie. it can be changed by someone else). undefined isn't really being
16
- // passed in so we can ensure the value of it is truly undefined. In ES5, undefined
17
- // can no longer be modified.
18
-
19
- // window is passed through as local variable rather than global
20
- // as this (slightly) quickens the resolution process and can be more efficiently
21
- // minified (especially when both are regularly referenced in your plugin).
22
-
23
- // Create the defaults once
24
11
var pluginName = 'github' ,
25
- document = window . document ,
26
- defaults = {
12
+ document = window . document ,
13
+ defaults = {
27
14
propertyName : "value"
28
15
} ;
29
16
30
- // The actual plugin constructor
31
17
function Plugin ( element , options ) {
32
- this . element = element ;
33
- this . $container = $ ( element ) ;
34
- this . repo = this . $container . attr ( "data-repo" ) ;
18
+ var self = this ;
19
+
20
+ self . element = element ;
21
+ self . $container = $ ( element ) ;
22
+ self . repo = self . $container . attr ( "data-repo" ) ;
35
23
36
- // jQuery has an extend method which merges the contents of two or
37
- // more objects, storing the result in the first object. The first object
38
- // is generally empty as we don't want to alter the default options for
39
- // future instances of the plugin
40
- this . options = $ . extend ( { } , defaults , options ) ;
24
+ self . options = $ . extend ( { } , defaults , options ) ;
41
25
42
- this . _defaults = defaults ;
43
- this . _name = pluginName ;
26
+ self . _defaults = defaults ;
27
+ self . _name = pluginName ;
44
28
45
- this . init ( ) ;
29
+ self . init ( ) ;
46
30
}
47
31
32
+ /**
33
+ * Initializer
34
+ *
35
+ */
48
36
Plugin . prototype . init = function ( ) {
49
-
50
- var self = this ;
51
- var cached ;
52
-
53
- // Attempt to get cached repo data
54
- if ( window . sessionStorage ) {
55
- cached = sessionStorage . getItem ( 'gh-repos:' + this . repo ) ;
56
- }
37
+ var self = this ,
38
+ cached = self . getCache ( ) ;
57
39
58
40
if ( cached !== null ) {
59
41
self . applyTemplate ( JSON . parse ( cached ) ) ;
60
42
}
61
43
else {
44
+ self . requestData ( repo ) ;
45
+ }
46
+ } ;
62
47
63
- $ . ajax ( {
64
- url : 'https://api.github.com/repos/' + this . repo ,
65
- dataType : 'jsonp' ,
66
- success : function ( results ) {
48
+ /**
49
+ * Apply results to HTML template
50
+ *
51
+ */
52
+ Plugin . prototype . applyTemplate = function ( repo ) {
53
+ var self = this ,
54
+ $widget = self . parseTemplate ( repo ) ;
67
55
68
- // Handle API failures
69
- if ( results . meta . status >= 400 && results . data . message ) {
70
- console . warn ( results . data . message ) ;
71
- return ;
72
- }
73
- else {
56
+ $widget . appendTo ( self . $container ) ;
57
+ } ;
74
58
75
- self . applyTemplate ( results . data ) ;
59
+ /**
60
+ * Stores repostories in sessionStorage if available
61
+ *
62
+ */
63
+ Plugin . prototype . cacheResults = function ( result_data ) {
64
+ var self = this ;
76
65
77
- // Cache data
78
- if ( window . sessionStorage ) {
79
- sessionStorage . setItem ( 'gh-repos:' + self . repo , JSON . stringify ( results . data ) ) ;
80
- }
66
+ // Cache data
67
+ if ( window . sessionStorage ) {
68
+ sessionStorage . setItem ( 'gh-repos:' + self . repo , JSON . stringify ( result_data ) ) ;
69
+ }
70
+ } ;
81
71
82
- }
83
- }
84
- } ) ;
72
+ /**
73
+ * Grab cached results
74
+ *
75
+ */
76
+ Plugin . prototype . getCache = function ( ) {
77
+ var self = this ;
85
78
79
+ if ( window . sessionStorage ) {
80
+ return sessionStorage . getItem ( 'gh-repos:' + self . repo ) ;
81
+ }
82
+ else {
83
+ return false ;
86
84
}
85
+ } ;
86
+
87
+ /**
88
+ * Handle Errors requests
89
+ *
90
+ */
91
+ Plugin . prototype . handlerErrorRequests = function ( result_data ) {
92
+ var self = this ;
87
93
94
+ console . warn ( result_data . message ) ;
95
+ return ;
88
96
} ;
89
97
90
- Plugin . prototype . applyTemplate = function ( repo ) {
98
+ /**
99
+ * Handle Successful request
100
+ *
101
+ */
102
+ Plugin . prototype . handlerSuccessfulRequest = function ( result_data ) {
103
+ var self = this ;
104
+
105
+ self . applyTemplate ( result_data ) ;
106
+ self . cacheResults ( result_data ) ;
107
+ } ;
108
+
109
+ /**
110
+ * Parses Pushed date with date format
111
+ *
112
+ */
113
+ Plugin . prototype . parsePushedDate = function ( pushed_at ) {
114
+ var self = this ,
115
+ date = new Date ( pushed_at ) ;
116
+
117
+ return date . getDate ( ) + '/' + ( date . getMonth ( ) + 1 ) + '/' + date . getFullYear ( )
118
+ } ;
91
119
120
+ /**
121
+ * Parses repository URL to be friendly
122
+ *
123
+ */
124
+ Plugin . prototype . parseRepositoryURL = function ( url ) {
92
125
var self = this ;
93
- var date = new Date ( repo . pushed_at ) ;
94
- var pushed_at = date . getDate ( ) + '/' + ( date . getMonth ( ) + 1 ) + '/' + date . getFullYear ( ) ;
95
126
96
- var $widget = $ ( $ . parseHTML ( ' \
127
+ return url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) ;
128
+ } ;
129
+
130
+ /**
131
+ * Parses HTML template
132
+ *
133
+ */
134
+ Plugin . prototype . parseTemplate = function ( repo ) {
135
+ var self = this ,
136
+ pushed_at = self . parsePushedDate ( repo . pushed_at ) ,
137
+ repo_url = self . parseRepositoryURL ( repo . url ) ;
138
+
139
+ return $ ( $ . parseHTML ( ' \
97
140
<div class="github-box"> \
98
141
<div class="github-box-header"> \
99
142
<h3> \
100
- <a href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '">' + repo . name + '</a> \
143
+ <a href="' + repo_url + '">' + repo . name + '</a> \
101
144
</h3> \
102
145
<div class="github-stats"> \
103
- <a class="repo-watchers" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/watchers">' + repo . watchers + '</a> \
104
- <a class="repo-forks" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/forks">' + repo . forks + '</a> \
146
+ <a class="repo-watchers" href="' + repo_url + '/watchers">' + repo . watchers + '</a> \
147
+ <a class="repo-forks" href="' + repo_url + '/forks">' + repo . forks + '</a> \
105
148
</div> \
106
149
</div> \
107
150
<div class="github-box-content"> \
108
- <p>' + repo . description + ' — <a href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '#readme">Read More</a></p> \
151
+ <p>' + repo . description + ' — <a href="' + repo_url + '#readme">Read More</a></p> \
109
152
</div> \
110
153
<div class="github-box-download"> \
111
154
<p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
112
- <a class="repo-download" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/zipball/master">Download as zip</a> \
155
+ <a class="repo-download" href="' + repo_url + '/zipball/master">Download as zip</a> \
113
156
</div> \
114
157
</div> \
115
158
' ) ) ;
116
-
117
- self . appendTemplate ( $widget ) ;
118
-
119
159
} ;
120
160
121
- Plugin . prototype . appendTemplate = function ( $widget ) {
161
+ /**
162
+ * Request repositories from Github
163
+ *
164
+ */
165
+ Plugin . prototype . requestData = function ( repo ) {
122
166
var self = this ;
123
- $widget . appendTo ( self . $container ) ;
167
+
168
+ $ . ajax ( {
169
+ url : 'https://api.github.com/repos/' + repo ,
170
+ dataType : 'jsonp' ,
171
+ success : function ( results ) {
172
+ var result_data = results . data ;
173
+
174
+ // Handle API failures
175
+ if ( results . meta . status >= 400 && result_data . message ) {
176
+ self . handlerErrorRequest ( ) ;
177
+ }
178
+ else {
179
+ self . handlerSuccessfulRequest ( result_data ) ;
180
+ }
181
+ }
182
+ } ) ;
124
183
} ;
125
184
126
- // A really lightweight plugin wrapper around the constructor,
127
- // preventing against multiple instantiations
128
185
$ . fn [ pluginName ] = function ( options ) {
129
186
return this . each ( function ( ) {
130
187
if ( ! $ . data ( this , 'plugin_' + pluginName ) ) {
131
188
$ . data ( this , 'plugin_' + pluginName , new Plugin ( this , options ) ) ;
132
189
}
133
190
} ) ;
134
191
} ;
135
-
136
192
} ( jQuery , window ) ) ;
0 commit comments