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();
34 function newMessage(form) {
35 var message = form.formToDict();
36 var disabled = form.find("input[type=submit]");
38 $.postJSON("{% url fetch-new %}", message, function(response) {
39 updater.showMessage(response);
41 form.parent().remove();
43 form.find("input[type=text]").val("").select();
49 function getCookie(name) {
50 var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
51 return r ? r[1] : undefined;
54 jQuery.postJSON = function(url, args, callback) {
55 args._xsrf = getCookie("_xsrf");
56 $.ajax({url: url, data: $.param(args), dataType: "text", type: "POST",
57 success: function(response) {
58 if (callback) callback(eval("(" + response + ")"));
59 }, error: function(response) {
60 console.log("ERROR:", response)
64 jQuery.fn.formToDict = function() {
65 var fields = this.serializeArray();
67 for (var i = 0; i < fields.length; i++) {
68 json[fields[i].name] = fields[i].value;
70 if (json.next) delete json.next;
74 jQuery.fn.disable = function() {
79 jQuery.fn.enable = function(opt_enable) {
80 if (arguments.length && !opt_enable) {
81 this.attr("disabled", "disabled");
83 this.removeAttr("disabled");
93 var args = {"_xsrf": getCookie("_xsrf")};
94 if (updater.cursor) args.cursor = updater.cursor;
95 $.ajax({url: "{% url fetch-existing %}", type: "POST", dataType: "text",
96 data: $.param(args), success: updater.onFetchExisting,
97 error: updater.onError});
101 var args = {"_xsrf": getCookie("_xsrf")};
102 if (updater.cursor) args.cursor = updater.cursor;
103 $.ajax({url: "{% url fetch-updates %}", type: "POST", dataType: "text",
104 data: $.param(args), success: updater.onSuccess,
105 error: updater.onError});
108 onSuccess: function(response) {
110 updater.newMessages(eval("(" + response + ")"));
115 updater.errorSleepTime = 500;
116 window.setTimeout(updater.poll, 0);
119 onFetchExisting: function(response) {
121 updater.existingMessages(eval("(" + response + ")"));
123 // updater.onError();
128 onError: function(response) {
129 updater.errorSleepTime *= 2;
130 console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
131 window.setTimeout(updater.poll, updater.errorSleepTime);
134 newMessages: function(response) {
135 if (!response.messages) return;
136 updater.cursor = response.cursor;
137 var messages = response.messages;
138 updater.cursor = messages[messages.length - 1].id;
139 // console.log(messages.length, "new messages, cursor:", updater.cursor);
141 for (var i = 0; i < messages.length; i++) {
142 updater.showMessage(messages[i]);
144 if (($('#console').dialog('isOpen')) == false){
145 blink("#consolebutton");
149 existingMessages: function(response) {
150 if (!response.messages) return;
151 updater.cursor = response.cursor;
152 var messages = response.messages;
153 updater.cursor = messages[messages.length - 1].id;
154 for (var i = 0; i < messages.length; i++) {
155 updater.showMessage(messages[i]);
159 showMessage: function(message) {
160 var existing = $("#m" + message.id);
161 if (existing.length > 0) return;
162 var node = $(message.html);
164 // $('#inbox').val($('#inbox').val()+message.text);
165 $("#inbox").append(node);
170 function blink(selector){
171 $(selector).animate({ color: "red" }, 500, function(){
172 $(this).animate({ color: "#555555" }, 500, function(){