woocommerce-paypal-payments/modules.local/ppcp-button/resources/js/button.js

76 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-04-02 08:38:00 +03:00
import Renderer from './modules/Renderer';
import SingleProductConfig from './modules/SingleProductConfig';
import UpdateCart from './modules/UpdateCart';
import ErrorHandler from './modules/ErrorHandler';
2020-04-08 12:33:34 +03:00
import CartConfig from "./modules/CartConfig";
2020-04-02 08:38:00 +03:00
document.addEventListener(
'DOMContentLoaded',
() => {
if (! typeof(PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
return;
}
2020-04-08 12:33:34 +03:00
const context = PayPalCommerceGateway.context;
const errorHandler = new ErrorHandler();
const defaultConfigurator = new CartConfig(
PayPalCommerceGateway,
errorHandler
);
// Configure mini cart buttons
if (document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
}
jQuery( document.body ).on( 'wc_fragments_loaded wc_fragments_refreshed', () => {
if (! document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
return;
}
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
} );
// Configure context buttons
2020-04-02 08:38:00 +03:00
if (! document.querySelector(PayPalCommerceGateway.button.wrapper)) {
return;
}
2020-04-08 12:33:34 +03:00
let configurator = null;
if (context === 'product') {
if (! document.querySelector('form.cart')) {
return;
}
const updateCart = new UpdateCart(
PayPalCommerceGateway.ajax.change_cart.endpoint,
PayPalCommerceGateway.ajax.change_cart.nonce
);
configurator = new SingleProductConfig(
PayPalCommerceGateway,
updateCart,
renderer.showButtons.bind(renderer),
renderer.hideButtons.bind(renderer),
document.querySelector('form.cart'),
errorHandler
);
}
if (context === 'cart') {
configurator = defaultConfigurator;
}
if (! configurator) {
console.error('No context for button found.');
2020-04-02 08:38:00 +03:00
return;
}
2020-04-08 12:33:34 +03:00
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.wrapper
2020-04-02 08:38:00 +03:00
);
2020-04-08 12:33:34 +03:00
renderer.render(configurator.configuration());
2020-04-02 08:38:00 +03:00
}
);