2020-04-24 13:41:32 +00:00
|
|
|
const Event = window.Event || null;
|
|
|
|
|
2021-09-15 16:28:34 +00:00
|
|
|
interface DispatchedEventProperties {
|
|
|
|
// Whether the event bubbles.
|
|
|
|
bubbles?: boolean;
|
|
|
|
// Whether the event is cancelable.
|
|
|
|
cancelable?: boolean;
|
|
|
|
// Element that dispatches the event. By default, the body.
|
|
|
|
element?: HTMLElement;
|
|
|
|
}
|
2020-09-20 23:54:08 +00:00
|
|
|
|
2020-07-14 19:46:44 +00:00
|
|
|
/**
|
|
|
|
* Wrapper function to dispatch an event so it's compatible with IE11.
|
|
|
|
*/
|
2020-08-07 17:33:32 +00:00
|
|
|
export const dispatchEvent = (
|
2021-09-15 16:28:34 +00:00
|
|
|
name: string,
|
|
|
|
{ bubbles = false, cancelable = false, element }: DispatchedEventProperties
|
|
|
|
): void => {
|
2020-08-07 17:33:32 +00:00
|
|
|
if ( ! element ) {
|
|
|
|
element = document.body;
|
|
|
|
}
|
2020-04-24 13:41:32 +00:00
|
|
|
// In IE, Event is an object and can't be instantiated with `new Event()`.
|
|
|
|
if ( typeof Event === 'function' ) {
|
2020-07-14 19:46:44 +00:00
|
|
|
const event = new Event( name, {
|
|
|
|
bubbles,
|
|
|
|
cancelable,
|
2020-04-24 13:41:32 +00:00
|
|
|
} );
|
2020-08-07 17:33:32 +00:00
|
|
|
element.dispatchEvent( event );
|
2020-04-24 13:41:32 +00:00
|
|
|
} else {
|
|
|
|
const event = document.createEvent( 'Event' );
|
2020-07-14 19:46:44 +00:00
|
|
|
event.initEvent( name, bubbles, cancelable );
|
2020-08-07 17:33:32 +00:00
|
|
|
element.dispatchEvent( event );
|
2020-04-24 13:41:32 +00:00
|
|
|
}
|
|
|
|
};
|
2020-07-14 19:46:44 +00:00
|
|
|
|
2021-09-15 16:28:34 +00:00
|
|
|
let fragmentRequestTimeoutId: ReturnType< typeof setTimeout >;
|
2021-04-20 15:44:49 +00:00
|
|
|
|
2020-07-14 19:46:44 +00:00
|
|
|
// This is a hack to trigger cart updates till we migrate to block based cart
|
|
|
|
// that relies on the store, see
|
|
|
|
// https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/1247
|
2021-09-15 16:28:34 +00:00
|
|
|
export const triggerFragmentRefresh = (): void => {
|
2021-04-20 15:44:49 +00:00
|
|
|
if ( fragmentRequestTimeoutId ) {
|
|
|
|
clearTimeout( fragmentRequestTimeoutId );
|
|
|
|
}
|
|
|
|
fragmentRequestTimeoutId = setTimeout( () => {
|
|
|
|
dispatchEvent( 'wc_fragment_refresh', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
} );
|
|
|
|
}, 50 );
|
2020-07-14 19:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that listens to a jQuery event and dispatches a native JS event.
|
|
|
|
* Useful to convert WC Core events into events that can be read by blocks.
|
|
|
|
*
|
2021-09-15 16:28:34 +00:00
|
|
|
* Returns a function to remove the jQuery event handler. Ideally it should be
|
|
|
|
* used when the component is unmounted.
|
2020-07-14 19:46:44 +00:00
|
|
|
*/
|
|
|
|
export const translateJQueryEventToNative = (
|
2021-09-15 16:28:34 +00:00
|
|
|
// Name of the jQuery event to listen to.
|
|
|
|
jQueryEventName: string,
|
|
|
|
// Name of the native event to dispatch.
|
|
|
|
nativeEventName: string,
|
|
|
|
// Whether the event bubbles.
|
2020-07-14 19:46:44 +00:00
|
|
|
bubbles = false,
|
2021-09-15 16:28:34 +00:00
|
|
|
// Whether the event is cancelable.
|
2020-07-14 19:46:44 +00:00
|
|
|
cancelable = false
|
2021-09-15 16:28:34 +00:00
|
|
|
): ( () => void ) => {
|
2020-07-14 19:46:44 +00:00
|
|
|
if ( typeof jQuery !== 'function' ) {
|
|
|
|
return () => void null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventDispatcher = () => {
|
2020-08-07 17:33:32 +00:00
|
|
|
dispatchEvent( nativeEventName, { bubbles, cancelable } );
|
2020-07-14 19:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
jQuery( document ).on( jQueryEventName, eventDispatcher );
|
|
|
|
return () => jQuery( document ).off( jQueryEventName, eventDispatcher );
|
|
|
|
};
|