Migrate poll plugin to use components

This commit is contained in:
Robin Ward 2016-11-21 14:49:00 -05:00
parent 7d37c2b919
commit 28061316ad
5 changed files with 29 additions and 57 deletions

View File

@ -1,7 +1,16 @@
import { ajax } from 'discourse/lib/ajax';
import { default as computed, observes } from "ember-addons/ember-computed-decorators";
import { ajax } from 'discourse/lib/ajax';
export default Ember.Component.extend({
layoutName: 'components/discourse-poll',
classNames: ["poll"],
attributeBindings: ["data-poll-type", "data-poll-name", "data-poll-status", "data-poll-public"],
"data-poll-type": Ember.computed.alias("poll.type"),
"data-poll-name": Ember.computed.alias("poll.name"),
"data-poll-status": Ember.computed.alias("poll.status"),
"data-poll-public": Ember.computed.alias("poll.public"),
export default Ember.Controller.extend({
isMultiple: Ember.computed.equal("poll.type", "multiple"),
isNumber: Ember.computed.equal("poll.type", "number"),
isClosed: Ember.computed.equal("poll.status", "closed"),
@ -11,10 +20,10 @@ export default Ember.Controller.extend({
// - poll is closed
// - topic is archived
// - user wants to see the results
showingResults: Em.computed.or("isClosed", "post.topic.archived", "showResults"),
showingResults: Ember.computed.or("isClosed", "post.topic.archived", "showResults"),
showResultsDisabled: Em.computed.equal("poll.voters", 0),
hideResultsDisabled: Em.computed.or("isClosed", "post.topic.archived"),
showResultsDisabled: Ember.computed.equal("poll.voters", 0),
hideResultsDisabled: Ember.computed.or("isClosed", "post.topic.archived"),
@observes("post.polls")
_updatePoll() {

View File

@ -1,20 +1,10 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
import { observes } from "ember-addons/ember-computed-decorators";
function createPollView(container, post, poll, vote, publicPoll) {
const controller = container.lookup("controller:poll", { singleton: false });
const view = container.lookup("view:poll");
controller.setProperties({
model: poll,
vote: vote,
public: publicPoll,
post
});
view.set("controller", controller);
return view;
function createPollComponent(container, post, poll, vote) {
const component = container.lookup("component:discourse-poll");
component.setProperties({ model: poll, vote, post });
return component;
}
let _pollViews;
@ -89,20 +79,18 @@ function initializePolls(api) {
const $poll = $(pollElem);
const pollName = $poll.data("poll-name");
const publicPoll = $poll.data("poll-public");
const pollId = `${pollName}-${post.id}`;
const pollView = createPollView(
const pollComponent = createPollComponent(
helper.container,
post,
polls[pollName],
votes[pollName],
publicPoll
votes[pollName]
);
$poll.replaceWith($div);
Em.run.schedule('afterRender', () => pollView.renderer.replaceIn(pollView, $div[0]));
postPollViews[pollId] = pollView;
Em.run.schedule('afterRender', () => pollComponent.renderer.replaceIn(pollComponent, $div[0]));
postPollViews[pollId] = pollComponent;
});
_pollViews = postPollViews;

View File

@ -1,12 +0,0 @@
export default Em.View.extend({
templateName: "poll",
classNames: ["poll"],
attributeBindings: ["data-poll-type", "data-poll-name", "data-poll-status", "data-poll-public"],
poll: Em.computed.alias("controller.poll"),
"data-poll-type": Em.computed.alias("poll.type"),
"data-poll-name": Em.computed.alias("poll.name"),
"data-poll-status": Em.computed.alias("poll.status"),
"data-poll-public": Em.computed.alias("poll.public")
});

View File

@ -3,6 +3,9 @@ import { mapRoutes } from 'discourse/mapping-router';
moduleFor("controller:poll-ui-builder", "controller:poll-ui-builder", {
setup() {
this.registry.register('router:main', mapRoutes());
this.subject().set('toolbarEvent', {
getText: () => ""
});
},
needs: ['controller:modal']
});
@ -170,21 +173,13 @@ test("number pollOutput", function() {
equal(controller.get("pollOutput"), "[poll type=number min=1 max=20 step=1]\n[/poll]", "it should return the right output");
controller.set("pollName", 'test');
equal(controller.get("pollOutput"), "[poll name=test type=number min=1 max=20 step=1]\n[/poll]", "it should return the right output");
controller.set("pollName", ' test poll more ');
equal(controller.get("pollOutput"), "[poll name=test-poll-more type=number min=1 max=20 step=1]\n[/poll]", "it should return the right output");
controller.set("pollStep", 2);
equal(controller.get("pollOutput"), "[poll name=test-poll-more type=number min=1 max=20 step=2]\n[/poll]", "it should return the right output");
equal(controller.get("pollOutput"), "[poll type=number min=1 max=20 step=2]\n[/poll]", "it should return the right output");
controller.set("publicPoll", true);
equal(controller.get("pollOutput"), "[poll name=test-poll-more type=number min=1 max=20 step=2 public=true]\n[/poll]", "it should return the right output");
equal(controller.get("pollOutput"), "[poll type=number min=1 max=20 step=2 public=true]\n[/poll]", "it should return the right output");
});
test("regular pollOutput", function() {
@ -196,13 +191,9 @@ test("regular pollOutput", function() {
equal(controller.get("pollOutput"), "[poll]\n* 1\n* 2\n[/poll]", "it should return the right output");
controller.set("pollName", "test");
equal(controller.get("pollOutput"), "[poll name=test]\n* 1\n* 2\n[/poll]", "it should return the right output");
controller.set("publicPoll", "true");
equal(controller.get("pollOutput"), "[poll name=test public=true]\n* 1\n* 2\n[/poll]", "it should return the right output");
equal(controller.get("pollOutput"), "[poll public=true]\n* 1\n* 2\n[/poll]", "it should return the right output");
});
@ -220,11 +211,7 @@ test("multiple pollOutput", function() {
equal(controller.get("pollOutput"), "[poll type=multiple min=1 max=2]\n* 1\n* 2\n[/poll]", "it should return the right output");
controller.set("pollName", "test");
equal(controller.get("pollOutput"), "[poll name=test type=multiple min=1 max=2]\n* 1\n* 2\n[/poll]", "it should return the right output");
controller.set("publicPoll", "true");
equal(controller.get("pollOutput"), "[poll name=test type=multiple min=1 max=2 public=true]\n* 1\n* 2\n[/poll]", "it should return the right output");
equal(controller.get("pollOutput"), "[poll type=multiple min=1 max=2 public=true]\n* 1\n* 2\n[/poll]", "it should return the right output");
});