discourse/plugins/poll/assets/javascripts/initializers/extend-for-poll.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
3.6 KiB
JavaScript
Raw Normal View History

import EmberObject from "@ember/object";
2018-06-16 00:42:20 +08:00
import WidgetGlue from "discourse/widgets/glue";
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";
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 = [];
}
function initializePolls(api) {
const register = getRegister(api);
cleanUpPolls();
2018-06-16 00:42:20 +08:00
api.modifyClass("controller:topic", {
pluginId: PLUGIN_ID,
2018-06-16 00:42:20 +08:00
subscribe() {
this._super(...arguments);
this.messageBus.subscribe("/polls/" + this.get("model.id"), (msg) => {
2018-06-16 00:42:20 +08:00
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
if (post) {
2018-06-16 00:42:20 +08:00
post.set("polls", msg.polls);
}
});
},
2018-06-16 00:42:20 +08:00
unsubscribe() {
this.messageBus.unsubscribe("/polls/*");
this._super(...arguments);
},
});
2018-06-16 00:42:20 +08:00
api.modifyClass("model:post", {
pluginId: PLUGIN_ID,
_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() {
const polls = this.polls;
if (polls) {
this._polls = this._polls || {};
polls.forEach((p) => {
const existing = this._polls[p.name];
if (existing) {
this._polls[p.name].setProperties(p);
} else {
this._polls[p.name] = EmberObject.create(p);
}
});
this.set("pollsObject", this._polls);
rerender();
}
2016-06-07 18:55:01 +08:00
},
});
function attachPolls($elem, helper) {
2018-06-16 00:42:20 +08:00
const $polls = $(".poll", $elem);
if (!$polls.length || !helper) {
2018-06-16 00:42:20 +08:00
return;
}
const post = helper.getModel();
api.preventCloak(post.id);
post.pollsChanged();
const polls = post.pollsObject || {};
const votes = post.polls_votes || {};
_interval = _interval || setInterval(rerender, 30000);
$polls.each((idx, pollElem) => {
const $poll = $(pollElem);
const pollName = $poll.data("poll-name");
let poll = polls[pollName];
let pollPost = post;
let vote = votes[pollName] || [];
const quotedId = $poll.parent(".expanded-quote").data("post-id");
if (quotedId && post.quoted[quotedId]) {
pollPost = post.quoted[quotedId];
pollPost = EmberObject.create(pollPost);
poll = EmberObject.create(
pollPost.polls.find((p) => p.name === pollName)
);
vote = pollPost.polls_votes || {};
vote = vote[pollName] || [];
}
if (poll) {
const titleElement = pollElem.querySelector(".poll-title");
const attrs = {
id: `${pollName}-${pollPost.id}`,
post: pollPost,
poll,
vote,
hasSavedVote: vote.length > 0,
titleHTML: titleElement && titleElement.outerHTML,
groupableUserFields: (
api.container.lookup("site-settings:main")
.poll_groupable_user_fields || ""
)
.split("|")
.filter(Boolean),
};
const glue = new WidgetGlue("discourse-poll", register, attrs);
glue.appendTo(pollElem);
_glued.push(glue);
}
});
}
api.includePostAttributes("polls", "polls_votes");
api.decorateCooked(attachPolls, { onlyStream: true, id: "discourse-poll" });
api.cleanupStream(cleanUpPolls);
}
export default {
name: "extend-for-poll",
initialize() {
2018-06-16 00:42:20 +08:00
withPluginApi("0.8.7", initializePolls);
},
2015-05-01 00:25:38 +08:00
};