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.
31; ( 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
152 var pluginName = 'github' ,
16- document = window . document ,
17- defaults = {
3+ document = window . document ,
4+ defaults = {
185 propertyName : "value"
196 } ;
207
21- // The actual plugin constructor
228 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" ) ;
2614
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 ) ;
3216
33- this . _defaults = defaults ;
34- this . _name = pluginName ;
17+ self . _defaults = defaults ;
18+ self . _name = pluginName ;
3519
36- this . init ( ) ;
20+ self . init ( ) ;
3721 }
3822
23+ /**
24+ * Initializer
25+ *
26+ */
3927 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 ( ) ;
4830
4931 if ( cached !== null ) {
5032 self . applyTemplate ( JSON . parse ( cached ) ) ;
5133 }
5234 else {
35+ self . requestData ( repo ) ;
36+ }
37+ } ;
5338
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 ) ;
5846
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+ } ;
6549
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 ;
6756
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+ } ;
7262
73- }
74- }
75- } ) ;
63+ /**
64+ * Grab cached results
65+ *
66+ */
67+ Plugin . prototype . getCache = function ( ) {
68+ var self = this ;
7669
70+ if ( window . sessionStorage ) {
71+ return sessionStorage . getItem ( 'gh-repos:' + self . repo ) ;
72+ }
73+ else {
74+ return false ;
7775 }
76+ } ;
77+
78+ /**
79+ * Handle Errors requests
80+ *
81+ */
82+ Plugin . prototype . handlerErrorRequests = function ( result_data ) {
83+ var self = this ;
7884
85+ console . warn ( result_data . message ) ;
86+ return ;
7987 } ;
8088
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+ } ;
82110
111+ /**
112+ * Parses repository URL to be friendly
113+ *
114+ */
115+ Plugin . prototype . parseRepositoryURL = function ( url ) {
83116 var self = this ;
84- var date = new Date ( repo . pushed_at ) ;
85- var pushed_at = date . getDate ( ) + '/' + ( date . getMonth ( ) + 1 ) + '/' + date . getFullYear ( ) ;
86117
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 ( ' \
88131 <div class="github-box"> \
89132 <div class="github-box-header"> \
90133 <h3> \
91- <a href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '">' + repo . name + '</a> \
134+ <a href="' + repo_url + '">' + repo . name + '</a> \
92135 </h3> \
93136 <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> \
96139 </div> \
97140 </div> \
98141 <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> \
100143 </div> \
101144 <div class="github-box-download"> \
102145 <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> \
104147 </div> \
105148 </div> \
106149 ' ) ) ;
107-
108- self . appendTemplate ( $widget ) ;
109-
110150 } ;
111151
112- Plugin . prototype . appendTemplate = function ( $widget ) {
152+ /**
153+ * Request repositories from Github
154+ *
155+ */
156+ Plugin . prototype . requestData = function ( repo ) {
113157 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+ } ) ;
115174 } ;
116175
117- // A really lightweight plugin wrapper around the constructor,
118- // preventing against multiple instantiations
119176 $ . fn [ pluginName ] = function ( options ) {
120177 return this . each ( function ( ) {
121178 if ( ! $ . data ( this , 'plugin_' + pluginName ) ) {
122179 $ . data ( this , 'plugin_' + pluginName , new Plugin ( this , options ) ) ;
123180 }
124181 } ) ;
125182 } ;
126-
127183} ( jQuery , window ) ) ;
0 commit comments