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';
|
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
|
|
|
|
|
|
|
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',
|
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
|
|
|
} );
|
|
|
|
pages.push( {
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs/:component',
|
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( window.wcAdminFeatures.dashboard ) {
|
|
|
|
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',
|
2018-10-29 15:43:30 +00:00
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
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',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpClosedMenu: 'toplevel_page_woocommerce',
|
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',
|
|
|
|
wpClosedMenu: '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',
|
2018-10-29 15:43:30 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--analytics-revenue',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpClosedMenu: 'toplevel_page_woocommerce',
|
2019-02-12 20:02:02 +00:00
|
|
|
} );
|
|
|
|
}
|
2018-05-18 17:31:08 +00:00
|
|
|
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Controller extends Component {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 } );
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
// Update links in wp-admin menu to persist time related queries
|
2018-10-25 10:07:30 +00:00
|
|
|
window.wpNavMenuUrlUpdate = function( page, query ) {
|
2018-11-14 01:45:05 +00:00
|
|
|
const search = stringifyQuery( getPersistedQuery( query ) );
|
2018-10-18 04:37:28 +00:00
|
|
|
|
|
|
|
Array.from(
|
|
|
|
document.querySelectorAll( `#${ page.wpOpenMenu } a, #${ page.wpClosedMenu } a` )
|
|
|
|
).forEach( item => {
|
|
|
|
/**
|
|
|
|
* Example hrefs:
|
|
|
|
*
|
|
|
|
* http://example.com/wp-admin/admin.php?page=wc-admin#/analytics/orders?period=today&compare=previous_year
|
|
|
|
* http://example.com/wp-admin/admin.php?page=wc-admin#/?period=week&compare=previous_year
|
|
|
|
* http://example.com/wp-admin/admin.php?page=wc-admin
|
|
|
|
*/
|
|
|
|
if ( item.href.includes( 'wc-admin' ) && ! item.href.includes( 'devdocs' ) ) {
|
|
|
|
const url = item.href.split( 'wc-admin' );
|
|
|
|
const hashUrl = last( url );
|
|
|
|
const base = hashUrl.split( '?' )[ 0 ];
|
|
|
|
const href = `${ url[ 0 ] }wc-admin${ '#' === base[ 0 ] ? '' : '#/' }${ base }${ search }`;
|
|
|
|
item.href = href;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
const currentMenu = document.querySelector( '#' + page.wpOpenMenu );
|
2018-10-10 14:24:57 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Sometimes navigating from the subMenu to Dashboard does not close subMenu
|
|
|
|
const closedMenu = document.querySelector( '#' + page.wpClosedMenu );
|
|
|
|
closedMenu.classList.remove( 'wp-has-current-submenu' );
|
|
|
|
closedMenu.classList.remove( 'wp-menu-open' );
|
|
|
|
closedMenu.classList.add( 'wp-not-current-submenu' );
|
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 };
|