2015-03-03 10:17:07 -05:00
|
|
|
import Poll from "discourse/plugins/poll/models/poll";
|
|
|
|
import PollView from "discourse/plugins/poll/views/poll";
|
2014-05-18 04:06:27 -04:00
|
|
|
import PollController from "discourse/plugins/poll/controllers/poll";
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-03-03 10:17:07 -05:00
|
|
|
import PostView from "discourse/views/post";
|
2014-02-04 21:53:09 -05:00
|
|
|
|
|
|
|
function initializePollView(self) {
|
2015-03-03 10:17:07 -05:00
|
|
|
const post = self.get('post'),
|
|
|
|
pollDetails = post.get('poll_details');
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-03-03 10:17:07 -05:00
|
|
|
let poll = Poll.create({ post: post });
|
2014-04-10 22:43:02 -04:00
|
|
|
poll.updateFromJson(pollDetails);
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-03-03 10:17:07 -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
|
|
|
}
|
|
|
|
|
2014-05-18 04:06:27 -04:00
|
|
|
export default {
|
|
|
|
name: 'poll',
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2014-05-18 04:06:27 -04:00
|
|
|
initialize: function() {
|
2015-01-14 12:20:37 -05:00
|
|
|
PostView.reopen({
|
2014-05-18 04:06:27 -04:00
|
|
|
createPollUI: function($post) {
|
2015-03-03 10:17:07 -05:00
|
|
|
if (!this.get('post').get('poll_details')) {
|
2014-05-18 04:06:27 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-20 03:16:27 -05:00
|
|
|
|
2015-03-03 10:17:07 -05:00
|
|
|
let view = initializePollView(this),
|
|
|
|
pollContainer = $post.find(".poll-ui:first");
|
|
|
|
|
2014-05-18 04:06:27 -04:00
|
|
|
if (pollContainer.length === 0) {
|
|
|
|
pollContainer = $post.find("ul:first");
|
|
|
|
}
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-03-03 10:17:07 -05:00
|
|
|
let $div = $('<div>');
|
2014-12-04 08:48:25 -05:00
|
|
|
pollContainer.replaceWith($div);
|
|
|
|
view.constructor.renderer.appendTo(view, $div[0]);
|
2014-05-18 04:06:27 -04:00
|
|
|
this.set('pollView', view);
|
|
|
|
}.on('postViewInserted'),
|
|
|
|
|
|
|
|
clearPollView: function() {
|
2015-03-03 10:17:07 -05:00
|
|
|
if (this.get('pollView')) { this.get('pollView').destroy(); }
|
2014-05-18 04:06:27 -04:00
|
|
|
}.on('willClearRender')
|
|
|
|
});
|
|
|
|
}
|
2014-12-04 08:48:25 -05:00
|
|
|
};
|