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..fed2f57 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,30 @@ 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"); + } + }, + { currentUser: this.currentUser, attrs: this.attrs } + ); }, clickOutside() {