2020-03-07 06:49:28 +08:00
|
|
|
import EmberObject from "@ember/object";
|
2018-06-16 00:42:20 +08:00
|
|
|
import WidgetGlue from "discourse/widgets/glue";
|
2020-08-06 23:57:06 +08:00
|
|
|
import { getRegister } from "discourse-common/lib/get-owner";
|
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2018-06-16 00:42:20 +08:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2021-09-02 02:29:07 +08:00
|
|
|
const PLUGIN_ID = "discourse-poll";
|
|
|
|
let _glued = [];
|
|
|
|
let _interval = null;
|
|
|
|
|
|
|
|
function rerender() {
|
|
|
|
_glued.forEach((g) => g.queueRerender());
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanUpPolls() {
|
|
|
|
if (_interval) {
|
|
|
|
clearInterval(_interval);
|
|
|
|
_interval = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_glued.forEach((g) => g.cleanUp());
|
|
|
|
_glued = [];
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
function initializePolls(api) {
|
2021-12-16 02:13:06 +08:00
|
|
|
const register = getRegister(api),
|
2022-08-01 16:43:33 +08:00
|
|
|
pollGroupableUserFields = api.container.lookup(
|
|
|
|
"service:site-settings"
|
|
|
|
).poll_groupable_user_fields;
|
2021-09-02 02:29:07 +08:00
|
|
|
cleanUpPolls();
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
api.modifyClass("controller:topic", {
|
2021-09-02 02:29:07 +08:00
|
|
|
pluginId: PLUGIN_ID,
|
2022-09-12 20:05:21 +08:00
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
subscribe() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2022-09-12 20:05:21 +08:00
|
|
|
this.messageBus.subscribe(`/polls/${this.model.id}`, (msg) => {
|
2018-06-16 00:42:20 +08:00
|
|
|
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
|
2022-09-12 20:05:21 +08:00
|
|
|
post?.set("polls", msg.polls);
|
2016-02-19 04:17:53 +08:00
|
|
|
});
|
|
|
|
},
|
2022-09-12 20:05:21 +08:00
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
unsubscribe() {
|
|
|
|
this.messageBus.unsubscribe("/polls/*");
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2016-02-19 04:17:53 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
api.modifyClass("model:post", {
|
2021-09-02 02:29:07 +08:00
|
|
|
pluginId: PLUGIN_ID,
|
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() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const polls = this.polls;
|
2016-02-19 04:17:53 +08:00
|
|
|
if (polls) {
|
|
|
|
this._polls = this._polls || {};
|
2018-11-19 21:50:00 +08:00
|
|
|
polls.forEach((p) => {
|
|
|
|
const existing = this._polls[p.name];
|
2016-02-19 04:17:53 +08:00
|
|
|
if (existing) {
|
2018-11-19 21:50:00 +08:00
|
|
|
this._polls[p.name].setProperties(p);
|
2016-02-19 04:17:53 +08:00
|
|
|
} else {
|
2020-03-07 06:49:28 +08:00
|
|
|
this._polls[p.name] = EmberObject.create(p);
|
2016-02-19 04:17:53 +08:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
2021-12-14 23:52:18 +08:00
|
|
|
function attachPolls(elem, helper) {
|
2022-04-15 19:03:07 +08:00
|
|
|
let pollNodes = [...elem.querySelectorAll(".poll")];
|
|
|
|
pollNodes = pollNodes.filter(
|
|
|
|
(node) => node.parentNode.tagName !== "BLOCKQUOTE"
|
|
|
|
);
|
2021-12-14 23:52:18 +08:00
|
|
|
if (!pollNodes.length || !helper) {
|
2018-06-16 00:42:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2020-08-19 23:13:56 +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
|
|
|
post.pollsChanged();
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2019-10-24 19:00:25 +08:00
|
|
|
const polls = post.pollsObject || {};
|
|
|
|
const votes = post.polls_votes || {};
|
2016-01-06 17:35:27 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
_interval = _interval || setInterval(rerender, 30000);
|
|
|
|
|
2021-12-14 23:52:18 +08:00
|
|
|
pollNodes.forEach((pollNode) => {
|
|
|
|
const pollName = pollNode.dataset.pollName;
|
2019-10-24 19:00:25 +08:00
|
|
|
let poll = polls[pollName];
|
2020-08-19 23:13:56 +08:00
|
|
|
let pollPost = post;
|
2019-10-24 19:00:25 +08:00
|
|
|
let vote = votes[pollName] || [];
|
|
|
|
|
2021-12-14 23:52:18 +08:00
|
|
|
const quotedId = pollNode.closest(".expanded-quote")?.dataset.postId;
|
2020-08-19 23:13:56 +08:00
|
|
|
if (quotedId && post.quoted[quotedId]) {
|
|
|
|
pollPost = post.quoted[quotedId];
|
|
|
|
pollPost = EmberObject.create(pollPost);
|
2021-12-14 23:52:18 +08:00
|
|
|
poll = EmberObject.create(pollPost.polls.findBy("name", pollName));
|
2020-08-19 23:13:56 +08:00
|
|
|
vote = pollPost.polls_votes || {};
|
|
|
|
vote = vote[pollName] || [];
|
2019-10-24 19:00:25 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
if (poll) {
|
2021-12-14 23:52:18 +08:00
|
|
|
const titleElement = pollNode.querySelector(".poll-title");
|
2020-10-02 15:21:24 +08:00
|
|
|
|
2019-11-26 01:51:01 +08:00
|
|
|
const attrs = {
|
2020-08-19 23:13:56 +08:00
|
|
|
id: `${pollName}-${pollPost.id}`,
|
|
|
|
post: pollPost,
|
2016-12-08 04:48:47 +08:00
|
|
|
poll,
|
2019-11-26 01:51:01 +08:00
|
|
|
vote,
|
2021-10-05 16:38:49 +08:00
|
|
|
hasSavedVote: vote.length > 0,
|
2021-12-14 23:52:18 +08:00
|
|
|
titleHTML: titleElement?.outerHTML,
|
2021-12-16 02:13:06 +08:00
|
|
|
groupableUserFields: (pollGroupableUserFields || "")
|
2019-11-26 01:51:01 +08:00
|
|
|
.split("|")
|
|
|
|
.filter(Boolean),
|
|
|
|
};
|
|
|
|
const glue = new WidgetGlue("discourse-poll", register, attrs);
|
2021-12-14 23:52:18 +08:00
|
|
|
glue.appendTo(pollNode);
|
2016-12-08 04:48:47 +08:00
|
|
|
_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-06-21 17:41:53 +08:00
|
|
|
api.includePostAttributes("polls", "polls_votes");
|
2021-12-14 23:52:18 +08:00
|
|
|
api.decorateCookedElement(attachPolls, {
|
|
|
|
onlyStream: true,
|
|
|
|
id: "discourse-poll",
|
|
|
|
});
|
2016-12-08 04:48:47 +08:00
|
|
|
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() {
|
2018-06-16 00:42:20 +08:00
|
|
|
withPluginApi("0.8.7", initializePolls);
|
2015-04-24 01:33:29 +08:00
|
|
|
},
|
2015-05-01 00:25:38 +08:00
|
|
|
};
|