mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
♻️ Clean up the Google Pay preview button code
This commit is contained in:
parent
180afaad89
commit
93c6b40abc
1 changed files with 46 additions and 67 deletions
|
@ -3,92 +3,39 @@ import widgetBuilder from "../../../ppcp-button/resources/js/modules/Renderer/Wi
|
||||||
import PreviewButton from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButton";
|
import PreviewButton from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButton";
|
||||||
import PreviewButtonManager from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButtonManager";
|
import PreviewButtonManager from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButtonManager";
|
||||||
|
|
||||||
/**
|
|
||||||
* Button manager instance; we usually only need a single instance of this object.
|
|
||||||
*
|
|
||||||
* @see buttonManager()
|
|
||||||
*/
|
|
||||||
let managerInstance = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default attributes for new buttons.
|
|
||||||
*/
|
|
||||||
const defaultAttributes = {
|
|
||||||
button: {
|
|
||||||
style: {
|
|
||||||
type: 'pay',
|
|
||||||
color: 'black',
|
|
||||||
language: 'en'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accessor that creates and returns a single PreviewButtonManager instance.
|
* Accessor that creates and returns a single PreviewButtonManager instance.
|
||||||
*/
|
*/
|
||||||
const buttonManager = () => {
|
const buttonManager = () => {
|
||||||
if (!managerInstance) {
|
if (!GooglePayPreviewButtonManager.instance) {
|
||||||
managerInstance = new GooglePayPreviewButtonManager();
|
GooglePayPreviewButtonManager.instance = new GooglePayPreviewButtonManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
return managerInstance;
|
return GooglePayPreviewButtonManager.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
return button;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages all GooglePay preview buttons on this page.
|
* Manages all GooglePay preview buttons on this page.
|
||||||
*/
|
*/
|
||||||
class GooglePayPreviewButtonManager extends PreviewButtonManager {
|
class GooglePayPreviewButtonManager extends PreviewButtonManager {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
const defaultButton = {
|
||||||
|
style: {
|
||||||
|
type: 'pay',
|
||||||
|
color: 'black',
|
||||||
|
language: 'en'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const args = {
|
const args = {
|
||||||
// WooCommerce configuration object.
|
methodName: 'GooglePay',
|
||||||
buttonConfig: window.wc_ppcp_googlepay_admin,
|
buttonConfig: window.wc_ppcp_googlepay_admin,
|
||||||
// Internal widgetBuilder instance.
|
|
||||||
widgetBuilder,
|
widgetBuilder,
|
||||||
// Default button styles.
|
defaultAttributes: {button: defaultButton}
|
||||||
defaultAttributes
|
|
||||||
};
|
};
|
||||||
|
|
||||||
super(args);
|
super(args);
|
||||||
|
|
||||||
this.methodName = 'GooglePay';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,8 +70,40 @@ class GooglePayPreviewButtonManager extends PreviewButtonManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the preview button manager.
|
// Initialize the preview button manager.
|
||||||
buttonManager()
|
buttonManager();
|
||||||
|
|
||||||
// todo - Expose button manager for testing. Remove this!
|
// todo - Expose button manager for testing. Remove this!
|
||||||
window.gpay = buttonManager()
|
window.gpay = buttonManager()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue