2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import preloadScript from '@woocommerce/base-utils/preload-script';
|
|
|
|
import lazyLoadScript from '@woocommerce/base-utils/lazy-load-script';
|
2021-09-07 08:27:16 +00:00
|
|
|
import { translateJQueryEventToNative } from '@woocommerce/base-utils/legacy-events';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
interface dependencyData {
|
|
|
|
src: string;
|
|
|
|
version?: string;
|
|
|
|
after?: string;
|
|
|
|
before?: string;
|
|
|
|
translations?: string;
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:31:56 +00:00
|
|
|
window.addEventListener( 'load', () => {
|
2021-08-25 15:42:55 +00:00
|
|
|
const miniCartBlocks = document.querySelectorAll( '.wc-block-mini-cart' );
|
2021-09-07 08:27:16 +00:00
|
|
|
let wasLoadScriptsCalled = false;
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
if ( miniCartBlocks.length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dependencies = getSetting(
|
|
|
|
'mini_cart_block_frontend_dependencies',
|
|
|
|
{}
|
|
|
|
) as Record< string, dependencyData >;
|
|
|
|
|
|
|
|
// Preload scripts
|
|
|
|
for ( const dependencyHandle in dependencies ) {
|
|
|
|
const dependency = dependencies[ dependencyHandle ];
|
|
|
|
preloadScript( {
|
|
|
|
handle: dependencyHandle,
|
|
|
|
...dependency,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-09-07 08:27:16 +00:00
|
|
|
// Make it so we can read jQuery events triggered by WC Core elements.
|
|
|
|
const removeJQueryAddingToCartEvent = translateJQueryEventToNative(
|
|
|
|
'adding_to_cart',
|
|
|
|
'wc-blocks_adding_to_cart'
|
|
|
|
);
|
|
|
|
const removeJQueryAddedToCartEvent = translateJQueryEventToNative(
|
|
|
|
'added_to_cart',
|
|
|
|
'wc-blocks_added_to_cart'
|
|
|
|
);
|
2021-10-25 16:05:01 +00:00
|
|
|
const removeJQueryRemovedFromCartEvent = translateJQueryEventToNative(
|
|
|
|
'removed_from_cart',
|
|
|
|
'wc-blocks_removed_from_cart'
|
|
|
|
);
|
2021-09-07 08:27:16 +00:00
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
const loadScripts = async () => {
|
2021-09-07 08:27:16 +00:00
|
|
|
// Ensure we only call loadScripts once.
|
|
|
|
if ( wasLoadScriptsCalled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wasLoadScriptsCalled = true;
|
|
|
|
|
|
|
|
// Remove adding to cart event handler.
|
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_adding_to_cart',
|
|
|
|
loadScripts
|
|
|
|
);
|
|
|
|
removeJQueryAddingToCartEvent();
|
|
|
|
|
|
|
|
// Lazy load scripts.
|
2021-09-02 09:44:25 +00:00
|
|
|
for ( const dependencyHandle in dependencies ) {
|
|
|
|
const dependency = dependencies[ dependencyHandle ];
|
|
|
|
await lazyLoadScript( {
|
|
|
|
handle: dependencyHandle,
|
|
|
|
...dependency,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 08:27:16 +00:00
|
|
|
document.body.addEventListener( 'wc-blocks_adding_to_cart', loadScripts );
|
|
|
|
|
|
|
|
miniCartBlocks.forEach( ( miniCartBlock, i ) => {
|
2021-09-02 09:44:25 +00:00
|
|
|
if ( ! ( miniCartBlock instanceof HTMLElement ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:42:55 +00:00
|
|
|
const miniCartButton = miniCartBlock.querySelector(
|
|
|
|
'.wc-block-mini-cart__button'
|
|
|
|
);
|
2021-09-02 09:44:25 +00:00
|
|
|
const miniCartDrawerPlaceholderOverlay = miniCartBlock.querySelector(
|
|
|
|
'.wc-block-components-drawer__screen-overlay'
|
2021-08-25 15:42:55 +00:00
|
|
|
);
|
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
if ( ! miniCartButton || ! miniCartDrawerPlaceholderOverlay ) {
|
2021-08-25 15:42:55 +00:00
|
|
|
// Markup is not correct, abort.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:05:01 +00:00
|
|
|
const loadContents = () => {
|
2021-09-21 13:25:44 +00:00
|
|
|
if ( ! wasLoadScriptsCalled ) {
|
|
|
|
loadScripts();
|
|
|
|
}
|
2021-09-07 08:27:16 +00:00
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
2021-10-25 16:05:01 +00:00
|
|
|
openDrawerWithRefresh
|
|
|
|
);
|
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_removed_from_cart',
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
|
|
loadContentsWithRefresh
|
2021-09-07 08:27:16 +00:00
|
|
|
);
|
2021-10-25 16:05:01 +00:00
|
|
|
removeJQueryAddedToCartEvent();
|
|
|
|
removeJQueryRemovedFromCartEvent();
|
|
|
|
};
|
|
|
|
|
|
|
|
const openDrawer = () => {
|
|
|
|
miniCartBlock.dataset.isInitiallyOpen = 'true';
|
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
miniCartDrawerPlaceholderOverlay.classList.add(
|
|
|
|
'wc-block-components-drawer__screen-overlay--with-slide-in'
|
|
|
|
);
|
|
|
|
miniCartDrawerPlaceholderOverlay.classList.remove(
|
|
|
|
'wc-block-components-drawer__screen-overlay--is-hidden'
|
|
|
|
);
|
2021-10-25 16:05:01 +00:00
|
|
|
|
|
|
|
loadContents();
|
2021-09-07 08:27:16 +00:00
|
|
|
};
|
|
|
|
|
2021-10-25 16:05:01 +00:00
|
|
|
const openDrawerWithRefresh = () => {
|
2021-09-07 08:27:16 +00:00
|
|
|
miniCartBlock.dataset.isDataOutdated = 'true';
|
2021-10-25 16:05:01 +00:00
|
|
|
openDrawer();
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadContentsWithRefresh = () => {
|
|
|
|
miniCartBlock.dataset.isDataOutdated = 'true';
|
|
|
|
miniCartBlock.dataset.isInitiallyOpen = 'false';
|
|
|
|
loadContents();
|
2021-08-25 15:42:55 +00:00
|
|
|
};
|
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
miniCartButton.addEventListener( 'mouseover', loadScripts );
|
|
|
|
miniCartButton.addEventListener( 'focus', loadScripts );
|
2021-10-25 16:05:01 +00:00
|
|
|
miniCartButton.addEventListener( 'click', openDrawer );
|
2021-09-07 08:27:16 +00:00
|
|
|
|
|
|
|
// There might be more than one Mini Cart block in the page. Make sure
|
|
|
|
// only one opens when adding a product to the cart.
|
|
|
|
if ( i === 0 ) {
|
|
|
|
document.body.addEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
2021-10-25 16:05:01 +00:00
|
|
|
openDrawerWithRefresh
|
|
|
|
);
|
|
|
|
document.body.addEventListener(
|
|
|
|
'wc-blocks_removed_from_cart',
|
|
|
|
loadContentsWithRefresh
|
2021-09-07 08:27:16 +00:00
|
|
|
);
|
|
|
|
}
|
2021-08-25 15:42:55 +00:00
|
|
|
} );
|
2021-11-04 11:31:56 +00:00
|
|
|
} );
|