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

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-04-02 08:38:00 +03:00
import ErrorHandler from './modules/ErrorHandler';
import CartConfig from './modules/CartConfig';
import MiniCartBootstap from './modules/MiniCartBootstap';
import SingleProductBootstap from './modules/SingleProductBootstap';
import CartBootstrap from './modules/CartBootstap';
import CheckoutBootstap from './modules/CheckoutBootstap';
2020-04-09 12:23:44 +03:00
import Renderer from './modules/Renderer';
2020-04-02 08:38:00 +03:00
const bootstrap = () => {
const context = PayPalCommerceGateway.context;
2020-04-09 12:23:44 +03:00
const renderer = new Renderer;
const errorHandler = new ErrorHandler();
const defaultConfig = new CartConfig(
PayPalCommerceGateway,
errorHandler,
);
if (context === 'mini-cart') {
2020-04-09 12:23:44 +03:00
const miniCartBootstap = new MiniCartBootstap(renderer, defaultConfig);
miniCartBootstap.init();
}
if (context === 'product') {
2020-04-09 12:23:44 +03:00
const singleProductBootstap = new SingleProductBootstap(renderer);
const miniCartBootstap = new MiniCartBootstap(renderer, defaultConfig);
singleProductBootstap.init();
miniCartBootstap.init();
}
if (context === 'cart') {
2020-04-09 12:23:44 +03:00
const cartBootstrap = new CartBootstrap(renderer, defaultConfig);
2020-04-08 12:33:34 +03:00
cartBootstrap.init();
}
2020-04-08 12:33:34 +03:00
if (context === 'checkout') {
2020-04-09 12:23:44 +03:00
const checkoutBootstap = new CheckoutBootstap(renderer, defaultConfig);
checkoutBootstap.init();
}
};
document.addEventListener(
'DOMContentLoaded',
() => {
if (!typeof (PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
2020-04-02 08:38:00 +03:00
return;
}
2020-04-08 12:33:34 +03:00
const script = document.createElement('script');
script.setAttribute('src', PayPalCommerceGateway.button.url);
script.addEventListener('load', (event) => {
bootstrap();
});
document.body.append(script);
},
2020-04-02 08:38:00 +03:00
);