From 31714d0d1b6e78a9414a2e72242e2fb3a291c4ed Mon Sep 17 00:00:00 2001 From: Zeno Rocha Date: Sat, 5 Oct 2013 17:55:58 -0300 Subject: [PATCH] 0.3.2 --- dist/jquery.github.js | 314 ++++++++++++++++++++------------------ dist/jquery.github.min.js | 4 +- github.jquery.json | 2 +- package.json | 2 +- 4 files changed, 168 insertions(+), 154 deletions(-) diff --git a/dist/jquery.github.js b/dist/jquery.github.js index 8581e7f..220967d 100644 --- a/dist/jquery.github.js +++ b/dist/jquery.github.js @@ -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 $( + "
" + + "
" + + "

" + + "" + self.name + "" + + "

" + + "" + + "
" + + "
" + + "

" + self.description + " — Read More

" + + "
" + + "
" + + "

Latest commit to master on " + self.pushed_at + "

" + + "" + + "
" + + "
"); +}; + +// 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 $( - "
" + - "
" + - "

" + - "" + repo.name + "" + - "

" + - "" + - "
" + - "
" + - "

" + repo.description + " — Read More

" + - "
" + - "
" + - "

Latest commit to master on " + pushed_at + "

" + - "" + - "
" + - "
"); - }; +// -- 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 ) ); diff --git a/dist/jquery.github.min.js b/dist/jquery.github.min.js index 151704c..87920e9 100644 --- a/dist/jquery.github.min.js +++ b/dist/jquery.github.min.js @@ -1,9 +1,9 @@ /* - * 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(s,e){function t(e,t){var i=this;i.element=e,i.$container=s(e),i.repo=i.$container.attr("data-repo"),i.options=s.extend({},a,t),i._defaults=a,i._name=o,i.init(),i.displayIcons()}var o="github",a=(e.document,{iconStars:!0,iconForks:!0,iconIssues:!1});t.prototype.init=function(){var s=this,e=s.getCache();null!==e?s.applyTemplate(JSON.parse(e)):s.requestData(s.repo)},t.prototype.displayIcons=function(){$iconStars=s(".repo-stars"),$iconForks=s(".repo-forks"),$iconIssues=s(".repo-issues"),this.options.iconStars?$iconStars.css("display","inline-block"):$iconStars.css("display","none"),this.options.iconForks?$iconForks.css("display","inline-block"):$iconForks.css("display","none"),this.options.iconIssues?$iconIssues.css("display","inline-block"):$iconIssues.css("display","none")},t.prototype.applyTemplate=function(s){var e=this,t=e.parseTemplate(s);t.appendTo(e.$container)},t.prototype.cacheResults=function(s){var t=this;e.sessionStorage&&e.sessionStorage.setItem("gh-repos:"+t.repo,JSON.stringify(s))},t.prototype.getCache=function(){var s=this;return e.sessionStorage?e.sessionStorage.getItem("gh-repos:"+s.repo):!1},t.prototype.handlerErrorRequests=function(s){console.warn(s.message)},t.prototype.handlerSuccessfulRequest=function(s){var e=this;e.applyTemplate(s),e.cacheResults(s)},t.prototype.parsePushedDate=function(s){var e=new Date(s);return e.getDate()+"/"+(e.getMonth()+1)+"/"+e.getFullYear()},t.prototype.parseRepositoryURL=function(s){return s.replace("api.","").replace("repos/","")},t.prototype.parseTemplate=function(e){var t=this,o=t.parsePushedDate(e.pushed_at),a=t.parseRepositoryURL(e.url);return s("
"+"
"+"

"+e.description+" — Read More

"+"
"+"
"+"

Latest commit to master on "+o+"

"+""+"
"+"
")},t.prototype.requestData=function(e){var t=this;s.ajax({url:"https://api.github.com/repos/"+e,dataType:"jsonp",success:function(s){var e=s.data;s.meta.status>=400&&e.message?t.handlerErrorRequest():t.handlerSuccessfulRequest(e)}})},s.fn[o]=function(e){return this.each(function(){s(this).data("plugin_"+o)||s(this).data("plugin_"+o,new t(this,e))})}})(window.jQuery||window.Zepto,window); \ No newline at end of file +function GithubRepo(s){this.description=s.description,this.forks=s.forks,this.name=s.name,this.open_issues=s.open_issues,this.pushed_at=s.pushed_at,this.url=s.url,this.watchers=s.watchers}function Github(s,t){var e=this,o={iconStars:!0,iconForks:!0,iconIssues:!1};e.element=s,e.$container=$(s),e.repo=e.$container.attr("data-repo"),e.options=$.extend({},o,t),e._defaults=o,e.init(),e.displayIcons()}GithubRepo.prototype.toHTML=function(){var s=this;return s.pushed_at=s._parsePushedDate(s.pushed_at),s.url=s._parseURL(s.url),$("
"+"
"+"

"+s.description+" — Read More

"+"
"+"
"+"

Latest commit to master on "+s.pushed_at+"

"+""+"
"+"
")},GithubRepo.prototype._parsePushedDate=function(s){var t=new Date(s);return t.getDate()+"/"+(t.getMonth()+1)+"/"+t.getFullYear()},GithubRepo.prototype._parseURL=function(s){return s.replace("api.","").replace("repos/","")},Github.prototype.init=function(){var s=this,t=s.getCache();null!==t?s.applyTemplate(JSON.parse(t)):s.requestData(s.repo)},Github.prototype.displayIcons=function(){$iconStars=$(".repo-stars"),$iconForks=$(".repo-forks"),$iconIssues=$(".repo-issues"),this.options.iconStars?$iconStars.css("display","inline-block"):$iconStars.css("display","none"),this.options.iconForks?$iconForks.css("display","inline-block"):$iconForks.css("display","none"),this.options.iconIssues?$iconIssues.css("display","inline-block"):$iconIssues.css("display","none")},Github.prototype.requestData=function(s){var t=this;$.ajax({url:"https://api.github.com/repos/"+s,dataType:"jsonp",success:function(s){var e=s.data;s.meta.status>=400&&e.message?t.handleErrorRequest(e):t.handleSuccessfulRequest(e)}})},Github.prototype.handleErrorRequest=function(s){console.warn(s.message)},Github.prototype.handleSuccessfulRequest=function(s){var t=this;t.applyTemplate(s),t.setCache(s)},Github.prototype.setCache=function(s){var t=this;window.sessionStorage&&window.sessionStorage.setItem("gh-repos:"+t.repo,JSON.stringify(s))},Github.prototype.getCache=function(){var s=this;return window.sessionStorage?window.sessionStorage.getItem("gh-repos:"+s.repo):!1},Github.prototype.applyTemplate=function(s){var t=this,e=new GithubRepo(s),o=e.toHTML();o.appendTo(t.$container)},function(s){s.fn.github=function(t){return this.each(function(){s(this).data("plugin_github")||s(this).data("plugin_github",new Github(this,t))})}}(window.jQuery||window.Zepto,window); \ No newline at end of file diff --git a/github.jquery.json b/github.jquery.json index f8c915c..4ab80ff 100644 --- a/github.jquery.json +++ b/github.jquery.json @@ -7,7 +7,7 @@ "repositories", "git" ], - "version": "0.3.0", + "version": "0.3.2", "author": { "name": "Zeno Rocha", "url": "https://github.com/zenorocha" diff --git a/package.json b/package.json index a77a78f..c988199 100644 --- a/package.json +++ b/package.json @@ -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",