root / templates / poller.js @ 7c4bc8de
History | View | Annotate | Download (5.2 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 | ca6bb3d5 | Leonidas Poulopoulos | updater.keepalive(); |
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 | 94fb8123 | Leonidas Poulopoulos | $.ajax({url: url, dataType: "text", type: "POST", |
61 | 97e42c7d | Leonidas Poulopoulos | success: function(response) { |
62 | 97e42c7d | Leonidas Poulopoulos | if (callback) callback(eval("(" + response + ")")); |
63 | 97e42c7d | Leonidas Poulopoulos | }, error: function(response) { |
64 | 97e42c7d | 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 | ca6bb3d5 | Leonidas Poulopoulos | xhrlp: null, |
96 | 9b17e57f | Leonidas Poulopoulos | keepalivetime: 120000, |
97 | ca6bb3d5 | Leonidas Poulopoulos | keepalive: function (){ |
98 | ca6bb3d5 | Leonidas Poulopoulos | try {
|
99 | ca6bb3d5 | Leonidas Poulopoulos | updater.xhrlp.abort(); |
100 | ca6bb3d5 | Leonidas Poulopoulos | } |
101 | ca6bb3d5 | Leonidas Poulopoulos | catch (e) {
|
102 | ca6bb3d5 | Leonidas Poulopoulos | } |
103 | ca6bb3d5 | Leonidas Poulopoulos | updater.poll(); |
104 | ca6bb3d5 | Leonidas Poulopoulos | if (updater.errorSleepTime == 500){ |
105 | 9b17e57f | Leonidas Poulopoulos | window.setTimeout(updater.keepalive, updater.keepalivetime); |
106 | 9b17e57f | Leonidas Poulopoulos | } |
107 | 9b17e57f | Leonidas Poulopoulos | else{
|
108 | 9b17e57f | Leonidas Poulopoulos | window.setTimeout(updater.keepalive, updater.keepalivetime+updater.errorSleepTime); |
109 | ca6bb3d5 | Leonidas Poulopoulos | } |
110 | ca6bb3d5 | Leonidas Poulopoulos | }, |
111 | 97e42c7d | Leonidas Poulopoulos | |
112 | 97e42c7d | Leonidas Poulopoulos | start: function() { |
113 | ca6bb3d5 | Leonidas Poulopoulos | $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "text", |
114 | 94fb8123 | Leonidas Poulopoulos | success: updater.onFetchExisting,
|
115 | 97e42c7d | Leonidas Poulopoulos | error: updater.onError});
|
116 | 97e42c7d | Leonidas Poulopoulos | }, |
117 | 97e42c7d | Leonidas Poulopoulos | |
118 | 97e42c7d | Leonidas Poulopoulos | poll: function() { |
119 | ca6bb3d5 | Leonidas Poulopoulos | {% if user.is_authenticated %}
|
120 | ca6bb3d5 | Leonidas Poulopoulos | if (updater.errorSleepTime > 60000){ |
121 | ca6bb3d5 | Leonidas Poulopoulos | window.setTimeout('location.reload()', 1000); |
122 | ca6bb3d5 | Leonidas Poulopoulos | } |
123 | ca6bb3d5 | Leonidas Poulopoulos | updater.xhrlp=$.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "text", |
124 | ca6bb3d5 | Leonidas Poulopoulos | success: updater.onSuccess,
|
125 | ca6bb3d5 | Leonidas Poulopoulos | error: updater.onError});
|
126 | ca6bb3d5 | Leonidas Poulopoulos | {% endif %} |
127 | 97e42c7d | Leonidas Poulopoulos | }, |
128 | 97e42c7d | Leonidas Poulopoulos | onSuccess: function(response) { |
129 | 97e42c7d | Leonidas Poulopoulos | try {
|
130 | 97e42c7d | Leonidas Poulopoulos | updater.newMessages(eval("(" + response + ")")); |
131 | 97e42c7d | Leonidas Poulopoulos | } catch (e) {
|
132 | 97e42c7d | Leonidas Poulopoulos | updater.onError(); |
133 | 97e42c7d | Leonidas Poulopoulos | return;
|
134 | 97e42c7d | Leonidas Poulopoulos | } |
135 | 97e42c7d | Leonidas Poulopoulos | updater.errorSleepTime = 500;
|
136 | 97e42c7d | Leonidas Poulopoulos | window.setTimeout(updater.poll, 0);
|
137 | 97e42c7d | Leonidas Poulopoulos | }, |
138 | 97e42c7d | Leonidas Poulopoulos | |
139 | 97e42c7d | Leonidas Poulopoulos | onFetchExisting: function(response) { |
140 | 97e42c7d | Leonidas Poulopoulos | try {
|
141 | 97e42c7d | Leonidas Poulopoulos | updater.existingMessages(eval("(" + response + ")")); |
142 | 97e42c7d | Leonidas Poulopoulos | } catch (e) {
|
143 | 97e42c7d | Leonidas Poulopoulos | // updater.onError();
|
144 | 97e42c7d | Leonidas Poulopoulos | return;
|
145 | 97e42c7d | Leonidas Poulopoulos | } |
146 | 97e42c7d | Leonidas Poulopoulos | }, |
147 | 97e42c7d | Leonidas Poulopoulos | |
148 | ca6bb3d5 | Leonidas Poulopoulos | onError: function(response, text) { |
149 | ca6bb3d5 | Leonidas Poulopoulos | if (text != 'abort'){ |
150 | ca6bb3d5 | Leonidas Poulopoulos | updater.errorSleepTime *= 2;
|
151 | ca6bb3d5 | Leonidas Poulopoulos | console.log("Poll error; sleeping for", updater.errorSleepTime, "ms"); |
152 | ca6bb3d5 | Leonidas Poulopoulos | window.setTimeout(updater.keepalive, updater.errorSleepTime); |
153 | ca6bb3d5 | Leonidas Poulopoulos | } |
154 | 97e42c7d | Leonidas Poulopoulos | }, |
155 | 97e42c7d | Leonidas Poulopoulos | |
156 | 97e42c7d | Leonidas Poulopoulos | newMessages: function(response) { |
157 | 97e42c7d | Leonidas Poulopoulos | if (!response.messages) return; |
158 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = response.cursor; |
159 | 97e42c7d | Leonidas Poulopoulos | var messages = response.messages;
|
160 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = messages[messages.length - 1].id;
|
161 | 97e42c7d | Leonidas Poulopoulos | // console.log(messages.length, "new messages, cursor:", updater.cursor);
|
162 | 97e42c7d | Leonidas Poulopoulos | |
163 | 97e42c7d | Leonidas Poulopoulos | for (var i = 0; i < messages.length; i++) { |
164 | 97e42c7d | Leonidas Poulopoulos | updater.showMessage(messages[i]); |
165 | 97e42c7d | Leonidas Poulopoulos | } |
166 | 049a5a10 | Leonidas Poulopoulos | $("#hid_mid").val('UPDATED'); |
167 | 97e42c7d | Leonidas Poulopoulos | if (($('#console').dialog('isOpen')) == false){ |
168 | 97e42c7d | Leonidas Poulopoulos | blink("#consolebutton");
|
169 | 049a5a10 | Leonidas Poulopoulos | window.setTimeout('location.reload()', 3000); |
170 | 97e42c7d | Leonidas Poulopoulos | } |
171 | 97e42c7d | Leonidas Poulopoulos | }, |
172 | 97e42c7d | Leonidas Poulopoulos | |
173 | 97e42c7d | Leonidas Poulopoulos | existingMessages: function(response) { |
174 | 97e42c7d | Leonidas Poulopoulos | if (!response.messages) return; |
175 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = response.cursor; |
176 | 97e42c7d | Leonidas Poulopoulos | var messages = response.messages;
|
177 | 97e42c7d | Leonidas Poulopoulos | updater.cursor = messages[messages.length - 1].id;
|
178 | 97e42c7d | Leonidas Poulopoulos | for (var i = 0; i < messages.length; i++) { |
179 | 97e42c7d | Leonidas Poulopoulos | updater.showMessage(messages[i]); |
180 | 97e42c7d | Leonidas Poulopoulos | } |
181 | 97e42c7d | Leonidas Poulopoulos | }, |
182 | 97e42c7d | Leonidas Poulopoulos | |
183 | 97e42c7d | Leonidas Poulopoulos | showMessage: function(message) { |
184 | 97e42c7d | Leonidas Poulopoulos | var existing = $("#m" + message.id); |
185 | 97e42c7d | Leonidas Poulopoulos | if (existing.length > 0) return; |
186 | 97e42c7d | Leonidas Poulopoulos | var node = $(message.html); |
187 | 97e42c7d | Leonidas Poulopoulos | node.hide(); |
188 | 97e42c7d | Leonidas Poulopoulos | // $('#inbox').val($('#inbox').val()+message.text);
|
189 | 97e42c7d | Leonidas Poulopoulos | $("#inbox").append(node); |
190 | 97e42c7d | Leonidas Poulopoulos | node.slideDown(); |
191 | 97e42c7d | Leonidas Poulopoulos | } |
192 | 97e42c7d | Leonidas Poulopoulos | }; |
193 | 97e42c7d | Leonidas Poulopoulos | |
194 | 97e42c7d | Leonidas Poulopoulos | function blink(selector){ |
195 | 97e42c7d | Leonidas Poulopoulos | $(selector).animate({ color: "red" }, 500, function(){ |
196 | 97e42c7d | Leonidas Poulopoulos | $(this).animate({ color: "#555555" }, 500, function(){ |
197 | 97e42c7d | Leonidas Poulopoulos | blink(this);
|
198 | 97e42c7d | Leonidas Poulopoulos | }); |
199 | 97e42c7d | Leonidas Poulopoulos | }); |
200 | 97e42c7d | Leonidas Poulopoulos | } |