2014-02-04 21:53:09 -05:00
|
|
|
var Poll = Discourse.Model.extend({
|
|
|
|
post: null,
|
|
|
|
options: [],
|
2014-04-10 22:43:02 -04:00
|
|
|
closed: false,
|
2014-02-04 21:53:09 -05:00
|
|
|
|
|
|
|
postObserver: function() {
|
2014-04-10 22:43:02 -04:00
|
|
|
this.updateFromJson(this.get('post.poll_details'));
|
2014-02-04 21:53:09 -05:00
|
|
|
}.observes('post.poll_details'),
|
|
|
|
|
2014-04-10 23:35:07 -04:00
|
|
|
fetchNewPostDetails: function() {
|
|
|
|
this.get('post.topic.postStream').triggerChangedPost(this.get('post.id'), this.get('post.topic.updated_at'));
|
|
|
|
}.observes('post.topic.title'),
|
|
|
|
|
2014-04-10 22:43:02 -04:00
|
|
|
updateFromJson: function(json) {
|
2014-02-04 21:53:09 -05:00
|
|
|
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 01:35:12 -05:00
|
|
|
checked: (option === selectedOption)
|
2014-02-04 21:53:09 -05:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
this.set('options', options);
|
2014-04-10 22:43:02 -04:00
|
|
|
|
|
|
|
this.set('closed', json.closed);
|
2014-02-04 21:53:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
saveVote: function(option) {
|
|
|
|
this.get('options').forEach(function(opt) {
|
2014-03-08 01:35:12 -05:00
|
|
|
opt.set('checked', opt.get('option') === option);
|
2014-02-04 21:53:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return Discourse.ajax("/poll", {
|
|
|
|
type: "PUT",
|
|
|
|
data: {post_id: this.get('post.id'), option: option}
|
|
|
|
}).then(function(newJSON) {
|
2014-04-10 22:43:02 -04:00
|
|
|
this.updateFromJson(newJSON);
|
2014-02-04 21:53:09 -05:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var PollController = Discourse.Controller.extend({
|
|
|
|
poll: null,
|
2014-04-10 22:43:02 -04:00
|
|
|
showResults: Em.computed.oneWay('poll.closed'),
|
|
|
|
disableRadio: Em.computed.any('poll.closed', 'loading'),
|
2014-04-12 13:30:51 -04:00
|
|
|
showToggleClosePoll: function() {
|
|
|
|
return this.get('poll.post.topic.details.can_edit') && !Discourse.SiteSettings.allow_user_locale;
|
|
|
|
}.property('poll.post.topic.details.can_edit'),
|
2014-03-16 09:48:40 -04:00
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
actions: {
|
|
|
|
selectOption: function(option) {
|
2014-03-16 09:48:40 -04:00
|
|
|
if (this.get('disableRadio')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-04 21:53:09 -05: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'));
|
2014-04-12 13:23:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleClosePoll: function() {
|
|
|
|
this.set('loading', true);
|
|
|
|
return Discourse.ajax("/poll/toggle_close", {
|
|
|
|
type: "PUT",
|
|
|
|
data: {post_id: this.get('poll.post.id')}
|
|
|
|
}).then(function(topicJson) {
|
|
|
|
this.set('poll.post.topic.title', topicJson.basic_topic.title);
|
|
|
|
this.set('poll.post.topic.fancy_title', topicJson.basic_topic.title);
|
|
|
|
this.set('loading', false);
|
|
|
|
}.bind(this));
|
2014-02-04 21:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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});
|
2014-04-10 22:43:02 -04:00
|
|
|
poll.updateFromJson(pollDetails);
|
2014-02-04 21:53:09 -05:00
|
|
|
|
|
|
|
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 03:16:27 -05:00
|
|
|
|
|
|
|
var pollContainer = $post.find(".poll-ui:first");
|
2014-03-08 01:35:12 -05:00
|
|
|
if (pollContainer.length === 0) {
|
2014-02-20 03:16:27 -05:00
|
|
|
pollContainer = $post.find("ul:first");
|
|
|
|
}
|
|
|
|
|
|
|
|
view.replaceElement(pollContainer);
|
2014-02-04 21:53:09 -05:00
|
|
|
this.set('pollView', view);
|
|
|
|
|
|
|
|
}.on('postViewInserted'),
|
|
|
|
|
|
|
|
clearPollView: function() {
|
|
|
|
if (this.get('pollView')) {
|
|
|
|
this.get('pollView').destroy();
|
|
|
|
}
|
|
|
|
}.on('willClearRender')
|
|
|
|
});
|