2018-05-10 18:35:55 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-11-02 19:37:28 +00:00
|
|
|
import { Component, findDOMNode } from '@wordpress/element';
|
2018-07-09 15:46:31 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2018-05-10 18:35:55 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2018-11-05 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-02-14 03:15:30 +00:00
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2018-11-05 21:02:04 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2019-11-22 17:07:26 +00:00
|
|
|
import { getAdminLink, getSetting } from '@woocommerce/wc-admin-settings';
|
2018-11-05 21:02:04 +00:00
|
|
|
|
2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-09-21 15:19:05 +00:00
|
|
|
import ActivityPanel from './activity-panel';
|
2019-07-09 19:10:49 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
class Header extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
isScrolled: false,
|
|
|
|
};
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
this.onWindowScroll = this.onWindowScroll.bind( this );
|
|
|
|
this.updateIsScrolled = this.updateIsScrolled.bind( this );
|
2019-07-09 19:10:49 +00:00
|
|
|
this.trackLinkClick = this.trackLinkClick.bind( this );
|
2018-07-09 15:46:31 +00:00
|
|
|
}
|
2018-05-24 16:03:03 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
componentDidMount() {
|
2018-11-02 19:37:28 +00:00
|
|
|
this.threshold = findDOMNode( this ).offsetTop;
|
2018-07-09 15:46:31 +00:00
|
|
|
window.addEventListener( 'scroll', this.onWindowScroll );
|
|
|
|
this.updateIsScrolled();
|
|
|
|
}
|
2018-05-24 16:03:03 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener( 'scroll', this.onWindowScroll );
|
|
|
|
window.cancelAnimationFrame( this.handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
onWindowScroll() {
|
|
|
|
this.handle = window.requestAnimationFrame( this.updateIsScrolled );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateIsScrolled() {
|
|
|
|
const isScrolled = window.pageYOffset > this.threshold - 20;
|
|
|
|
if ( isScrolled !== this.state.isScrolled ) {
|
|
|
|
this.setState( {
|
|
|
|
isScrolled: isScrolled,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 19:10:49 +00:00
|
|
|
trackLinkClick( event ) {
|
|
|
|
const href = event.target.closest( 'a' ).getAttribute( 'href' );
|
|
|
|
|
|
|
|
recordEvent( 'navbar_breadcrumb_click', { href, text: event.target.innerText } );
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
render() {
|
|
|
|
const { sections, isEmbedded } = this.props;
|
|
|
|
const { isScrolled } = this.state;
|
2018-09-03 15:09:09 +00:00
|
|
|
const _sections = Array.isArray( sections ) ? sections : [ sections ];
|
2018-07-09 15:46:31 +00:00
|
|
|
|
|
|
|
const documentTitle = _sections
|
|
|
|
.map( section => {
|
2018-09-03 15:09:09 +00:00
|
|
|
return Array.isArray( section ) ? section[ 1 ] : section;
|
2018-07-09 15:46:31 +00:00
|
|
|
} )
|
|
|
|
.reverse()
|
|
|
|
.join( ' ‹ ' );
|
|
|
|
|
|
|
|
document.title = decodeEntities(
|
2018-08-01 19:19:25 +00:00
|
|
|
sprintf(
|
2019-03-13 17:14:02 +00:00
|
|
|
__( '%1$s ‹ %2$s — WooCommerce', 'woocommerce-admin' ),
|
2018-08-01 19:19:25 +00:00
|
|
|
documentTitle,
|
2019-10-07 11:51:25 +00:00
|
|
|
getSetting( 'siteTitle', '' )
|
2018-08-01 19:19:25 +00:00
|
|
|
)
|
2018-07-09 15:46:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const className = classnames( 'woocommerce-layout__header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
|
|
|
|
2019-11-22 17:07:26 +00:00
|
|
|
const firstBreadCrumbPath = 'admin.php?page=wc-admin';
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
return (
|
|
|
|
<div className={ className }>
|
|
|
|
<h1 className="woocommerce-layout__header-breadcrumbs">
|
|
|
|
<span>
|
2019-07-09 19:10:49 +00:00
|
|
|
<Link
|
2019-11-22 17:07:26 +00:00
|
|
|
href={ isEmbedded ? getAdminLink( firstBreadCrumbPath ) : firstBreadCrumbPath }
|
2019-07-09 19:10:49 +00:00
|
|
|
type={ isEmbedded ? 'wp-admin' : 'wc-admin' }
|
|
|
|
onClick={ this.trackLinkClick }
|
|
|
|
>
|
2019-11-14 15:24:42 +00:00
|
|
|
{ __( 'WooCommerce', 'woocommerce-admin' ) }
|
2019-07-05 08:15:49 +00:00
|
|
|
</Link>
|
2018-07-09 15:46:31 +00:00
|
|
|
</span>
|
|
|
|
{ _sections.map( ( section, i ) => {
|
2018-09-03 15:09:09 +00:00
|
|
|
const sectionPiece = Array.isArray( section ) ? (
|
2018-11-02 21:38:16 +00:00
|
|
|
<Link
|
2019-11-22 17:07:26 +00:00
|
|
|
href={
|
|
|
|
isEmbedded ? getAdminLink( section[ 0 ] ) : getNewPath( {}, section[ 0 ], {} )
|
|
|
|
}
|
2018-11-02 21:38:16 +00:00
|
|
|
type={ isEmbedded ? 'wp-admin' : 'wc-admin' }
|
2019-07-09 19:10:49 +00:00
|
|
|
onClick={ this.trackLinkClick }
|
2018-11-02 21:38:16 +00:00
|
|
|
>
|
2018-07-09 15:46:31 +00:00
|
|
|
{ section[ 1 ] }
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
section
|
|
|
|
);
|
|
|
|
return <span key={ i }>{ sectionPiece }</span>;
|
|
|
|
} ) }
|
|
|
|
</h1>
|
2019-04-11 18:31:31 +00:00
|
|
|
{ window.wcAdminFeatures[ 'activity-panels' ] && <ActivityPanel /> }
|
2018-07-09 15:46:31 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 18:35:55 +00:00
|
|
|
|
|
|
|
Header.propTypes = {
|
|
|
|
sections: PropTypes.node.isRequired,
|
2018-06-26 14:49:42 +00:00
|
|
|
isEmbedded: PropTypes.bool,
|
2018-05-10 18:35:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Header.defaultProps = {
|
2018-06-26 14:49:42 +00:00
|
|
|
isEmbedded: false,
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2019-07-05 08:15:49 +00:00
|
|
|
export default Header;
|