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";
|
2016-12-08 04:48:47 +08:00
|
|
|
import { getRegister } from 'discourse-common/lib/get-owner';
|
|
|
|
import WidgetGlue from 'discourse/widgets/glue';
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
function initializePolls(api) {
|
2016-12-08 04:48:47 +08:00
|
|
|
const register = getRegister(api);
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2017-06-14 03:52:08 +08:00
|
|
|
api.modifyClass('controller:topic', {
|
2016-02-19 04:17:53 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
let _glued = [];
|
|
|
|
let _interval = null;
|
|
|
|
|
|
|
|
function rerender() {
|
|
|
|
_glued.forEach(g => g.queueRerender());
|
|
|
|
}
|
|
|
|
|
2017-06-14 03:52:08 +08:00
|
|
|
api.modifyClass('model:post', {
|
2016-02-19 04:17:53 +08:00
|
|
|
_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);
|
2018-05-03 08:12:19 +08:00
|
|
|
rerender();
|
2016-01-06 17:35:27 +08:00
|
|
|
}
|
2016-06-07 18:55:01 +08:00
|
|
|
}
|
2016-02-19 04:17:53 +08:00
|
|
|
});
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
function attachPolls($elem, helper) {
|
2016-02-19 04:17:53 +08:00
|
|
|
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
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
_interval = _interval || setInterval(rerender, 30000);
|
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
$polls.each((idx, pollElem) => {
|
|
|
|
const $poll = $(pollElem);
|
|
|
|
const pollName = $poll.data("poll-name");
|
2016-12-08 04:48:47 +08:00
|
|
|
const poll = polls[pollName];
|
|
|
|
if (poll) {
|
|
|
|
const isMultiple = poll.get('type') === 'multiple';
|
|
|
|
|
|
|
|
const glue = new WidgetGlue('discourse-poll', register, {
|
|
|
|
id: `${pollName}-${post.id}`,
|
|
|
|
post,
|
|
|
|
poll,
|
|
|
|
vote: votes[pollName] || [],
|
|
|
|
isMultiple,
|
|
|
|
});
|
|
|
|
glue.appendTo(pollElem);
|
|
|
|
_glued.push(glue);
|
2016-12-03 05:41:51 +08:00
|
|
|
}
|
2016-02-19 04:17:53 +08:00
|
|
|
});
|
2016-12-08 04:48:47 +08:00
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
function cleanUpPolls() {
|
2018-05-03 08:12:19 +08:00
|
|
|
if (_interval) {
|
|
|
|
clearInterval(_interval);
|
|
|
|
_interval = null;
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
_glued.forEach(g => g.cleanUp());
|
2018-05-03 08:12:19 +08:00
|
|
|
_glued = [];
|
2016-02-19 04:17:53 +08:00
|
|
|
}
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-06-21 17:41:53 +08:00
|
|
|
api.includePostAttributes("polls", "polls_votes");
|
2016-12-08 04:48:47 +08:00
|
|
|
api.decorateCooked(attachPolls, { onlyStream: true });
|
|
|
|
api.cleanupStream(cleanUpPolls);
|
2016-02-19 04:17:53 +08:00
|
|
|
}
|
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() {
|
2017-06-14 03:52:08 +08:00
|
|
|
withPluginApi('0.8.7', initializePolls);
|
2015-04-24 01:33:29 +08:00
|
|
|
}
|
2015-05-01 00:25:38 +08:00
|
|
|
};
|