1 // Copyright 2009 FriendFeed
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
15 $(document).ready(function() {
16 if (!window.console) window.console = {};
17 if (!window.console.log) window.console.log = function() {};
19 $("#messageform").live("submit", function() {
23 $("#messageform").live("keypress", function(e) {
24 if (e.keyCode == 13) {
29 $("#message").select();
30 {% if user.is_authenticated %}
39 function newMessage(form) {
40 var message = form.formToDict();
41 var disabled = form.find("input[type=submit]");
43 $.postJSON("{% url fetch-new %}", message, function(response) {
44 updater.showMessage(response);
46 form.parent().remove();
48 form.find("input[type=text]").val("").select();
54 function getCookie(name) {
55 var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
56 return r ? r[1] : undefined;
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);
68 jQuery.fn.formToDict = function() {
69 var fields = this.serializeArray();
71 for (var i = 0; i < fields.length; i++) {
72 json[fields[i].name] = fields[i].value;
74 if (json.next) delete json.next;
78 jQuery.fn.disable = function() {
83 jQuery.fn.enable = function(opt_enable) {
84 if (arguments.length && !opt_enable) {
85 this.attr("disabled", "disabled");
87 this.removeAttr("disabled");
96 $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "json",
97 success: updater.onFetchExisting,
98 error: updater.onError});
102 {% if user.is_authenticated %}
103 if (updater.errorSleepTime > 128000){
104 window.setTimeout('location.reload()', 500);
106 timeout = {{timeout}};
107 $.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "json",
108 success: updater.onSuccess,
110 error: updater.onError});
113 onSuccess: function(response) {
115 updater.newMessages(response);
120 updater.errorSleepTime = 500;
121 window.setTimeout(updater.poll, 0);
124 onFetchExisting: function(response) {
126 updater.existingMessages(response);
134 onError: function(response, text) {
135 if (text == 'timeout'){
136 window.setTimeout('location.reload()', 3000);
138 updater.errorSleepTime *= 2;
139 console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
140 window.setTimeout(updater.poll, updater.errorSleepTime);
144 newMessages: function(response) {
145 if (!response.messages) return;
146 if (response.messages.length == 0){
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);
154 for (var i = 0; i < messages.length; i++) {
155 updater.showMessage(messages[i]);
157 $("#hid_mid").val('UPDATED');
158 if (($('#console').dialog('isOpen')) == false){
159 blink("#consolebutton");
160 window.setTimeout('location.reload()', 3000);
164 existingMessages: function(response) {
165 if (!response.messages) return;
166 if (response.messages.length == 0){
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]);
177 showMessage: function(message) {
178 var existing = $("#m" + message.id);
179 if (existing.length > 0) return;
180 var node = $(message.html);
182 // $('#inbox').val($('#inbox').val()+message.text);
183 $("#inbox").append(node);
188 function blink(selector){
189 $(selector).animate({ color: "red" }, 500, function(){
190 $(this).animate({ color: "#555555" }, 500, function(){