Reduced post upload presenter complexity somewhat

This commit is contained in:
Marcin Kurczewski 2014-09-15 09:20:49 +02:00
parent 0ca03cae60
commit b53e2752ca

View file

@ -33,28 +33,28 @@ App.Presenters.PostUploadPresenter = function(
$el.html(template()); $el.html(template());
$messages = $el.find('.messages'); $messages = $el.find('.messages');
new App.Controls.FileDropper($el.find('[name=post-content]'), fileHandlerChanged, jQuery); tagInput = new App.Controls.TagInput($el.find('form [name=tags]'), _, jQuery);
App.Controls.FileDropper($el.find('[name=post-content]'), fileHandlerChanged, jQuery);
$el.find('.url-handler input').keydown(urlHandlerKeyPressed); $el.find('.url-handler input').keydown(urlHandlerKeyPressed);
$el.find('.url-handler button').click(urlHandlerButtonClicked); $el.find('.url-handler button').click(urlHandlerButtonClicked);
$el.find('thead th.checkbox').click(postTableSelectAllCheckboxClicked); $el.find('thead th.checkbox').click(postTableSelectAllCheckboxClicked);
mousetrap.bind('a', function(e) { mousetrap.bind('a', simpleKeyPressed(selectPrevPostTableRow), 'keyup');
if (!e.altKey && !e.ctrlKey) { mousetrap.bind('d', simpleKeyPressed(selectNextPostTableRow), 'keyup');
selectPrevPostInTable();
}
}, 'keyup');
mousetrap.bind('d', function(e) {
if (!e.altKey && !e.ctrlKey) {
selectNextPostInTable();
}
}, 'keyup');
$el.find('.remove').click(removeButtonClicked); $el.find('.remove').click(removeButtonClicked);
$el.find('.move-up').click(moveUpButtonClicked); $el.find('.move-up').click(moveUpButtonClicked);
$el.find('.move-down').click(moveDownButtonClicked); $el.find('.move-down').click(moveDownButtonClicked);
$el.find('.submit').click(submitButtonClicked); $el.find('.submit').click(submitButtonClicked);
}
tagInput = new App.Controls.TagInput($el.find('form [name=tags]'), _, jQuery); function simpleKeyPressed(callback) {
return function(e) {
if (!e.altKey && !e.ctrlKey) {
callback();
}
};
} }
function getDefaultPost() { function getDefaultPost() {
@ -143,7 +143,14 @@ App.Presenters.PostUploadPresenter = function(
var allPosts = getAllPosts(); var allPosts = getAllPosts();
allPosts.push(post); allPosts.push(post);
setAllPosts(allPosts); setAllPosts(allPosts);
createPostTableRow(post);
}
function postChanged(post) {
updatePostTableRow(post);
}
function createPostTableRow(post) {
var $table = $el.find('table'); var $table = $el.find('table');
var $row = $table.find('.template').clone(true); var $row = $table.find('.template').clone(true);
@ -159,11 +166,11 @@ App.Presenters.PostUploadPresenter = function(
postChanged(post); postChanged(post);
selectPostInTable(post); selectPostTableRow(post);
showOrHidePostsTable(); showOrHidePostsTable();
} }
function postChanged(post) { function updatePostTableRow(post) {
var $row = post.$tableRow; var $row = post.$tableRow;
$row.find('.tags').text(post.tags.join(', ') || '-'); $row.find('.tags').text(post.tags.join(', ') || '-');
$row.find('.safety div').attr('class', 'safety-' + post.safety); $row.find('.safety div').attr('class', 'safety-' + post.safety);
@ -373,8 +380,6 @@ App.Presenters.PostUploadPresenter = function(
if (index === -1) { if (index === -1) {
post.tags.push(tag); post.tags.push(tag);
} }
});
jQuery.each(posts, function(i, post) {
postChanged(post); postChanged(post);
}); });
} }
@ -385,26 +390,22 @@ App.Presenters.PostUploadPresenter = function(
if (index !== -1) { if (index !== -1) {
post.tags.splice(index, 1); post.tags.splice(index, 1);
} }
});
jQuery.each(posts, function(i, post) {
postChanged(post); postChanged(post);
}); });
} }
function postTableRowImageHovered(e) { function postTableRowImageHovered(e) {
var $img = jQuery(this); var $img = jQuery(this);
if (!$img.attr('src')) { if ($img.attr('src')) {
return; var $lightbox = jQuery('#lightbox');
$lightbox.find('img').attr('src', $img.attr('src'));
$lightbox
.show()
.css({
left: ($img.position().left + $img.outerWidth()) + 'px',
top: ($img.position().top + ($img.outerHeight() - $lightbox.outerHeight()) / 2) + 'px',
});
} }
var $lightbox = jQuery('#lightbox');
$lightbox.find('img').attr('src', $img.attr('src'));
$lightbox
.show()
.css({
left: ($img.position().left + $img.outerWidth()) + 'px',
top: ($img.position().top + ($img.outerHeight() - $lightbox.outerHeight()) / 2) + 'px',
});
} }
function postTableRowImageUnhovered(e) { function postTableRowImageUnhovered(e) {
@ -412,31 +413,27 @@ App.Presenters.PostUploadPresenter = function(
$lightbox.hide(); $lightbox.hide();
} }
function selectPostInTable(post) { function selectPostTableRow(post) {
var $table = $el.find('table'); if (post) {
$table.find('tbody input[type=checkbox]').prop('checked', false); var $table = $el.find('table');
$table.find('tbody tr').each(function(i, row) { $table.find('tbody input[type=checkbox]').prop('checked', false);
var $row = jQuery(row); $table.find('tbody tr').each(function(i, row) {
if (post === $row.data('post')) { var $row = jQuery(row);
$row.find('input[type=checkbox]').prop('checked', true); if (post === $row.data('post')) {
return false; $row.find('input[type=checkbox]').prop('checked', true);
} return false;
}); }
postTableCheckboxesChanged(); });
} postTableCheckboxesChanged();
function selectPrevPostInTable() {
var prevPost = $el.find('tbody tr.selected:eq(0)').prev().data('post');
if (prevPost) {
selectPostInTable(prevPost);
} }
} }
function selectNextPostInTable() { function selectPrevPostTableRow() {
var nextPost = $el.find('tbody tr.selected:eq(0)').next().data('post'); selectPostTableRow($el.find('tbody tr.selected:eq(0)').prev().data('post'));
if (nextPost) { }
selectPostInTable(nextPost);
} function selectNextPostTableRow() {
selectPostTableRow($el.find('tbody tr.selected:eq(0)').next().data('post'));
} }
function showOrHidePostsTable() { function showOrHidePostsTable() {