/**
This view renders a menu below a post. It uses buffered rendering for performance.
@class PostMenuView
@extends Discourse.View
@namespace Discourse
@module Discourse
**/
Discourse.PostMenuView = Discourse.View.extend({
tagName: 'section',
classNames: ['post-menu-area', 'clearfix'],
shouldRerender: Discourse.View.renderIfChanged(
'post.deleted_at',
'post.flagsAvailable.@each',
'post.url',
'post.bookmarked',
'post.reply_count',
'post.showRepliesBelow',
'post.can_delete',
'post.read',
'post.topic.last_read_post_number',
'post.topic.deleted_at'),
render: function(buffer) {
var post = this.get('post');
buffer.push("");
},
// Delegate click actions
click: function(e) {
var $target = $(e.target);
var action = $target.data('action') || $target.parent().data('action');
if (!action) return;
var handler = this["click" + action.capitalize()];
if (!handler) return;
handler.call(this);
},
// Replies Button
renderReplies: function(post, buffer) {
if (!post.get('showRepliesBelow')) return;
var reply_count = post.get('reply_count');
buffer.push("");
},
clickReplies: function() {
this.get('postView').showReplies();
},
// Delete button
renderDelete: function(post, buffer) {
var label, action, icon;
if (post.get('post_number') === 1) {
// If if it's the first post, the delete/undo actions are related to the topic
var topic = post.get('topic');
if (topic.get('deleted_at')) {
if (!topic.get('details.can_recover')) { return; }
label = "topic.actions.recover";
action = "recoverTopic";
icon = "undo";
} else {
if (!topic.get('details.can_delete')) { return; }
label = "topic.actions.delete";
action = "deleteTopic";
icon = "trash";
}
} else {
// The delete actions target the post iteself
if (post.get('deleted_at')) {
if (!post.get('can_recover')) { return; }
label = "post.controls.undelete";
action = "recover";
icon = "undo";
} else {
if (!post.get('can_delete')) { return; }
label = "post.controls.delete";
action = "delete";
icon = "trash";
}
}
buffer.push("");
},
clickDeleteTopic: function() {
this.get('controller').deleteTopic();
},
clickRecoverTopic: function() {
this.get('controller').recoverTopic();
},
clickRecover: function() {
this.get('controller').recoverPost(this.get('post'));
},
clickDelete: function() {
this.get('controller').deletePost(this.get('post'));
},
// Like button
renderLike: function(post, buffer) {
if (!post.get('actionByName.like.can_act')) return;
buffer.push("");
},
clickLike: function() {
var likeAction = this.get('post.actionByName.like');
if (likeAction) likeAction.act();
},
// Flag button
renderFlag: function(post, buffer) {
if (!this.present('post.flagsAvailable')) return;
buffer.push("");
},
clickFlag: function() {
this.get('controller').send('showFlags', this.get('post'));
},
// Edit button
renderEdit: function(post, buffer) {
if (!post.get('can_edit')) return;
buffer.push("");
},
clickEdit: function() {
this.get('controller').editPost(this.get('post'));
},
// Share button
renderShare: function(post, buffer) {
buffer.push("");
},
// Reply button
renderReply: function(post, buffer) {
if (!this.get('controller.model.details.can_create_post')) return;
buffer.push("");
},
clickReply: function() {
this.get('controller').replyToPost(this.get('post'));
},
// Bookmark button
renderBookmark: function(post, buffer) {
if (!Discourse.User.current()) return;
buffer.push("");
},
clickBookmark: function() {
this.get('post').toggleProperty('bookmarked');
}
});