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';
|
|
|
|
|
|
|
|
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',
|
|
|
|
wpMenu: 'toplevel_page_woodash--analytics',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/analytics/:report',
|
|
|
|
wpMenu: 'toplevel_page_woodash--analytics',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
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 } );
|
|
|
|
window.wpNavMenuClassChange( page.wpMenu );
|
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
|
|
|
|
window.wpNavMenuClassChange = function( menuClass ) {
|
|
|
|
jQuery( '.current' ).each( function( i, obj ) {
|
|
|
|
jQuery( obj ).removeClass( 'current' );
|
|
|
|
} );
|
|
|
|
jQuery( '.wp-has-current-submenu' )
|
|
|
|
.removeClass( 'wp-has-current-submenu' )
|
|
|
|
.removeClass( 'wp-menu-open' )
|
|
|
|
.removeClass( 'selected' )
|
|
|
|
.addClass( 'wp-not-current-submenu menu-top' );
|
|
|
|
jQuery( 'li > a[href$="admin.php?page=woodash' + window.location.hash + '"]' )
|
|
|
|
.parent()
|
|
|
|
.addClass( 'current' );
|
|
|
|
jQuery( '#' + menuClass )
|
|
|
|
.removeClass( 'wp-not-current-submenu' )
|
|
|
|
.addClass( 'wp-has-current-submenu wp-menu-open current' );
|
|
|
|
};
|
|
|
|
|
|
|
|
export { Controller, getPages };
|