public
Description: Simple javascript that allows for easy inclussion of your GitHub repo information in any webpage.
Homepage: http://nickelcode.com/2008/11/27/github-repo-information-in-your-webpages/
Clone URL: git://github.com/johnbender/github-repos.git
johnbender (author)
Fri Dec 19 09:22:39 -0800 2008
commit  e87418d3341dcb191be036a26f006e34ca7732b7
tree    643e579611c4770909781043c71663483b5786b9
parent  b41fe6ba373fea008c724dac8d2cab5211a918b0
github-repos / github-repos.js
100644 116 lines (104 sloc) 4.22 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//Written by John Bender for http://erlanguid.com
//See: http://nickelcode.com/2008/11/27/github-repo-information-in-your-webpages/
//for an explanation of implementation and how to use it
//released under the GPL v2
//http://www.gnu.org/licenses/gpl-2.0.txt
GITHUB_JSON_URL = "http://github.com/api/v1/json/{0}?callback={1}";
DYNAMIC_SCRIPT_TEMPLATE = "<script src='{0}' type='text/javascript' ></script>";
GITHUB_PROJECTS_SELECTOR = ".github-projects";
REPO_TEMPLATE =
    "<div class='github-repo'>"
    + "<a href='{0}' target='_blank'>"
    + "{1}" //+ "<img class='github-repo-link-image' src='http://erlanguid.com/icons/external.png'"
    + "</a>"
    +"</div>";
 
var GitHubRepos = new function(){
    this.displayObjects = new Array();
    
    //retrieve the repo information from github
    this.GetRepos = function()
    {
        //create an array of the github data, used to prevent mutiple hits
        //to the server for the same user
        var github_usernames = new Array();
 
        //for all the github-projects divs
        $(GITHUB_PROJECTS_SELECTOR).each(function(i) {
            //create an array of all unique userids in the page
            if(this.id != undefined)
            {
                var username = this.id;
                //add this object (div by default) that met the query parameters
                //to a list to be altered later
                if( GitHubRepos.displayObjects[username] == undefined)
                {
                   GitHubRepos.displayObjects[username] = new Array();
                }
                GitHubRepos.displayObjects[username][i] = this;
            }
        });
        //for each unique user
        for(var i in GitHubRepos.displayObjects)
        {
            //format the github url for our given user and callback function
            var github_url = format(GITHUB_JSON_URL, i, "GitHubRepos.DisplayRepos");
            //insert a script into the DOM to add our JSON data via the github url
            //once the script loads it will callback the DATA_DISPLAY_FUCNTION
            $('head').append(format(DYNAMIC_SCRIPT_TEMPLATE, github_url));
        }
    }
    //add the repo data to the div using the template
    this.DisplayRepos = function(data)
    {
        //if we got properly formed data
        if(data.user != undefined)
        {
            var user = data.user;
            //for all the div objects that correspond to that user
            for(var j in GitHubRepos.displayObjects[user.login])
            {
                var proj_div = GitHubRepos.displayObjects[user.login][j];
                //for each of the user repositories in the data
                $.each(user.repositories, function(i, repo) {
                    //add the templated information to the divs existing content
                    proj_div.innerHTML = proj_div.innerHTML + format(REPO_TEMPLATE, repo.url, repo.name);
                });
                //the style div.github-projects is display: none by default
                //display it if there was no error message
                proj_div.style.display = "block";
            }
        }
    }
 
    this.ToggleRepoVisibility = function(element)
    {
        var HIDE_TEXT = 'hide';
        var SHOW_TEXT = 'view';
 
        //take the show hide element, generally a link
        //and change the text to what makes sense.
        $(element).children('a.show-github-repos').each( function() {
            if(this.innerHTML == SHOW_TEXT)
                this.innerHTML = HIDE_TEXT;
            else
                this.innerHTML = SHOW_TEXT;
        });
 
        //show hide the elements given their state
        $(element).siblings().each( function(){
 
            var jElement = $(this);
            if(jElement.css('display') == 'none')
                jElement.show();
            else
                jElement.hide();
        });
    }
}
//used to create clean insertion of strings into templates
function format(str)
{
    for(i = 1; i < arguments.length; i++)
    {
        str = str.replace("{" + (i - 1) + "}", arguments[i]);
    }
    return str;
}
//Go Get Those REPOS!
$(document).ready(function(){
 
    GitHubRepos.GetRepos();
 
    });