2015-04-23 13:33:29 -04:00
|
|
|
import PostView from "discourse/views/post";
|
2015-09-16 07:01:08 -04:00
|
|
|
import { on } from "ember-addons/ember-computed-decorators";
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
function createPollView(container, post, poll, vote) {
|
|
|
|
const controller = container.lookup("controller:poll", { singleton: false }),
|
|
|
|
view = container.lookup("view:poll");
|
|
|
|
|
|
|
|
controller.set("vote", vote);
|
2015-05-06 11:49:55 -04:00
|
|
|
controller.setProperties({ model: Em.Object.create(poll), post });
|
2015-04-23 13:33:29 -04:00
|
|
|
view.set("controller", controller);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "extend-for-poll",
|
|
|
|
|
|
|
|
initialize(container) {
|
|
|
|
|
2015-04-27 12:59:29 -04:00
|
|
|
const messageBus = container.lookup("message-bus:main");
|
|
|
|
|
|
|
|
// listen for back-end to tell us when a post has a poll
|
|
|
|
messageBus.subscribe("/polls", data => {
|
2015-07-07 10:51:40 -04:00
|
|
|
const post = container.lookup("controller:topic").get('model.postStream').findLoadedPost(data.post_id);
|
2015-04-27 12:59:29 -04:00
|
|
|
// HACK to trigger the "postViewUpdated" event
|
2015-09-16 07:01:08 -04:00
|
|
|
Em.run.next(() => post.set("cooked", post.get("cooked") + " "));
|
2015-04-27 12:59:29 -04:00
|
|
|
});
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
// overwrite polls
|
|
|
|
PostView.reopen({
|
2015-09-16 07:01:08 -04:00
|
|
|
|
|
|
|
@on("postViewInserted", "postViewUpdated")
|
|
|
|
_createPollViews($post) {
|
2015-04-30 12:25:38 -04:00
|
|
|
const post = this.get("post"),
|
2015-04-23 13:33:29 -04:00
|
|
|
polls = post.get("polls"),
|
|
|
|
votes = post.get("polls_votes") || {};
|
|
|
|
|
|
|
|
// don't even bother when there's no poll
|
|
|
|
if (!polls) { return; }
|
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
// clean-up if needed
|
|
|
|
this._cleanUpPollViews();
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
const pollViews = {};
|
|
|
|
|
|
|
|
// iterate over all polls
|
|
|
|
$(".poll", $post).each(function() {
|
|
|
|
const $div = $("<div>"),
|
|
|
|
$poll = $(this),
|
|
|
|
pollName = $poll.data("poll-name"),
|
|
|
|
pollView = createPollView(container, post, polls[pollName], votes[pollName]);
|
|
|
|
|
|
|
|
$poll.replaceWith($div);
|
2015-09-16 07:01:08 -04:00
|
|
|
Em.run.next(() => pollView.renderer.replaceIn(pollView, $div[0]));
|
2015-04-23 13:33:29 -04:00
|
|
|
pollViews[pollName] = pollView;
|
|
|
|
});
|
|
|
|
|
2015-09-16 07:01:08 -04:00
|
|
|
messageBus.subscribe(`/polls/${this.get("post.id")}`, results => {
|
2015-04-25 18:12:19 -04:00
|
|
|
if (results && results.polls) {
|
|
|
|
_.forEach(results.polls, poll => {
|
|
|
|
if (pollViews[poll.name]) {
|
|
|
|
pollViews[poll.name].get("controller").set("model", Em.Object.create(poll));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.set("pollViews", pollViews);
|
2015-09-16 07:01:08 -04:00
|
|
|
},
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-09-16 07:01:08 -04:00
|
|
|
@on("willClearRender")
|
|
|
|
_cleanUpPollViews() {
|
|
|
|
messageBus.unsubscribe(`/polls/${this.get("post.id")}`);
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
if (this.get("pollViews")) {
|
|
|
|
_.forEach(this.get("pollViews"), v => v.destroy());
|
|
|
|
}
|
2015-09-16 07:01:08 -04:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
});
|
|
|
|
}
|
2015-04-30 12:25:38 -04:00
|
|
|
};
|