Extracted post edit presenter

This commit is contained in:
Marcin Kurczewski 2014-10-22 10:27:20 +02:00
parent ea16d5c9df
commit 82d59b57e6
5 changed files with 183 additions and 121 deletions

View file

@ -144,7 +144,7 @@
font-size: 12px; font-size: 12px;
} }
#post-view-wrapper .post-edit-wrapper { #post-view #post-edit-target {
padding: 1em; padding: 1em;
position: absolute; position: absolute;
background: rgba(255, 255, 255, 0.8); background: rgba(255, 255, 255, 0.8);
@ -156,7 +156,7 @@
z-index: -1; z-index: -1;
} }
#post-view-wrapper .post-edit-wrapper .file-handler { #post-edit-target .file-handler {
margin: 0.5em 0; margin: 0.5em 0;
} }

View file

@ -120,6 +120,7 @@
<script type="text/javascript" src="/js/Presenters/PostListPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostListPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostUploadPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostUploadPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostEditPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/GlobalCommentListPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/GlobalCommentListPresenter.js"></script>

View file

@ -0,0 +1,156 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PostEditPresenter = function(
util,
promise,
api,
auth) {
var $target;
var post;
var updateCallback;
var privileges = {};
var templates = {};
var tagInput;
var postContentFileDropper;
var postThumbnailFileDropper;
var postContent;
var postThumbnail;
privileges.canChangeSafety = auth.hasPrivilege(auth.privileges.changePostSafety);
privileges.canChangeSource = auth.hasPrivilege(auth.privileges.changePostSource);
privileges.canChangeTags = auth.hasPrivilege(auth.privileges.changePostTags);
privileges.canChangeContent = auth.hasPrivilege(auth.privileges.changePostContent);
privileges.canChangeThumbnail = auth.hasPrivilege(auth.privileges.changePostThumbnail);
privileges.canChangeRelations = auth.hasPrivilege(auth.privileges.changePostRelations);
privileges.canChangeFlags = auth.hasPrivilege(auth.privileges.changePostFlags);
function init(params, loaded) {
post = params.post;
updateCallback = params.updateCallback;
$target = params.$target;
promise.wait(util.promiseTemplate('post-edit'))
.then(function(postEditTemplate) {
templates.postEdit = postEditTemplate;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}
function render() {
$target.html(templates.postEdit({post: post, privileges: privileges}));
postContentFileDropper = new App.Controls.FileDropper($target.find('form [name=content]'));
postContentFileDropper.onChange = postContentChanged;
postContentFileDropper.setNames = true;
postThumbnailFileDropper = new App.Controls.FileDropper($target.find('form [name=thumbnail]'));
postThumbnailFileDropper.onChange = postThumbnailChanged;
postThumbnailFileDropper.setNames = true;
if (privileges.canChangeTags) {
tagInput = new App.Controls.TagInput($target.find('form [name=tags]'));
tagInput.inputConfirmed = editPost;
}
$target.find('.post-edit-wrapper form').submit(editFormSubmitted);
}
function focus() {
if (tagInput) {
tagInput.focus();
}
}
function editFormSubmitted(e) {
e.preventDefault();
editPost();
}
function postContentChanged(files) {
postContentFileDropper.readAsDataURL(files[0], function(content) {
postContent = content;
});
}
function postThumbnailChanged(files) {
postThumbnailFileDropper.readAsDataURL(files[0], function(content) {
postThumbnail = content;
});
}
function getPrivileges() {
return privileges;
}
function editPost() {
var $form = $target.find('form');
var formData = {};
formData.seenEditTime = post.lastEditTime;
formData.flags = {};
if (privileges.canChangeContent && postContent) {
formData.content = postContent;
}
if (privileges.canChangeThumbnail && postThumbnail) {
formData.thumbnail = postThumbnail;
}
if (privileges.canChangeSource) {
formData.source = $form.find('[name=source]').val();
}
if (privileges.canChangeSafety) {
formData.safety = $form.find('[name=safety]:checked').val();
}
if (privileges.canChangeTags) {
formData.tags = tagInput.getTags().join(' ');
}
if (privileges.canChangeRelations) {
formData.relations = $form.find('[name=relations]').val();
}
if (privileges.canChangeFlags) {
if (post.contentType === 'video') {
formData.flags.loop = $form.find('[name=loop]').is(':checked') ? 1 : 0;
}
}
if (post.tags.length === 0) {
showEditError('No tags set.');
return;
}
promise.wait(api.put('/posts/' + post.id, formData))
.then(function(response) {
if (typeof(updateCallback) !== 'undefined') {
updateCallback(post = response.json);
}
}).fail(function(response) {
showEditError(response);
});
}
function showEditError(response) {
window.alert(response.json && response.json.error || response);
}
return {
init: init,
render: render,
getPrivileges: getPrivileges,
focus: focus,
};
};
App.DI.register('postEditPresenter', ['util', 'promise', 'api', 'auth'], App.Presenters.PostEditPresenter);

View file

@ -12,6 +12,7 @@ App.Presenters.PostPresenter = function(
keyboard, keyboard,
presenterManager, presenterManager,
postsAroundCalculator, postsAroundCalculator,
postEditPresenter,
postCommentListPresenter, postCommentListPresenter,
topNavigationPresenter, topNavigationPresenter,
messagePresenter) { messagePresenter) {
@ -26,13 +27,6 @@ App.Presenters.PostPresenter = function(
var post; var post;
var privileges = {}; var privileges = {};
var editPrivileges = {};
var tagInput;
var postContentFileDropper;
var postThumbnailFileDropper;
var postContent;
var postThumbnail;
function init(params, loaded) { function init(params, loaded) {
topNavigationPresenter.select('posts'); topNavigationPresenter.select('posts');
@ -41,26 +35,16 @@ App.Presenters.PostPresenter = function(
privileges.canDeletePosts = auth.hasPrivilege(auth.privileges.deletePosts); privileges.canDeletePosts = auth.hasPrivilege(auth.privileges.deletePosts);
privileges.canFeaturePosts = auth.hasPrivilege(auth.privileges.featurePosts); privileges.canFeaturePosts = auth.hasPrivilege(auth.privileges.featurePosts);
privileges.canViewHistory = auth.hasPrivilege(auth.privileges.viewHistory); privileges.canViewHistory = auth.hasPrivilege(auth.privileges.viewHistory);
editPrivileges.canChangeSafety = auth.hasPrivilege(auth.privileges.changePostSafety);
editPrivileges.canChangeSource = auth.hasPrivilege(auth.privileges.changePostSource);
editPrivileges.canChangeTags = auth.hasPrivilege(auth.privileges.changePostTags);
editPrivileges.canChangeContent = auth.hasPrivilege(auth.privileges.changePostContent);
editPrivileges.canChangeThumbnail = auth.hasPrivilege(auth.privileges.changePostThumbnail);
editPrivileges.canChangeRelations = auth.hasPrivilege(auth.privileges.changePostRelations);
editPrivileges.canChangeFlags = auth.hasPrivilege(auth.privileges.changePostFlags);
promise.wait( promise.wait(
util.promiseTemplate('post'), util.promiseTemplate('post'),
util.promiseTemplate('post-edit'),
util.promiseTemplate('post-content'), util.promiseTemplate('post-content'),
util.promiseTemplate('history')) util.promiseTemplate('history'))
.then(function( .then(function(
postTemplate, postTemplate,
postEditTemplate,
postContentTemplate, postContentTemplate,
historyTemplate) { historyTemplate) {
templates.post = postTemplate; templates.post = postTemplate;
templates.postEdit = postEditTemplate;
templates.postContent = postContentTemplate; templates.postContent = postContentTemplate;
templates.history = historyTemplate; templates.history = historyTemplate;
@ -83,10 +67,17 @@ App.Presenters.PostPresenter = function(
topNavigationPresenter.changeTitle('@' + post.id); topNavigationPresenter.changeTitle('@' + post.id);
render(); render();
loaded(); loaded();
presenterManager.initPresenters([
[postEditPresenter, {post: post, $target: $el.find('#post-edit-target'), updateCallback: postEdited}],
[postCommentListPresenter, {post: post, $target: $el.find('#post-comments-target')}]],
function() {});
}).fail(function() { }).fail(function() {
console.log(arguments); console.log(arguments);
loaded(); loaded();
}); });
} }
function attachLinksToPostsAround() { function attachLinksToPostsAround() {
@ -138,34 +129,21 @@ App.Presenters.PostPresenter = function(
$el.html(renderPostTemplate()); $el.html(renderPostTemplate());
$messages = $el.find('.messages'); $messages = $el.find('.messages');
if (editPrivileges.canChangeTags) {
tagInput = new App.Controls.TagInput($el.find('form [name=tags]'));
tagInput.inputConfirmed = editPost;
}
postContentFileDropper = new App.Controls.FileDropper($el.find('form [name=content]'));
postContentFileDropper.onChange = postContentChanged;
postContentFileDropper.setNames = true;
postThumbnailFileDropper = new App.Controls.FileDropper($el.find('form [name=thumbnail]'));
postThumbnailFileDropper.onChange = postThumbnailChanged;
postThumbnailFileDropper.setNames = true;
if (_.any(editPrivileges)) {
keyboard.keyup('e', function() { keyboard.keyup('e', function() {
editButtonClicked(null); editButtonClicked(null);
}); });
}
$el.find('.post-edit-wrapper form').submit(editFormSubmitted);
attachSidebarEvents(); attachSidebarEvents();
presenterManager.initPresenters([
[postCommentListPresenter, _.extend({post: post}, {$target: $el.find('#post-comments-target')})]],
function() { });
attachLinksToPostsAround(); attachLinksToPostsAround();
} }
function postEdited(newPost) {
post = newPost;
hideEditForm();
softRender();
}
function softRender() { function softRender() {
renderSidebar(); renderSidebar();
$el.find('video').prop('loop', post.flags.loop); $el.find('video').prop('loop', post.flags.loop);
@ -188,13 +166,12 @@ App.Presenters.PostPresenter = function(
formatFileSize: util.formatFileSize, formatFileSize: util.formatFileSize,
postContentTemplate: templates.postContent, postContentTemplate: templates.postContent,
postEditTemplate: templates.postEdit,
historyTemplate: templates.history, historyTemplate: templates.history,
hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }), hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }),
isLoggedIn: auth.isLoggedIn(), isLoggedIn: auth.isLoggedIn(),
privileges: privileges, privileges: privileges,
editPrivileges: editPrivileges, editPrivileges: postEditPresenter.getPrivileges(),
}); });
} }
@ -244,92 +221,24 @@ App.Presenters.PostPresenter = function(
e.preventDefault(); e.preventDefault();
} }
messagePresenter.hideMessages($messages); messagePresenter.hideMessages($messages);
if ($el.find('.post-edit-wrapper').is(':visible')) { if ($el.find('#post-edit-target').is(':visible')) {
hideEditForm(); hideEditForm();
} else { } else {
showEditForm(); showEditForm();
} }
} }
function editFormSubmitted(e) {
e.preventDefault();
editPost();
}
function showEditForm() { function showEditForm() {
$el.find('.post-edit-wrapper').slideDown('fast'); $el.find('#post-edit-target').slideDown('fast');
util.enableExitConfirmation(); util.enableExitConfirmation();
tagInput.focus(); postEditPresenter.focus();
} }
function hideEditForm() { function hideEditForm() {
$el.find('.post-edit-wrapper').slideUp('fast'); $el.find('#post-edit-target').slideUp('fast');
util.disableExitConfirmation(); util.disableExitConfirmation();
} }
function editPost() {
var $form = $el.find('form');
var formData = {};
formData.seenEditTime = post.lastEditTime;
formData.flags = {};
if (editPrivileges.canChangeContent && postContent) {
formData.content = postContent;
}
if (editPrivileges.canChangeThumbnail && postThumbnail) {
formData.thumbnail = postThumbnail;
}
if (editPrivileges.canChangeSource) {
formData.source = $form.find('[name=source]').val();
}
if (editPrivileges.canChangeSafety) {
formData.safety = $form.find('[name=safety]:checked').val();
}
if (editPrivileges.canChangeTags) {
formData.tags = tagInput.getTags().join(' ');
}
if (editPrivileges.canChangeRelations) {
formData.relations = $form.find('[name=relations]').val();
}
if (editPrivileges.canChangeFlags) {
if (post.contentType === 'video') {
formData.flags.loop = $form.find('[name=loop]').is(':checked') ? 1 : 0;
}
}
if (post.tags.length === 0) {
showEditError('No tags set.');
return;
}
promise.wait(api.put('/posts/' + post.id, formData))
.then(function(response) {
post = response.json;
hideEditForm();
softRender();
}).fail(function(response) {
showEditError(response);
});
}
function postContentChanged(files) {
postContentFileDropper.readAsDataURL(files[0], function(content) {
postContent = content;
});
}
function postThumbnailChanged(files) {
postThumbnailFileDropper.readAsDataURL(files[0], function(content) {
postThumbnail = content;
});
}
function historyButtonClicked(e) { function historyButtonClicked(e) {
e.preventDefault(); e.preventDefault();
if ($el.find('.post-history-wrapper').is(':visible')) { if ($el.find('.post-history-wrapper').is(':visible')) {
@ -390,10 +299,6 @@ App.Presenters.PostPresenter = function(
}).fail(showGenericError); }).fail(showGenericError);
} }
function showEditError(response) {
window.alert(response.json && response.json.error || response);
}
function showGenericError(response) { function showGenericError(response) {
if ($messages === $el) { if ($messages === $el) {
$el.empty(); $el.empty();
@ -420,6 +325,7 @@ App.DI.register('postPresenter', [
'keyboard', 'keyboard',
'presenterManager', 'presenterManager',
'postsAroundCalculator', 'postsAroundCalculator',
'postEditPresenter',
'postCommentListPresenter', 'postCommentListPresenter',
'topNavigationPresenter', 'topNavigationPresenter',
'messagePresenter'], 'messagePresenter'],

View file

@ -252,8 +252,7 @@
<div id="post-view"> <div id="post-view">
<div class="messages"></div> <div class="messages"></div>
<div class="post-edit-wrapper"> <div id="post-edit-target">
<%= postEditTemplate({post: post, privileges: editPrivileges}) %>
</div> </div>
<%= postContentTemplate({post: post}) %> <%= postContentTemplate({post: post}) %>