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