2018-04-17 23:51:48 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { addFilter } from '@wordpress/hooks';
|
2018-05-15 15:06:15 +00:00
|
|
|
import { APIProvider } from '@wordpress/components';
|
|
|
|
import { Component, createElement, render } from '@wordpress/element';
|
|
|
|
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
|
|
|
|
import { parse } from 'qs';
|
|
|
|
import { pick, find } from 'lodash';
|
2018-04-17 23:51:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-05-16 14:42:39 +00:00
|
|
|
import './stylesheets/_wpadmin-reset.scss';
|
2018-05-15 15:06:15 +00:00
|
|
|
import Analytics from './analytics';
|
|
|
|
import AnalyticsReport from './analytics/report';
|
2018-04-17 23:51:48 +00:00
|
|
|
import Dashboard from './dashboard';
|
|
|
|
|
2018-05-15 15:06:15 +00:00
|
|
|
const getPages = () => {
|
|
|
|
const pages = [
|
|
|
|
{
|
|
|
|
container: Dashboard,
|
|
|
|
path: '/',
|
|
|
|
wpMenu: 'toplevel_page_woodash',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
container: Analytics,
|
|
|
|
path: '/analytics',
|
|
|
|
wpMenu: 'toplevel_page_woodash--analytics',
|
|
|
|
},
|
2018-05-14 13:41:30 +00:00
|
|
|
{
|
2018-05-15 15:06:15 +00:00
|
|
|
container: AnalyticsReport,
|
|
|
|
path: '/analytics/:report',
|
|
|
|
wpMenu: 'toplevel_page_woodash--analytics',
|
2018-05-14 13:41:30 +00:00
|
|
|
},
|
2018-05-15 15:06:15 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Controller extends Component {
|
|
|
|
render() {
|
|
|
|
// Pass URL parameters (example :report -> params.report) and query string parameters
|
|
|
|
const { path, params } = this.props.match;
|
|
|
|
const search = this.props.location.search.substring( 1 );
|
|
|
|
const query = parse( search );
|
|
|
|
const page = find( getPages(), { path } );
|
|
|
|
window.wpNavMenuClassChange( page.wpMenu );
|
|
|
|
return createElement( page.container, { params, query } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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' );
|
|
|
|
};
|
|
|
|
|
|
|
|
render(
|
|
|
|
<APIProvider
|
|
|
|
{ ...wpApiSettings }
|
|
|
|
{ ...pick( wp.api, [ 'postTypeRestBaseMapping', 'taxonomyRestBaseMapping' ] ) }
|
|
|
|
>
|
|
|
|
<Router>
|
|
|
|
<Switch>
|
|
|
|
{ getPages().map( page => {
|
|
|
|
return <Route path={ page.path } exact component={ Controller } />;
|
|
|
|
} ) }
|
|
|
|
<Route component={ Controller } />
|
|
|
|
</Switch>
|
|
|
|
</Router>
|
|
|
|
</APIProvider>,
|
2018-04-17 23:51:48 +00:00
|
|
|
document.getElementById( 'root' )
|
|
|
|
);
|
|
|
|
|
2018-05-03 18:38:12 +00:00
|
|
|
function editText( string ) {
|
2018-04-17 23:51:48 +00:00
|
|
|
return `Filtered: ${ string }`;
|
|
|
|
}
|
2018-05-03 18:38:12 +00:00
|
|
|
addFilter( 'woodash.example', 'editText', editText );
|