Updated notice form counter

This commit is contained in:
Sarven Capadisli 2009-10-30 15:43:41 +01:00
parent 1ddf69f30e
commit 4024a5ee65

View File

@ -17,58 +17,13 @@
*/
$(document).ready(function(){
var counterBlackout = false;
// count character on keyup
function counter(event){
if (maxLength <= 0) {
return;
}
var currentLength = $("#notice_data-text").val().length;
var remaining = maxLength - currentLength;
var counter = $("#notice_text-count");
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");
}
// 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);
}
// define maxLength if it wasn't defined already
if (typeof(maxLength) == "undefined") {
maxLength = 140;
}
if ($("#notice_data-text").length) {
if ($('#'+SN.C.S.NoticeDataText).length) {
if (maxLength > 0) {
$("#notice_data-text").bind("keyup", counter);
$('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
SN.U.Counter();
});
// run once in case there's something in there
counter();
SN.U.Counter();
}
$('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
@ -76,7 +31,7 @@ $(document).ready(function(){
});
if($('body')[0].id != 'conversation') {
$("#notice_data-text").focus();
$('#'+SN.C.S.NoticeDataText).focus();
}
}
@ -99,7 +54,8 @@ $(document).ready(function(){
var SN = { // StatusNet
C: { // Config
I: {
NoticeTextCharMax: 140,
CounterBlackout: false,
MaxLength: 140,
PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
},
@ -116,7 +72,7 @@ var SN = { // StatusNet
NoticeInReplyTo: 'notice_in-reply-to',
NoticeDataAttach: 'notice_data-attach',
NoticeDataAttachSelected: 'notice_data-attach_selected',
NoticeActionSubmit: 'notice_action-submit'
NoticeActionSubmit: 'notice_action-submit',
}
},
@ -133,6 +89,45 @@ var SN = { // StatusNet
return true;
},
Counter: function() {
if (typeof(maxLength) == "undefined") {
maxLength = SN.C.I.MaxLength;
}
if (maxLength <= 0) {
return;
}
var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length;
var counter = $('#'+SN.C.S.NoticeTextCount);
if (remaining.toString() != counter.text()) {
if (!SN.C.I.CounterBlackout || remaining == 0) {
if (counter.text() != String(remaining)) {
counter.text(remaining);
}
if (remaining < 0) {
$('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning);
} else {
$('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning);
}
// Skip updates for the next 500ms.
// On slower hardware, updating on every keypress is unpleasant.
if (!SN.C.I.CounterBlackout) {
SN.C.I.CounterBlackout = true;
window.setTimeout(SN.U.ClearCounterBlackout, 500);
}
}
}
},
ClearCounterBlackout: function() {
// Allow keyup events to poke the counter again
SN.C.I.CounterBlackout = false;
// Check if the string changed since we last looked
SN.U.Counter(null);
},
FormXHR: function(f) {
f.bind('submit', function(e) {
form_id = $(this)[0].id;