Skip to content

Commit

Permalink
0.1.4
Browse files Browse the repository at this point in the history
- OOP.
- better text sanitization.
- github api responses cached.
  • Loading branch information
fatbattk committed Sep 2, 2014
1 parent 23281e6 commit fc6cb27
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 77 deletions.
176 changes: 101 additions & 75 deletions js/jquery.gvg.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,116 @@
// gitvsgit jquery widget v 0.1.3 (http://git.io/XIauew)
// gitvsgit jquery widget v 0.1.4 (http://git.io/XIauew)
// by @fatbattk

;(function($){
var $gvgs = $('div.gvg');
if($gvgs.length)
{
var gvg_table_class = 'gvg-ring',
footer = 'made with /<a href="http://git.io/XIauew" target="_blank">gitvsgit</a>',
i = 0;

$('head').prepend('<style type="text/css">'
+'div.gvg{background-color:#EEE;border-radius:3px;padding:3px;width:500px;}'
+'table.'+ gvg_table_class +'{border:1px solid #CCC;font:13px Helvetica,arial,sans-serif;width:100%;}'
+'.'+ gvg_table_class +' td{background-color:#FFF;font-size:12px;padding:6px;}'
+'.'+ gvg_table_class +' .hilite td{background-color:#E6F1F6;}'
+'.'+ gvg_table_class +' thead th,.'+ gvg_table_class +' tfoot th{background-color:#FAFAFA;color:#555;}'
+'.'+ gvg_table_class +' thead th{background-image:-moz-linear-gradient(top,#FAFAFA,#EAEAEA);background-image:-webkit-linear-gradient(top,#FAFAFA,#EAEAEA);background-image:-ms-linear-gradient(top,#FAFAFA,#EAEAEA);background-image:linear-gradient(top,#FAFAFA,#EAEAEA);background-repeat:repeat-x;border-bottom:1px solid #D8D8D8;font-size:13px;padding:4px;}'
+'.'+ gvg_table_class +' tfoot th{font-size:10px;padding:2px 4px;text-align:right;}'
+'</style>');

$gvgs.each(function(idx){
var $gvg = $(this),
$repos = $gvg.data('repos');

if($repos.length>7)
{
var repo = $repos.split(','),
repo_count = repo.length,
repo_highlights = [],
gvgaj = [];

if($gvg.data('highlight'))
{
repo_highlights = $gvg.data('highlight').split(',');
$.fn.gvg = function(){
var table_class = 'gvg-ring', // class name of generated table.
header_html = '<thead><tr><th>Repos</th><th>Created</th><th>Updated</th><th>Stars</th><th>Forks</th><th>Open Issues</th></tr></thead>', // html added to table header.
footer_html = '<tfoot><tr><th colspan="6">made with /<a href="//git.io/XIauew" target="_blank">gitvsgit</a></th></tr></tfoot>', // html added to table footer.
api_cache = {}, // cache of api responses.
addCSS = function(){
$('head').prepend('<style type="text/css">'+
'div.gvg{background-color:#eee;border-radius:3px;padding:3px;width:500px;}'+
'table.'+ table_class +'{border:1px solid #ccc;font:13px Helvetica,arial,sans-serif;width:100%;}'+
'.'+ table_class +' td{background-color:#fff;font-size:12px;padding:6px;}'+
'.'+ table_class +' .hilite td{background-color:#e6f1f6;}'+
'.'+ table_class +' thead th,.'+ table_class +' tfoot th{background-color:#fafafa;color:#555;}'+
'.'+ table_class +' thead th{background-image:-moz-linear-gradient(top,#fafafa,#eaeaea);background-image:-webkit-linear-gradient(top,#fafafa,#eaeaea);background-image:-ms-linear-gradient(top,#fafafa,#eaeaea);background-image:linear-gradient(top,#fafafa,#eaeaea);background-repeat:repeat-x;border-bottom:1px solid #d8d8d8;font-size:13px;padding:4px;}'+
'.'+ table_class +' tfoot th{font-size:10px;padding:2px 4px;text-align:right;}'+
'</style>');
},
validRepo = function(str){
if(str.length>3 && str.indexOf('/')!==-1){
return true;
}

for(i=0; i<repo_count; i++)
{
if(repo[i].length>3)
{
gvgaj[i] = $.getJSON('https://api.github.com/repos/'+ repo[i] +'?callback=?');
}
return false;
},
getHilites = function(obj){
if(obj.data('highlight')){
return obj.data('highlight').split(',');
}
return [];
},
doAjax = function(url){
if(typeof api_cache[url]==='undefined'){
api_cache[url] = $.getJSON(url);
}
return api_cache[url];
},
// http://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
htmlEntities = function(str){
return String(str).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
},
formatDate = function(date){
return new Date(date).toDateString().replace(/[a-z]+ (.*?)/i,'');
},
formatNumber = function(num){
return String(num).replace(/\B(?=(\d{3})+(?!\d))/g,',');
},
buildTableBody = function(arg,hilites,rcount){
var response = arg[0],
table_body = '',
d = {};

$.when.apply(this,gvgaj).done(function(){
var gvgtbl = '<table class="'+ gvg_table_class +'" id="'+ gvg_table_class +'-'+ idx +'" border="0" cellspacing="1" cellpadding="2"><thead><tr><th>Repos</th><th>Created</th><th>Updated</th><th>Stars</th><th>Forks</th><th>Open Issues</th></tr></thead>';
if(footer.length)
{
gvgtbl += '<tfoot><tr><th colspan="6">'+ footer +'</th></tr></tfoot>';
for(var i=0; i<rcount; i++){
if(rcount>1){
response = arg[i][0];
}
gvgtbl += '<tbody>';
if(response){
d = response.data;

var gvga,gvgd;
for(i=0; i<repo_count; i++)
{
if(repo_count>1)
{
gvga = arguments[i][0];
}
else
{
gvga = arguments[0];
}
if(gvga)
{
gvgd = gvga.data;
if(gvga.meta.status===200 || gvga.meta.status===304)
{
gvgtbl += '<tr';
if(repo_highlights.length>0 && $.inArray(gvgd.full_name,repo_highlights)>-1)
{
gvgtbl += ' class="hilite"';
}
gvgtbl += '><td title="'+ gvgd.description +'"><a href="'+ gvgd.html_url +'" target="_blank">'+ gvgd.full_name +'</a></td><td>'+ new Date(gvgd.created_at).toDateString().replace(/[a-z]+ (.*?)/i,'') +'</td><td>'+ new Date(gvgd.updated_at).toDateString().replace(/[a-z]+ (.*?)/i,'') +'</td><td align="right">'+ gvgd.watchers_count.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',') +'</td><td align="right">'+ gvgd.forks_count.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',') +'</td><td align="right">'+ gvgd.open_issues_count.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',') +'</td></tr>';
}
else if(gvga.meta.status===403)
{
gvgtbl += '<tr><td colspan="6" align="center">'+ gvgd.message +'</td></tr>';
break;
// success.
if(response.meta.status===200 || response.meta.status===304){
table_body += '<tr';
if(hilites.length>0 && $.inArray(d.full_name,hilites)>-1){
table_body += ' class="hilite"';
}
table_body += '><td title="'+ htmlEntities(d.description) +'"><a href="'+ d.html_url +'" target="_blank">'+ htmlEntities(d.full_name) +'</a></td><td>'+ formatDate(d.created_at) +'</td><td>'+ formatDate(d.updated_at) +'</td><td align="right">'+ formatNumber(d.watchers_count) +'</td><td align="right">'+ formatNumber(d.forks_count) +'</td><td align="right">'+ formatNumber(d.open_issues_count) +'</td></tr>';
}
// fail.
else if(response.meta.status===403){
table_body += '<tr><td colspan="6" align="center">'+ htmlEntities(d.message) +'</td></tr>';
}
}
}

return table_body;
},
addTable = function(obj,id,body){
obj.html('<table class="'+ table_class +'" id="'+ table_class +'-'+ id +'" border="0" cellspacing="1" cellpadding="2">'+ header_html + footer_html +'<tbody>'+ body +'</tbody></table>');
};

addCSS();
// loop each <div.gvg>.
return this.each(function(idx){
var $gvg = $(this), // selected dom.
$repos = $gvg.data('repos'); // csv of repos.

if(!validRepo($repos)){
return;
}

$gvg.html(gvgtbl +'</tbody></table>');
});
var repo = $repos.split(','), // array of repos.
repo_count = repo.length, // number of repo.
repo_highlights = getHilites($gvg), // array of repos that need "hilite" class.
xhr = []; // array of XMLHTTPRequest objects.

// loop each <data-repos=>.
for(var i=0; i<repo_count; i++){
if(validRepo(repo[i])){
// https://developer.github.com/v3/repos/#get
xhr[i] = doAjax('https://api.github.com/repos/'+ repo[i] +'?callback=?');
}
}

// generate table when all ajax call is complete.
$.when.apply(this,xhr).done(function(){
var body = buildTableBody(arguments,repo_highlights,repo_count); // html of table body.

addTable($gvg,idx,body);
});
});
}
};

// self-invoke.
$('div.gvg').gvg();
})(jQuery);
4 changes: 2 additions & 2 deletions js/jquery.gvg.min.js

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

0 comments on commit fc6cb27

Please sign in to comment.