root / templates / poller.js @ 35e81722
History | View | Annotate | Download (5 kB)
1 | 97e42c7d | Leonidas Poulopoulos | // Copyright 2009 FriendFeed
|
---|---|---|---|
2 | 97e42c7d | Leonidas Poulopoulos | //
|
3 | 97e42c7d | Leonidas Poulopoulos | // Licensed under the Apache License, Version 2.0 (the "License"); you may
|
4 | 97e42c7d | Leonidas Poulopoulos | // not use this file except in compliance with the License. You may obtain
|
5 | 97e42c7d | Leonidas Poulopoulos | // a copy of the License at
|
6 | 97e42c7d | Leonidas Poulopoulos | //
|
7 | 97e42c7d | Leonidas Poulopoulos | // http://www.apache.org/licenses/LICENSE-2.0
|
8 | 97e42c7d | Leonidas Poulopoulos | //
|
9 | 97e42c7d | Leonidas Poulopoulos | // Unless required by applicable law or agreed to in writing, software
|
10 | 97e42c7d | Leonidas Poulopoulos | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
11 | 97e42c7d | Leonidas Poulopoulos | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
12 | 97e42c7d | Leonidas Poulopoulos | // License for the specific language governing permissions and limitations
|
13 | 97e42c7d | Leonidas Poulopoulos | // under the License.
|
14 | ca6bb3d5 | Leonidas Poulopoulos | var xhrlp = ''; |
15 | 97e42c7d | Leonidas Poulopoulos | $(document).ready(function() { |
16 | 97e42c7d | Leonidas Poulopoulos | if (!window.console) window.console = {};
|
17 | 97e42c7d | Leonidas Poulopoulos | if (!window.console.log) window.console.log = function() {}; |
18 | 97e42c7d | Leonidas Poulopoulos | |
19 | 97e42c7d | Leonidas Poulopoulos | $("#messageform").live("submit", function() { |
20 | 97e42c7d | Leonidas Poulopoulos | newMessage($(this)); |
21 | 97e42c7d | Leonidas Poulopoulos | return false; |
22 | 97e42c7d | Leonidas Poulopoulos | }); |
23 | 97e42c7d | Leonidas Poulopoulos | $("#messageform").live("keypress", function(e) { |
24 | 97e42c7d | Leonidas Poulopoulos | if (e.keyCode == 13) { |
25 | 97e42c7d | Leonidas Poulopoulos | newMessage($(this)); |
26 | 97e42c7d | Leonidas Poulopoulos | return false; |
27 | 97e42c7d | Leonidas Poulopoulos | } |
28 | 97e42c7d | Leonidas Poulopoulos | }); |
29 | 97e42c7d | Leonidas Poulopoulos | $("#message").select(); |
30 | 94fb8123 | Leonidas Poulopoulos | {% if user.is_authenticated %}
|
31 | 97e42c7d | Leonidas Poulopoulos | updater.start(); |
32 | bec1a51b | Leonidas Poulopoulos | updater.poll(); |
33 | ca6bb3d5 | Leonidas Poulopoulos | |
34 | 94fb8123 | Leonidas Poulopoulos | {% endif %} |
35 | 97e42c7d | Leonidas Poulopoulos | }); |
36 | 97e42c7d | Leonidas Poulopoulos | |
37 | ca6bb3d5 | Leonidas Poulopoulos | |
38 | ca6bb3d5 | Leonidas Poulopoulos | |
39 | 97e42c7d | Leonidas Poulopoulos | function newMessage(form) { |
40 | 97e42c7d | Leonidas Poulopoulos | var message = form.formToDict();
|
41 | 97e42c7d | Leonidas Poulopoulos | var disabled = form.find("input[type=submit]"); |
42 | 97e42c7d | Leonidas Poulopoulos | disabled.disable(); |
43 | 97e42c7d | Leonidas Poulopoulos | $.postJSON("{% url fetch-new %}", message, function(response) { |
44 | 97e42c7d | Leonidas Poulopoulos | updater.showMessage(response); |
45 | 97e42c7d | Leonidas Poulopoulos | if (message.id) {
|
46 | 97e42c7d | Leonidas Poulopoulos | form.parent().remove(); |
47 | 97e42c7d | Leonidas Poulopoulos | } else {
|
48 | 97e42c7d | Leonidas Poulopoulos | form.find("input[type=text]").val("").select(); |
49 | 97e42c7d | Leonidas Poulopoulos | disabled.enable(); |
50 | 97e42c7d | Leonidas Poulopoulos | } |
51 | 97e42c7d | Leonidas Poulopoulos | }); |
52 | 97e42c7d | Leonidas Poulopoulos | } |
53 | 97e42c7d | Leonidas Poulopoulos | |
54 | 97e42c7d | Leonidas Poulopoulos | function getCookie(name) { |
55 | 97e42c7d | Leonidas Poulopoulos | var r = document.cookie.match("\\b" + name + "=([^;]*)\\b"); |
56 | 97e42c7d | Leonidas Poulopoulos | return r ? r[1] : undefined; |
57 | 97e42c7d | Leonidas Poulopoulos | } |
58 | 97e42c7d | Leonidas Poulopoulos | |
59 | 97e42c7d | Leonidas Poulopoulos | jQuery.postJSON = function(url, args, callback) { |
60 | bec1a51b | Leonidas Poulopoulos | $.ajax({url: url, dataType: "json", type: "POST", |
61 | 97e42c7d | Leonidas Poulopoulos | success: function(response) { |
62 | bec1a51b | Leonidas Poulopoulos | if (callback) callback(response);
|
63 | 97e42c7d | Leonidas Poulopoulos | }, error: function(response) { |
64 | bec1a51b | Leonidas Poulopoulos | console.log("ERROR:", response);
|
65 | 97e42c7d | Leonidas Poulopoulos | }}); |
66 | 97e42c7d | Leonidas Poulopoulos | }; |
67 | 97e42c7d | Leonidas Poulopoulos | |
68 | 97e42c7d | Leonidas Poulopoulos | jQuery.fn.formToDict = function() { |
69 | 97e42c7d | Leonidas Poulopoulos | var fields = this.serializeArray(); |
70 | 97e42c7d | Leonidas Poulopoulos | var json = {}
|
71 | 97e42c7d | Leonidas Poulopoulos | for (var i = 0; i < fields.length; i++) { |
72 | 97e42c7d | Leonidas Poulopoulos | json[fields[i].name] = fields[i].value; |
73 | 97e42c7d | Leonidas Poulopoulos | } |
74 | 97e42c7d | Leonidas Poulopoulos | if (json.next) delete json.next; |
75 | 97e42c7d | Leonidas Poulopoulos | return json;
|
76 | 97e42c7d | Leonidas Poulopoulos | }; |
77 | 97e42c7d | Leonidas Poulopoulos | |
78 | 97e42c7d | Leonidas Poulopoulos | jQuery.fn.disable = function() { |
79 | 97e42c7d | Leonidas Poulopoulos | this.enable(false); |
80 | 97e42c7d | Leonidas Poulopoulos | return this; |
81 | 97e42c7d | Leonidas Poulopoulos | }; |
82 | 97e42c7d | Leonidas Poulopoulos | |
83 | 97e42c7d | Leonidas Poulopoulos | jQuery.fn.enable = function(opt_enable) { |
84 | 97e42c7d | Leonidas Poulopoulos | if (arguments.length && !opt_enable) { |
85 | 97e42c7d | Leonidas Poulopoulos | this.attr("disabled", "disabled"); |
86 | 97e42c7d | Leonidas Poulopoulos | } else {
|
87 | 97e42c7d | Leonidas Poulopoulos | this.removeAttr("disabled"); |
88 | 97e42c7d | Leonidas Poulopoulos | } |
89 | 97e42c7d | Leonidas Poulopoulos | return this; |
90 | 97e42c7d | Leonidas Poulopoulos | }; |
91 | 97e42c7d | Leonidas Poulopoulos | |
92 | 97e42c7d | Leonidas Poulopoulos | var updater = {
|
93 | 97e42c7d | Leonidas Poulopoulos | errorSleepTime: 500, |
94 | 97e42c7d | Leonidas Poulopoulos | cursor: null, |
95 | 97e42c7d | Leonidas Poulopoulos | start: function() { |
96 | bec1a51b | Leonidas Poulopoulos | $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "json", |
97 | 94fb8123 | Leonidas Poulopoulos | success: updater.onFetchExisting,
|
98 | 97e42c7d | Leonidas Poulopoulos | error: updater.onError});
|
99 | 97e42c7d | Leonidas Poulopoulos | }, |
100 | 97e42c7d | Leonidas Poulopoulos | |
101 | 97e42c7d | Leonidas Poulopoulos | poll: function() { |
102 | ca6bb3d5 | Leonidas Poulopoulos | {% if user.is_authenticated %}
|
103 | 828ef1fd | Leonidas Poulopoulos | if (updater.errorSleepTime > 128000){ |
104 | bec1a51b | Leonidas Poulopoulos | window.setTimeout('location.reload()', 500); |
105 | bec1a51b | Leonidas Poulopoulos | } |
106 | bec1a51b | Leonidas Poulopoulos | timeout = {{timeout}}; |
107 | bec1a51b | Leonidas Poulopoulos | $.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "json", |
108 | ca6bb3d5 | Leonidas Poulopoulos | success: updater.onSuccess,
|
109 | bec1a51b | Leonidas Poulopoulos | timeout: timeout,
|
110 | ca6bb3d5 | Leonidas Poulopoulos | error: updater.onError});
|
111 | ca6bb3d5 | Leonidas Poulopoulos | {% endif %} |
112 | 97e42c7d | Leonidas Poulopoulos | }, |
113 | 97e42c7d | Leonidas Poulopoulos | onSuccess: function(response) { |
114 | 97e42c7d | Leonidas Poulopoulos | try {
|
115 | bec1a51b | Leonidas Poulopoulos | updater.newMessages(response); |
116 | 97e42c7d | Leonidas Poulopoulos | } catch (e) {
|
117 | 97e42c7d | Leonidas Poulopoulos | updater.onError(); |
118 | 97e42c7d | Leonidas Poulopoulos | return;
|
119 | 97e42c7d | Leonidas Poulopoulos | } |
120 | 97e42c7d | Leonidas Poulopoulos | updater.errorSleepTime = 500;
|
121 | 97e42c7d | Leonidas Poulopoulos | window.setTimeout(updater.poll, 0);
|
122 | 97e42c7d | Leonidas Poulopoulos | }, |
123 | 97e42c7d | Leonidas Poulopoulos | |
124 | 97e42c7d | Leonidas Poulopoulos | onFetchExisting: function(response) { |
125 | 97e42c7d | Leonidas Poulopoulos | try {
|
126 | bec1a51b | Leonidas Poulopoulos | updater.existingMessages(response); |
127 | bec1a51b | Leonidas Poulopoulos | |
128 | 97e42c7d | Leonidas Poulopoulos | } catch (e) {
|
129 | bec1a51b | Leonidas Poulopoulos | updater.onError(); |
130 | 97e42c7d | Leonidas Poulopoulos | return;
|
131 | 97e42c7d | Leonidas Poulopoulos | } |
132 | 97e42c7d | Leonidas Poulopoulos | }, |
133 | 97e42c7d | Leonidas Poulopoulos | |
134 | ca6bb3d5 | Leonidas Poulopoulos | onError: function(response, text) { |
135 | bec1a51b | Leonidas Poulopoulos | if (text == 'timeout'){ |
136 | bec1a51b | Leonidas Poulopoulos | window.setTimeout('location.reload()', 3000); |
137 | ca6bb3d5 | Leonidas Poulopoulos | } |
138 | bec1a51b | Leonidas Poulopoulos | updater.errorSleepTime *= 2;
|
139 | bec1a51b | Leonidas Poulopoulos | console.log("Poll error; sleeping for", updater.errorSleepTime, "ms"); |
140 | bec1a51b | Leonidas Poulopoulos | window.setTimeout(updater.poll, updater.errorSleepTime); |
141 | bec1a51b | Leonidas Poulopoulos | |
142 | 97e42c7d | Leonidas Poulopoulos | }, |
143 | 97e42c7d | Leonidas Poulopoulos | |
144 | 97e42c7d | Leonidas Poulopoulos | newMessages: function(response) { |
145 | 97e42c7d | Leonidas Poulopoulos | if (!response.messages) return; |
146 | bec1a51b | Leonidas Poulopoulos | if (response.messages.length == 0){ |
147 | bec1a51b | Leonidas Poulopoulos | return true; |
148 | bec1a51b | Leonidas Poulopoulos | } |
149 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = response.cursor; |
150 | 97e42c7d | Leonidas Poulopoulos | var messages = response.messages;
|
151 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = messages[messages.length - 1].id;
|
152 | bec1a51b | Leonidas Poulopoulos | console.log(messages.length, "new messages, cursor:", updater.cursor);
|
153 | 97e42c7d | Leonidas Poulopoulos | |
154 | 97e42c7d | Leonidas Poulopoulos | for (var i = 0; i < messages.length; i++) { |
155 | 97e42c7d | Leonidas Poulopoulos | updater.showMessage(messages[i]); |
156 | 97e42c7d | Leonidas Poulopoulos | } |
157 | 049a5a10 | Leonidas Poulopoulos | $("#hid_mid").val('UPDATED'); |
158 | 97e42c7d | Leonidas Poulopoulos | if (($('#console').dialog('isOpen')) == false){ |
159 | 97e42c7d | Leonidas Poulopoulos | blink("#consolebutton");
|
160 | 049a5a10 | Leonidas Poulopoulos | window.setTimeout('location.reload()', 3000); |
161 | 97e42c7d | Leonidas Poulopoulos | } |
162 | 97e42c7d | Leonidas Poulopoulos | }, |
163 | 97e42c7d | Leonidas Poulopoulos | |
164 | 97e42c7d | Leonidas Poulopoulos | existingMessages: function(response) { |
165 | 97e42c7d | Leonidas Poulopoulos | if (!response.messages) return; |
166 | bec1a51b | Leonidas Poulopoulos | if (response.messages.length == 0){ |
167 | bec1a51b | Leonidas Poulopoulos | return true; |
168 | bec1a51b | Leonidas Poulopoulos | } |
169 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = response.cursor; |
170 | 97e42c7d | Leonidas Poulopoulos | var messages = response.messages;
|
171 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = messages[messages.length - 1].id;
|
172 | 97e42c7d | Leonidas Poulopoulos | for (var i = 0; i < messages.length; i++) { |
173 | 97e42c7d | Leonidas Poulopoulos | updater.showMessage(messages[i]); |
174 | 97e42c7d | Leonidas Poulopoulos | } |
175 | 97e42c7d | Leonidas Poulopoulos | }, |
176 | 97e42c7d | Leonidas Poulopoulos | |
177 | 97e42c7d | Leonidas Poulopoulos | showMessage: function(message) { |
178 | 97e42c7d | Leonidas Poulopoulos | var existing = $("#m" + message.id); |
179 | 97e42c7d | Leonidas Poulopoulos | if (existing.length > 0) return; |
180 | 97e42c7d | Leonidas Poulopoulos | var node = $(message.html); |
181 | 97e42c7d | Leonidas Poulopoulos | node.hide(); |
182 | 97e42c7d | Leonidas Poulopoulos | // $('#inbox').val($('#inbox').val()+message.text);
|
183 | 97e42c7d | Leonidas Poulopoulos | $("#inbox").append(node); |
184 | 97e42c7d | Leonidas Poulopoulos | node.slideDown(); |
185 | 97e42c7d | Leonidas Poulopoulos | } |
186 | 97e42c7d | Leonidas Poulopoulos | }; |
187 | 97e42c7d | Leonidas Poulopoulos | |
188 | 97e42c7d | Leonidas Poulopoulos | function blink(selector){ |
189 | f86873f9 | Leonidas Poulopoulos | $(selector).animate({color: "#EE5F5B"}, 500, function(){ |
190 | f86873f9 | Leonidas Poulopoulos | $(this).animate({ color: "white" }, 500, function(){ |
191 | f86873f9 | Leonidas Poulopoulos | blink(this);
|
192 | f86873f9 | Leonidas Poulopoulos | }); |
193 | 97e42c7d | Leonidas Poulopoulos | }); |
194 | 97e42c7d | Leonidas Poulopoulos | } |