FIX: Actions summary expanding was broken

This commit is contained in:
Robin Ward 2015-04-15 19:08:16 -04:00
parent 2bec7253e9
commit 9cb928e893
5 changed files with 39 additions and 47 deletions

View File

@ -10,20 +10,20 @@ export default Em.Component.extend(StringBuffer, {
rerenderTriggers: ['actionsHistory.@each', 'actionsHistory.users.length', 'post.deleted'],
// This was creating way too many bound ifs and subviews in the handlebars version.
renderString: function(buffer) {
renderString(buffer) {
if (!this.get('emptyHistory')) {
this.get('actionsHistory').forEach(function(c) {
buffer.push("<div class='post-action'>");
var renderActionIf = function(property, dataAttribute, text) {
const renderActionIf = function(property, dataAttribute, text) {
if (!c.get(property)) { return; }
buffer.push(" <span class='action-link " + dataAttribute +"-action'><a href='#' data-" + dataAttribute + "='" + c.get('id') + "'>" + text + "</a>.</span>");
};
// TODO multi line expansion for flags
var iconsHtml = "";
let iconsHtml = "";
if (c.get('usersExpanded')) {
var postUrl;
let postUrl;
c.get('users').forEach(function(u) {
iconsHtml += "<a href=\"" + Discourse.getURL("/users/") + u.get('username_lower') + "\" data-user-card=\"" + u.get('username_lower') + "\">";
if (u.post_url) {
@ -37,7 +37,7 @@ export default Em.Component.extend(StringBuffer, {
iconsHtml += "</a>";
});
var key = 'post.actions.people.' + c.get('actionType.name_key');
let key = 'post.actions.people.' + c.get('actionType.name_key');
if (postUrl) { key = key + "_with_url"; }
// TODO postUrl might be uninitialized? pick a good default
@ -52,7 +52,7 @@ export default Em.Component.extend(StringBuffer, {
});
}
var post = this.get('post');
const post = this.get('post');
if (post.get('deleted')) {
buffer.push("<div class='post-action'>" +
"<i class='fa fa-trash-o'></i>&nbsp;" +
@ -62,32 +62,34 @@ export default Em.Component.extend(StringBuffer, {
}
},
actionTypeById: function(actionTypeId) {
actionTypeById(actionTypeId) {
return this.get('actionsHistory').findProperty('id', actionTypeId);
},
click: function(e) {
var $target = $(e.target),
actionTypeId;
click(e) {
const $target = $(e.target);
let actionTypeId;
const post = this.get('post');
if (actionTypeId = $target.data('defer-flags')) {
this.actionTypeById(actionTypeId).deferFlags();
this.actionTypeById(actionTypeId).deferFlags(post);
return false;
}
// User wants to know who actioned it
if (actionTypeId = $target.data('who-acted')) {
this.actionTypeById(actionTypeId).loadUsers();
this.actionTypeById(actionTypeId).loadUsers(post);
return false;
}
if (actionTypeId = $target.data('act')) {
this.get('actionsHistory').findProperty('id', actionTypeId).act();
this.get('actionsHistory').findProperty('id', actionTypeId).act(post);
return false;
}
if (actionTypeId = $target.data('undo')) {
this.get('actionsHistory').findProperty('id', actionTypeId).undo();
this.get('actionsHistory').findProperty('id', actionTypeId).undo(post);
return false;
}

View File

@ -81,7 +81,8 @@ export default ObjectController.extend(ModalFunctionality, {
if (opts) params = $.extend(params, opts);
this.send('hideModal');
postAction.act(params).then(function() {
postAction.act(this.get('model'), params).then(function() {
self.send('closeModal');
}, function(errors) {
self.send('closeModal');

View File

@ -150,7 +150,7 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, BufferedCon
toggleLike(post) {
const likeAction = post.get('actionByName.like');
if (likeAction && likeAction.get('canToggle')) {
likeAction.toggle();
likeAction.toggle(post);
}
},

View File

@ -1,11 +1,3 @@
/**
A data model for summarizing actions a user has taken, for example liking a post.
@class ActionSummary
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.ActionSummary = Discourse.Model.extend({
// Description for the action
@ -44,16 +36,16 @@ Discourse.ActionSummary = Discourse.Model.extend({
}
},
toggle: function() {
toggle: function(post) {
if (!this.get('acted')) {
this.act();
this.act(post);
} else {
this.undo();
this.undo(post);
}
},
// Perform this action
act: function(opts) {
act: function(post, opts) {
if (!opts) opts = {};
var action = this.get('actionType.name_key');
@ -82,18 +74,14 @@ Discourse.ActionSummary = Discourse.Model.extend({
return Discourse.ajax("/post_actions", {
type: 'POST',
data: {
id: this.get('flagTopic') ? this.get('flagTopic.id') : this.get('post.id'),
id: this.get('flagTopic') ? this.get('flagTopic.id') : post.get('id'),
post_action_type_id: this.get('id'),
message: opts.message,
take_action: opts.takeAction,
flag_topic: this.get('flagTopic') ? true : false
}
}).then(function(result) {
var post = self.get('post');
if (post && result && result.id === post.get('id')) {
post.updateFromJson(result);
}
return post;
return post.updateActionsSummary(result);
}).catch(function (error) {
self.removeAction();
var message = $.parseJSON(error.responseText).errors;
@ -102,43 +90,38 @@ Discourse.ActionSummary = Discourse.Model.extend({
},
// Undo this action
undo: function() {
undo: function(post) {
this.removeAction();
// Remove our post action
var self = this;
return Discourse.ajax("/post_actions/" + (this.get('post.id')), {
return Discourse.ajax("/post_actions/" + post.get('id'), {
type: 'DELETE',
data: {
post_action_type_id: this.get('id')
}
}).then(function(result) {
var post = self.get('post');
if (post && result && result.id === post.get('id')) {
post.updateFromJson(result);
}
return post;
return post.updateActionsSummary(result);
});
},
deferFlags: function() {
deferFlags: function(post) {
var self = this;
return Discourse.ajax("/post_actions/defer_flags", {
type: "POST",
data: {
post_action_type_id: this.get("id"),
id: this.get("post.id")
id: post.get('id')
}
}).then(function () {
self.set("count", 0);
});
},
loadUsers: function() {
loadUsers: function(post) {
var self = this;
Discourse.ajax("/post_actions/users", {
data: {
id: this.get('post.id'),
id: post.get('id'),
post_action_type_id: this.get('id')
}
}).then(function (result) {

View File

@ -332,6 +332,13 @@ const Post = RestModel.extend({
if (bookmarkedTopic) {self.set("topic.bookmarked", false); }
throw e;
});
},
updateActionsSummary(json) {
if (json && json.id === this.get('id')) {
json = Post.munge(json);
this.set('actions_summary', json.actions_summary);
}
}
});
@ -343,7 +350,6 @@ Post.reopenClass({
const lookup = Em.Object.create();
// this area should be optimized, it is creating way too many objects per post
json.actions_summary = json.actions_summary.map(function(a) {
a.post = json;
a.actionType = Discourse.Site.current().postActionTypeById(a.id);
const actionSummary = Discourse.ActionSummary.create(a);
lookup[a.actionType.name_key] = actionSummary;