From 773e198cb3d18150af59bfd590c9d44c975bfb21 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 4 Jul 2023 15:25:34 +0100 Subject: [PATCH] DEV: Convert poll modals to new component-based API (#22164) --- .../components/modal/poll-breakdown.hbs | 76 ++++++ .../modal}/poll-breakdown.js | 29 +- .../components/modal/poll-ui-builder.hbs | 251 ++++++++++++++++++ .../modal}/poll-ui-builder.js | 49 +--- .../components/poll-breakdown-chart.js | 2 +- .../initializers/add-poll-ui-builder.js | 9 +- .../templates/modal/poll-breakdown.hbs | 55 ---- .../templates/modal/poll-ui-builder.hbs | 244 ----------------- .../discourse/widgets/discourse-poll.js | 11 +- .../component/poll-ui-builder-test.js | 214 +++++++++++++++ .../unit/controllers/poll-ui-builder-test.js | 205 -------------- 11 files changed, 579 insertions(+), 566 deletions(-) create mode 100644 plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.hbs rename plugins/poll/assets/javascripts/discourse/{controllers => components/modal}/poll-breakdown.js (88%) create mode 100644 plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs rename plugins/poll/assets/javascripts/discourse/{controllers => components/modal}/poll-ui-builder.js (90%) delete mode 100644 plugins/poll/assets/javascripts/discourse/templates/modal/poll-breakdown.hbs delete mode 100644 plugins/poll/assets/javascripts/discourse/templates/modal/poll-ui-builder.hbs create mode 100644 plugins/poll/test/javascripts/component/poll-ui-builder-test.js delete mode 100644 plugins/poll/test/javascripts/unit/controllers/poll-ui-builder-test.js diff --git a/plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.hbs b/plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.hbs new file mode 100644 index 00000000000..218405dd995 --- /dev/null +++ b/plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.hbs @@ -0,0 +1,76 @@ +{{! template-lint-disable no-invalid-interactive }} + + <:headerBelowTitle> + + + <:body> +
+

+ {{this.title}} +

+ +
{{i18n + "poll.breakdown.votes" + count=this.model.poll.voters + }}
+ +
    + {{#each this.model.poll.options as |option index|}} + + {{/each}} +
+
+ +
+
+ + + +
+ +
+ {{#each this.charts as |chart|}} + + {{/each}} +
+
+ +
\ No newline at end of file diff --git a/plugins/poll/assets/javascripts/discourse/controllers/poll-breakdown.js b/plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.js similarity index 88% rename from plugins/poll/assets/javascripts/discourse/controllers/poll-breakdown.js rename to plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.js index ba2e2514e8c..0cd13fa67fa 100644 --- a/plugins/poll/assets/javascripts/discourse/controllers/poll-breakdown.js +++ b/plugins/poll/assets/javascripts/discourse/components/modal/poll-breakdown.js @@ -1,7 +1,6 @@ import { inject as service } from "@ember/service"; -import Controller from "@ember/controller"; +import Component from "@ember/component"; import I18n from "I18n"; -import ModalFunctionality from "discourse/mixins/modal-functionality"; import { action } from "@ember/object"; import { ajax } from "discourse/lib/ajax"; import { classify } from "@ember/string"; @@ -10,9 +9,7 @@ import { htmlSafe } from "@ember/template"; import loadScript from "discourse/lib/load-script"; import { popupAjaxError } from "discourse/lib/ajax-error"; -export default class PollBreakdownController extends Controller.extend( - ModalFunctionality -) { +export default class PollBreakdownModal extends Component { @service dialog; model = null; @@ -21,6 +18,16 @@ export default class PollBreakdownController extends Controller.extend( highlightedOption = null; displayMode = "percentage"; + init() { + this.set("groupedBy", this.model.groupableUserFields[0]); + loadScript("/javascripts/Chart.min.js") + .then(() => loadScript("/javascripts/chartjs-plugin-datalabels.min.js")) + .then(() => { + this.fetchGroupedPollData(); + }); + super.init(...arguments); + } + @discourseComputed("model.poll.title", "model.post.topic.title") title(pollTitle, topicTitle) { return pollTitle ? htmlSafe(pollTitle) : topicTitle; @@ -44,18 +51,6 @@ export default class PollBreakdownController extends Controller.extend( return options.reduce((sum, option) => sum + option.votes, 0); } - onShow() { - this.set("charts", null); - this.set("displayMode", "percentage"); - this.set("groupedBy", this.model.groupableUserFields[0]); - - loadScript("/javascripts/Chart.min.js") - .then(() => loadScript("/javascripts/chartjs-plugin-datalabels.min.js")) - .then(() => { - this.fetchGroupedPollData(); - }); - } - fetchGroupedPollData() { return ajax("/polls/grouped_poll_results.json", { data: { diff --git a/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs new file mode 100644 index 00000000000..0955f70b3ff --- /dev/null +++ b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.hbs @@ -0,0 +1,251 @@ + + <:body> + + + {{#if this.showAdvanced}} +
+ + +
+ {{/if}} + + {{#unless this.isNumber}} +
+ {{#if this.showAdvanced}} + +