|
1 | 1 | /* |
2 | | - * jQuery Github - v0.3.1 |
| 2 | + * jQuery Github - v0.3.2 |
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 | | -;( function ( $, window, undefined ) { |
10 | | - |
11 | | - var pluginName = "github", |
12 | | - document = window.document, |
13 | | - defaults = { |
| 9 | +// -- Github Repository -------------------------------------------------------- |
| 10 | + |
| 11 | +function GithubRepo( repo ) { |
| 12 | + this.description = repo.description; |
| 13 | + this.forks = repo.forks; |
| 14 | + this.name = repo.name; |
| 15 | + this.open_issues = repo.open_issues; |
| 16 | + this.pushed_at = repo.pushed_at; |
| 17 | + this.url = repo.url; |
| 18 | + this.watchers = repo.watchers; |
| 19 | +} |
| 20 | + |
| 21 | +// Parses HTML template |
| 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 ); |
| 27 | + |
| 28 | + return $( |
| 29 | + "<div class='github-box'>" + |
| 30 | + "<div class='github-box-header'>" + |
| 31 | + "<h3>" + |
| 32 | + "<a href='" + self.url + "'>" + self.name + "</a>" + |
| 33 | + "</h3>" + |
| 34 | + "<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>" + |
| 38 | + "</div>" + |
| 39 | + "</div>" + |
| 40 | + "<div class='github-box-content'>" + |
| 41 | + "<p>" + self.description + " — <a href='" + self.url + "#readme'>Read More</a></p>" + |
| 42 | + "</div>" + |
| 43 | + "<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>" + |
| 46 | + "</div>" + |
| 47 | + "</div>"); |
| 48 | +}; |
| 49 | + |
| 50 | +// Parses pushed_at with date format |
| 51 | +GithubRepo.prototype._parsePushedDate = function ( pushed_at ) { |
| 52 | + var self = this, |
| 53 | + date = new Date( pushed_at ); |
| 54 | + |
| 55 | + return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear(); |
| 56 | +}; |
| 57 | + |
| 58 | +// Parses URL to be friendly |
| 59 | +GithubRepo.prototype._parseURL = function ( url ) { |
| 60 | + var self = this; |
| 61 | + |
| 62 | + return url.replace( "api.", "" ).replace( "repos/", "" ); |
| 63 | +}; |
| 64 | + |
| 65 | +// -- Github Plugin ------------------------------------------------------------ |
| 66 | + |
| 67 | +function Github( element, options ) { |
| 68 | + var self = this, |
| 69 | + defaults = { |
14 | 70 | iconStars: true, |
15 | 71 | iconForks: true, |
16 | 72 | iconIssues: false |
17 | 73 | }; |
18 | 74 |
|
19 | | - function Plugin( element, options ) { |
20 | | - var self = this; |
| 75 | + self.element = element; |
| 76 | + self.$container = $( element ); |
| 77 | + self.repo = self.$container.attr( "data-repo" ); |
21 | 78 |
|
22 | | - self.element = element; |
23 | | - self.$container = $( element ); |
24 | | - self.repo = self.$container.attr( "data-repo" ); |
| 79 | + self.options = $.extend( {}, defaults, options ) ; |
25 | 80 |
|
26 | | - self.options = $.extend( {}, defaults, options ) ; |
| 81 | + self._defaults = defaults; |
27 | 82 |
|
28 | | - self._defaults = defaults; |
29 | | - self._name = pluginName; |
| 83 | + self.init(); |
| 84 | + self.displayIcons(); |
| 85 | +} |
30 | 86 |
|
31 | | - self.init(); |
32 | | - self.displayIcons(); |
33 | | - } |
34 | | - |
35 | | - // Initializer |
36 | | - Plugin.prototype.init = function () { |
37 | | - var self = this, |
38 | | - cached = self.getCache(); |
| 87 | +// Initializer |
| 88 | +Github.prototype.init = function () { |
| 89 | + var self = this, |
| 90 | + cached = self.getCache(); |
39 | 91 |
|
40 | | - if ( cached !== null ) { |
41 | | - self.applyTemplate( JSON.parse( cached ) ); |
42 | | - } |
43 | | - else { |
44 | | - self.requestData( self.repo ); |
45 | | - } |
46 | | - }; |
47 | | - |
48 | | - // Display or hide icons |
49 | | - Plugin.prototype.displayIcons = function () { |
50 | | - $iconStars = $( ".repo-stars" ); |
51 | | - $iconForks = $( ".repo-forks" ); |
52 | | - $iconIssues = $( ".repo-issues" ); |
53 | | - |
54 | | - if ( this.options.iconStars ) { |
55 | | - $iconStars.css( "display", "inline-block" ); |
56 | | - } else { |
57 | | - $iconStars.css( "display", "none" ); |
58 | | - } |
59 | | - |
60 | | - if ( this.options.iconForks ) { |
61 | | - $iconForks.css( "display", "inline-block" ); |
62 | | - } else { |
63 | | - $iconForks.css( "display", "none" ); |
64 | | - } |
| 92 | + if ( cached !== null ) { |
| 93 | + self.applyTemplate( JSON.parse( cached ) ); |
| 94 | + } |
| 95 | + else { |
| 96 | + self.requestData( self.repo ); |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +// Display or hide icons |
| 101 | +Github.prototype.displayIcons = function () { |
| 102 | + $iconStars = $( ".repo-stars" ); |
| 103 | + $iconForks = $( ".repo-forks" ); |
| 104 | + $iconIssues = $( ".repo-issues" ); |
| 105 | + |
| 106 | + if ( this.options.iconStars ) { |
| 107 | + $iconStars.css( "display", "inline-block" ); |
| 108 | + } else { |
| 109 | + $iconStars.css( "display", "none" ); |
| 110 | + } |
65 | 111 |
|
66 | | - if ( this.options.iconIssues ) { |
67 | | - $iconIssues.css( "display", "inline-block" ); |
68 | | - } else { |
69 | | - $iconIssues.css( "display", "none" ); |
70 | | - } |
71 | | - }; |
| 112 | + if ( this.options.iconForks ) { |
| 113 | + $iconForks.css( "display", "inline-block" ); |
| 114 | + } else { |
| 115 | + $iconForks.css( "display", "none" ); |
| 116 | + } |
72 | 117 |
|
73 | | - // Apply results to HTML template |
74 | | - Plugin.prototype.applyTemplate = function ( repo ) { |
75 | | - var self = this, |
76 | | - $widget = self.parseTemplate( repo ); |
| 118 | + if ( this.options.iconIssues ) { |
| 119 | + $iconIssues.css( "display", "inline-block" ); |
| 120 | + } else { |
| 121 | + $iconIssues.css( "display", "none" ); |
| 122 | + } |
| 123 | +}; |
77 | 124 |
|
78 | | - $widget.appendTo( self.$container ); |
79 | | - }; |
| 125 | +// Request repositories from Github |
| 126 | +Github.prototype.requestData = function ( repo ) { |
| 127 | + var self = this; |
80 | 128 |
|
81 | | - // Stores repostories in sessionStorage if available |
82 | | - Plugin.prototype.cacheResults = function ( result_data ) { |
83 | | - var self = this; |
| 129 | + $.ajax({ |
| 130 | + url: "https://api.github.com/repos/" + repo, |
| 131 | + dataType: "jsonp", |
| 132 | + success: function( results ) { |
| 133 | + var result_data = results.data; |
84 | 134 |
|
85 | | - // Cache data |
86 | | - if ( window.sessionStorage ) { |
87 | | - window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) ); |
| 135 | + // Handle API failures |
| 136 | + if ( results.meta.status >= 400 && result_data.message ) { |
| 137 | + self.handleErrorRequest( result_data ); |
| 138 | + } |
| 139 | + else { |
| 140 | + self.handleSuccessfulRequest( result_data ); |
| 141 | + } |
88 | 142 | } |
89 | | - }; |
| 143 | + }); |
| 144 | +}; |
90 | 145 |
|
91 | | - // Grab cached results |
92 | | - Plugin.prototype.getCache = function() { |
93 | | - var self = this; |
| 146 | +// Handle Errors requests |
| 147 | +Github.prototype.handleErrorRequest = function ( result_data ) { |
| 148 | + var self = this; |
94 | 149 |
|
95 | | - if ( window.sessionStorage ) { |
96 | | - return window.sessionStorage.getItem( "gh-repos:" + self.repo ); |
97 | | - } |
98 | | - else { |
99 | | - return false; |
100 | | - } |
101 | | - }; |
| 150 | + console.warn( result_data.message ); |
| 151 | + return; |
| 152 | +}; |
102 | 153 |
|
103 | | - // Handle Errors requests |
104 | | - Plugin.prototype.handlerErrorRequests = function ( result_data ) { |
105 | | - var self = this; |
| 154 | +// Handle Successful request |
| 155 | +Github.prototype.handleSuccessfulRequest = function ( result_data ) { |
| 156 | + var self = this; |
106 | 157 |
|
107 | | - console.warn( result_data.message ); |
108 | | - return; |
109 | | - }; |
| 158 | + self.applyTemplate( result_data ); |
| 159 | + self.setCache( result_data ); |
| 160 | +}; |
110 | 161 |
|
111 | | - // Handle Successful request |
112 | | - Plugin.prototype.handlerSuccessfulRequest = function ( result_data ) { |
113 | | - var self = this; |
| 162 | +// Stores repostories in sessionStorage if available |
| 163 | +Github.prototype.setCache = function ( result_data ) { |
| 164 | + var self = this; |
114 | 165 |
|
115 | | - self.applyTemplate( result_data ); |
116 | | - self.cacheResults( result_data ); |
117 | | - }; |
| 166 | + // Cache data |
| 167 | + if ( window.sessionStorage ) { |
| 168 | + window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) ); |
| 169 | + } |
| 170 | +}; |
118 | 171 |
|
119 | | - // Parses Pushed date with date format |
120 | | - Plugin.prototype.parsePushedDate = function ( pushed_at ) { |
121 | | - var self = this, |
122 | | - date = new Date( pushed_at ); |
| 172 | +// Grab cached results |
| 173 | +Github.prototype.getCache = function() { |
| 174 | + var self = this; |
123 | 175 |
|
124 | | - return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear(); |
125 | | - }; |
| 176 | + if ( window.sessionStorage ) { |
| 177 | + return window.sessionStorage.getItem( "gh-repos:" + self.repo ); |
| 178 | + } |
| 179 | + else { |
| 180 | + return false; |
| 181 | + } |
| 182 | +}; |
126 | 183 |
|
127 | | - // Parses repository URL to be friendly |
128 | | - Plugin.prototype.parseRepositoryURL = function ( url ) { |
129 | | - var self = this; |
| 184 | +// Apply results to HTML template |
| 185 | +Github.prototype.applyTemplate = function ( repo ) { |
| 186 | + var self = this, |
| 187 | + githubRepo = new GithubRepo( repo ), |
| 188 | + $widget = githubRepo.toHTML(); |
130 | 189 |
|
131 | | - return url.replace( "api.", "" ).replace( "repos/", "" ); |
132 | | - }; |
| 190 | + $widget.appendTo( self.$container ); |
| 191 | +}; |
133 | 192 |
|
134 | | - // Parses HTML template |
135 | | - Plugin.prototype.parseTemplate = function ( repo ) { |
136 | | - var self = this, |
137 | | - pushed_at = self.parsePushedDate( repo.pushed_at ), |
138 | | - repo_url = self.parseRepositoryURL( repo.url ); |
139 | | - |
140 | | - return $( |
141 | | - "<div class='github-box'>" + |
142 | | - "<div class='github-box-header'>" + |
143 | | - "<h3>" + |
144 | | - "<a href='" + repo_url + "'>" + repo.name + "</a>" + |
145 | | - "</h3>" + |
146 | | - "<div class='github-stats'>" + |
147 | | - "<a class='repo-stars' title='Stars' data-icon='7' href='" + repo_url + "/watchers'>" + repo.watchers + "</a>" + |
148 | | - "<a class='repo-forks' title='Forks' data-icon='f' href='" + repo_url + "/network'>" + repo.forks + "</a>" + |
149 | | - "<a class='repo-issues' title='Issues' data-icon='i' href='" + repo_url + "/issues'>" + repo.open_issues + "</a>" + |
150 | | - "</div>" + |
151 | | - "</div>" + |
152 | | - "<div class='github-box-content'>" + |
153 | | - "<p>" + repo.description + " — <a href='" + repo_url + "#readme'>Read More</a></p>" + |
154 | | - "</div>" + |
155 | | - "<div class='github-box-download'>" + |
156 | | - "<p class='repo-update'>Latest commit to <strong>master</strong> on " + pushed_at + "</p>" + |
157 | | - "<a class='repo-download' title='Download as zip' data-icon='w' href='" + repo_url + "/zipball/master'></a>" + |
158 | | - "</div>" + |
159 | | - "</div>"); |
160 | | - }; |
| 193 | +// -- Attach plugin to jQuery's prototype -------------------------------------- |
161 | 194 |
|
162 | | - // Request repositories from Github |
163 | | - Plugin.prototype.requestData = function ( repo ) { |
164 | | - var self = this; |
165 | | - |
166 | | - $.ajax({ |
167 | | - url: "https://api.github.com/repos/" + repo, |
168 | | - dataType: "jsonp", |
169 | | - success: function( results ) { |
170 | | - var result_data = results.data; |
171 | | - |
172 | | - // Handle API failures |
173 | | - if ( results.meta.status >= 400 && result_data.message ) { |
174 | | - self.handlerErrorRequest(); |
175 | | - } |
176 | | - else { |
177 | | - self.handlerSuccessfulRequest( result_data ); |
178 | | - } |
179 | | - } |
180 | | - }); |
181 | | - }; |
| 195 | +;( function ( $, window, undefined ) { |
182 | 196 |
|
183 | | - $.fn[pluginName] = function ( options ) { |
| 197 | + $.fn.github = function ( options ) { |
184 | 198 | return this.each(function () { |
185 | | - if ( !$( this ).data( "plugin_" + pluginName ) ) { |
186 | | - $( this ).data( "plugin_" + pluginName, new Plugin( this, options ) ); |
| 199 | + if ( !$( this ).data( "plugin_github" ) ) { |
| 200 | + $( this ).data( "plugin_github", new Github( this, options ) ); |
187 | 201 | } |
188 | 202 | }); |
189 | 203 | }; |
190 | 204 |
|
191 | | -}( window.jQuery || window.Zepto, window )); |
| 205 | +}( window.jQuery || window.Zepto, window ) ); |
0 commit comments