2020-10-21 17:02:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2020-10-21 17:02:45 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
type MenuId = 'primary' | 'favorites' | 'plugins' | 'secondary';
|
|
|
|
|
|
|
|
interface Item {
|
|
|
|
id: string;
|
|
|
|
matchExpression: string;
|
|
|
|
url: string;
|
|
|
|
order: number;
|
|
|
|
title: string;
|
|
|
|
parent: string;
|
|
|
|
menuId: MenuId;
|
|
|
|
capability: string;
|
|
|
|
isCategory: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Category {
|
|
|
|
id: string;
|
|
|
|
isCategory: boolean;
|
|
|
|
menuId: MenuId;
|
|
|
|
migrate: boolean;
|
|
|
|
order: number;
|
|
|
|
parent: string;
|
|
|
|
title: string;
|
|
|
|
primary?: Item[];
|
|
|
|
favorites?: Item[];
|
|
|
|
plugins?: Item[];
|
|
|
|
secondary?: Item[];
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
/**
|
|
|
|
* Get the full URL if a relative path is passed.
|
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const getFullUrl = ( url: string ): string => {
|
2020-10-13 01:40:53 +00:00
|
|
|
if ( url.indexOf( 'http' ) === 0 ) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:18:48 +00:00
|
|
|
return getAdminLink( url );
|
2020-10-13 01:40:53 +00:00
|
|
|
};
|
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
/**
|
|
|
|
* Get a default regex expression to match the path and provided params.
|
|
|
|
*/
|
|
|
|
export const getDefaultMatchExpression = ( url: string ): string => {
|
|
|
|
const escapedUrl = url.replace( /[-\/\\^$*+?.()|[\]{}]/gi, '\\$&' );
|
|
|
|
const [ path, args, hash ] = escapedUrl.split( /\\\?|#/ );
|
|
|
|
const hashExpression = hash ? `(.*#${ hash }$)` : '';
|
|
|
|
const argsExpression = args
|
|
|
|
? args.split( '&' ).reduce( ( acc, param ) => {
|
|
|
|
return `${ acc }(?=.*[?|&]${ param }(&|$|#))`;
|
|
|
|
}, '' )
|
|
|
|
: '';
|
|
|
|
return '^' + path + argsExpression + hashExpression;
|
|
|
|
};
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
/**
|
2020-12-09 00:45:30 +00:00
|
|
|
* Get a match score for a menu item given a location.
|
2020-10-13 01:40:53 +00:00
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const getMatchScore = (
|
|
|
|
location: Location,
|
|
|
|
itemUrl: string,
|
|
|
|
itemExpression: string | null = null
|
|
|
|
): number => {
|
2020-12-09 00:45:30 +00:00
|
|
|
if ( ! itemUrl ) {
|
2021-03-18 03:38:38 +00:00
|
|
|
return 0;
|
2020-10-13 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
const fullUrl = getFullUrl( itemUrl );
|
|
|
|
const { href } = location;
|
2020-10-13 01:40:53 +00:00
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
// Return highest possible score for exact match.
|
|
|
|
if ( fullUrl === href ) {
|
2020-10-13 01:40:53 +00:00
|
|
|
return Number.MAX_SAFE_INTEGER;
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
const defaultExpression = getDefaultMatchExpression( fullUrl );
|
|
|
|
const regexp = new RegExp( itemExpression || defaultExpression, 'i' );
|
|
|
|
return ( decodeURIComponent( href ).match( regexp ) || [] ).length;
|
|
|
|
};
|
2020-10-13 01:40:53 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
interface wcNavigation {
|
|
|
|
menuItems: Item[];
|
|
|
|
rootBackLabel: string;
|
|
|
|
rootBackUrl: string;
|
|
|
|
historyPatched: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
wcNavigation: wcNavigation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface wcNavigation {
|
|
|
|
menuItems: Item[];
|
|
|
|
rootBackLabel: string;
|
|
|
|
rootBackUrl: string;
|
|
|
|
historyPatched: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
wcNavigation: wcNavigation;
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the closest matching item.
|
|
|
|
*
|
|
|
|
* @param {Array} items An array of items to match against.
|
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const getMatchingItem = ( items: Item[] ): Item | null => {
|
2020-10-13 01:40:53 +00:00
|
|
|
let matchedItem = null;
|
2020-12-09 00:45:30 +00:00
|
|
|
let highestMatchScore = 0;
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
items.forEach( ( item ) => {
|
2020-10-21 17:02:45 +00:00
|
|
|
const score = getMatchScore(
|
|
|
|
window.location,
|
2021-03-02 16:18:48 +00:00
|
|
|
item.url,
|
2020-12-09 00:45:30 +00:00
|
|
|
item.matchExpression
|
2020-10-21 17:02:45 +00:00
|
|
|
);
|
2020-12-09 00:45:30 +00:00
|
|
|
if ( score > 0 && score >= highestMatchScore ) {
|
|
|
|
highestMatchScore = score;
|
2020-10-13 01:40:53 +00:00
|
|
|
matchedItem = item;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2020-11-11 18:16:12 +00:00
|
|
|
return matchedItem || null;
|
2020-10-13 01:40:53 +00:00
|
|
|
};
|
2021-02-22 18:54:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Available menu IDs.
|
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const menuIds: MenuId[] = [
|
|
|
|
'primary',
|
|
|
|
'favorites',
|
|
|
|
'plugins',
|
|
|
|
'secondary',
|
|
|
|
];
|
|
|
|
|
|
|
|
interface Category {
|
|
|
|
id: string;
|
|
|
|
isCategory: boolean;
|
|
|
|
menuId: MenuId;
|
|
|
|
migrate: boolean;
|
|
|
|
order: number;
|
|
|
|
parent: string;
|
|
|
|
title: string;
|
|
|
|
primary?: Item[];
|
|
|
|
favorites?: Item[];
|
|
|
|
plugins?: Item[];
|
|
|
|
secondary?: Item[];
|
|
|
|
}
|
2021-02-22 18:54:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default categories for the menu.
|
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const defaultCategories: {
|
|
|
|
[ key: string ]: Category;
|
|
|
|
} = {
|
2021-02-22 18:54:27 +00:00
|
|
|
woocommerce: {
|
|
|
|
id: 'woocommerce',
|
|
|
|
isCategory: true,
|
|
|
|
menuId: 'primary',
|
|
|
|
migrate: true,
|
|
|
|
order: 10,
|
|
|
|
parent: '',
|
|
|
|
title: 'WooCommerce',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort an array of menu items by their order property.
|
|
|
|
*
|
|
|
|
* @param {Array} menuItems Array of menu items.
|
|
|
|
* @return {Array} Sorted menu items.
|
|
|
|
*/
|
2021-03-18 03:38:38 +00:00
|
|
|
export const sortMenuItems = ( menuItems: Item[] ): Item[] => {
|
2021-02-22 18:54:27 +00:00
|
|
|
return menuItems.sort( ( a, b ) => {
|
|
|
|
if ( a.order === b.order ) {
|
|
|
|
return a.title.localeCompare( b.title );
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.order - b.order;
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a flat tree structure of all Categories and thier children grouped by menuId
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Array} menuItems Array of menu items.
|
2021-02-22 18:54:27 +00:00
|
|
|
* @param {Function} currentUserCan Callback method passed the capability to determine if a menu item is visible.
|
|
|
|
* @return {Object} Mapped menu items and categories.
|
|
|
|
*/
|
|
|
|
export const getMappedItemsCategories = (
|
2021-03-18 03:38:38 +00:00
|
|
|
menuItems: Item[],
|
|
|
|
currentUserCan: ( capability: string ) => boolean
|
|
|
|
): {
|
|
|
|
items: Record< string, Category | Record< string, Item[] > >;
|
|
|
|
categories: Record< string, Category | Item >;
|
|
|
|
} => {
|
|
|
|
const categories: {
|
|
|
|
[ key: string ]: Category | Item;
|
|
|
|
} = { ...defaultCategories };
|
2021-02-22 18:54:27 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
const items = sortMenuItems( menuItems ).reduce(
|
|
|
|
(
|
|
|
|
acc: {
|
|
|
|
[ key: string ]: Category | { [ key: string ]: Item[] };
|
|
|
|
},
|
|
|
|
item: Item
|
|
|
|
) => {
|
|
|
|
// Set up the category if it doesn't yet exist.
|
|
|
|
if ( ! acc[ item.parent ] ) {
|
|
|
|
acc[ item.parent ] = {};
|
|
|
|
menuIds.forEach( ( menuId ) => {
|
|
|
|
acc[ item.parent ][ menuId ] = [];
|
|
|
|
} );
|
|
|
|
}
|
2021-02-22 18:54:27 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
// Incorrect menu ID.
|
|
|
|
if ( ! acc[ item.parent ][ item.menuId ] ) {
|
|
|
|
return acc;
|
|
|
|
}
|
2021-02-22 18:54:27 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
// User does not have permission to view this item.
|
|
|
|
if (
|
|
|
|
currentUserCan &&
|
|
|
|
item.capability &&
|
|
|
|
! currentUserCan( item.capability )
|
|
|
|
) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add categories.
|
|
|
|
if ( item.isCategory ) {
|
|
|
|
categories[ item.id ] = item;
|
|
|
|
}
|
2021-02-22 18:54:27 +00:00
|
|
|
|
2021-03-18 03:38:38 +00:00
|
|
|
const menuIdArray = acc[ item.parent ][ item.menuId ];
|
|
|
|
|
|
|
|
if ( menuIdArray ) {
|
|
|
|
menuIdArray.push( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2021-02-22 18:54:27 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
items,
|
|
|
|
categories,
|
|
|
|
};
|
|
|
|
};
|