const COMMENT_FORM_URL = 'form/'
const COMMENT_POST_URL = '.'
const COMMENT_PREVIEW_URL = 'preview/'
function getFormData() {
var formData = {}
$('#comment\-form').find(':input, :text').each(function() {
var formName = $(this).attr('name');
formData[formName] = $(this).val();
});
return formData;
}
function submitFormCallback(response) {
if (response.status == 'ok') {
$('#comments\-box').append(response.comment);
$('#comment\-form').fadeOut('slow', function() { $(this).remove() });
} else if (response.status == 'fail') {
$('#comment\-form').html(response.form);
$('#comment\-form :submit').click(submitForm);
} else {
// server error?
};
};
function showCommentForm(commentForm) {
$('#comment\-form').html(commentForm);
$('#comment\-form :submit').click(submitForm);
$('#comment\-form :submit:last').after(
'<input id="form-submit-preview" type="submit" value="Podgląd"/>')
$('#form\-submit\-preview').click(previewSubmit);
};
function getCommentFormCallback(response) {
if (response.status == "ok")
showCommentForm(response.form);
// else.. well, fail
};
function previewSubmitCallback(response) {
if (response.status == 'ok') {
$('#comment-preview').html(response.comment);
} else {
// fail
}
}
function previewSubmit() {
var postData =
$.post(COMMENT_PREVIEW_URL, getFormData(), previewSubmitCallback, 'json');
return false;
};
function submitForm() {
$.post(COMMENT_POST_URL, getFormData(), submitFormCallback, 'json');
return false;
};
function getCommentForm() {
$.get(COMMENT_FORM_URL, {}, getCommentFormCallback, 'json');
};
$(document).ready(function() {
getCommentForm();
});