2018-06-15 12:42:20 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2019-11-25 12:51:01 -05:00
|
|
|
import { observes } from "discourse-common/utils/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() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-02-18 15:17:53 -05:00
|
|
|
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/*");
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-02-18 15:17:53 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const polls = this.polls;
|
2016-02-18 15:17:53 -05:00
|
|
|
if (polls) {
|
|
|
|
this._polls = this._polls || {};
|
2018-11-19 08:50:00 -05:00
|
|
|
polls.forEach(p => {
|
|
|
|
const existing = this._polls[p.name];
|
2016-02-18 15:17:53 -05:00
|
|
|
if (existing) {
|
2018-11-19 08:50:00 -05:00
|
|
|
this._polls[p.name].setProperties(p);
|
2016-02-18 15:17:53 -05:00
|
|
|
} else {
|
2019-01-11 11:54:23 -05:00
|
|
|
this._polls[p.name] = Ember.Object.create(p);
|
2016-02-18 15:17:53 -05:00
|
|
|
}
|
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);
|
2019-10-24 07:00:25 -04:00
|
|
|
if (!$polls.length || !helper) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-04-27 12:59:29 -04:00
|
|
|
|
2019-10-24 07:00:25 -04:00
|
|
|
let post = helper.getModel();
|
2016-03-28 12:17:37 -04:00
|
|
|
api.preventCloak(post.id);
|
2016-02-18 15:17:53 -05:00
|
|
|
post.pollsChanged();
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2019-10-24 07:00:25 -04:00
|
|
|
const polls = post.pollsObject || {};
|
|
|
|
const votes = post.polls_votes || {};
|
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");
|
2019-10-24 07:00:25 -04:00
|
|
|
let poll = polls[pollName];
|
|
|
|
let vote = votes[pollName] || [];
|
|
|
|
|
|
|
|
const quotedId = $poll.parent(".expanded-quote").data("post-id");
|
|
|
|
if (quotedId) {
|
|
|
|
const quotedPost = post.quoted[quotedId];
|
|
|
|
if (quotedPost) {
|
|
|
|
post = Ember.Object.create(quotedPost);
|
|
|
|
poll = Ember.Object.create(
|
|
|
|
quotedPost.polls.find(p => p.name === pollName)
|
|
|
|
);
|
|
|
|
vote = quotedPost.polls_votes || {};
|
|
|
|
vote = vote[pollName] || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:48:47 -05:00
|
|
|
if (poll) {
|
2019-11-25 12:51:01 -05:00
|
|
|
const attrs = {
|
2016-12-07 15:48:47 -05:00
|
|
|
id: `${pollName}-${post.id}`,
|
|
|
|
post,
|
|
|
|
poll,
|
2019-11-25 12:51:01 -05:00
|
|
|
vote,
|
|
|
|
groupableUserFields: (
|
|
|
|
api.container.lookup("site-settings:main")
|
|
|
|
.poll_groupable_user_fields || ""
|
|
|
|
)
|
|
|
|
.split("|")
|
|
|
|
.filter(Boolean)
|
|
|
|
};
|
|
|
|
const glue = new WidgetGlue("discourse-poll", register, attrs);
|
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");
|
2019-06-11 11:21:23 -04:00
|
|
|
api.decorateCooked(attachPolls, { onlyStream: true, id: "discourse-poll" });
|
2016-12-07 15:48:47 -05:00
|
|
|
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
|
|
|
};
|