2018-05-18 17:31:08 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component, createElement } from '@wordpress/element';
|
|
|
|
import { parse } from 'qs';
|
|
|
|
import { find } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
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-05-18 17:31:08 +00:00
|
|
|
|
|
|
|
const getPages = () => {
|
|
|
|
const pages = [
|
|
|
|
{
|
|
|
|
container: Dashboard,
|
|
|
|
path: '/',
|
2018-05-22 19:59:32 +00:00
|
|
|
wpMenu: 'toplevel_page_woocommerce',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: Analytics,
|
|
|
|
path: '/analytics',
|
2018-07-10 12:48:06 +00:00
|
|
|
wpMenu: 'toplevel_page_wc-admin--analytics',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/analytics/:report',
|
2018-07-10 12:48:06 +00:00
|
|
|
wpMenu: 'toplevel_page_wc-admin--analytics',
|
2018-05-18 17:31:08 +00:00
|
|
|
},
|
2018-09-24 15:36:35 +00:00
|
|
|
{
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs',
|
|
|
|
wpMenu: 'toplevel_page_woocommerce',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: DevDocs,
|
|
|
|
path: '/devdocs/:component',
|
|
|
|
wpMenu: 'toplevel_page_woocommerce',
|
|
|
|
},
|
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-09-05 18:37:26 +00:00
|
|
|
window.wpNavMenuClassChange( page.wpMenu, this.props.location.pathname );
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the route changes, we need to update wp-admin's menu with the correct section & current link
|
2018-09-05 18:37:26 +00:00
|
|
|
window.wpNavMenuClassChange = function( menuClass, pathname ) {
|
|
|
|
const path = '/' === pathname ? '' : '#' + pathname;
|
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' );
|
|
|
|
|
|
|
|
document
|
|
|
|
.querySelector( `li > a[href$="admin.php?page=wc-admin${ path }"]` )
|
|
|
|
.parentElement.classList.add( 'current' );
|
|
|
|
|
|
|
|
const currentMenu = document.querySelector( '#' + menuClass );
|
|
|
|
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-05-18 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { Controller, getPages };
|