Skip to content

Commit 1184558

Browse files
committed
Move repository logic to another object
1 parent 57bdc15 commit 1184558

File tree

1 file changed

+60
-45
lines changed

1 file changed

+60
-45
lines changed

src/jquery.github.js

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
1+
// -- Github Repository --------------------------------------------------------
2+
3+
function GithubRepo( repo ) {
4+
this.name = repo.name;
5+
this.description = repo.description;
6+
this.forks = repo.forks;
7+
this.pushed_at = repo.pushed_at;
8+
this.url = repo.url;
9+
this.watchers = repo.watchers;
10+
}
11+
12+
// Parses HTML template
13+
GithubRepo.prototype.toHTML = function () {
14+
var self = this;
15+
16+
self.pushed_at = self._parsePushedDate( self.pushed_at ),
17+
self.url = self._parseURL( self.url );
18+
19+
return $(
20+
"<div class='github-box'>" +
21+
"<div class='github-box-header'>" +
22+
"<h3>" +
23+
"<a href='" + self.url + "'>" + self.name + "</a>" +
24+
"</h3>" +
25+
"<div class='github-stats'>" +
26+
"<a class='repo-stars' title='Stars' data-icon='7' href='" + self.url + "/watchers'>" + self.watchers + "</a>" +
27+
"<a class='repo-forks' title='Forks' data-icon='f' href='" + self.url + "/network'>" + self.forks + "</a>" +
28+
"<a class='repo-issues' title='Issues' data-icon='i' href='" + self.url + "/issues'>" + self.open_issues + "</a>" +
29+
"</div>" +
30+
"</div>" +
31+
"<div class='github-box-content'>" +
32+
"<p>" + self.description + " &mdash; <a href='" + self.url + "#readme'>Read More</a></p>" +
33+
"</div>" +
34+
"<div class='github-box-download'>" +
35+
"<p class='repo-update'>Latest commit to <strong>master</strong> on " + self.pushed_at + "</p>" +
36+
"<a class='repo-download' title='Download as zip' data-icon='w' href='" + self.url + "/zipball/master'></a>" +
37+
"</div>" +
38+
"</div>");
39+
};
40+
41+
// Parses pushed_at with date format
42+
GithubRepo.prototype._parsePushedDate = function ( pushed_at ) {
43+
var self = this,
44+
date = new Date( pushed_at );
45+
46+
return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
47+
};
48+
49+
// Parses URL to be friendly
50+
GithubRepo.prototype._parseURL = function ( url ) {
51+
var self = this;
52+
53+
return url.replace( "api.", "" ).replace( "repos/", "" );
54+
};
55+
56+
// -- Github Plugin ------------------------------------------------------------
57+
158
function Github( element, options ) {
259
var self = this,
3-
defaults = {
60+
defaults = {
461
iconStars: true,
562
iconForks: true,
663
iconIssues: false
@@ -59,7 +116,8 @@ Github.prototype.displayIcons = function () {
59116
// Apply results to HTML template
60117
Github.prototype.applyTemplate = function ( repo ) {
61118
var self = this,
62-
$widget = self.parseTemplate( repo );
119+
githubRepo = new GithubRepo( repo ),
120+
$widget = githubRepo.toHTML();
63121

64122
$widget.appendTo( self.$container );
65123
};
@@ -102,49 +160,6 @@ Github.prototype.handlerSuccessfulRequest = function ( result_data ) {
102160
self.cacheResults( result_data );
103161
};
104162

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>" +
136-
"</div>" +
137-
"</div>" +
138-
"<div class='github-box-content'>" +
139-
"<p>" + repo.description + " &mdash; <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-
148163
// Request repositories from Github
149164
Github.prototype.requestData = function ( repo ) {
150165
var self = this;

0 commit comments

Comments
 (0)