diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js index 978ee2578..504fc4c42 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js @@ -1,6 +1,6 @@ import CartActionHandler from '../ActionHandler/CartActionHandler'; +import BootstrapHelper from "../Helper/BootstrapHelper"; import {setVisible} from "../Helper/Hiding"; -import {disable, enable} from "../Helper/ButtonDisabler"; class CartBootstrap { constructor(gateway, renderer, errorHandler) { @@ -48,15 +48,7 @@ class CartBootstrap { } handleButtonStatus() { - if (!this.shouldEnable()) { - this.renderer.disableSmartButtons(this.gateway.button.wrapper); - disable(this.gateway.button.wrapper); - disable(this.gateway.messages.wrapper); - return; - } - this.renderer.enableSmartButtons(this.gateway.button.wrapper); - enable(this.gateway.button.wrapper); - enable(this.gateway.messages.wrapper); + BootstrapHelper.handleButtonStatus(this); } shouldRender() { @@ -64,8 +56,7 @@ class CartBootstrap { } shouldEnable() { - return this.shouldRender() - && this.gateway.button.is_disabled !== true; + return BootstrapHelper.shouldEnable(this); } render() { diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js index 551baa75b..b426ac3ee 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/CheckoutBootstap.js @@ -5,6 +5,7 @@ import { isSavedCardSelected, ORDER_BUTTON_SELECTOR, PaymentMethods } from "../Helper/CheckoutMethodState"; +import BootstrapHelper from "../Helper/BootstrapHelper"; import {disable, enable} from "../Helper/ButtonDisabler"; class CheckoutBootstap { @@ -51,15 +52,7 @@ class CheckoutBootstap { } handleButtonStatus() { - if (!this.shouldEnable()) { - this.renderer.disableSmartButtons(this.gateway.button.wrapper); - disable(this.gateway.button.wrapper); - disable(this.gateway.messages.wrapper); - return; - } - this.renderer.enableSmartButtons(this.gateway.button.wrapper); - enable(this.gateway.button.wrapper); - enable(this.gateway.messages.wrapper); + BootstrapHelper.handleButtonStatus(this); } shouldRender() { @@ -71,8 +64,7 @@ class CheckoutBootstap { } shouldEnable() { - return this.shouldRender() - && this.gateway.button.is_disabled !== true; + return BootstrapHelper.shouldEnable(this); } render() { diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js index c993f3be0..4b4e3efd6 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/MiniCartBootstap.js @@ -1,5 +1,5 @@ import CartActionHandler from '../ActionHandler/CartActionHandler'; -import {disable, enable} from "../Helper/ButtonDisabler"; +import BootstrapHelper from "../Helper/BootstrapHelper"; class MiniCartBootstap { constructor(gateway, renderer, errorHandler) { @@ -29,13 +29,10 @@ class MiniCartBootstap { } handleButtonStatus() { - if (!this.shouldEnable()) { - this.renderer.disableSmartButtons(this.gateway.button.mini_cart_wrapper); - disable(this.gateway.button.mini_cart_wrapper); - return; - } - this.renderer.enableSmartButtons(this.gateway.button.mini_cart_wrapper); - enable(this.gateway.button.mini_cart_wrapper); + BootstrapHelper.handleButtonStatus(this, { + wrapper: this.gateway.button.mini_cart_wrapper, + skipMessages: true + }); } shouldRender() { @@ -44,8 +41,9 @@ class MiniCartBootstap { } shouldEnable() { - return this.shouldRender() - && this.gateway.button.is_mini_cart_disabled !== true; + return BootstrapHelper.shouldEnable(this, { + isDisabled: !!this.gateway.button.is_mini_cart_disabled + }); } render() { diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index b7addb4a5..231460642 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -1,7 +1,7 @@ import UpdateCart from "../Helper/UpdateCart"; import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler"; import {hide, show} from "../Helper/Hiding"; -import {disable, enable} from "../Helper/ButtonDisabler"; +import BootstrapHelper from "../Helper/BootstrapHelper"; class SingleProductBootstap { constructor(gateway, renderer, messages, errorHandler) { @@ -39,15 +39,9 @@ class SingleProductBootstap { } handleButtonStatus() { - if (!this.shouldEnable()) { - this.renderer.disableSmartButtons(this.gateway.button.wrapper); - disable(this.gateway.button.wrapper, this.formSelector); - disable(this.gateway.messages.wrapper); - return; - } - this.renderer.enableSmartButtons(this.gateway.button.wrapper); - enable(this.gateway.button.wrapper); - enable(this.gateway.messages.wrapper); + BootstrapHelper.handleButtonStatus(this, { + formSelector: this.formSelector + }); } init() { @@ -92,10 +86,9 @@ class SingleProductBootstap { const form = this.form(); const addToCartButton = form ? form.querySelector('.single_add_to_cart_button') : null; - return this.shouldRender() + return BootstrapHelper.shouldEnable(this) && !this.priceAmountIsZero() - && ((null === addToCartButton) || !addToCartButton.classList.contains('disabled')) - && this.gateway.button.is_disabled !== true; + && ((null === addToCartButton) || !addToCartButton.classList.contains('disabled')); } priceAmount() { diff --git a/modules/ppcp-button/resources/js/modules/Helper/BootstrapHelper.js b/modules/ppcp-button/resources/js/modules/Helper/BootstrapHelper.js new file mode 100644 index 000000000..ba00e2c76 --- /dev/null +++ b/modules/ppcp-button/resources/js/modules/Helper/BootstrapHelper.js @@ -0,0 +1,40 @@ +import {disable, enable} from "./ButtonDisabler"; + +/** + * Common Bootstrap methods to avoid code repetition. + */ +export default class BootstrapHelper { + + static handleButtonStatus(bs, options) { + options = options || {}; + options.wrapper = options.wrapper || bs.gateway.button.wrapper; + options.messagesWrapper = options.messagesWrapper || bs.gateway.messages.wrapper; + options.skipMessages = options.skipMessages || false; + + if (!bs.shouldEnable()) { + bs.renderer.disableSmartButtons(options.wrapper); + disable(options.wrapper, options.formSelector || null); + + if (!options.skipMessages) { + disable(options.messagesWrapper); + } + return; + } + bs.renderer.enableSmartButtons(options.wrapper); + enable(options.wrapper); + + if (!options.skipMessages) { + enable(options.messagesWrapper); + } + } + + static shouldEnable(bs, options) { + options = options || {}; + if (typeof options.isDisabled === 'undefined') { + options.isDisabled = bs.gateway.button.is_disabled; + } + + return bs.shouldRender() + && options.isDisabled !== true; + } +}