/* * jQuery GitHub Badge 1.0 * * Copyright (c) 2009 Erik Zaadi * * Inspired by http://drnicjavascript.rubyforge.org/github_badge and * http://mattn.github.com/jquery-github-badge/ * * Home Page : http://projects.erikzaadi.com/jQueryPlugins/jQuery.GitHubBadge * jQuery Plugin home page : http://plugins.jquery.com/project/GitHubBadge * Wiki : http://wiki.github.com/erikzaadi/jQueryPlugins/jquerygithubbadge * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html */ ; (function($) { $.fn.GitHubBadge = function(options) { if (typeof (options) == 'string') options = { user: options }; var mainOptions = $.extend({}, $.fn.GitHubBadge.defaults, options); return this.each(function() { //Support Metadata Plug-in if available var opts = $.meta ? $.extend({}, mainOptions, $this.data()) : mainOptions; _GitHubBadge($(this), opts); }); }; $.fn.GitHubBadge.defaults = // Default options, can either be object, or simply the user name as string { user: '', // Mandatory (Duh!) showErrors: false, //Display error messages showForks: true, //Shows not only personal public repositories, but forks as well validateUser: false //Validates that user exists (404 can not be detecteted, due to JSONP..), NOTE: Generates an extra request }; /* CSS Classes used: GithubBadge -> Main container GithubBadgeTitle GithubBadgeRepo GithubBadgeFork GithubBadgeError */ function _GitHubBadge($gitHubBadgeElement, options) { if (options.validateUser) { _GitHubBadgeValidateUser($gitHubBadgeElement, options); } else { _getGitHubBadge($gitHubBadgeElement, options); } } function _GitHubBadgeValidateUser($gitHubBadgeElement, options) { $.ajax({ url: 'http://github.com/api/v2/json/user/search/' + options.user, data: {}, success: function(data) { if (data && data.users && data.users.length > 0) _getGitHubBadge($gitHubBadgeElement, options); else _GitHubBadgeError($gitHubBadgeElement, options); }, dataType: 'jsonp', error: function(XMLHttpRequest, textStatus, errorThrown) { if (console && console.log) { console.log('Error occured while getting the GitHub repositories for : "' + options.user + '"'); console.log(XMLHttpRequest); console.log(textStatus); console.log(errorThrown); } _GitHubBadgeError($gitHubBadgeElement, options); } }); } function _GitHubBadgeError($gitHubBadgeElement, options) { if (options.showErrors) { $gitHubBadgeElement.html('