2018-05-18 17:31:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-04-29 18:01:27 +00:00
|
|
|
import { Component, Suspense, lazy } from '@wordpress/element';
|
2019-06-13 19:58:21 +00:00
|
|
|
import { parse, stringify } from 'qs';
|
2019-07-11 17:22:26 +00:00
|
|
|
import { find, isEqual, last, omit } from 'lodash';
|
2019-05-14 03:58:37 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2019-07-05 08:15:49 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-05-18 17:31:08 +00:00
|
|
|
|
2018-11-05 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
getNewPath,
|
|
|
|
getPersistedQuery,
|
|
|
|
getHistory,
|
|
|
|
} from '@woocommerce/navigation';
|
2020-04-29 18:01:27 +00:00
|
|
|
import { Spinner } from '@woocommerce/components';
|
2018-11-05 21:02:04 +00:00
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
/**
|
2018-10-18 04:37:28 +00:00
|
|
|
* Internal dependencies
|
2018-05-18 17:31:08 +00:00
|
|
|
*/
|
2020-06-05 16:07:28 +00:00
|
|
|
import { getUrlParams } from 'utils';
|
2020-06-03 20:42:30 +00:00
|
|
|
|
2020-04-29 18:01:27 +00:00
|
|
|
const AnalyticsReport = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "analytics-report" */ 'analytics/report' )
|
|
|
|
);
|
|
|
|
const AnalyticsSettings = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "analytics-settings" */ 'analytics/settings' )
|
|
|
|
);
|
|
|
|
const Dashboard = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "dashboard" */ 'dashboard' )
|
|
|
|
);
|
|
|
|
const DevDocs = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "devdocs" */ 'devdocs' )
|
|
|
|
);
|
2020-06-15 02:17:12 +00:00
|
|
|
const Homescreen = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "homescreen" */ 'homescreen' )
|
2020-04-29 18:01:27 +00:00
|
|
|
);
|
|
|
|
const MarketingOverview = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "marketing-overview" */ 'marketing/overview' )
|
|
|
|
);
|
|
|
|
import getReports from 'analytics/report/get-reports';
|
2018-05-18 17:31:08 +00:00
|
|
|
|
2019-05-14 03:58:37 +00:00
|
|
|
const TIME_EXCLUDED_SCREENS_FILTER = 'woocommerce_admin_time_excluded_screens';
|
|
|
|
|
2019-06-26 23:00:23 +00:00
|
|
|
export const PAGES_FILTER = 'woocommerce_admin_pages_list';
|
|
|
|
|
2020-06-11 00:26:20 +00:00
|
|
|
export const getPages = () => {
|
2019-02-12 20:02:02 +00:00
|
|
|
const pages = [];
|
2020-03-23 22:57:16 +00:00
|
|
|
const initialBreadcrumbs = [ [ '', wcSettings.woocommerceTranslation ] ];
|
2019-02-12 20:02:02 +00:00
|
|
|
|
|
|
|
if ( window.wcAdminFeatures.devdocs ) {
|
|
|
|
pages.push( {
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs',
|
2019-08-30 16:07:29 +00:00
|
|
|
breadcrumbs: ( { location } ) => {
|
2020-06-05 16:07:28 +00:00
|
|
|
const { component } = getUrlParams( location.search );
|
2019-08-30 16:07:29 +00:00
|
|
|
|
|
|
|
if ( component ) {
|
2020-03-23 22:57:16 +00:00
|
|
|
return [
|
|
|
|
...initialBreadcrumbs,
|
|
|
|
[ '/devdocs', 'Documentation' ],
|
|
|
|
component,
|
|
|
|
];
|
2019-08-30 16:07:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:57:16 +00:00
|
|
|
return [ ...initialBreadcrumbs, 'Documentation' ];
|
2019-08-30 16:07:29 +00:00
|
|
|
},
|
2019-02-12 20:02:02 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:32:44 +00:00
|
|
|
if (
|
|
|
|
window.wcAdminFeatures[ 'analytics-dashboard' ] &&
|
2020-06-15 02:17:12 +00:00
|
|
|
! window.wcAdminFeatures.homescreen
|
2020-04-22 21:32:44 +00:00
|
|
|
) {
|
2019-02-12 20:02:02 +00:00
|
|
|
pages.push( {
|
2018-05-18 17:31:08 +00:00
|
|
|
container: Dashboard,
|
|
|
|
path: '/',
|
2020-03-23 22:57:16 +00:00
|
|
|
breadcrumbs: [
|
|
|
|
...initialBreadcrumbs,
|
|
|
|
__( 'Dashboard', 'woocommerce-admin' ),
|
|
|
|
],
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-06-15 02:17:12 +00:00
|
|
|
if ( window.wcAdminFeatures.homescreen ) {
|
2020-04-22 21:32:44 +00:00
|
|
|
pages.push( {
|
2020-06-15 02:17:12 +00:00
|
|
|
container: Homescreen,
|
2020-04-22 21:32:44 +00:00
|
|
|
path: '/',
|
|
|
|
breadcrumbs: [
|
|
|
|
...initialBreadcrumbs,
|
|
|
|
__( 'Home', 'woocommerce-admin' ),
|
|
|
|
],
|
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:02:02 +00:00
|
|
|
if ( window.wcAdminFeatures.analytics ) {
|
2020-06-15 02:17:12 +00:00
|
|
|
if ( window.wcAdminFeatures.homescreen ) {
|
2020-04-22 21:32:44 +00:00
|
|
|
pages.push( {
|
|
|
|
container: Dashboard,
|
|
|
|
path: '/analytics/overview',
|
|
|
|
breadcrumbs: [
|
|
|
|
...initialBreadcrumbs,
|
|
|
|
[
|
|
|
|
'/analytics/overview',
|
|
|
|
__( 'Analytics', 'woocommerce-admin' ),
|
|
|
|
],
|
|
|
|
__( 'Overview', 'woocommerce-admin' ),
|
|
|
|
],
|
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin-path--analytics-overview',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
const ReportWpOpenMenu = `toplevel_page_wc-admin-path--analytics-${
|
2020-06-15 02:17:12 +00:00
|
|
|
window.wcAdminFeatures.homescreen ? 'overview' : 'revenue'
|
2020-04-22 21:32:44 +00:00
|
|
|
}`;
|
|
|
|
|
2019-02-12 20:02:02 +00:00
|
|
|
pages.push( {
|
2019-01-31 01:04:11 +00:00
|
|
|
container: AnalyticsSettings,
|
|
|
|
path: '/analytics/settings',
|
2019-07-05 08:15:49 +00:00
|
|
|
breadcrumbs: [
|
2020-03-23 22:57:16 +00:00
|
|
|
...initialBreadcrumbs,
|
2020-02-14 02:23:21 +00:00
|
|
|
[
|
|
|
|
'/analytics/revenue',
|
|
|
|
__( 'Analytics', 'woocommerce-admin' ),
|
|
|
|
],
|
2019-07-05 08:15:49 +00:00
|
|
|
__( 'Settings', 'woocommerce-admin' ),
|
|
|
|
],
|
2020-04-22 21:32:44 +00:00
|
|
|
wpOpenMenu: ReportWpOpenMenu,
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
2020-02-20 11:59:02 +00:00
|
|
|
pages.push( {
|
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/customers',
|
2020-03-23 22:57:16 +00:00
|
|
|
breadcrumbs: [
|
|
|
|
...initialBreadcrumbs,
|
|
|
|
__( 'Customers', 'woocommerce-admin' ),
|
|
|
|
],
|
2020-02-20 11:59:02 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
} );
|
2019-02-12 20:02:02 +00:00
|
|
|
pages.push( {
|
2018-05-18 17:31:08 +00:00
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/analytics/:report',
|
2019-07-05 08:15:49 +00:00
|
|
|
breadcrumbs: ( { match } ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const report = find( getReports(), {
|
|
|
|
report: match.params.report,
|
|
|
|
} );
|
2019-07-05 08:15:49 +00:00
|
|
|
if ( ! report ) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-02-14 02:23:21 +00:00
|
|
|
return [
|
2020-03-23 22:57:16 +00:00
|
|
|
...initialBreadcrumbs,
|
2020-02-14 02:23:21 +00:00
|
|
|
[
|
|
|
|
'/analytics/revenue',
|
|
|
|
__( 'Analytics', 'woocommerce-admin' ),
|
|
|
|
],
|
|
|
|
report.title,
|
|
|
|
];
|
2019-07-05 08:15:49 +00:00
|
|
|
},
|
2020-04-22 21:32:44 +00:00
|
|
|
wpOpenMenu: ReportWpOpenMenu,
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
2018-05-18 17:31:08 +00:00
|
|
|
|
2020-03-28 13:48:27 +00:00
|
|
|
if ( window.wcAdminFeatures.marketing ) {
|
|
|
|
pages.push( {
|
|
|
|
container: MarketingOverview,
|
|
|
|
path: '/marketing',
|
|
|
|
breadcrumbs: [
|
|
|
|
...initialBreadcrumbs,
|
2020-06-12 21:55:15 +00:00
|
|
|
[
|
|
|
|
'/marketing',
|
|
|
|
__( 'Marketing', 'woocommerce-admin' ),
|
|
|
|
],
|
|
|
|
__( 'Overview', 'woocommerce-admin' ),
|
2020-03-28 13:48:27 +00:00
|
|
|
],
|
2020-06-12 21:55:15 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce-marketing',
|
2020-03-28 13:48:27 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:00:23 +00:00
|
|
|
return applyFilters( PAGES_FILTER, pages );
|
2018-05-18 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
2019-06-26 23:00:23 +00:00
|
|
|
export class Controller extends Component {
|
2019-04-08 17:53:28 +00:00
|
|
|
componentDidMount() {
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:55:06 +00:00
|
|
|
componentDidUpdate( prevProps ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const prevBaseQuery = omit(
|
2020-07-14 12:20:51 +00:00
|
|
|
prevProps.query,
|
2020-06-01 23:03:26 +00:00
|
|
|
'chartType',
|
|
|
|
'filter',
|
2020-02-14 02:23:21 +00:00
|
|
|
'paged'
|
|
|
|
);
|
|
|
|
const baseQuery = omit(
|
2020-07-14 12:20:51 +00:00
|
|
|
this.props.query,
|
2020-06-01 23:03:26 +00:00
|
|
|
'chartType',
|
|
|
|
'filter',
|
2020-02-14 02:23:21 +00:00
|
|
|
'paged'
|
|
|
|
);
|
2019-02-04 02:55:06 +00:00
|
|
|
|
2020-07-14 12:20:51 +00:00
|
|
|
if ( prevProps.query.paged > 1 && ! isEqual( prevBaseQuery, baseQuery ) ) {
|
2019-07-11 17:22:26 +00:00
|
|
|
getHistory().replace( getNewPath( { paged: 1 } ) );
|
2019-02-04 02:55:06 +00:00
|
|
|
}
|
2019-04-08 17:53:28 +00:00
|
|
|
|
|
|
|
if ( prevProps.match.url !== this.props.match.url ) {
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
|
|
|
}
|
2019-02-04 02:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
render() {
|
2020-07-14 12:20:51 +00:00
|
|
|
const { page, match, query } = this.props;
|
2019-06-13 19:58:21 +00:00
|
|
|
const { url, params } = match;
|
2019-04-11 18:31:31 +00:00
|
|
|
|
2019-11-28 20:54:15 +00:00
|
|
|
window.wpNavMenuUrlUpdate( query );
|
2019-06-13 19:58:21 +00:00
|
|
|
window.wpNavMenuClassChange( page, url );
|
2020-04-29 18:01:27 +00:00
|
|
|
return (
|
|
|
|
<Suspense fallback={ <Spinner /> }>
|
|
|
|
<page.container
|
|
|
|
params={ params }
|
|
|
|
path={ url }
|
|
|
|
pathMatch={ page.path }
|
|
|
|
query={ query }
|
|
|
|
/>
|
|
|
|
</Suspense>
|
|
|
|
);
|
2018-05-18 17:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 03:58:37 +00:00
|
|
|
/**
|
|
|
|
* Update an anchor's link in sidebar to include persisted queries. Leave excluded screens
|
|
|
|
* as is.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} item - Sidebar anchor link.
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} nextQuery - A query object to be added to updated hrefs.
|
2019-05-14 03:58:37 +00:00
|
|
|
* @param {Array} excludedScreens - wc-admin screens to avoid updating.
|
|
|
|
*/
|
|
|
|
export function updateLinkHref( item, nextQuery, excludedScreens ) {
|
2019-06-13 19:58:21 +00:00
|
|
|
const isWCAdmin = /admin.php\?page=wc-admin/.test( item.href );
|
|
|
|
|
|
|
|
if ( isWCAdmin ) {
|
|
|
|
const search = last( item.href.split( '?' ) );
|
|
|
|
const query = parse( search );
|
2020-06-03 20:42:30 +00:00
|
|
|
const defaultPath =
|
2020-06-15 02:17:12 +00:00
|
|
|
window.wcAdminFeatures.homescreen
|
|
|
|
? 'homescreen'
|
2020-06-03 20:42:30 +00:00
|
|
|
: 'dashboard';
|
2020-05-14 03:23:03 +00:00
|
|
|
const path = query.path || defaultPath;
|
2019-06-13 19:58:21 +00:00
|
|
|
const screen = path.replace( '/analytics', '' ).replace( '/', '' );
|
|
|
|
|
|
|
|
const isExcludedScreen = excludedScreens.includes( screen );
|
|
|
|
|
|
|
|
const href =
|
2020-02-14 02:23:21 +00:00
|
|
|
'admin.php?' +
|
|
|
|
stringify(
|
|
|
|
Object.assign( query, isExcludedScreen ? {} : nextQuery )
|
|
|
|
);
|
2019-06-13 19:58:21 +00:00
|
|
|
|
|
|
|
// Replace the href so you can see the url on hover.
|
|
|
|
item.href = href;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
item.onclick = ( e ) => {
|
2019-06-13 19:58:21 +00:00
|
|
|
e.preventDefault();
|
|
|
|
getHistory().push( href );
|
|
|
|
};
|
2019-05-14 03:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:58:21 +00:00
|
|
|
// Update's wc-admin links in wp-admin menu
|
2019-11-28 20:54:15 +00:00
|
|
|
window.wpNavMenuUrlUpdate = function( query ) {
|
2019-05-14 03:58:37 +00:00
|
|
|
const excludedScreens = applyFilters( TIME_EXCLUDED_SCREENS_FILTER, [
|
|
|
|
'devdocs',
|
|
|
|
'stock',
|
|
|
|
'settings',
|
|
|
|
'customers',
|
2020-06-15 02:17:12 +00:00
|
|
|
'homescreen',
|
2019-05-14 03:58:37 +00:00
|
|
|
] );
|
2019-06-13 19:58:21 +00:00
|
|
|
const nextQuery = getPersistedQuery( query );
|
2019-05-14 03:58:37 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
Array.from(
|
|
|
|
document.querySelectorAll( '#adminmenu a' )
|
|
|
|
).forEach( ( item ) => updateLinkHref( item, nextQuery, excludedScreens ) );
|
2018-10-18 04:37:28 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
// When the route changes, we need to update wp-admin's menu with the correct section & current link
|
2019-06-13 19:58:21 +00:00
|
|
|
window.wpNavMenuClassChange = function( page, url ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
Array.from( document.getElementsByClassName( 'current' ) ).forEach(
|
|
|
|
function( item ) {
|
|
|
|
item.classList.remove( 'current' );
|
|
|
|
}
|
|
|
|
);
|
2018-10-10 14:24:57 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const submenu = Array.from(
|
|
|
|
document.querySelectorAll( '.wp-has-current-submenu' )
|
|
|
|
);
|
2018-10-27 17:38:35 +00:00
|
|
|
submenu.forEach( function( element ) {
|
|
|
|
element.classList.remove( 'wp-has-current-submenu' );
|
|
|
|
element.classList.remove( 'wp-menu-open' );
|
|
|
|
element.classList.remove( 'selected' );
|
|
|
|
element.classList.add( 'wp-not-current-submenu' );
|
|
|
|
element.classList.add( 'menu-top' );
|
|
|
|
} );
|
2018-10-10 14:24:57 +00:00
|
|
|
|
2019-06-13 19:58:21 +00:00
|
|
|
const pageUrl =
|
2020-02-14 02:23:21 +00:00
|
|
|
url === '/'
|
2019-06-13 19:58:21 +00:00
|
|
|
? 'admin.php?page=wc-admin'
|
|
|
|
: 'admin.php?page=wc-admin&path=' + encodeURIComponent( url );
|
2018-10-29 15:43:30 +00:00
|
|
|
const currentItemsSelector =
|
2019-06-13 19:58:21 +00:00
|
|
|
url === '/'
|
|
|
|
? `li > a[href$="${ pageUrl }"], li > a[href*="${ pageUrl }?"]`
|
|
|
|
: `li > a[href*="${ pageUrl }"]`;
|
2018-10-29 15:43:30 +00:00
|
|
|
const currentItems = document.querySelectorAll( currentItemsSelector );
|
2018-10-18 04:37:28 +00:00
|
|
|
|
|
|
|
Array.from( currentItems ).forEach( function( item ) {
|
2018-10-10 16:06:32 +00:00
|
|
|
item.parentElement.classList.add( 'current' );
|
|
|
|
} );
|
2018-10-10 14:24:57 +00:00
|
|
|
|
2019-04-11 18:31:31 +00:00
|
|
|
if ( page.wpOpenMenu ) {
|
|
|
|
const currentMenu = document.querySelector( '#' + page.wpOpenMenu );
|
2020-06-10 21:56:01 +00:00
|
|
|
if ( currentMenu ) {
|
|
|
|
currentMenu.classList.remove( 'wp-not-current-submenu' );
|
|
|
|
currentMenu.classList.add( 'wp-has-current-submenu' );
|
|
|
|
currentMenu.classList.add( 'wp-menu-open' );
|
|
|
|
currentMenu.classList.add( 'current' );
|
|
|
|
}
|
2019-04-11 18:31:31 +00:00
|
|
|
}
|
2018-10-18 04:37:28 +00:00
|
|
|
|
2019-02-20 15:18:27 +00:00
|
|
|
const wpWrap = document.querySelector( '#wpwrap' );
|
|
|
|
wpWrap.classList.remove( 'wp-responsive-open' );
|
2018-05-18 17:31:08 +00:00
|
|
|
};
|