2018-06-16 00:42:20 +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";
|
2018-06-16 00:42:20 +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
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
api.modifyClass("controller:topic", {
|
|
|
|
subscribe() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2016-02-19 04:17:53 +08:00
|
|
|
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);
|
2016-02-19 04:17:53 +08:00
|
|
|
if (post) {
|
2018-06-16 00:42:20 +08:00
|
|
|
post.set("polls", msg.polls);
|
2016-02-19 04:17:53 +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-05-03 08:12:19 +08:00
|
|
|
let _glued = [];
|
|
|
|
let _interval = null;
|
|
|
|
|
|
|
|
function rerender() {
|
|
|
|
_glued.forEach(g => g.queueRerender());
|
|
|
|
}
|
|
|
|
|
2018-06-16 00:42:20 +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() {
|
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 {
|
2019-01-12 00:54:23 +08:00
|
|
|
this._polls[p.name] = Ember.Object.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
|
|
|
});
|
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
function attachPolls($elem, helper) {
|
2018-06-16 00:42:20 +08:00
|
|
|
const $polls = $(".poll", $elem);
|
|
|
|
if (!$polls.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-28 00:59:29 +08:00
|
|
|
|
2018-06-18 23:34:32 +08:00
|
|
|
if (!helper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:17:53 +08:00
|
|
|
const post = helper.getModel();
|
2016-03-29 00:17:37 +08:00
|
|
|
api.preventCloak(post.id);
|
2018-06-16 00:42:20 +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");
|
2018-06-16 00:42:20 +08:00
|
|
|
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) {
|
2018-06-16 00:42:20 +08:00
|
|
|
const glue = new WidgetGlue("discourse-poll", register, {
|
2016-12-08 04:48:47 +08:00
|
|
|
id: `${pollName}-${post.id}`,
|
|
|
|
post,
|
|
|
|
poll,
|
2018-11-19 21:50:00 +08:00
|
|
|
vote: votes[pollName] || []
|
2016-12-08 04:48:47 +08:00
|
|
|
});
|
|
|
|
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");
|
2019-06-11 23:21:23 +08:00
|
|
|
api.decorateCooked(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
|
|
|
};
|