X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Futil.js;h=0a943512f2a33c4bd7c551a43c6f71d07e47c278;hb=e63a435868318bd183d8ae36ff0f6a110cdc85db;hp=1ab711cd134747f2762d6a813cb647c4938957a0;hpb=91cba7a76f97a1669cbe9a2c2fa1b3e653786f26;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index 1ab711cd13..0a943512f2 100644 --- a/js/util.js +++ b/js/util.js @@ -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 @@ -17,39 +17,72 @@ */ $(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;