2018-05-18 17:31:08 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component, createElement } 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
|
|
|
|
*/
|
2019-06-13 19:58:21 +00:00
|
|
|
import { getNewPath, getPersistedQuery, getHistory } from '@woocommerce/navigation';
|
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
|
|
|
*/
|
2019-07-05 08:15:49 +00:00
|
|
|
import AnalyticsReport, { getReports } from 'analytics/report';
|
2019-01-31 01:04:11 +00:00
|
|
|
import AnalyticsSettings from 'analytics/settings';
|
2018-05-18 17:31:08 +00:00
|
|
|
import Dashboard from 'dashboard';
|
2018-09-24 15:36:35 +00:00
|
|
|
import DevDocs from 'devdocs';
|
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';
|
|
|
|
|
|
|
|
export const getPages = () => {
|
2019-02-12 20:02:02 +00:00
|
|
|
const pages = [];
|
|
|
|
|
|
|
|
if ( window.wcAdminFeatures.devdocs ) {
|
|
|
|
pages.push( {
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs',
|
2019-08-30 16:07:29 +00:00
|
|
|
breadcrumbs: ( { location } ) => {
|
|
|
|
const searchParams = new URLSearchParams( location.search );
|
|
|
|
const component = searchParams.get( 'component' );
|
|
|
|
|
|
|
|
if ( component ) {
|
|
|
|
return [ [ '/devdocs', 'Documentation' ], component ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ 'Documentation' ];
|
|
|
|
},
|
2019-02-12 20:02:02 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:10:05 +00:00
|
|
|
if ( window.wcAdminFeatures[ 'analytics-dashboard' ] ) {
|
2019-02-12 20:02:02 +00:00
|
|
|
pages.push( {
|
2018-05-18 17:31:08 +00:00
|
|
|
container: Dashboard,
|
|
|
|
path: '/',
|
2019-07-05 08:15:49 +00:00
|
|
|
breadcrumbs: [ __( 'Dashboard', 'woocommerce-admin' ) ],
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( window.wcAdminFeatures.analytics ) {
|
|
|
|
pages.push( {
|
2019-01-31 01:04:11 +00:00
|
|
|
container: AnalyticsSettings,
|
|
|
|
path: '/analytics/settings',
|
2019-07-05 08:15:49 +00:00
|
|
|
breadcrumbs: [
|
|
|
|
[ '/analytics/revenue', __( 'Analytics', 'woocommerce-admin' ) ],
|
|
|
|
__( 'Settings', 'woocommerce-admin' ),
|
|
|
|
],
|
2019-07-01 00:33:52 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin-path--analytics-revenue',
|
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 } ) => {
|
|
|
|
const report = find( getReports(), { report: match.params.report } );
|
|
|
|
if ( ! report ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [ [ '/analytics/revenue', __( 'Analytics', 'woocommerce-admin' ) ], report.title ];
|
|
|
|
},
|
2019-07-01 00:33:52 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin-path--analytics-revenue',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
2018-05-18 17:31:08 +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 ) {
|
|
|
|
const prevQuery = this.getQuery( prevProps.location.search );
|
2019-07-11 21:26:58 +00:00
|
|
|
const prevBaseQuery = omit( this.getQuery( prevProps.location.search ), 'paged' );
|
|
|
|
const baseQuery = omit( this.getQuery( this.props.location.search ), 'paged' );
|
2019-02-04 02:55:06 +00:00
|
|
|
|
2019-07-11 17:22:26 +00:00
|
|
|
if ( prevQuery.paged > 1 && ! isEqual( prevBaseQuery, baseQuery ) ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
getQuery( searchString ) {
|
|
|
|
if ( ! searchString ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const search = searchString.substring( 1 );
|
|
|
|
return parse( search );
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
render() {
|
2019-06-13 19:58:21 +00:00
|
|
|
const { page, match, location } = this.props;
|
|
|
|
const { url, params } = match;
|
2019-07-11 21:26:58 +00:00
|
|
|
const query = this.getQuery( location.search );
|
2019-04-11 18:31:31 +00:00
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
window.wpNavMenuUrlUpdate( page, query );
|
2019-06-13 19:58:21 +00:00
|
|
|
window.wpNavMenuClassChange( page, url );
|
|
|
|
return createElement( page.container, { params, path: url, pathMatch: page.path, query } );
|
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.
|
2019-06-13 19:58: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 );
|
|
|
|
const path = query.path || 'dashboard';
|
|
|
|
const screen = path.replace( '/analytics', '' ).replace( '/', '' );
|
|
|
|
|
|
|
|
const isExcludedScreen = excludedScreens.includes( screen );
|
|
|
|
|
|
|
|
const href =
|
|
|
|
'admin.php?' + stringify( Object.assign( query, isExcludedScreen ? {} : nextQuery ) );
|
|
|
|
|
|
|
|
// Replace the href so you can see the url on hover.
|
|
|
|
item.href = href;
|
|
|
|
|
|
|
|
item.onclick = e => {
|
|
|
|
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-05-14 03:58:37 +00:00
|
|
|
window.wpNavMenuUrlUpdate = function( page, query ) {
|
|
|
|
const excludedScreens = applyFilters( TIME_EXCLUDED_SCREENS_FILTER, [
|
|
|
|
'devdocs',
|
|
|
|
'stock',
|
|
|
|
'settings',
|
|
|
|
'customers',
|
|
|
|
] );
|
2019-06-13 19:58:21 +00:00
|
|
|
const nextQuery = getPersistedQuery( query );
|
2019-05-14 03:58:37 +00:00
|
|
|
|
2019-06-24 07:43:19 +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 ) {
|
2018-10-10 14:24:57 +00:00
|
|
|
Array.from( document.getElementsByClassName( 'current' ) ).forEach( function( item ) {
|
|
|
|
item.classList.remove( 'current' );
|
2018-05-18 17:31:08 +00:00
|
|
|
} );
|
2018-10-10 14:24:57 +00:00
|
|
|
|
2018-10-27 17:38:35 +00:00
|
|
|
const submenu = Array.from( document.querySelectorAll( '.wp-has-current-submenu' ) );
|
|
|
|
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 =
|
|
|
|
'/' === url
|
|
|
|
? '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 );
|
|
|
|
currentMenu.classList.remove( 'wp-not-current-submenu' );
|
|
|
|
currentMenu.classList.add( 'wp-has-current-submenu' );
|
|
|
|
currentMenu.classList.add( 'wp-menu-open' );
|
|
|
|
currentMenu.classList.add( 'current' );
|
|
|
|
}
|
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
|
|
|
};
|