aboutsummaryrefslogtreecommitdiffstats
path: root/syte/static/js/libs/github.js
blob: dfb12fb40b520e4a2b5acd101fc4eaaac0499f3a (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
/*
UI functions dedicated to the Github modal panel
*/

var github_api_user = 'https://api.github.com/users/';
var github_api_repos = '/repos';

var spinner = (new Spinner(spin_opts)).spin();
var template = null;
var url = null;
var github_data = {};

$('a[id^="Github-link"]').click(function (e)
{
    var url = prepare_link(e, this);
    adjustSelection("Github-link");
    remove_modal();
    showGithub(url, this);
});

function showGithub(e, t) {
    url = t.href;
    var github_profile = $("#github-profile");
    if (github_profile.length > 0) {
        github_profile.modal('show');
    }
    else {
        $("#Github-link").append(spinner.el);

        $.get('/theme/templates/github-view.html', function(data) {
            // Request succeeded, data contains HTML template, we can load data
            template = Handlebars.compile(data);
            var user_url = github_api_user+github_username;

            try {
                $.ajax({
                    url: user_url,
                    dataType: "jsonp",
                    jsonpCallback: "readGithubData",
                    error: function(s, statusCode, errorThrown) {
                        window.location.href = url;
                        spinner.stop();
                    }
                });
            }
            catch (err) {
                window.location.href = url;
                spinner.stop();
            }
        })
        .error(function() {
            window.location.href = url;
            spinner.stop();
        });
    }
}

function readGithubData(user) {
    try {
        github_data['user'] = user.data

        var repos_url = github_api_user+github_username+github_api_repos;
        $.ajax({
            url: repos_url,
            dataType: "jsonp",
            jsonpCallback: "readRepositories",
            error: function(s, statusCode, errorThrown) {
                window.location.href = url;
                spinner.stop();
            }
        });
    }
    catch (err) {
        window.location.href = url;
        spinner.stop();
    }
}

function readRepositories(repos) {
    try {
        github_data['repositories'] = repos.data

        var html = template(github_data);
        $('body').append(html);
        $("#github-profile").modal();
        spinner.stop();
    }
    catch (err) {
        window.location.href = url;
        spinner.stop();
    }
}