2018-05-18 17:31:08 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component, createElement } from '@wordpress/element';
|
|
|
|
import { parse } from 'qs';
|
2018-10-25 10:07:30 +00:00
|
|
|
import { find, last } from 'lodash';
|
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';
|
|
|
|
import Dashboard from 'dashboard';
|
2018-09-24 15:36:35 +00:00
|
|
|
import DevDocs from 'devdocs';
|
2018-10-25 10:07:30 +00:00
|
|
|
import { getTimeRelatedQuery, stringifyQuery } from 'lib/nav-utils';
|
2018-05-18 17:31:08 +00:00
|
|
|
|
|
|
|
const getPages = () => {
|
|
|
|
const pages = [
|
|
|
|
{
|
|
|
|
container: Dashboard,
|
|
|
|
path: '/',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: Analytics,
|
|
|
|
path: '/analytics',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--analytics',
|
|
|
|
wpClosedMenu: 'toplevel_page_woocommerce',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/analytics/:report',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_wc-admin--analytics',
|
|
|
|
wpClosedMenu: 'toplevel_page_woocommerce',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
2018-09-24 15:36:35 +00:00
|
|
|
{
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics',
|
2018-09-24 15:36:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs/:component',
|
2018-10-18 04:37:28 +00:00
|
|
|
wpOpenMenu: 'toplevel_page_woocommerce',
|
|
|
|
wpClosedMenu: 'toplevel_page_wc-admin--analytics',
|
2018-09-24 15:36:35 +00:00
|
|
|
},
|
2018-05-18 17:31:08 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Controller extends Component {
|
|
|
|
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;
|
2018-05-18 17:31:08 +00:00
|
|
|
const search = this.props.location.search.substring( 1 );
|
|
|
|
const query = parse( search );
|
|
|
|
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 ) {
|
|
|
|
const search = stringifyQuery( getTimeRelatedQuery( 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
|
|
|
|
|
|
|
const submenu = document.querySelector( '.wp-has-current-submenu' );
|
|
|
|
submenu.classList.remove( 'wp-has-current-submenu' );
|
|
|
|
submenu.classList.remove( 'wp-menu-open' );
|
|
|
|
submenu.classList.remove( 'selected' );
|
|
|
|
submenu.classList.add( 'wp-not-current-submenu' );
|
|
|
|
submenu.classList.add( 'menu-top' );
|
|
|
|
|
2018-10-18 04:37:28 +00:00
|
|
|
const currentItems = document.querySelectorAll( `li > a[href="${ window.location.href }"]` );
|
|
|
|
|
|
|
|
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' );
|
2018-05-18 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { Controller, getPages };
|