Statistics
| Branch: | Tag: | Revision:

root / templates / poller.js @ bec1a51b

History | View | Annotate | Download (5 kB)

1
// Copyright 2009 FriendFeed
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
// not use this file except in compliance with the License. You may obtain
5
// a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
// License for the specific language governing permissions and limitations
13
// under the License.
14
var xhrlp = '';
15
$(document).ready(function() {
16
    if (!window.console) window.console = {};
17
    if (!window.console.log) window.console.log = function() {};
18

    
19
    $("#messageform").live("submit", function() {
20
        newMessage($(this));
21
        return false;
22
    });
23
    $("#messageform").live("keypress", function(e) {
24
        if (e.keyCode == 13) {
25
            newMessage($(this));
26
            return false;
27
        }
28
    });
29
    $("#message").select();
30
    {% if user.is_authenticated %}
31
    updater.start();
32
    updater.poll();
33
    
34
    {% endif %}
35
});
36

    
37

    
38

    
39
function newMessage(form) {
40
    var message = form.formToDict();
41
    var disabled = form.find("input[type=submit]");
42
    disabled.disable();
43
    $.postJSON("{% url fetch-new %}", message, function(response) {
44
        updater.showMessage(response);
45
        if (message.id) {
46
            form.parent().remove();
47
        } else {
48
            form.find("input[type=text]").val("").select();
49
            disabled.enable();
50
        }
51
    });
52
}
53

    
54
function getCookie(name) {
55
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
56
    return r ? r[1] : undefined;
57
}
58

    
59
jQuery.postJSON = function(url, args, callback) {
60
    $.ajax({url: url, dataType: "json", type: "POST",
61
            success: function(response) {
62
        if (callback) callback(response);
63
    }, error: function(response) {
64
        console.log("ERROR:", response);
65
    }});
66
};
67

    
68
jQuery.fn.formToDict = function() {
69
    var fields = this.serializeArray();
70
    var json = {}
71
    for (var i = 0; i < fields.length; i++) {
72
        json[fields[i].name] = fields[i].value;
73
    }
74
    if (json.next) delete json.next;
75
    return json;
76
};
77

    
78
jQuery.fn.disable = function() {
79
    this.enable(false);
80
    return this;
81
};
82

    
83
jQuery.fn.enable = function(opt_enable) {
84
    if (arguments.length && !opt_enable) {
85
        this.attr("disabled", "disabled");
86
    } else {
87
        this.removeAttr("disabled");
88
    }
89
    return this;
90
};
91

    
92
var updater = {
93
    errorSleepTime: 500,
94
    cursor: null,
95
    start: function() {
96
                $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "json",
97
                    success: updater.onFetchExisting,
98
                    error: updater.onError});
99
        },
100
    
101
    poll: function() {
102
            {% if user.is_authenticated %}
103
            if (errorSleepTime > 128000){
104
                    window.setTimeout('location.reload()', 500);
105
            }
106
            timeout = {{timeout}};
107
            $.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "json",
108
                    success: updater.onSuccess,
109
                    timeout: timeout,
110
                    error: updater.onError});
111
            {% endif %}
112
    },
113
    onSuccess: function(response) {
114
        try {
115
            updater.newMessages(response);
116
        } catch (e) {
117
            updater.onError();
118
            return;
119
        }
120
        updater.errorSleepTime = 500;
121
        window.setTimeout(updater.poll, 0);
122
    },
123

    
124
    onFetchExisting: function(response) {
125
            try {
126
                updater.existingMessages(response);
127

    
128
            } catch (e) {
129
                updater.onError();
130
                return;
131
            }
132
        },
133
     
134
    onError: function(response, text) {
135
                if (text == 'timeout'){
136
                        window.setTimeout('location.reload()', 3000);
137
                }
138
                updater.errorSleepTime *= 2;
139
                        console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
140
                        window.setTimeout(updater.poll, updater.errorSleepTime);
141
                
142
    },
143

    
144
    newMessages: function(response) {
145
        if (!response.messages) return;
146
        if (response.messages.length == 0){
147
                return true;
148
        }
149
        updater.cursor = response.cursor;
150
        var messages = response.messages;
151
        updater.cursor = messages[messages.length - 1].id;
152
        console.log(messages.length, "new messages, cursor:", updater.cursor);
153
        
154
        for (var i = 0; i < messages.length; i++) {
155
            updater.showMessage(messages[i]);
156
        }
157
        $("#hid_mid").val('UPDATED');
158
        if (($('#console').dialog('isOpen')) == false){
159
                blink("#consolebutton");
160
                window.setTimeout('location.reload()', 3000);
161
        }
162
    },
163

    
164
    existingMessages: function(response) {
165
            if (!response.messages) return;
166
            if (response.messages.length == 0){
167
                    return true;
168
            }
169
            updater.cursor = response.cursor;
170
            var messages = response.messages;
171
            updater.cursor = messages[messages.length - 1].id;
172
            for (var i = 0; i < messages.length; i++) {
173
                updater.showMessage(messages[i]);
174
            }
175
        },
176
   
177
    showMessage: function(message) {
178
        var existing = $("#m" + message.id);
179
        if (existing.length > 0) return;
180
        var node = $(message.html);
181
        node.hide();
182
//         $('#inbox').val($('#inbox').val()+message.text); 
183
        $("#inbox").append(node);
184
        node.slideDown();
185
    }
186
};
187

    
188
function blink(selector){
189
        $(selector).animate({ color: "red" }, 500, function(){
190
        $(this).animate({ color: "#555555" }, 500, function(){
191
        blink(this);
192
        });
193
        });
194
}
195