2018-05-18 17:31:08 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component, createElement } from '@wordpress/element';
|
|
|
|
import { parse } from 'qs';
|
2019-02-04 02:55:06 +00:00
|
|
|
import { find, last, isEqual } from 'lodash';
|
2019-05-14 03:58:37 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2018-05-18 17:31:08 +00:00
|
|
|
|
2018-11-05 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-02-06 19:28:29 +00:00
|
|
|
import { getNewPath, getPersistedQuery, getHistory, stringifyQuery } 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
|
|
|
*/
|
|
|
|
import Analytics from 'analytics';
|
|
|
|
import AnalyticsReport 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';
|
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
const getPages = () => {
|
2019-02-12 20:02:02 +00:00
|
|
|
const pages = [];
|
|
|
|
|
|
|
|
if ( window.wcAdminFeatures.devdocs ) {
|
|
|
|
pages.push( {
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs',
|
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
} );
|
|
|
|
pages.push( {
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs/:component',
|
|
|
|
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: '/',
|
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( {
|
2018-05-18 17:31:08 +00:00
|
|
|
container: Analytics,
|
|
|
|
path: '/analytics',
|
2018-10-29 15:43:30 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
pages.push( {
|
2019-01-31 01:04:11 +00:00
|
|
|
container: AnalyticsSettings,
|
|
|
|
path: '/analytics/settings',
|
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--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',
|
2018-10-29 15:43:30 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
2018-05-18 17:31:08 +00:00
|
|
|
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 );
|
|
|
|
const prevBaseQuery = this.getBaseQuery( prevProps.location.search );
|
|
|
|
const baseQuery = this.getBaseQuery( this.props.location.search );
|
|
|
|
|
|
|
|
if ( prevQuery.page > 1 && ! isEqual( prevBaseQuery, baseQuery ) ) {
|
2019-02-06 19:28:29 +00:00
|
|
|
getHistory().replace( getNewPath( { page: 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
getBaseQuery( searchString ) {
|
|
|
|
const query = this.getQuery( searchString );
|
|
|
|
delete query.page;
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
render() {
|
|
|
|
// Pass URL parameters (example :report -> params.report) and query string parameters
|
2018-06-26 14:59:35 +00:00
|
|
|
const { path, url, params } = this.props.match;
|
2019-02-04 02:55:06 +00:00
|
|
|
const query = this.getQuery( this.props.location.search );
|
2018-05-18 17:31:08 +00:00
|
|
|
const page = find( getPages(), { path } );
|
2019-04-11 18:31:31 +00:00
|
|
|
|
|
|
|
if ( ! page ) {
|
|
|
|
return null; // @todo What should we display or do when a route/page doesn't exist?
|
|
|
|
}
|
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
window.wpNavMenuUrlUpdate( page, query );
|
|
|
|
window.wpNavMenuClassChange( page );
|
2018-06-26 14:59:35 +00:00
|
|
|
return createElement( page.container, { params, path: url, pathMatch: 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.
|
|
|
|
* @param {string} nextQuery - A query string to be added to updated hrefs.
|
|
|
|
* @param {Array} excludedScreens - wc-admin screens to avoid updating.
|
|
|
|
*/
|
|
|
|
export function updateLinkHref( item, nextQuery, excludedScreens ) {
|
|
|
|
/**
|
|
|
|
* Regular expression for finding any WooCommerce Admin screen.
|
|
|
|
* The groupings are as follows:
|
|
|
|
*
|
|
|
|
* 0 - Full match
|
|
|
|
* 1 - "#/" (optional)
|
|
|
|
* 2 - "analytics/" (optional)
|
|
|
|
* 3 - Any string, eg "orders"
|
|
|
|
* 4 - "?" or end of line
|
|
|
|
*/
|
|
|
|
const _exp = /page=wc-admin(#\/)?(analytics\/)?(.*?)(\?|$)/;
|
|
|
|
const wcAdminMatches = item.href.match( _exp );
|
|
|
|
|
|
|
|
if ( wcAdminMatches ) {
|
|
|
|
// Get fourth grouping
|
|
|
|
const screen = wcAdminMatches[ 3 ];
|
|
|
|
|
|
|
|
if ( ! excludedScreens.includes( screen ) ) {
|
2018-10-18 04:37:28 +00:00
|
|
|
const url = item.href.split( 'wc-admin' );
|
|
|
|
const hashUrl = last( url );
|
|
|
|
const base = hashUrl.split( '?' )[ 0 ];
|
2019-05-14 03:58:37 +00:00
|
|
|
const href = `${ url[ 0 ] }wc-admin${ '#' === base[ 0 ] ? '' : '#/' }${ base }${ nextQuery }`;
|
2018-10-18 04:37:28 +00:00
|
|
|
item.href = href;
|
|
|
|
}
|
2019-05-14 03:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update links in wp-admin menu to persist time related queries
|
|
|
|
window.wpNavMenuUrlUpdate = function( page, query ) {
|
|
|
|
const excludedScreens = applyFilters( TIME_EXCLUDED_SCREENS_FILTER, [
|
|
|
|
'devdocs',
|
|
|
|
'stock',
|
|
|
|
'settings',
|
|
|
|
'customers',
|
|
|
|
] );
|
|
|
|
const nextQuery = stringifyQuery( getPersistedQuery( query ) );
|
|
|
|
|
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
|
2018-10-18 04:37:28 +00:00
|
|
|
window.wpNavMenuClassChange = function( page ) {
|
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
|
|
|
|
2018-10-27 17:38:35 +00:00
|
|
|
const pageHash = window.location.hash.split( '?' )[ 0 ];
|
2018-10-29 15:43:30 +00:00
|
|
|
const currentItemsSelector =
|
|
|
|
pageHash === '#/'
|
|
|
|
? `li > a[href$="${ pageHash }"], li > a[href*="${ pageHash }?"]`
|
|
|
|
: `li > a[href*="${ pageHash }"]`;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
export { Controller, getPages };
|