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

80 lines
2.1 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 = () => {
2020-04-09 12:23:44 +03:00
const renderer = new Renderer;
const errorHandler = new ErrorHandler();
const defaultConfig = new CartConfig(
PayPalCommerceGateway,
errorHandler,
);
const context = PayPalCommerceGateway.context;
if (context === 'mini-cart') {
const miniCartBootstap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
miniCartBootstap.init();
}
if (context === 'product') {
const singleProductBootstap = new SingleProductBootstap(
PayPalCommerceGateway,
renderer,
);
const miniCartBootstap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
singleProductBootstap.init();
miniCartBootstap.init();
}
if (context === 'cart') {
const cartBootstrap = new CartBootstrap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
2020-04-08 12:33:34 +03:00
cartBootstrap.init();
}
2020-04-08 12:33:34 +03:00
if (context === 'checkout') {
const checkoutBootstap = new CheckoutBootstap(
PayPalCommerceGateway,
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
);