diff --git a/dist/jquery.github.js b/dist/jquery.github.js index 5f2ea58..d69c723 100644 --- a/dist/jquery.github.js +++ b/dist/jquery.github.js @@ -1,5 +1,5 @@ /* - * jQuery Github - v0.2.5 + * jQuery Github - v0.2.6 * A jQuery plugin to display your Github Repositories. * https://github.com/zenorocha/jquery-github/ @@ -7,124 +7,181 @@ * MIT License */ -// the semi-colon before function invocation is a safety net against concatenated -// scripts and/or other plugins which may not be closed properly. ;(function ( $, window, undefined ) { - - // undefined is used here as the undefined global variable in ECMAScript 3 is - // mutable (ie. it can be changed by someone else). undefined isn't really being - // passed in so we can ensure the value of it is truly undefined. In ES5, undefined - // can no longer be modified. - - // window is passed through as local variable rather than global - // as this (slightly) quickens the resolution process and can be more efficiently - // minified (especially when both are regularly referenced in your plugin). - - // Create the defaults once var pluginName = 'github', - document = window.document, - defaults = { + document = window.document, + defaults = { propertyName: "value" }; - // The actual plugin constructor function Plugin( element, options ) { - this.element = element; - this.$container = $(element); - this.repo = this.$container.attr("data-repo"); + var self = this; + + self.element = element; + self.$container = $(element); + self.repo = self.$container.attr("data-repo"); - // jQuery has an extend method which merges the contents of two or - // more objects, storing the result in the first object. The first object - // is generally empty as we don't want to alter the default options for - // future instances of the plugin - this.options = $.extend( {}, defaults, options) ; + self.options = $.extend( {}, defaults, options) ; - this._defaults = defaults; - this._name = pluginName; + self._defaults = defaults; + self._name = pluginName; - this.init(); + self.init(); } + /** + * Initializer + * + */ Plugin.prototype.init = function () { - - var self = this; - var cached; - - // Attempt to get cached repo data - if (window.sessionStorage) { - cached = sessionStorage.getItem('gh-repos:' + this.repo); - } + var self = this, + cached = self.getCache(); if (cached !== null) { self.applyTemplate(JSON.parse(cached)); } else { + self.requestData(repo); + } + }; - $.ajax({ - url: 'https://api.github.com/repos/' + this.repo, - dataType: 'jsonp', - success: function(results){ + /** + * Apply results to HTML template + * + */ + Plugin.prototype.applyTemplate = function (repo) { + var self = this, + $widget = self.parseTemplate(repo); - // Handle API failures - if (results.meta.status >= 400 && results.data.message){ - console.warn(results.data.message); - return; - } - else { + $widget.appendTo(self.$container); + }; - self.applyTemplate(results.data); + /** + * Stores repostories in sessionStorage if available + * + */ + Plugin.prototype.cacheResults = function (result_data) { + var self = this; - // Cache data - if (window.sessionStorage) { - sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(results.data)); - } + // Cache data + if (window.sessionStorage) { + sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(result_data)); + } + }; - } - } - }); + /** + * Grab cached results + * + */ + Plugin.prototype.getCache = function () { + var self = this; + if (window.sessionStorage) { + return sessionStorage.getItem('gh-repos:' + self.repo); + } + else { + return false; } + }; + + /** + * Handle Errors requests + * + */ + Plugin.prototype.handlerErrorRequests = function (result_data) { + var self = this; + console.warn(result_data.message); + return; }; - Plugin.prototype.applyTemplate = function (repo) { + /** + * Handle Successful request + * + */ + Plugin.prototype.handlerSuccessfulRequest = function (result_data) { + var self = this; + + self.applyTemplate(result_data); + self.cacheResults(result_data); + }; + + /** + * Parses Pushed date with date format + * + */ + Plugin.prototype.parsePushedDate = function (pushed_at) { + var self = this, + date = new Date(pushed_at); + + return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + }; + /** + * Parses repository URL to be friendly + * + */ + Plugin.prototype.parseRepositoryURL = function (url) { var self = this; - var date = new Date(repo.pushed_at); - var pushed_at = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); - var $widget = $($.parseHTML(' \ + return url.replace('api.','').replace('repos/',''); + }; + + /** + * 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 $($.parseHTML(' \
\
\

\ - ' + repo.name + ' \ + ' + repo.name + ' \

\ \
\
\ -

' + repo.description + ' — Read More

\ +

' + repo.description + ' — Read More

\
\
\

Latest commit to master on ' + pushed_at + '

\ - Download as zip \ + Download as zip \
\
\ ')); - - self.appendTemplate($widget); - }; - Plugin.prototype.appendTemplate = function ($widget) { + /** + * Request repositories from Github + * + */ + Plugin.prototype.requestData = function (repo) { var self = this; - $widget.appendTo(self.$container); + + $.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); + } + } + }); }; - // A really lightweight plugin wrapper around the constructor, - // preventing against multiple instantiations $.fn[pluginName] = function ( options ) { return this.each(function () { if (!$.data(this, 'plugin_' + pluginName)) { @@ -132,5 +189,4 @@ } }); }; - }(jQuery, window)); \ No newline at end of file diff --git a/dist/jquery.github.min.js b/dist/jquery.github.min.js index 9067633..c601377 100644 --- a/dist/jquery.github.min.js +++ b/dist/jquery.github.min.js @@ -1,9 +1,9 @@ /* - * jQuery Github - v0.2.5 + * jQuery Github - v0.2.6 * A jQuery plugin to display your Github Repositories. * https://github.com/zenorocha/jquery-github/ * Copyright (c) 2013 * MIT License */ -(function(e,t,n){function o(t,n){this.element=t,this.$container=e(t),this.repo=this.$container.attr("data-repo"),this.options=e.extend({},s,n),this._defaults=s,this._name=r,this.init()}var r="github",i=t.document,s={propertyName:"value"};o.prototype.init=function(){var n=this,r;t.sessionStorage&&(r=sessionStorage.getItem("gh-repos:"+this.repo)),r!==null?n.applyTemplate(JSON.parse(r)):e.ajax({url:"https://api.github.com/repos/"+this.repo,dataType:"jsonp",success:function(e){if(e.meta.status>=400&&e.data.message){console.warn(e.data.message);return}n.applyTemplate(e.data),t.sessionStorage&&sessionStorage.setItem("gh-repos:"+n.repo,JSON.stringify(e.data))}})},o.prototype.applyTemplate=function(t){var n=this,r=new Date(t.pushed_at),i=r.getDate()+"/"+(r.getMonth()+1)+"/"+r.getFullYear(),s=e(e.parseHTML('

'+t.name+'

'+t.description+' — Read More

Latest commit to master on '+i+'

Download as zip
'));n.appendTemplate(s)},o.prototype.appendTemplate=function(e){var t=this;e.appendTo(t.$container)},e.fn[r]=function(t){return this.each(function(){e.data(this,"plugin_"+r)||e.data(this,"plugin_"+r,new o(this,t))})}})(jQuery,window); \ No newline at end of file +(function(a,b){function g(b,c){var e=this;e.element=b,e.$container=a(b),e.repo=e.$container.attr("data-repo"),e.options=a.extend({},f,c),e._defaults=f,e._name=d,e.init()}var d="github",f=(b.document,{propertyName:"value"});g.prototype.init=function(){var a=this,b=a.getCache();null!==b?a.applyTemplate(JSON.parse(b)):a.requestData(repo)},g.prototype.applyTemplate=function(a){var b=this,c=b.parseTemplate(a);c.appendTo(b.$container)},g.prototype.cacheResults=function(a){var c=this;b.sessionStorage&&sessionStorage.setItem("gh-repos:"+c.repo,JSON.stringify(a))},g.prototype.getCache=function(){var a=this;return b.sessionStorage?sessionStorage.getItem("gh-repos:"+a.repo):!1},g.prototype.handlerErrorRequests=function(a){console.warn(a.message)},g.prototype.handlerSuccessfulRequest=function(a){var b=this;b.applyTemplate(a),b.cacheResults(a)},g.prototype.parsePushedDate=function(a){var c=new Date(a);return c.getDate()+"/"+(c.getMonth()+1)+"/"+c.getFullYear()},g.prototype.parseRepositoryURL=function(a){return a.replace("api.","").replace("repos/","")},g.prototype.parseTemplate=function(b){var c=this,d=c.parsePushedDate(b.pushed_at),e=c.parseRepositoryURL(b.url);return a(a.parseHTML('

'+b.name+'

'+b.description+' — Read More

Latest commit to master on '+d+'

Download as zip
'))},g.prototype.requestData=function(b){var c=this;a.ajax({url:"https://api.github.com/repos/"+b,dataType:"jsonp",success:function(a){var b=a.data;a.meta.status>=400&&b.message?c.handlerErrorRequest():c.handlerSuccessfulRequest(b)}})},a.fn[d]=function(b){return this.each(function(){a.data(this,"plugin_"+d)||a.data(this,"plugin_"+d,new g(this,b))})}})(jQuery,window); \ No newline at end of file diff --git a/src/jquery.github.js b/src/jquery.github.js index b5817ba..392cf73 100644 --- a/src/jquery.github.js +++ b/src/jquery.github.js @@ -1,121 +1,178 @@ -// the semi-colon before function invocation is a safety net against concatenated -// scripts and/or other plugins which may not be closed properly. -;(function ( $, window, undefined ) { - - // undefined is used here as the undefined global variable in ECMAScript 3 is - // mutable (ie. it can be changed by someone else). undefined isn't really being - // passed in so we can ensure the value of it is truly undefined. In ES5, undefined - // can no longer be modified. - - // window is passed through as local variable rather than global - // as this (slightly) quickens the resolution process and can be more efficiently - // minified (especially when both are regularly referenced in your plugin). - - // Create the defaults once +;( function ( $, window, undefined ) { var pluginName = 'github', - document = window.document, - defaults = { + document = window.document, + defaults = { propertyName: "value" }; - // The actual plugin constructor function Plugin( element, options ) { - this.element = element; - this.$container = $(element); - this.repo = this.$container.attr("data-repo"); + var self = this; + + self.element = element; + self.$container = $(element); + self.repo = self.$container.attr("data-repo"); - // jQuery has an extend method which merges the contents of two or - // more objects, storing the result in the first object. The first object - // is generally empty as we don't want to alter the default options for - // future instances of the plugin - this.options = $.extend( {}, defaults, options) ; + self.options = $.extend( {}, defaults, options) ; - this._defaults = defaults; - this._name = pluginName; + self._defaults = defaults; + self._name = pluginName; - this.init(); + self.init(); } + /** + * Initializer + * + */ Plugin.prototype.init = function () { - - var self = this; - var cached; - - // Attempt to get cached repo data - if (window.sessionStorage) { - cached = sessionStorage.getItem('gh-repos:' + this.repo); - } + var self = this, + cached = self.getCache(); if (cached !== null) { self.applyTemplate(JSON.parse(cached)); } else { + self.requestData(repo); + } + }; - $.ajax({ - url: 'https://api.github.com/repos/' + this.repo, - dataType: 'jsonp', - success: function(results){ + /** + * Apply results to HTML template + * + */ + Plugin.prototype.applyTemplate = function (repo) { + var self = this, + $widget = self.parseTemplate(repo); - // Handle API failures - if (results.meta.status >= 400 && results.data.message){ - console.warn(results.data.message); - return; - } - else { + $widget.appendTo(self.$container); + }; - self.applyTemplate(results.data); + /** + * Stores repostories in sessionStorage if available + * + */ + Plugin.prototype.cacheResults = function (result_data) { + var self = this; - // Cache data - if (window.sessionStorage) { - sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(results.data)); - } + // Cache data + if (window.sessionStorage) { + window.sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(result_data)); + } + }; - } - } - }); + /** + * Grab cached results + * + */ + Plugin.prototype.getCache = function () { + var self = this; + if (window.sessionStorage) { + return window.sessionStorage.getItem('gh-repos:' + self.repo); + } + else { + return false; } + }; + + /** + * Handle Errors requests + * + */ + Plugin.prototype.handlerErrorRequests = function (result_data) { + var self = this; + console.warn(result_data.message); + return; }; - Plugin.prototype.applyTemplate = function (repo) { + /** + * Handle Successful request + * + */ + Plugin.prototype.handlerSuccessfulRequest = function (result_data) { + var self = this; + + self.applyTemplate(result_data); + self.cacheResults(result_data); + }; + + /** + * Parses Pushed date with date format + * + */ + Plugin.prototype.parsePushedDate = function (pushed_at) { + var self = this, + date = new Date(pushed_at); + + return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); + }; + /** + * Parses repository URL to be friendly + * + */ + Plugin.prototype.parseRepositoryURL = function (url) { var self = this; - var date = new Date(repo.pushed_at); - var pushed_at = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); - var $widget = $($.parseHTML(' \ + return url.replace('api.','').replace('repos/',''); + }; + + /** + * 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 $($.parseHTML(' \
\
\

\ - ' + repo.name + ' \ + ' + repo.name + ' \

\ \
\
\ -

' + repo.description + ' — Read More

\ +

' + repo.description + ' — Read More

\
\
\

Latest commit to master on ' + pushed_at + '

\ - Download as zip \ + Download as zip \
\
\ ')); - - self.appendTemplate($widget); - }; - Plugin.prototype.appendTemplate = function ($widget) { + /** + * Request repositories from Github + * + */ + Plugin.prototype.requestData = function (repo) { var self = this; - $widget.appendTo(self.$container); + + $.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); + } + } + }); }; - // A really lightweight plugin wrapper around the constructor, - // preventing against multiple instantiations $.fn[pluginName] = function ( options ) { return this.each(function () { if (!$.data(this, 'plugin_' + pluginName)) { @@ -123,5 +180,4 @@ } }); }; - }(jQuery, window)); \ No newline at end of file