2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-02-14 02:23:21 +00:00
|
|
|
import { Component, createRef } 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
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
this.headerRef = createRef();
|
|
|
|
|
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 );
|
2020-01-20 17:34:24 +00:00
|
|
|
this.updateDocumentTitle = this.updateDocumentTitle.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() {
|
2020-02-14 02:23:21 +00:00
|
|
|
this.threshold = this.headerRef.current.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( {
|
2020-02-14 02:23:21 +00:00
|
|
|
isScrolled,
|
2018-07-09 15:46:31 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 19:10:49 +00:00
|
|
|
trackLinkClick( event ) {
|
|
|
|
const href = event.target.closest( 'a' ).getAttribute( 'href' );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent( 'navbar_breadcrumb_click', {
|
|
|
|
href,
|
|
|
|
text: event.target.innerText,
|
|
|
|
} );
|
2019-07-09 19:10:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 17:34:24 +00:00
|
|
|
updateDocumentTitle() {
|
2018-07-09 15:46:31 +00:00
|
|
|
const { sections, isEmbedded } = this.props;
|
2020-01-20 17:34:24 +00:00
|
|
|
|
|
|
|
// Don't modify the document title on existing WooCommerce pages.
|
|
|
|
if ( isEmbedded ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
2020-02-14 02:23:21 +00:00
|
|
|
.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(
|
2020-02-14 02:23:21 +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
|
|
|
);
|
2020-01-20 17:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { sections, isEmbedded } = this.props;
|
|
|
|
const { isScrolled } = this.state;
|
|
|
|
const _sections = Array.isArray( sections ) ? sections : [ sections ];
|
|
|
|
|
|
|
|
this.updateDocumentTitle();
|
2018-07-09 15:46:31 +00:00
|
|
|
|
|
|
|
const className = classnames( 'woocommerce-layout__header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
2020-02-14 02:23:21 +00:00
|
|
|
<div className={ className } ref={ this.headerRef }>
|
2018-07-09 15:46:31 +00:00
|
|
|
<h1 className="woocommerce-layout__header-breadcrumbs">
|
|
|
|
{ _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={
|
2020-02-14 02:23:21 +00:00
|
|
|
isEmbedded
|
|
|
|
? getAdminLink( section[ 0 ] )
|
|
|
|
: getNewPath( {}, section[ 0 ], {} )
|
2019-11-22 17:07:26 +00:00
|
|
|
}
|
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
|
|
|
|
);
|
2020-02-14 02:23:21 +00:00
|
|
|
return (
|
|
|
|
<span key={ i }>
|
|
|
|
{ decodeEntities( sectionPiece ) }
|
|
|
|
</span>
|
|
|
|
);
|
2018-07-09 15:46:31 +00:00
|
|
|
} ) }
|
|
|
|
</h1>
|
2020-02-14 02:23:21 +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;
|