2016-02-19 04:17:53 +08:00
|
|
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
2016-06-07 18:55:01 +08:00
|
|
|
import { observes } from "ember-addons/ember-computed-decorators";
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-11-26 00:57:51 +08:00
|
|
|
function createPollComponent(register, post, poll, vote) {
|
|
|
|
const component = register.lookup("component:discourse-poll");
|
2016-11-22 03:49:00 +08:00
|
|
|
component.setProperties({ model: poll, vote, post });
|
|
|
|
return component;
|
2015-04-24 01:33:29 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
let _pollViews;
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
function initializePolls(api) {
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +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
|
2016-06-07 18:55:01 +08:00
|
|
|
@observes("polls")
|
|
|
|
pollsChanged() {
|
2016-02-19 04:17:53 +08:00
|
|
|
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);
|
|
|
|
}
|
2016-01-06 17:35:27 +08:00
|
|
|
});
|
2016-02-19 04:17:53 +08:00
|
|
|
this.set("pollsObject", this._polls);
|
2016-01-06 17:35:27 +08:00
|
|
|
}
|
2016-06-07 18:55:01 +08:00
|
|
|
}
|
2016-02-19 04:17:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
function cleanUpPollViews() {
|
|
|
|
if (_pollViews) {
|
|
|
|
Object.keys(_pollViews).forEach(pollName => _pollViews[pollName].destroy());
|
|
|
|
}
|
|
|
|
_pollViews = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPollViews($elem, helper) {
|
|
|
|
const $polls = $('.poll', $elem);
|
|
|
|
if (!$polls.length) { return; }
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
const post = helper.getModel();
|
2016-03-29 00:17:37 +08:00
|
|
|
api.preventCloak(post.id);
|
2016-02-19 04:17:53 +08:00
|
|
|
const votes = post.get('polls_votes') || {};
|
2015-09-16 19:01:08 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
post.pollsChanged();
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
const polls = post.get("pollsObject");
|
|
|
|
if (!polls) { return; }
|
2016-01-06 17:35:27 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
const postPollViews = {};
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
$polls.each((idx, pollElem) => {
|
|
|
|
const $div = $("<div>");
|
|
|
|
const $poll = $(pollElem);
|
2016-01-06 17:35:27 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
const pollName = $poll.data("poll-name");
|
2016-03-29 00:17:37 +08:00
|
|
|
const pollId = `${pollName}-${post.id}`;
|
2016-06-07 18:55:01 +08:00
|
|
|
|
2016-11-22 03:49:00 +08:00
|
|
|
const pollComponent = createPollComponent(
|
2016-11-26 00:57:51 +08:00
|
|
|
helper.register,
|
2016-06-07 18:55:01 +08:00
|
|
|
post,
|
|
|
|
polls[pollName],
|
2016-11-22 03:49:00 +08:00
|
|
|
votes[pollName]
|
2016-06-07 18:55:01 +08:00
|
|
|
);
|
2015-04-26 06:12:19 +08:00
|
|
|
|
2016-12-03 05:41:51 +08:00
|
|
|
// Destroy a poll view if we're replacing it
|
|
|
|
if (_pollViews && _pollViews[pollId]) {
|
|
|
|
_pollViews[pollId].destroy();
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
$poll.replaceWith($div);
|
2016-12-03 05:41:51 +08:00
|
|
|
Ember.run.scheduleOnce('afterRender', () => {
|
|
|
|
pollComponent.renderer.appendTo(pollComponent, $div[0]);
|
|
|
|
});
|
|
|
|
|
2016-11-22 03:49:00 +08:00
|
|
|
postPollViews[pollId] = pollComponent;
|
2016-02-19 04:17:53 +08:00
|
|
|
});
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
_pollViews = postPollViews;
|
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-06-21 17:41:53 +08:00
|
|
|
api.includePostAttributes("polls", "polls_votes");
|
2016-02-19 04:17:53 +08:00
|
|
|
api.decorateCooked(createPollViews, { onlyStream: true });
|
|
|
|
api.cleanupStream(cleanUpPollViews);
|
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
export default {
|
|
|
|
name: "extend-for-poll",
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
initialize() {
|
|
|
|
withPluginApi('0.1', initializePolls);
|
2015-04-24 01:33:29 +08:00
|
|
|
}
|
2015-05-01 00:25:38 +08:00
|
|
|
};
|