2023-08-15 11:26:37 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
|
2022-01-24 16:23:12 +00:00
|
|
|
export * from './plugins';
|
2022-08-04 13:15:30 +00:00
|
|
|
export * from './slot-fill-ordering';
|
2022-01-24 16:23:12 +00:00
|
|
|
|
2020-06-25 13:36:59 +00:00
|
|
|
/**
|
|
|
|
* Get the URL params.
|
|
|
|
*
|
|
|
|
* @param {string} locationSearch - Querystring part of a URL, including the question mark (?).
|
|
|
|
* @return {Object} - URL params.
|
|
|
|
*/
|
2020-06-05 16:07:28 +00:00
|
|
|
export function getUrlParams( locationSearch ) {
|
|
|
|
if ( locationSearch ) {
|
|
|
|
return locationSearch
|
|
|
|
.substr( 1 )
|
|
|
|
.split( '&' )
|
|
|
|
.reduce( ( params, query ) => {
|
|
|
|
const chunks = query.split( '=' );
|
|
|
|
const key = chunks[ 0 ];
|
|
|
|
let value = decodeURIComponent( chunks[ 1 ] );
|
|
|
|
value = isNaN( Number( value ) ) ? value : Number( value );
|
|
|
|
return ( params[ key ] = value ), params;
|
|
|
|
}, {} );
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-25 13:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current screen name.
|
|
|
|
*
|
|
|
|
* @return {string} - Screen name.
|
|
|
|
*/
|
|
|
|
export function getScreenName() {
|
|
|
|
let screenName = '';
|
2022-06-21 08:37:34 +00:00
|
|
|
const {
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
post_type: postType,
|
|
|
|
} = getUrlParams( window.location.search );
|
2020-06-25 13:36:59 +00:00
|
|
|
if ( page ) {
|
|
|
|
const currentPage = page === 'wc-admin' ? 'home_screen' : page;
|
|
|
|
screenName = path
|
|
|
|
? path.replace( /\//g, '_' ).substring( 1 )
|
|
|
|
: currentPage;
|
|
|
|
} else if ( postType ) {
|
|
|
|
screenName = postType;
|
|
|
|
}
|
|
|
|
return screenName;
|
|
|
|
}
|
2021-04-15 18:42:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to filter, but return two arrays separated by a partitioner function
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Array} arr - Original array of values.
|
2021-04-15 18:42:31 +00:00
|
|
|
* @param {Function} partitioner - Function to return truthy/falsy values to separate items in array.
|
|
|
|
*
|
|
|
|
* @return {Array} - Array of two arrays, first including truthy values, and second including falsy.
|
|
|
|
*/
|
|
|
|
export const sift = ( arr, partitioner ) =>
|
|
|
|
arr.reduce(
|
|
|
|
( all, curr ) => {
|
|
|
|
all[ !! partitioner( curr ) ? 0 : 1 ].push( curr );
|
|
|
|
return all;
|
|
|
|
},
|
|
|
|
[ [], [] ]
|
|
|
|
);
|
2022-05-10 00:58:23 +00:00
|
|
|
|
|
|
|
const timeFrames = [
|
|
|
|
{ name: '0-2s', max: 2 },
|
|
|
|
{ name: '2-5s', max: 5 },
|
|
|
|
{ name: '5-10s', max: 10 },
|
|
|
|
{ name: '10-15s', max: 15 },
|
|
|
|
{ name: '15-20s', max: 20 },
|
|
|
|
{ name: '20-30s', max: 30 },
|
|
|
|
{ name: '30-60s', max: 60 },
|
|
|
|
{ name: '>60s' },
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns time frame for a given time in milliseconds.
|
|
|
|
*
|
|
|
|
* @param {number} timeInMs - time in milliseconds
|
|
|
|
*
|
|
|
|
* @return {string} - Time frame.
|
|
|
|
*/
|
|
|
|
export const getTimeFrame = ( timeInMs ) => {
|
|
|
|
for ( const timeFrame of timeFrames ) {
|
|
|
|
if ( ! timeFrame.max ) {
|
|
|
|
return timeFrame.name;
|
|
|
|
}
|
|
|
|
if ( timeInMs < timeFrame.max * 1000 ) {
|
|
|
|
return timeFrame.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-08-15 11:26:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Goes into fullscreen mode when the component is loaded
|
|
|
|
*
|
|
|
|
* @param {string[]} classes - classes to add to document.body
|
|
|
|
*/
|
|
|
|
export const useFullScreen = ( classes ) => {
|
|
|
|
useEffect( () => {
|
2023-09-06 06:21:09 +00:00
|
|
|
const hasToolbarClass =
|
|
|
|
document.documentElement.classList.contains( 'wp-toolbar' );
|
2023-08-15 11:26:37 +00:00
|
|
|
document.body.classList.remove( 'woocommerce-admin-is-loading' );
|
|
|
|
document.body.classList.add( classes );
|
|
|
|
document.body.classList.add( 'woocommerce-admin-full-screen' );
|
|
|
|
document.body.classList.add( 'is-wp-toolbar-disabled' );
|
2023-09-06 06:21:09 +00:00
|
|
|
if ( hasToolbarClass ) {
|
|
|
|
document.documentElement.classList.remove( 'wp-toolbar' );
|
|
|
|
}
|
2023-08-15 11:26:37 +00:00
|
|
|
return () => {
|
|
|
|
document.body.classList.remove( classes );
|
|
|
|
document.body.classList.remove( 'woocommerce-admin-full-screen' );
|
|
|
|
document.body.classList.remove( 'is-wp-toolbar-disabled' );
|
2023-09-06 06:21:09 +00:00
|
|
|
if ( hasToolbarClass ) {
|
|
|
|
document.documentElement.classList.add( 'wp-toolbar' );
|
|
|
|
}
|
2023-08-15 11:26:37 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
};
|