From 4f10bf152d13f5bdb7b4cc3fa331ff477bd5dd65 Mon Sep 17 00:00:00 2001 From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:15:16 -0500 Subject: [PATCH] DEV: Add topic-vote click transformer for theme/plugin extensions (#207) --- .../pre-initializers/transformers.js | 12 ++++++ .../discourse/widgets/vote-button.js | 39 ++++++++++--------- 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 assets/javascripts/discourse/pre-initializers/transformers.js diff --git a/assets/javascripts/discourse/pre-initializers/transformers.js b/assets/javascripts/discourse/pre-initializers/transformers.js new file mode 100644 index 0000000..f372a9e --- /dev/null +++ b/assets/javascripts/discourse/pre-initializers/transformers.js @@ -0,0 +1,12 @@ +import { withPluginApi } from "discourse/lib/plugin-api"; + +export default { + name: "discourse-topic-voting-transformers", + before: "freeze-valid-transformers", + + initialize() { + withPluginApi("1.35.0", (api) => { + api.addBehaviorTransformerName("topic-vote-button-click"); + }); + }, +}; diff --git a/assets/javascripts/discourse/widgets/vote-button.js b/assets/javascripts/discourse/widgets/vote-button.js index e46cf9e..30f29b3 100644 --- a/assets/javascripts/discourse/widgets/vote-button.js +++ b/assets/javascripts/discourse/widgets/vote-button.js @@ -1,5 +1,6 @@ import { h } from "virtual-dom"; import cookie from "discourse/lib/cookie"; +import { applyBehaviorTransformer } from "discourse/lib/transformer"; import { createWidget } from "discourse/widgets/widget"; import I18n from "I18n"; @@ -71,24 +72,26 @@ export default createWidget("vote-button", { }, click() { - if (!this.currentUser) { - this.sendWidgetAction("showLogin"); - cookie("destination_url", window.location.href, { path: "/" }); - return; - } - if ( - !this.attrs.closed && - this.parentWidget.state.allowClick && - !this.attrs.user_voted && - !this.currentUser.votes_exceeded - ) { - this.parentWidget.state.allowClick = false; - this.parentWidget.state.initialVote = true; - this.sendWidgetAction("addVote"); - } - if (this.attrs.user_voted || this.currentUser.votes_exceeded) { - document.querySelector(".vote-options").classList.toggle("hidden"); - } + applyBehaviorTransformer("topic-vote-button-click", () => { + if (!this.currentUser) { + this.sendWidgetAction("showLogin"); + cookie("destination_url", window.location.href, { path: "/" }); + return; + } + if ( + !this.attrs.closed && + this.parentWidget.state.allowClick && + !this.attrs.user_voted && + !this.currentUser.votes_exceeded + ) { + this.parentWidget.state.allowClick = false; + this.parentWidget.state.initialVote = true; + this.sendWidgetAction("addVote"); + } + if (this.attrs.user_voted || this.currentUser.votes_exceeded) { + document.querySelector(".vote-options").classList.toggle("hidden"); + } + }); }, clickOutside() {