2014-01-25 16:34:20 +01:00
|
|
|
$(function()
|
|
|
|
{
|
|
|
|
function onDomUpdate()
|
|
|
|
{
|
2014-02-01 10:10:09 +01:00
|
|
|
$('form.edit-comment textarea, form.add-comment textarea')
|
2014-02-19 20:27:45 +01:00
|
|
|
.off('change keyp')
|
|
|
|
.on('change keyup', function(e)
|
2014-02-01 10:10:09 +01:00
|
|
|
{
|
|
|
|
enableExitConfirmation();
|
|
|
|
});
|
|
|
|
|
2014-02-19 20:27:45 +01:00
|
|
|
$('form.edit-comment, form.add-comment')
|
|
|
|
.off('submit')
|
|
|
|
.on('submit', function(e)
|
2014-01-25 16:34:20 +01:00
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
rememberLastSearchQuery();
|
|
|
|
|
|
|
|
var formDom = $(this);
|
|
|
|
if (formDom.hasClass('inactive'))
|
|
|
|
return;
|
|
|
|
formDom.addClass('inactive');
|
|
|
|
formDom.find(':input').attr('readonly', true);
|
|
|
|
|
|
|
|
var url = formDom.attr('action') + '?json';
|
|
|
|
var fd = new FormData(formDom[0]);
|
|
|
|
|
|
|
|
var preview = false;
|
|
|
|
$.each(formDom.serializeArray(), function(i, x)
|
|
|
|
{
|
|
|
|
if (x.name == 'sender' && x.value == 'preview')
|
|
|
|
preview = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
var ajaxData =
|
|
|
|
{
|
|
|
|
url: url,
|
|
|
|
data: fd,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
type: 'POST',
|
|
|
|
|
|
|
|
success: function(data)
|
|
|
|
{
|
|
|
|
if (data['success'])
|
|
|
|
{
|
|
|
|
if (preview)
|
|
|
|
{
|
|
|
|
formDom.find('.preview').html(data['textPreview']).show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-01 10:10:09 +01:00
|
|
|
disableExitConfirmation();
|
|
|
|
|
2014-01-25 16:34:20 +01:00
|
|
|
formDom.find('.preview').hide();
|
|
|
|
var cb = function()
|
|
|
|
{
|
|
|
|
$.get(window.location.href, function(data)
|
|
|
|
{
|
|
|
|
$('.comments-wrapper').replaceWith($(data).find('.comments-wrapper'));
|
|
|
|
$('body').trigger('dom-update');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (formDom.hasClass('add-comment'))
|
|
|
|
{
|
|
|
|
cb();
|
|
|
|
formDom.find('textarea').val('');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
formDom.slideUp(function()
|
|
|
|
{
|
|
|
|
cb();
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formDom.find(':input').attr('readonly', false);
|
|
|
|
formDom.removeClass('inactive');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alert(data['message']);
|
|
|
|
formDom.find(':input').attr('readonly', false);
|
|
|
|
formDom.removeClass('inactive');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function()
|
|
|
|
{
|
|
|
|
alert('Fatal error');
|
|
|
|
formDom.find(':input').attr('readonly', false);
|
|
|
|
formDom.removeClass('inactive');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax(ajaxData);
|
|
|
|
});
|
|
|
|
|
2014-02-19 20:27:45 +01:00
|
|
|
$('.comment .edit a').off('click').on('click').click(function(e)
|
2014-01-25 16:34:20 +01:00
|
|
|
{
|
|
|
|
e.preventDefault();
|
|
|
|
var commentDom = $(this).parents('.comment');
|
2014-02-19 20:27:45 +01:00
|
|
|
var formDom = commentDom.find('form.edit-comment');
|
|
|
|
var cb = function(formDom)
|
2014-01-25 16:34:20 +01:00
|
|
|
{
|
2014-02-19 20:27:45 +01:00
|
|
|
formDom.slideToggle();
|
2014-01-25 16:34:20 +01:00
|
|
|
$('body').trigger('dom-update');
|
2014-02-19 20:27:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (formDom.length == 0)
|
|
|
|
{
|
|
|
|
$.get($(this).attr('href'), function(data)
|
|
|
|
{
|
|
|
|
var otherForm = $(data).find('form.edit-comment');
|
|
|
|
otherForm.hide();
|
|
|
|
commentDom.find('.body').append(otherForm);
|
|
|
|
formDom = commentDom.find('form.edit-comment');
|
|
|
|
cb(formDom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cb(formDom);
|
2014-01-25 16:34:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$('body').bind('dom-update', onDomUpdate);
|
|
|
|
});
|