2015-04-24 01:33:29 +08:00
|
|
|
import PostView from "discourse/views/post";
|
2016-01-06 17:35:27 +08:00
|
|
|
import TopicController from "discourse/controllers/topic";
|
|
|
|
import Post from "discourse/models/post";
|
|
|
|
|
2015-09-16 19:01:08 +08:00
|
|
|
import { on } from "ember-addons/ember-computed-decorators";
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
function createPollView(container, post, poll, vote) {
|
|
|
|
const controller = container.lookup("controller:poll", { singleton: false }),
|
|
|
|
view = container.lookup("view:poll");
|
|
|
|
|
|
|
|
controller.set("vote", vote);
|
2016-01-06 17:35:27 +08:00
|
|
|
controller.setProperties({ model: poll, post });
|
2015-04-24 01:33:29 +08:00
|
|
|
view.set("controller", controller);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "extend-for-poll",
|
|
|
|
|
|
|
|
initialize(container) {
|
|
|
|
|
2016-01-06 17:35:27 +08:00
|
|
|
Post.reopen({
|
|
|
|
// we need a proper ember object so it is bindable
|
|
|
|
pollsChanged: function(){
|
|
|
|
const polls = this.get("polls");
|
|
|
|
if (polls) {
|
|
|
|
this._polls = this._polls || {};
|
|
|
|
_.map(polls, (v,k) => {
|
|
|
|
const existing = this._polls[k];
|
|
|
|
if (existing) {
|
|
|
|
this._polls[k].setProperties(v);
|
|
|
|
} else {
|
|
|
|
this._polls[k] = Em.Object.create(v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.set("pollsObject", this._polls);
|
|
|
|
}
|
|
|
|
}.observes("polls")
|
|
|
|
});
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2016-01-06 17:35:27 +08:00
|
|
|
TopicController.reopen({
|
|
|
|
subscribe(){
|
|
|
|
this._super();
|
|
|
|
this.messageBus.subscribe("/polls/" + this.get("model.id"), msg => {
|
|
|
|
const post = this.get('model.postStream').findLoadedPost(msg.post_id);
|
|
|
|
if (post) {
|
|
|
|
post.set('polls', msg.polls);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
unsubscribe(){
|
|
|
|
this.messageBus.unsubscribe('/polls/*');
|
|
|
|
this._super();
|
|
|
|
}
|
2015-04-28 00:59:29 +08:00
|
|
|
});
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
// overwrite polls
|
|
|
|
PostView.reopen({
|
2015-09-16 19:01:08 +08:00
|
|
|
|
|
|
|
@on("postViewInserted", "postViewUpdated")
|
|
|
|
_createPollViews($post) {
|
2015-05-01 00:25:38 +08:00
|
|
|
const post = this.get("post"),
|
2015-04-24 01:33:29 +08:00
|
|
|
votes = post.get("polls_votes") || {};
|
|
|
|
|
2016-01-06 17:35:27 +08:00
|
|
|
post.pollsChanged();
|
|
|
|
const polls = post.get("pollsObject");
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
// don't even bother when there's no poll
|
|
|
|
if (!polls) { return; }
|
|
|
|
|
2016-01-06 17:35:27 +08:00
|
|
|
// TODO inject cleanly into
|
|
|
|
|
2015-04-26 06:12:19 +08:00
|
|
|
// clean-up if needed
|
|
|
|
this._cleanUpPollViews();
|
|
|
|
|
2015-04-24 01:33:29 +08: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 19:01:08 +08:00
|
|
|
Em.run.next(() => pollView.renderer.replaceIn(pollView, $div[0]));
|
2015-04-24 01:33:29 +08:00
|
|
|
pollViews[pollName] = pollView;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set("pollViews", pollViews);
|
2015-09-16 19:01:08 +08:00
|
|
|
},
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-09-16 19:01:08 +08:00
|
|
|
@on("willClearRender")
|
|
|
|
_cleanUpPollViews() {
|
2015-04-24 01:33:29 +08:00
|
|
|
if (this.get("pollViews")) {
|
|
|
|
_.forEach(this.get("pollViews"), v => v.destroy());
|
|
|
|
}
|
2015-09-16 19:01:08 +08:00
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
});
|
|
|
|
}
|
2015-05-01 00:25:38 +08:00
|
|
|
};
|