aboutsummaryrefslogtreecommitdiffstats
path: root/syte/static/js/libs/twitter.js
blob: 2b61c3d5d1d44d78eafaaf198d15fb6190cfaec8 (plain) (blame)
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
/*
UI functions dedicated to the Twitter modal panel
*/

var twitter_api_user = 'https://twitter.com/users/';
var twitter_api_timeline = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=';
var twitter_api_json = '.json';

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

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

function showTwitter(e, t) {
    url = t.href;
    var twitter_profile = $("#twitter-profile");
    if (twitter_profile.length > 0) {
        twitter_profile.modal('show');
    }
    else {
        $("#Twitter-link").append(spinner.el);

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

            try {
                $.ajax({
                    url: user_url,
                    dataType: "jsonp",
                    jsonpCallback: "readTwitterData",
                    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 readTwitterData(user) {
    try {
        user.statuses_count = numberWithCommas(user.statuses_count);
        user.friends_count = numberWithCommas(user.friends_count);
        user.followers_count = numberWithCommas(user.followers_count);
        user.description = twitterLinkify(user.description);
        twitter_data['user'] = user

        var tweets_url = twitter_api_timeline+twitter_username;
        $.ajax({
            url: tweets_url,
            dataType: "jsonp",
            jsonpCallback: "readTweets",
            error: function(s, statusCode, errorThrown) {
                window.location.href = url;
                spinner.stop();
            }
        });
    }
    catch (err) {
        window.location.href = url;
        spinner.stop();
    }
}

function readTweets(tweets) {
    try {
        for(var index = 0 ; index < tweets.length ; index++) {
            var tweet = tweets[index];
            tweet.formated_date = moment(tweet.created_at).fromNow();
            tweet.text = twitterLinkify(tweet.text);
        }
        twitter_data['tweets'] = tweets
        
        var html = template(twitter_data);
        $('body').append(html);
        $("#twitter-profile").modal();
        spinner.stop();
    }
    catch (err) {
        window.location.href = url;
        spinner.stop();
    }
}

function twitterLinkify(e) {
    return e = e.replace(/(https?:\/\/\S+)/gi, function (e) {
        return '<a href="' + e + '">' + e + "</a>"
    }), e = e.replace(/(^|) @(\w+)/gi, function (e) {
        return '<a href="http://twitter.com/' + e + '">' + e + "</a>"
    }), e = e.replace(/(^|) #(\w+)/gi, function (e) {
        return '<a href="http://search.twitter.com/search?q=' + e.replace(/#/, "%23") + '">' + e + "</a>"
    }), e
}