szurubooru/public_html/media/js/post-view.js

147 lines
3.7 KiB
JavaScript
Raw Normal View History

$(function()
2013-10-12 14:53:47 +02:00
{
function onDomUpdate()
2013-10-13 12:28:16 +02:00
{
$('#sidebar a.edit-post').bindOnce('edit-post', 'click', function(e)
{
e.preventDefault();
2013-10-13 21:42:43 +02:00
var aDom = $(this);
if (aDom.hasClass('inactive'))
return;
aDom.addClass('inactive');
2013-10-13 12:28:16 +02:00
var formDom = $('form.edit-post');
if (formDom.find('.tagit').length == 0)
{
attachTagIt($('.tags input'));
aDom.removeClass('inactive');
2014-02-01 10:10:09 +01:00
formDom.find('input[type=text]:visible:eq(0)').focus();
formDom.find('textarea, input').bind('change keyup', function()
{
if (formDom.serialize() != formDom.data('original-data'))
enableExitConfirmation();
});
}
else
aDom.removeClass('inactive');
var editUnit = formDom.parents('.unit');
var postUnit = $('.post-wrapper');
if (!$(formDom).is(':visible'))
2014-01-25 22:50:15 +01:00
{
formDom.data('original-data', formDom.serialize());
editUnit.show();
var editUnitHeight = formDom.height();
editUnit.css('height', editUnitHeight);
editUnit.hide();
if (postUnit.height() < editUnitHeight)
postUnit.animate({height: editUnitHeight + 'px'}, 'fast');
editUnit.slideDown('fast', function()
2014-01-25 22:50:15 +01:00
{
$(this).css('height', 'auto');
});
}
else
{
editUnit.slideUp('fast');
var postUnitOldHeight = postUnit.height();
postUnit.height('auto');
var postUnitHeight = postUnit.height();
postUnit.height(postUnitOldHeight);
if (postUnitHeight != postUnitOldHeight)
postUnit.animate({height: postUnitHeight + 'px'});
if ($('.post-wrapper').height() < editUnitHeight)
$('.post-wrapper').animate({height: editUnitHeight + 'px'});
return;
}
formDom.find('input[type=text]:visible:eq(0)').focus();
2013-10-13 12:28:16 +02:00
});
$('.comments.unit a.simple-action').data('callback', function()
{
$.get(window.location.href, function(data)
{
$('.comments-wrapper').replaceWith($(data).find('.comments-wrapper'));
$('body').trigger('dom-update');
});
});
$('#sidebar a.simple-action').data('callback', function()
{
$.get(window.location.href, function(data)
{
$('#sidebar').replaceWith($(data).find('#sidebar'));
$('body').trigger('dom-update');
});
});
}
$('body').bind('dom-update', onDomUpdate);
$('form.edit-post').submit(function(e)
2013-10-13 12:28:16 +02:00
{
e.preventDefault();
rememberLastSearchQuery();
2013-10-13 12:28:16 +02:00
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 ajaxData =
{
url: url,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data)
{
if (data['success'])
{
2014-02-01 10:10:09 +01:00
disableExitConfirmation();
$.get(window.location.href, function(data)
{
$('#sidebar').replaceWith($(data).find('#sidebar'));
$('#edit-token').replaceWith($(data).find('#edit-token'));
$('body').trigger('dom-update');
});
formDom.parents('.unit').hide();
2013-10-13 12:28:16 +02:00
}
else
{
2013-11-16 21:14:27 +01:00
alert(data['message']);
2013-10-13 12:28:16 +02:00
}
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);
});
Mousetrap.bind('a', function() { var a = $('#sidebar .left a'); var url = a.attr('href'); if (typeof url !== 'undefined') { a.click(); window.location.href = url; } }, 'keyup');
Mousetrap.bind('d', function() { var a = $('#sidebar .right a'); var url = a.attr('href'); if (typeof url !== 'undefined') { a.click(); window.location.href = url; } }, 'keyup');
2014-02-17 19:50:02 +01:00
Mousetrap.bind('e', function() { $('a.edit-post').trigger('click'); return false; }, 'keyup');
2013-10-12 14:53:47 +02:00
});