2016-04-01 05:33:25 +08:00
|
|
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
function createPollView(container, post, poll, vote) {
|
2016-04-01 05:33:25 +08:00
|
|
|
const controller = container.lookup("controller:poll", { singleton: false });
|
|
|
|
const view = container.lookup("view:poll");
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
controller.set("vote", vote);
|
2016-04-01 05:33:25 +08:00
|
|
|
controller.setProperties({ model: poll, post });
|
2015-04-24 01:33:29 +08:00
|
|
|
view.set("controller", controller);
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
let _pollViews;
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
function initializePolls(api) {
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
const TopicController = api.container.lookupFactory('controller:topic');
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Post = api.container.lookupFactory('model:post');
|
|
|
|
Post.reopen({
|
|
|
|
_polls: null,
|
|
|
|
pollsObject: null,
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
});
|
|
|
|
|
|
|
|
function cleanUpPollViews() {
|
|
|
|
if (_pollViews) {
|
|
|
|
Object.keys(_pollViews).forEach(pollName => _pollViews[pollName].destroy());
|
|
|
|
}
|
|
|
|
_pollViews = null;
|
|
|
|
}
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
function createPollViews($elem, helper) {
|
|
|
|
const $polls = $('.poll', $elem);
|
|
|
|
if (!$polls.length) { return; }
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
const post = helper.getModel();
|
|
|
|
api.preventCloak(post.id);
|
|
|
|
const votes = post.get('polls_votes') || {};
|
2015-09-16 19:01:08 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
post.pollsChanged();
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
const polls = post.get("pollsObject");
|
|
|
|
if (!polls) { return; }
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
cleanUpPollViews();
|
|
|
|
const postPollViews = {};
|
2015-04-26 06:12:19 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
$polls.each((idx, pollElem) => {
|
|
|
|
const $div = $("<div>");
|
|
|
|
const $poll = $(pollElem);
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
const pollName = $poll.data("poll-name");
|
|
|
|
const pollId = `${pollName}-${post.id}`;
|
|
|
|
const pollView = createPollView(helper.container, post, polls[pollName], votes[pollName]);
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
$poll.replaceWith($div);
|
|
|
|
Em.run.next(() => pollView.renderer.replaceIn(pollView, $div[0]));
|
|
|
|
postPollViews[pollId] = pollView;
|
|
|
|
});
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
_pollViews = postPollViews;
|
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
api.decorateCooked(createPollViews, { onlyStream: true });
|
|
|
|
api.cleanupStream(cleanUpPollViews);
|
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
export default {
|
|
|
|
name: "extend-for-poll",
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
initialize() {
|
|
|
|
withPluginApi('0.1', initializePolls);
|
2015-04-24 01:33:29 +08:00
|
|
|
}
|
2015-05-01 00:25:38 +08:00
|
|
|
};
|