- FEATURE: revamped poll plugin

- add User.staff scope
- inject MessageBus into Ember views (so it can be used by the poll plugin)
- REFACTOR: use more accurate is_first_post? method instead of post_number == 1
- FEATURE: add support for JSON-typed custom fields
- FEATURE: allow plugins to add validation
- FEATURE: add post_custom_fields to PostSerializer
- FEATURE: allow plugins to whitelist post_custom_fields
- FIX: don't bump when post did not save successfully
- FEATURE: polls are supported in any post
- FEATURE: allow for multiple polls in the same post
- FEATURE: multiple choice polls
- FEATURE: rating polls
- FEATURE: new dialect allowing users to preview polls in the composer
This commit is contained in:
Régis Hanol
2015-04-23 19:33:29 +02:00
parent 17dc8b8e4f
commit a737090442
89 changed files with 1334 additions and 1569 deletions

View File

@@ -0,0 +1,65 @@
import PostView from "discourse/views/post";
function createPollView(container, post, poll, vote) {
const controller = container.lookup("controller:poll", { singleton: false }),
view = container.lookup("view:poll");
controller.set("vote", vote);
controller.setProperties({
model: Em.Object.create(poll),
post: post,
});
view.set("controller", controller);
return view;
}
export default {
name: "extend-for-poll",
initialize(container) {
// overwrite polls
PostView.reopen({
_createPollViews: function($post) {
const self = this,
post = this.get("post"),
polls = post.get("polls"),
votes = post.get("polls_votes") || {};
// don't even bother when there's no poll
if (!polls) { return; }
const pollViews = {};
// iterate over all polls
$(".poll", $post).each(function() {
const $div = $("<div>"),
$poll = $(this),
pollName = $poll.data("poll-name"),
pollView = createPollView(container, post, polls[pollName], votes[pollName]);
$poll.replaceWith($div);
pollView.constructor.renderer.replaceIn(pollView, $div[0]);
pollViews[pollName] = pollView;
});
this.messageBus.subscribe("/polls/" + this.get("post.id"), results => {
pollViews[results.poll.name].get("controller").set("model", Em.Object.create(results.poll));
});
this.set("pollViews", pollViews);
}.on("postViewInserted"),
_cleanUpPollViews: function() {
this.messageBus.unsubscribe("/polls/*");
if (this.get("pollViews")) {
_.forEach(this.get("pollViews"), v => v.destroy());
}
}.on("willClearRender")
});
}
}

View File

@@ -1,51 +0,0 @@
import Poll from "discourse/plugins/poll/models/poll";
import PollView from "discourse/plugins/poll/views/poll";
import PollController from "discourse/plugins/poll/controllers/poll";
import PostView from "discourse/views/post";
function initializePollView(self) {
const post = self.get('post'),
pollDetails = post.get('poll_details');
let poll = Poll.create({ post: post });
poll.updateFromJson(pollDetails);
const pollController = PollController.create({
poll: poll,
showResults: pollDetails["selected"],
postController: self.get('controller')
});
return self.createChildView(PollView, { controller: pollController });
}
export default {
name: 'poll',
initialize: function() {
PostView.reopen({
createPollUI: function($post) {
if (!this.get('post').get('poll_details')) {
return;
}
let view = initializePollView(this),
pollContainer = $post.find(".poll-ui:first");
if (pollContainer.length === 0) {
pollContainer = $post.find("ul:first");
}
let $div = $('<div>');
pollContainer.replaceWith($div);
view.constructor.renderer.appendTo(view, $div[0]);
this.set('pollView', view);
}.on('postViewInserted'),
clearPollView: function() {
if (this.get('pollView')) { this.get('pollView').destroy(); }
}.on('willClearRender')
});
}
};