Add poll plugin.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<table>
|
||||
{{#each poll.options}}
|
||||
<tr {{bind-attr class=checked:active}} {{action selectOption option}}>
|
||||
<td class="radio"><input type="radio" name="poll" {{bind-attr checked=checked disabled=controller.loading}}></td>
|
||||
<td class="option">
|
||||
<div class="option">
|
||||
{{option}}
|
||||
</div>
|
||||
{{#if controller.showResults}}
|
||||
<div class="result">{{i18n poll.voteCount count=votes}}</div>
|
||||
{{/if}}
|
||||
</td>
|
||||
</tr>
|
||||
{{/each}}
|
||||
</table>
|
||||
|
||||
<button {{action toggleShowResults}}>
|
||||
{{#if showResults}}
|
||||
{{i18n poll.results.hide}}
|
||||
{{else}}
|
||||
{{i18n poll.results.show}}
|
||||
{{/if}}
|
||||
</button>
|
||||
|
||||
{{#if loading}}
|
||||
<i class="fa fa-spin fa-spinner"></i>
|
||||
{{/if}}
|
||||
@@ -0,0 +1,9 @@
|
||||
Discourse.Dialect.inlineBetween({
|
||||
start: '[poll]',
|
||||
stop: '[/poll]',
|
||||
rawContents: true,
|
||||
emitter: function(contents) {
|
||||
var list = Discourse.Dialect.cook(contents, {});
|
||||
return ['div', {class: 'poll-ui'}, list];
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
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],
|
||||
checked: (option == selectedOption)
|
||||
}));
|
||||
});
|
||||
this.set('options', options);
|
||||
},
|
||||
|
||||
saveVote: function(option) {
|
||||
this.get('options').forEach(function(opt) {
|
||||
opt.set('checked', opt.get('option') == option);
|
||||
});
|
||||
|
||||
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,
|
||||
showResults: false,
|
||||
|
||||
actions: {
|
||||
selectOption: function(option) {
|
||||
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);
|
||||
view.replaceElement($post.find(".poll-ui:first"));
|
||||
this.set('pollView', view);
|
||||
|
||||
}.on('postViewInserted'),
|
||||
|
||||
clearPollView: function() {
|
||||
if (this.get('pollView')) {
|
||||
this.get('pollView').destroy();
|
||||
}
|
||||
}.on('willClearRender')
|
||||
});
|
||||
Reference in New Issue
Block a user