Skip to content

Commit

Permalink
0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zenorocha committed Oct 5, 2013
1 parent 0c55f2f commit 31714d0
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 154 deletions.
314 changes: 164 additions & 150 deletions dist/jquery.github.js
Original file line number Diff line number Diff line change
@@ -1,191 +1,205 @@
/*
* jQuery Github - v0.3.1
* jQuery Github - v0.3.2
* A jQuery plugin to display your Github Repositories.
* https://github.com/zenorocha/jquery-github/
*
* Copyright (c) 2013
* MIT License
*/
;( function ( $, window, undefined ) {

var pluginName = "github",
document = window.document,
defaults = {
// -- Github Repository --------------------------------------------------------

function GithubRepo( repo ) {
this.description = repo.description;
this.forks = repo.forks;
this.name = repo.name;
this.open_issues = repo.open_issues;
this.pushed_at = repo.pushed_at;
this.url = repo.url;
this.watchers = repo.watchers;
}

// Parses HTML template
GithubRepo.prototype.toHTML = function () {
var self = this;

self.pushed_at = self._parsePushedDate( self.pushed_at ),
self.url = self._parseURL( self.url );

return $(
"<div class='github-box'>" +
"<div class='github-box-header'>" +
"<h3>" +
"<a href='" + self.url + "'>" + self.name + "</a>" +
"</h3>" +
"<div class='github-stats'>" +
"<a class='repo-stars' title='Stars' data-icon='7' href='" + self.url + "/watchers'>" + self.watchers + "</a>" +
"<a class='repo-forks' title='Forks' data-icon='f' href='" + self.url + "/network'>" + self.forks + "</a>" +
"<a class='repo-issues' title='Issues' data-icon='i' href='" + self.url + "/issues'>" + self.open_issues + "</a>" +
"</div>" +
"</div>" +
"<div class='github-box-content'>" +
"<p>" + self.description + " &mdash; <a href='" + self.url + "#readme'>Read More</a></p>" +
"</div>" +
"<div class='github-box-download'>" +
"<p class='repo-update'>Latest commit to <strong>master</strong> on " + self.pushed_at + "</p>" +
"<a class='repo-download' title='Download as zip' data-icon='w' href='" + self.url + "/zipball/master'></a>" +
"</div>" +
"</div>");
};

// Parses pushed_at with date format
GithubRepo.prototype._parsePushedDate = function ( pushed_at ) {
var self = this,
date = new Date( pushed_at );

return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
};

// Parses URL to be friendly
GithubRepo.prototype._parseURL = function ( url ) {
var self = this;

return url.replace( "api.", "" ).replace( "repos/", "" );
};

// -- Github Plugin ------------------------------------------------------------

function Github( element, options ) {
var self = this,
defaults = {
iconStars: true,
iconForks: true,
iconIssues: false
};

function Plugin( element, options ) {
var self = this;
self.element = element;
self.$container = $( element );
self.repo = self.$container.attr( "data-repo" );

self.element = element;
self.$container = $( element );
self.repo = self.$container.attr( "data-repo" );
self.options = $.extend( {}, defaults, options ) ;

self.options = $.extend( {}, defaults, options ) ;
self._defaults = defaults;

self._defaults = defaults;
self._name = pluginName;
self.init();
self.displayIcons();
}

self.init();
self.displayIcons();
}

// Initializer
Plugin.prototype.init = function () {
var self = this,
cached = self.getCache();
// Initializer
Github.prototype.init = function () {
var self = this,
cached = self.getCache();

if ( cached !== null ) {
self.applyTemplate( JSON.parse( cached ) );
}
else {
self.requestData( self.repo );
}
};

// Display or hide icons
Plugin.prototype.displayIcons = function () {
$iconStars = $( ".repo-stars" );
$iconForks = $( ".repo-forks" );
$iconIssues = $( ".repo-issues" );

if ( this.options.iconStars ) {
$iconStars.css( "display", "inline-block" );
} else {
$iconStars.css( "display", "none" );
}

if ( this.options.iconForks ) {
$iconForks.css( "display", "inline-block" );
} else {
$iconForks.css( "display", "none" );
}
if ( cached !== null ) {
self.applyTemplate( JSON.parse( cached ) );
}
else {
self.requestData( self.repo );
}
};

// Display or hide icons
Github.prototype.displayIcons = function () {
$iconStars = $( ".repo-stars" );
$iconForks = $( ".repo-forks" );
$iconIssues = $( ".repo-issues" );

if ( this.options.iconStars ) {
$iconStars.css( "display", "inline-block" );
} else {
$iconStars.css( "display", "none" );
}

if ( this.options.iconIssues ) {
$iconIssues.css( "display", "inline-block" );
} else {
$iconIssues.css( "display", "none" );
}
};
if ( this.options.iconForks ) {
$iconForks.css( "display", "inline-block" );
} else {
$iconForks.css( "display", "none" );
}

// Apply results to HTML template
Plugin.prototype.applyTemplate = function ( repo ) {
var self = this,
$widget = self.parseTemplate( repo );
if ( this.options.iconIssues ) {
$iconIssues.css( "display", "inline-block" );
} else {
$iconIssues.css( "display", "none" );
}
};

$widget.appendTo( self.$container );
};
// Request repositories from Github
Github.prototype.requestData = function ( repo ) {
var self = this;

// Stores repostories in sessionStorage if available
Plugin.prototype.cacheResults = function ( result_data ) {
var self = this;
$.ajax({
url: "https://api.github.com/repos/" + repo,
dataType: "jsonp",
success: function( results ) {
var result_data = results.data;

// Cache data
if ( window.sessionStorage ) {
window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) );
// Handle API failures
if ( results.meta.status >= 400 && result_data.message ) {
self.handleErrorRequest( result_data );
}
else {
self.handleSuccessfulRequest( result_data );
}
}
};
});
};

// Grab cached results
Plugin.prototype.getCache = function() {
var self = this;
// Handle Errors requests
Github.prototype.handleErrorRequest = function ( result_data ) {
var self = this;

if ( window.sessionStorage ) {
return window.sessionStorage.getItem( "gh-repos:" + self.repo );
}
else {
return false;
}
};
console.warn( result_data.message );
return;
};

// Handle Errors requests
Plugin.prototype.handlerErrorRequests = function ( result_data ) {
var self = this;
// Handle Successful request
Github.prototype.handleSuccessfulRequest = function ( result_data ) {
var self = this;

console.warn( result_data.message );
return;
};
self.applyTemplate( result_data );
self.setCache( result_data );
};

// Handle Successful request
Plugin.prototype.handlerSuccessfulRequest = function ( result_data ) {
var self = this;
// Stores repostories in sessionStorage if available
Github.prototype.setCache = function ( result_data ) {
var self = this;

self.applyTemplate( result_data );
self.cacheResults( result_data );
};
// Cache data
if ( window.sessionStorage ) {
window.sessionStorage.setItem( "gh-repos:" + self.repo, JSON.stringify( result_data ) );
}
};

// Parses Pushed date with date format
Plugin.prototype.parsePushedDate = function ( pushed_at ) {
var self = this,
date = new Date( pushed_at );
// Grab cached results
Github.prototype.getCache = function() {
var self = this;

return date.getDate() + "/" + ( date.getMonth() + 1 ) + "/" + date.getFullYear();
};
if ( window.sessionStorage ) {
return window.sessionStorage.getItem( "gh-repos:" + self.repo );
}
else {
return false;
}
};

// Parses repository URL to be friendly
Plugin.prototype.parseRepositoryURL = function ( url ) {
var self = this;
// Apply results to HTML template
Github.prototype.applyTemplate = function ( repo ) {
var self = this,
githubRepo = new GithubRepo( repo ),
$widget = githubRepo.toHTML();

return url.replace( "api.", "" ).replace( "repos/", "" );
};
$widget.appendTo( self.$container );
};

// Parses HTML template
Plugin.prototype.parseTemplate = function ( repo ) {
var self = this,
pushed_at = self.parsePushedDate( repo.pushed_at ),
repo_url = self.parseRepositoryURL( repo.url );

return $(
"<div class='github-box'>" +
"<div class='github-box-header'>" +
"<h3>" +
"<a href='" + repo_url + "'>" + repo.name + "</a>" +
"</h3>" +
"<div class='github-stats'>" +
"<a class='repo-stars' title='Stars' data-icon='7' href='" + repo_url + "/watchers'>" + repo.watchers + "</a>" +
"<a class='repo-forks' title='Forks' data-icon='f' href='" + repo_url + "/network'>" + repo.forks + "</a>" +
"<a class='repo-issues' title='Issues' data-icon='i' href='" + repo_url + "/issues'>" + repo.open_issues + "</a>" +
"</div>" +
"</div>" +
"<div class='github-box-content'>" +
"<p>" + repo.description + " &mdash; <a href='" + repo_url + "#readme'>Read More</a></p>" +
"</div>" +
"<div class='github-box-download'>" +
"<p class='repo-update'>Latest commit to <strong>master</strong> on " + pushed_at + "</p>" +
"<a class='repo-download' title='Download as zip' data-icon='w' href='" + repo_url + "/zipball/master'></a>" +
"</div>" +
"</div>");
};
// -- Attach plugin to jQuery's prototype --------------------------------------

// Request repositories from Github
Plugin.prototype.requestData = function ( repo ) {
var self = this;

$.ajax({
url: "https://api.github.com/repos/" + repo,
dataType: "jsonp",
success: function( results ) {
var result_data = results.data;

// Handle API failures
if ( results.meta.status >= 400 && result_data.message ) {
self.handlerErrorRequest();
}
else {
self.handlerSuccessfulRequest( result_data );
}
}
});
};
;( function ( $, window, undefined ) {

$.fn[pluginName] = function ( options ) {
$.fn.github = function ( options ) {
return this.each(function () {
if ( !$( this ).data( "plugin_" + pluginName ) ) {
$( this ).data( "plugin_" + pluginName, new Plugin( this, options ) );
if ( !$( this ).data( "plugin_github" ) ) {
$( this ).data( "plugin_github", new Github( this, options ) );
}
});
};

}( window.jQuery || window.Zepto, window ));
}( window.jQuery || window.Zepto, window ) );
4 changes: 2 additions & 2 deletions dist/jquery.github.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion github.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"repositories",
"git"
],
"version": "0.3.0",
"version": "0.3.2",
"author": {
"name": "Zeno Rocha",
"url": "https://github.com/zenorocha"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A jQuery plugin to display your Github Repositories.",
"author": "Zeno Rocha",
"homepage": "https://github.com/zenorocha/jquery-github/",
"version": "0.3.1",
"version": "0.3.2",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-cli": "~0.1.7",
Expand Down

0 comments on commit 31714d0

Please sign in to comment.