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