Updated counter, submitonreturn to be reused by any form. Created

FormNoticeEnhancements to call those at one go for any form.
This commit is contained in:
Sarven Capadisli 2009-10-31 18:55:13 +01:00
parent dd50368cb2
commit 8a333805df

View File

@ -26,23 +26,7 @@
$(document).ready(function(){ $(document).ready(function(){
if ($('body.user_in').length > 0) { if ($('body.user_in').length > 0) {
if ($('#'+SN.C.S.NoticeDataText).length) { $('.'+SN.C.S.FormNotice).each(function() { SN.U.FormNoticeEnhancements($(this)); });
if (maxLength > 0) {
$('#'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
SN.U.Counter();
});
// run once in case there's something in there
SN.U.Counter();
}
$('#'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
SN.U.SubmitOnReturn(e, $('#'+SN.C.S.FormNotice));
});
if($('body')[0].id != 'conversation') {
$('.'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeDataText).focus();
}
}
$('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); }); $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); }); $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
@ -52,8 +36,6 @@ $(document).ready(function(){
$('.form_group_leave').each(function() { SN.U.FormXHR($(this)); }); $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); }); $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
SN.U.FormNoticeXHR($('.'+SN.C.S.FormNotice));
SN.U.NoticeReply(); SN.U.NoticeReply();
SN.U.NoticeDataAttach(); SN.U.NoticeDataAttach();
@ -92,19 +74,42 @@ var SN = { // StatusNet
}, },
U: { // Utils U: { // Utils
FormNoticeEnhancements: function(form) {
form_id = form.attr('id');
if (maxLength > 0) {
$('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
SN.U.Counter(form);
});
// run once in case there's something in there
SN.U.Counter(form);
}
$('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keydown', function(e) {
SN.U.SubmitOnReturn(e, form);
});
if($('body')[0].id != 'conversation') {
$('#'+form_id+' textarea').focus();
}
SN.U.FormNoticeXHR(form);
},
SubmitOnReturn: function(event, el) { SubmitOnReturn: function(event, el) {
if (event.keyCode == 13 || event.keyCode == 10) { if (event.keyCode == 13 || event.keyCode == 10) {
el.submit(); el.submit();
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
$('#'+SN.U.NoticeDataText).blur(); $('#'+el[0].id+' #'+SN.U.NoticeDataText).blur();
$('body').focus(); $('body').focus();
return false; return false;
} }
return true; return true;
}, },
Counter: function() { Counter: function(form) {
SN.C.I.FormNoticeCurrent = form;
form_id = form.attr('id');
if (typeof(maxLength) == "undefined") { if (typeof(maxLength) == "undefined") {
maxLength = SN.C.I.MaxLength; maxLength = SN.C.I.MaxLength;
} }
@ -113,8 +118,8 @@ var SN = { // StatusNet
return; return;
} }
var remaining = maxLength - $('#'+SN.C.S.NoticeDataText).val().length; var remaining = maxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
var counter = $('#'+SN.C.S.NoticeTextCount); var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
if (remaining.toString() != counter.text()) { if (remaining.toString() != counter.text()) {
if (!SN.C.I.CounterBlackout || remaining == 0) { if (!SN.C.I.CounterBlackout || remaining == 0) {
@ -122,25 +127,26 @@ var SN = { // StatusNet
counter.text(remaining); counter.text(remaining);
} }
if (remaining < 0) { if (remaining < 0) {
$('#'+SN.C.S.FormNotice).addClass(SN.C.S.Warning); form.addClass(SN.C.S.Warning);
} else { } else {
$('#'+SN.C.S.FormNotice).removeClass(SN.C.S.Warning); form.removeClass(SN.C.S.Warning);
} }
// Skip updates for the next 500ms. // Skip updates for the next 500ms.
// On slower hardware, updating on every keypress is unpleasant. // On slower hardware, updating on every keypress is unpleasant.
if (!SN.C.I.CounterBlackout) { if (!SN.C.I.CounterBlackout) {
SN.C.I.CounterBlackout = true; SN.C.I.CounterBlackout = true;
window.setTimeout(SN.U.ClearCounterBlackout, 500); SN.C.I.FormNoticeCurrent = form;
window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
} }
} }
} }
}, },
ClearCounterBlackout: function() { ClearCounterBlackout: function(form) {
// Allow keyup events to poke the counter again // Allow keyup events to poke the counter again
SN.C.I.CounterBlackout = false; SN.C.I.CounterBlackout = false;
// Check if the string changed since we last looked // Check if the string changed since we last looked
SN.U.Counter(null); SN.U.Counter(form);
}, },
FormXHR: function(f) { FormXHR: function(f) {
@ -176,7 +182,6 @@ var SN = { // StatusNet
FormNoticeXHR: function(form) { FormNoticeXHR: function(form) {
form_id = form.attr('id'); form_id = form.attr('id');
console.log(form_id);
form.append('<input type="hidden" name="ajax" value="1"/>'); form.append('<input type="hidden" name="ajax" value="1"/>');
form.ajaxForm({ form.ajaxForm({
dataType: 'xml', dataType: 'xml',
@ -208,7 +213,7 @@ var SN = { // StatusNet
} }
else { else {
$('#'+form_id+' #'+SN.C.S.NoticeDataText).val(''); $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
SN.U.Counter(); SN.U.Counter($('#'+SN.C.S.FormNotice));
} }
} }
} }
@ -251,7 +256,7 @@ var SN = { // StatusNet
$('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val(''); $('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val('');
$('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val(''); $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val('');
$('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove(); $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
SN.U.Counter(); SN.U.Counter($('#'+SN.C.S.FormNotice));
} }
}, },
complete: function(xhr, textStatus) { complete: function(xhr, textStatus) {
@ -366,14 +371,12 @@ var SN = { // StatusNet
$.get(NDM.attr('href'), null, function(data) { $.get(NDM.attr('href'), null, function(data) {
$('.entity_send-a-message').append(document._importNode($('form', data).get(0), true)); $('.entity_send-a-message').append(document._importNode($('form', data).get(0), true));
NDMF = $('.entity_send-a-message .form_notice'); NDMF = $('.entity_send-a-message .form_notice');
SN.U.FormNoticeXHR(NDMF); SN.U.FormNoticeEnhancements(NDMF);
NDMF.append('<button>&#215;</button>'); NDMF.append('<button>&#215;</button>');
$('.entity_send-a-message button').click(function(){ $('.entity_send-a-message button').click(function(){
NDMF.hide(); NDMF.hide();
return false; return false;
}); });
$('.entity_send-a-message textarea').focus();
}); });
} }
else { else {