aboutsummaryrefslogtreecommitdiff
path: root/widget.js
blob: 4d6a8f0da5f0a1e34dee2b3f29d38671a69b4072 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Widget that shows the last commit made in a users github repositories.
 *
 * Based on the avvikelse widget by Erik Pettersson (@ptz0n)
 * http://dev.av.vikel.se
 *
 * By Johan Nilsson (http://markupartist.com)
 */
(function() {

    // Localize jQuery variable
    var jQuery;
    var remoteJquery = 'http://code.jquery.com/jquery-1.7.min.js';
    var localJquery = 'http://johannilsson.com/lastcommit/jquery-1.7.min.js';

    /**
     * Load jQuery if not present
     */
    if (window.jQuery === undefined || window.jQuery.fn.jquery >= '1.6.4') {
        appendJqueryScriptTag(remoteJquery, localJquery);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        init();
    }

    /**
     * Init widgets
     */
    function init() {
        jQuery(document).ready(function($) {
            var selector = '.lastcommit-widget';

            $.each($(selector), function(i, e) {
                var element = $(this);
                var user = element.attr('data-user');
                getLastCommit(user, function(repository, commit) {
                    renderWidget(element, {
                        user: user,
                        repository: repository,
                        commit: commit
                    });
                });
            });

            // Style
            var cssLink = $('<link>', {
                rel: 'stylesheet',
                type: 'text/css',
                href: 'http://johannilsson.com/lastcommit/widget.css'
            });
            cssLink.appendTo('head');

            /**
             * Finds the last commit for the user.
             *
             * @param user The user
             * @param callback A function to call when commit has been
             * retrieved will be called with repository and commit.
             */
            function getLastCommit(user, callback) {
                $.ajax({
                    url: 'https://api.github.com/users/' + user + '/repos',
                    dataType: 'jsonp',
                    data: {},
                    success: function(data) {
                        repositories = data.data;
                        repositories.sort(function(a, b) {
                            return Date.parse(a.pushed_at) - Date.parse(b.pushed_at);
                        });
                        var repository = repositories[repositories.length - 1];
                        $.ajax({
                            url: 'https://api.github.com/repos/' + user + '/' + repository.name + '/commits',
                            dataType: 'jsonp',
                            data: {},
                            success: function(data) {
                                // TODO: Verify author.
                                var lastCommit = data.data.shift();
                                callback(repository,lastCommit);
                            }
                        });
                    }
                });
            }

            /**
             * Render widget.
             *
             * @param element The element to render the widget to.
             * @param data Data to populate the widget with.
             */
            function renderWidget(element, data) {
                url = 'https://github.com/' + data.user + '/' + data.repository.name +'/commit/' + data.commit.sha
                var html = '<a href="' + url +'"> ' + data.commit.commit.message + '</a>';
                element.html('<span class="lastcommit">' + html + '</span>');
            }
        });
    }

    /**
     * Append jQuery script tag
     *
     * @param url
     * @param fallbackUrl
     */
    function appendJqueryScriptTag(url, fallbackUrl) {
        var scriptTag = document.createElement('script');
        scriptTag.setAttribute('type', 'text/javascript');
        scriptTag.setAttribute('src', url);
        scriptTag.onload = scriptLoadHandler;
        scriptTag.onreadystatechange = scriptReadyStateChangeHandler;

        if(fallbackUrl) {
            scriptTag.onerror = function()
            {
                appendJqueryScriptTag(fallbackUrl);
            };
        }

        (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(scriptTag);
    }

    /**
     * Script ready state change handler
     *
     * Same thing but for IE
     */
    function scriptReadyStateChangeHandler() {
        if(this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    }

    /**
     * Script load handler
     *
     * Called once jQuery has loaded
     */
    function scriptLoadHandler() {
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery.noConflict(true);

        // Call widgets init function
        init();
    }

})();