2014-02-05 10:53:09 +08:00
|
|
|
var Poll = Discourse.Model.extend({
|
|
|
|
post: null,
|
|
|
|
options: [],
|
|
|
|
|
|
|
|
postObserver: function() {
|
|
|
|
this.updateOptionsFromJson(this.get('post.poll_details'));
|
|
|
|
}.observes('post.poll_details'),
|
|
|
|
|
|
|
|
updateOptionsFromJson: function(json) {
|
|
|
|
var selectedOption = json["selected"];
|
|
|
|
|
|
|
|
var options = [];
|
|
|
|
Object.keys(json["options"]).forEach(function(option) {
|
|
|
|
options.push(Ember.Object.create({
|
|
|
|
option: option,
|
|
|
|
votes: json["options"][option],
|
2014-03-08 14:35:12 +08:00
|
|
|
checked: (option === selectedOption)
|
2014-02-05 10:53:09 +08:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
this.set('options', options);
|
|
|
|
},
|
|
|
|
|
|
|
|
saveVote: function(option) {
|
|
|
|
this.get('options').forEach(function(opt) {
|
2014-03-08 14:35:12 +08:00
|
|
|
opt.set('checked', opt.get('option') === option);
|
2014-02-05 10:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return Discourse.ajax("/poll", {
|
|
|
|
type: "PUT",
|
|
|
|
data: {post_id: this.get('post.id'), option: option}
|
|
|
|
}).then(function(newJSON) {
|
|
|
|
this.updateOptionsFromJson(newJSON);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var PollController = Discourse.Controller.extend({
|
|
|
|
poll: null,
|
2014-04-10 15:32:49 +08:00
|
|
|
showResults: Em.computed.oneWay('poll.post.poll_details.closed'),
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2014-04-08 04:54:22 +08:00
|
|
|
disableRadio: Em.computed.any('poll.post.poll_details.closed', 'loading'),
|
2014-03-16 21:48:40 +08:00
|
|
|
|
2014-02-05 10:53:09 +08:00
|
|
|
actions: {
|
|
|
|
selectOption: function(option) {
|
2014-03-16 21:48:40 +08:00
|
|
|
if (this.get('disableRadio')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-05 10:53:09 +08:00
|
|
|
if (!this.get('currentUser.id')) {
|
|
|
|
this.get('postController').send('showLogin');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('loading', true);
|
|
|
|
this.get('poll').saveVote(option).then(function() {
|
|
|
|
this.set('loading', false);
|
|
|
|
this.set('showResults', true);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleShowResults: function() {
|
|
|
|
this.set('showResults', !this.get('showResults'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var PollView = Ember.View.extend({
|
|
|
|
templateName: "poll",
|
|
|
|
classNames: ['poll-ui'],
|
|
|
|
|
|
|
|
replaceElement: function(target) {
|
|
|
|
this._insertElementLater(function() {
|
|
|
|
target.replaceWith(this.$());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function initializePollView(self) {
|
|
|
|
var post = self.get('post');
|
|
|
|
var pollDetails = post.get('poll_details');
|
|
|
|
|
|
|
|
var poll = Poll.create({post: post});
|
|
|
|
poll.updateOptionsFromJson(pollDetails);
|
|
|
|
|
|
|
|
var pollController = PollController.create({
|
|
|
|
poll: poll,
|
|
|
|
showResults: pollDetails["selected"],
|
|
|
|
postController: self.get('controller')
|
|
|
|
});
|
|
|
|
|
|
|
|
var pollView = self.createChildView(PollView, {
|
|
|
|
controller: pollController
|
|
|
|
});
|
|
|
|
return pollView;
|
|
|
|
}
|
|
|
|
|
|
|
|
Discourse.PostView.reopen({
|
|
|
|
createPollUI: function($post) {
|
|
|
|
var post = this.get('post');
|
|
|
|
|
|
|
|
if (!post.get('poll_details')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var view = initializePollView(this);
|
2014-02-20 16:16:27 +08:00
|
|
|
|
|
|
|
var pollContainer = $post.find(".poll-ui:first");
|
2014-03-08 14:35:12 +08:00
|
|
|
if (pollContainer.length === 0) {
|
2014-02-20 16:16:27 +08:00
|
|
|
pollContainer = $post.find("ul:first");
|
|
|
|
}
|
|
|
|
|
|
|
|
view.replaceElement(pollContainer);
|
2014-02-05 10:53:09 +08:00
|
|
|
this.set('pollView', view);
|
|
|
|
|
|
|
|
}.on('postViewInserted'),
|
|
|
|
|
|
|
|
clearPollView: function() {
|
|
|
|
if (this.get('pollView')) {
|
|
|
|
this.get('pollView').destroy();
|
|
|
|
}
|
|
|
|
}.on('willClearRender')
|
|
|
|
});
|