]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - js/util.js
Using 'CSS sprites' for common icons for the identica theme. Default
[quix0rs-gnu-social.git] / js / util.js
index 1ab711cd134747f2762d6a813cb647c4938957a0..0a943512f2a33c4bd7c551a43c6f71d07e47c278 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, Controlez-Vous, Inc.
+ * StatusNet - a distributed open-source microblogging tool
+ * Copyright (C) 2008, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  */
 
 $(document).ready(function(){
+       var counterBlackout = false;
+       
        // count character on keyup
        function counter(event){
-               var maxLength = 140;
+         if (maxLength <= 0) {
+              return;
+         }
                var currentLength = $("#notice_data-text").val().length;
                var remaining = maxLength - currentLength;
                var counter = $("#notice_text-count");
-               if (counter.text() != String(remaining)) {
-                       counter.text(remaining);
-               }
+               
+               if (remaining.toString() != counter.text()) {
+                   if (!counterBlackout || remaining == 0) {
+                        if (counter.text() != String(remaining)) {
+                            counter.text(remaining);
+                       }
 
-               if (remaining < 0) {
-                       $("#form_notice").addClass("warning");
-               } else {
-                       $("#form_notice").removeClass("warning");
-               }
+                        if (remaining < 0) {
+                            $("#form_notice").addClass("warning");
+                        } else {
+                            $("#form_notice").removeClass("warning");
+                        }
+                        // Skip updates for the next 500ms.
+                        // On slower hardware, updating on every keypress is unpleasant.
+                        if (!counterBlackout) {
+                            counterBlackout = true;
+                            window.setTimeout(clearCounterBlackout, 500);
+                        }
+                    }
+                }
+       }
+       
+       function clearCounterBlackout() {
+               // Allow keyup events to poke the counter again
+               counterBlackout = false;
+               // Check if the string changed since we last looked
+               counter(null);
        }
 
        function submitonreturn(event) {
-               if (event.keyCode == 13) {
+               if (event.keyCode == 13 || event.keyCode == 10) {
+                       // iPhone sends \n not \r for 'return'
                        $("#form_notice").submit();
                        event.preventDefault();
                        event.stopPropagation();
+                       $("#notice_data-text").blur();
+                       $("body").focus();
                        return false;
                }
                return true;
        }
 
+     // define maxLength if it wasn't defined already
+
+    if (typeof(maxLength) == "undefined") {
+         maxLength = 140;
+    }
+
        if ($("#notice_data-text").length) {
-               $("#notice_data-text").bind("keyup", counter);
-               $("#notice_data-text").bind("keydown", submitonreturn);
+         if (maxLength > 0) {
+              $("#notice_data-text").bind("keyup", counter);
+              // run once in case there's something in there
+              counter();
+         }
 
-               // run once in case there's something in there
-               counter();
+               $("#notice_data-text").bind("keydown", submitonreturn);
 
         if($('body')[0].id != 'conversation') {
             $("#notice_data-text").focus();
@@ -59,6 +92,10 @@ $(document).ready(function(){
        // XXX: refactor this code
 
        var favoptions = { dataType: 'xml',
+                                          beforeSubmit: function(data, target, options) {
+                                                                                               $(target).addClass('processing');
+                                                                                               return true;
+                                                                                         },
                                           success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
                                                                                                var dis = new_form.id;
                                                                                                var fav = dis.replace('disfavor', 'favor');
@@ -68,6 +105,10 @@ $(document).ready(function(){
                                         };
 
        var disoptions = { dataType: 'xml',
+                                          beforeSubmit: function(data, target, options) {
+                                                                                               $(target).addClass('processing');
+                                                                                               return true;
+                                                                                         },
                                           success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
                                                                                                var fav = new_form.id;
                                                                                                var dis = fav.replace('favor', 'disfavor');
@@ -187,7 +228,9 @@ $(document).ready(function(){
                                                                                                                                                                }
                                                                                                                                                                else {
                                                                                                                                                                        $("#notice_data-text").val("");
-                                                                                                                                                                       counter();
+                                                                                     if (maxLength > 0) {
+                                                                                          counter();
+                                                                                     }
                                                                                                                                                                }
                                                                                                                                                        }
                                                                                                                                                }
@@ -227,7 +270,9 @@ $(document).ready(function(){
                                                                                                $("#notice_data-attach").val("");
                                                                                                $("#notice_in-reply-to").val("");
                                                     $('#notice_data-attach_selected').remove();
-                                                    counter();
+                                                     if (maxLength > 0) {
+                                                          counter();
+                                                     }
                                                                                                }
                                                                                                $("#form_notice").removeClass("processing");
                                                                                                $("#notice_action-submit").removeAttr("disabled");
@@ -257,10 +302,10 @@ function NoticeReply() {
 function NoticeReplySet(nick,id) {
        rgx_username = /^[0-9a-zA-Z\-_.]*$/;
        if (nick.match(rgx_username)) {
-               replyto = "@" + nick + " ";
                var text = $("#notice_data-text");
                if (text.length) {
-                       text.val(replyto + text.val());
+                       replyto = "@" + nick + " ";
+                       text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
                        $("#form_notice input#notice_in-reply-to").val(id);
                        if (text.get(0).setSelectionRange) {
                                var len = text.val().length;