travisjeffery / twoot forked from peterk/twoot

An open source light-weight OS X twitter client based on jQuery and Fluid

This URL has Read+Write access

twoot / twoot.js
100644 196 lines (167 sloc) 7.358 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* The Twitter request code is based on the jquery tweet extension by http://tweet.seaofclouds.com/
*
* */
var LAST_UPDATE;
 
//Reverse collection
jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};
 
 
(function($) {
    $.fn.gettweets = function(o){
        return this.each(function(){
            var list = $('ul.tweet_list').prependTo(this);
            var url = 'http://twitter.com/statuses/friends_timeline.json' + getSinceParameter();
 
            $.getJSON(url, function(data){
                $.each(data.reverse(), function(i, item) {
                    if($("#msg-" + item.id).length == 0) { // <- fix for twitter caching which sometimes have problems with the "since" parameter
                    list.prepend('<li id="msg-' + item.id + '">' +
                    '<img class="profile_image" src="' + item.user.profile_image_url + '" alt="' + item.user.name + '" />' +
                    '<span class="time" title="' + item.created_at + '">' +
                    '<a class="visit_status" href="http://twitter.com/' + item.user.screen_name + '/status/' + item.id + '">' + relative_time(item.created_at) + '</a>' +
                    '</span>' +
                    ' <a class="user" title="' + item.user.name + '" href="http://twitter.com/' + item.user.screen_name + '">' + item.user.screen_name + '</a>' +
                    ' <a class="retweet" title="retweet this update" href="javascript:reTweet(\'' + item.user.screen_name + '\', \'' + item.text + '\')">&#x267A;</a>' +
                    ' <a class="favorite" title="favorite this update" href="javascript:toggleFavorite(' + item.id + ')">&#10029;</a>' +
                    ' <a class="reply" title="reply to ' + item.user.screen_name + '" href="javascript:replyTo(\'' + item.user.screen_name + '\', ' + item.id + ')">@</a>' +
                    '<div class="tweet_text">' + item.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/g, '<a href="$1">$1</a>').replace(/[\@]+([A-Za-z0-9-_]+)/g, '<a href="http://twitter.com/$1">@$1</a>').replace(/[&lt;]+[3]/g, "<tt class='heart'>&#x2665;</tt>") + '</div>' +
                    '</li>');
 
                    if (item.favorited) {
                        $('#msg-' + item.id + ' a.favorite').css('color', '#FF0');
                    }
 
                    // Don't want Growl notifications? Comment out the following method call
                    fluid.showGrowlNotification({
                        title: item.user.name + " @" + item.user.screen_name,
                        description: item.text,
                        priority: 2,
                        icon: item.user.profile_image_url
                    });
 
                }
            });
        });
    });
};
})(jQuery);
 
function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000, null);
    delta = delta + (relative_to.getTimezoneOffset() * 60);
    if (delta < 60) {
        return '< a minute ago';
    } else if(delta < 120) {
        return 'a minute ago';
    } else if(delta < (45*60)) {
        return (parseInt(delta / 60, null)).toString() + ' minutes ago';
    } else if(delta < (90*60)) {
        return 'an hour ago';
    } else if(delta < (24*60*60)) {
        return '' + (parseInt(delta / 3600, null)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400, null)).toString() + ' days ago';
    }
};
 
 
//get all span.time and recalc from title attribute
function recalcTime() {
    $('a.visit_status').each(function(index) {
        $(this).text(relative_time($(this).parents("span.time").attr("title")));
    });
}
 
 
function getSinceParameter() {
    if(LAST_UPDATE == null) {
        return "";
    } else {
        return "?since=" + LAST_UPDATE;
    }
}
 
function showAlert(message) {
    $("#alert p").text(message);
    $("#alert").fadeIn("fast");
    return;
}
 
function reTweet(username, text) {
    $('#status').val($("#status").val() + " RT @" + username + ": \"" + text + "\"");
    $('#status').focus();
    updateStatusCount();
    return;
}
 
function refreshMessages() {
    showAlert("Getting new tweets...");
    $(".tweets").gettweets();
    LAST_UPDATE = new Date().toGMTString();
    $("#alert").fadeOut(2000);
    return;
}
 
function replyTo(screen_name, status_id) {
    $("#status").val($("#status").val() + ' @' + screen_name + ' ');
    $("#status").focus();
    window.in_reply_to_status_id = status_id;
    return;
}
 
function setStatus(status_text) {
    if (window.in_reply_to_status_id) {
        $.post("http://twitter.com/statuses/update.json", { status: status_text, in_reply_to_status_id: window.in_reply_to_status_id, source: "twoot" }, function(data) { checkStatus(); refreshStatusField(); }, "json" );
    } else {
        $.post("http://twitter.com/statuses/update.json", { status: status_text, source: "twoot" }, function(data) { checkStatus(); refreshStatusField(); }, "json" );
    };
    window.in_reply_to_status_id = null;
    return;
}
 
function refreshStatusField() {
    //maybe show some text below field with last message sent?
    refreshMessages();
    $("#status_count").text("140");
    $('html').animate({scrollTop:0}, 'fast');
    return;
}
 
function updateStatusCount() {
    window.statusCount = 140 - $("#status").val().length;
    $("#status_count").text(window.statusCount.toString());
    return;
}
 
function checkStatus () {
    var origColor = $('#status').css("background-color");
    if ($('#status').val().length == 140) {
        $("#status").val("Twoosh!").css("background-color","#CF6").fadeOut('slow', function() {
            $("#status").val("").css("background-color", origColor).fadeIn('slow');
        });
    } else {
        $('#status').val($("#status").val()).css("background-color","#CF6").fadeOut('slow', function() {
            $('#status').val("").css("background-color", origColor).fadeIn('slow');
        });
    }
}
 
function toggleFavorite(id) {
    $.getJSON("http://twitter.com/statuses/show/" + id + ".json",
    function(data){
        if (data.favorited) {
            $.post('http://twitter.com/favorites/destroy/' + id + '.json');
            $('#msg-' + id + ' a.favorite').css('color', '#CCC');
        }
        else {
            $.post('http://twitter.com/favorites/create/' + id + '.json');
            $('#msg-' + id + ' a.favorite').css('color', '#FF0');
        }
    }
);
}
 
 
// set up basic stuff for first load
$(document).ready(function(){
    
    //get the user's messages
    refreshMessages();
 
    //add event capture to form submit
    $("#status_entry").submit(function() {
        setStatus($("#status").val());
        return false;
    });
 
    //set timer to reload messages every 70 secs
    window.setInterval("refreshMessages()", 65000);
 
    //set timer to recalc timestamps every 60 secs
    window.setInterval("recalcTime()", 60000);
 
    //Bind r key to request new messages
    $(document).bind('keydown', {combi:'r', disableInInput: true}, refreshMessages);
});