discourse/plugins/poll/assets/javascripts/initializers/poll.js.es6

52 lines
1.4 KiB
Plaintext
Raw Normal View History

import Poll from "discourse/plugins/poll/models/poll";
import PollView from "discourse/plugins/poll/views/poll";
import PollController from "discourse/plugins/poll/controllers/poll";
2014-02-04 21:53:09 -05:00
import PostView from "discourse/views/post";
2014-02-04 21:53:09 -05:00
function initializePollView(self) {
const post = self.get('post'),
pollDetails = post.get('poll_details');
2014-02-04 21:53:09 -05:00
let poll = Poll.create({ post: post });
poll.updateFromJson(pollDetails);
2014-02-04 21:53:09 -05:00
const pollController = PollController.create({
2014-02-04 21:53:09 -05:00
poll: poll,
showResults: pollDetails["selected"],
postController: self.get('controller')
});
2014-12-04 08:48:25 -05:00
return self.createChildView(PollView, { controller: pollController });
2014-02-04 21:53:09 -05:00
}
export default {
name: 'poll',
2014-02-04 21:53:09 -05:00
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");
}
2014-02-04 21:53:09 -05:00
let $div = $('<div>');
2014-12-04 08:48:25 -05:00
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')
});
}
2014-12-04 08:48:25 -05:00
};