woocommerce-paypal-payments/modules/ppcp-googlepay/resources/js/boot-admin.js
2024-06-07 15:42:16 +02:00

116 lines
3.2 KiB
JavaScript

import GooglepayButton from "./GooglepayButton";
import widgetBuilder from "../../../ppcp-button/resources/js/modules/Renderer/WidgetBuilder";
import PreviewButton from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButton";
import PreviewButtonManager from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButtonManager";
/**
* Accessor that creates and returns a single PreviewButtonManager instance.
*/
const buttonManager = () => {
if (!GooglePayPreviewButtonManager.instance) {
GooglePayPreviewButtonManager.instance = new GooglePayPreviewButtonManager();
}
return GooglePayPreviewButtonManager.instance;
}
/**
* Manages all GooglePay preview buttons on this page.
*/
class GooglePayPreviewButtonManager extends PreviewButtonManager {
constructor() {
const defaultButton = {
style: {
type: 'pay',
color: 'black',
language: 'en'
}
};
const args = {
methodName: 'GooglePay',
buttonConfig: window.wc_ppcp_googlepay_admin,
widgetBuilder,
defaultAttributes: {button: defaultButton}
};
super(args);
}
/**
* Responsible for fetching and returning the PayPal configuration object for this payment
* method.
*
* @return {Promise<{}>}
*/
async fetchConfig() {
const apiMethod = this.widgetBuilder?.paypal?.Googlepay()?.config
if (!apiMethod) {
this.error('configuration object cannot be retrieved from PayPal');
return {};
}
return await apiMethod();
}
/**
* This method is responsible for creating a new PreviewButton instance and returning it.
*
* @param {string} wrapperId - CSS ID of the wrapper element.
* @return {GooglePayPreviewButton}
*/
createButtonInst(wrapperId) {
return new GooglePayPreviewButton({
selector: wrapperId,
configResponse: this.configResponse,
defaultAttributes: this.defaultAttributes
});
}
}
/**
* A single GooglePay preview button instance.
*/
class GooglePayPreviewButton extends PreviewButton {
constructor(args) {
super(args);
this.selector = `${args.selector}GooglePay`
}
createNewWrapper() {
const element = super.createNewWrapper();
element.addClass('ppcp-button-googlepay');
return element;
}
createButton() {
const button = new GooglepayButton(
'preview',
null,
this.buttonConfig,
this.ppcpConfig,
);
button.init(this.configResponse);
}
/**
* Some style details need to be copied from the ppcpConfig object to buttonConfig.
*
* - ppcpConfig: Generated by JS, containing the current form values.
* - buttonConfig: Generated on server side, contains the full (saved) button details.
*/
applyPreviewConfig() {
if (this.ppcpConfig && this.buttonConfig) {
this.buttonConfig.button.style = this.ppcpConfig.button.style;
}
}
}
// Initialize the preview button manager.
buttonManager();