2018-06-15 12:42:20 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2016-06-07 06:55:01 -04:00
|
|
|
import { observes } from "ember-addons/ember-computed-decorators";
|
2018-06-15 12:42:20 -04:00
|
|
|
import { getRegister } from "discourse-common/lib/get-owner";
|
|
|
|
import WidgetGlue from "discourse/widgets/glue";
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
function initializePolls(api) {
|
2016-12-07 15:48:47 -05:00
|
|
|
const register = getRegister(api);
|
2015-04-27 12:59:29 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
api.modifyClass("controller:topic", {
|
|
|
|
subscribe() {
|
2016-02-18 15:17:53 -05:00
|
|
|
this._super();
|
|
|
|
this.messageBus.subscribe("/polls/" + this.get("model.id"), msg => {
|
2018-06-15 12:42:20 -04:00
|
|
|
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
|
2016-02-18 15:17:53 -05:00
|
|
|
if (post) {
|
2018-06-15 12:42:20 -04:00
|
|
|
post.set("polls", msg.polls);
|
2016-02-18 15:17:53 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-06-15 12:42:20 -04:00
|
|
|
unsubscribe() {
|
|
|
|
this.messageBus.unsubscribe("/polls/*");
|
2016-02-18 15:17:53 -05:00
|
|
|
this._super();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
let _glued = [];
|
|
|
|
let _interval = null;
|
|
|
|
|
|
|
|
function rerender() {
|
|
|
|
_glued.forEach(g => g.queueRerender());
|
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
api.modifyClass("model:post", {
|
2016-02-18 15:17:53 -05:00
|
|
|
_polls: null,
|
|
|
|
pollsObject: null,
|
|
|
|
|
|
|
|
// we need a proper ember object so it is bindable
|
2016-06-07 06:55:01 -04:00
|
|
|
@observes("polls")
|
|
|
|
pollsChanged() {
|
2016-02-18 15:17:53 -05:00
|
|
|
const polls = this.get("polls");
|
|
|
|
if (polls) {
|
|
|
|
this._polls = this._polls || {};
|
2018-06-15 12:42:20 -04:00
|
|
|
_.map(polls, (v, k) => {
|
2016-02-18 15:17:53 -05:00
|
|
|
const existing = this._polls[k];
|
|
|
|
if (existing) {
|
|
|
|
this._polls[k].setProperties(v);
|
|
|
|
} else {
|
|
|
|
this._polls[k] = Em.Object.create(v);
|
|
|
|
}
|
2016-01-06 04:35:27 -05:00
|
|
|
});
|
2016-02-18 15:17:53 -05:00
|
|
|
this.set("pollsObject", this._polls);
|
2018-05-02 20:12:19 -04:00
|
|
|
rerender();
|
2016-01-06 04:35:27 -05:00
|
|
|
}
|
2016-06-07 06:55:01 -04:00
|
|
|
}
|
2016-02-18 15:17:53 -05:00
|
|
|
});
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
function attachPolls($elem, helper) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const $polls = $(".poll", $elem);
|
|
|
|
if (!$polls.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-27 12:59:29 -04:00
|
|
|
|
2018-06-18 11:34:32 -04:00
|
|
|
if (!helper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
const post = helper.getModel();
|
2016-03-28 12:17:37 -04:00
|
|
|
api.preventCloak(post.id);
|
2018-06-15 12:42:20 -04:00
|
|
|
const votes = post.get("polls_votes") || {};
|
2015-09-16 07:01:08 -04:00
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
post.pollsChanged();
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
const polls = post.get("pollsObject");
|
2018-06-15 12:42:20 -04:00
|
|
|
if (!polls) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-06 04:35:27 -05:00
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
_interval = _interval || setInterval(rerender, 30000);
|
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
$polls.each((idx, pollElem) => {
|
|
|
|
const $poll = $(pollElem);
|
|
|
|
const pollName = $poll.data("poll-name");
|
2016-12-07 15:48:47 -05:00
|
|
|
const poll = polls[pollName];
|
|
|
|
if (poll) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const isMultiple = poll.get("type") === "multiple";
|
2016-12-07 15:48:47 -05:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const glue = new WidgetGlue("discourse-poll", register, {
|
2016-12-07 15:48:47 -05:00
|
|
|
id: `${pollName}-${post.id}`,
|
|
|
|
post,
|
|
|
|
poll,
|
|
|
|
vote: votes[pollName] || [],
|
2018-06-15 12:42:20 -04:00
|
|
|
isMultiple
|
2016-12-07 15:48:47 -05:00
|
|
|
});
|
|
|
|
glue.appendTo(pollElem);
|
|
|
|
_glued.push(glue);
|
2016-12-02 16:41:51 -05:00
|
|
|
}
|
2016-02-18 15:17:53 -05:00
|
|
|
});
|
2016-12-07 15:48:47 -05:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
function cleanUpPolls() {
|
2018-05-02 20:12:19 -04:00
|
|
|
if (_interval) {
|
|
|
|
clearInterval(_interval);
|
|
|
|
_interval = null;
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
_glued.forEach(g => g.cleanUp());
|
2018-05-02 20:12:19 -04:00
|
|
|
_glued = [];
|
2016-02-18 15:17:53 -05:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-06-21 05:41:53 -04:00
|
|
|
api.includePostAttributes("polls", "polls_votes");
|
2016-12-07 15:48:47 -05:00
|
|
|
api.decorateCooked(attachPolls, { onlyStream: true });
|
|
|
|
api.cleanupStream(cleanUpPolls);
|
2016-02-18 15:17:53 -05:00
|
|
|
}
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
export default {
|
|
|
|
name: "extend-for-poll",
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-02-18 15:17:53 -05:00
|
|
|
initialize() {
|
2018-06-15 12:42:20 -04:00
|
|
|
withPluginApi("0.8.7", initializePolls);
|
2015-04-23 13:33:29 -04:00
|
|
|
}
|
2015-04-30 12:25:38 -04:00
|
|
|
};
|