X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Futil.js;h=0a943512f2a33c4bd7c551a43c6f71d07e47c278;hb=e63a435868318bd183d8ae36ff0f6a110cdc85db;hp=0409dc60146904d009280f70f342aaadc2d374ad;hpb=8e59ee61f71c54dc187dc6542e7c92a11126be47;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index 0409dc6014..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,37 +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"); - 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(); @@ -57,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'); @@ -66,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'); @@ -185,7 +228,9 @@ $(document).ready(function(){ } else { $("#notice_data-text").val(""); - counter(); + if (maxLength > 0) { + counter(); + } } } } @@ -225,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"); @@ -244,7 +291,7 @@ function NoticeReply() { $('#content .notice').each(function() { var notice = $(this)[0]; $($('.notice_reply', notice)[0]).click(function() { - var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname'); + var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid'); NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text()); return false; }); @@ -255,19 +302,15 @@ 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; text.get(0).setSelectionRange(len,len); text.get(0).focus(); - } else if (text.get(0).createTextRange) { - var range = text.createTextRange(); - range.collapse(false); - range.select(); } return false; }