2018-06-04 14:25:41 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-06-29 15:20:08 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-07-18 15:20:00 +00:00
|
|
|
import classnames from 'classnames';
|
2018-06-04 14:25:41 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-06-28 15:23:26 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2018-07-18 15:20:00 +00:00
|
|
|
import { IconButton } from '@wordpress/components';
|
|
|
|
import { partial, noop } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-06-04 14:25:41 +00:00
|
|
|
|
|
|
|
class WordPressNotices extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
count: 0,
|
|
|
|
notices: null,
|
2018-07-13 19:19:54 +00:00
|
|
|
screenLinks: null,
|
|
|
|
screenMeta: null,
|
2018-06-04 14:25:41 +00:00
|
|
|
noticesOpen: false,
|
|
|
|
hasEventListeners: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.updateCount = this.updateCount.bind( this );
|
|
|
|
this.showNotices = this.showNotices.bind( this );
|
|
|
|
this.hideNotices = this.hideNotices.bind( this );
|
2018-06-26 14:49:42 +00:00
|
|
|
this.initialize = this.initialize.bind( this );
|
2018-06-04 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-06-26 14:49:42 +00:00
|
|
|
this.handleWooCommerceEmbedPage();
|
|
|
|
if ( 'complete' === document.readyState ) {
|
|
|
|
this.initialize();
|
|
|
|
} else {
|
|
|
|
window.addEventListener( 'DOMContentLoaded', this.initialize );
|
2018-06-14 01:19:36 +00:00
|
|
|
}
|
2018-06-26 14:49:42 +00:00
|
|
|
}
|
2018-06-04 14:25:41 +00:00
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
componentDidUpdate( prevProps ) {
|
|
|
|
if ( ! prevProps.showNotices && this.props.showNotices ) {
|
|
|
|
this.showNotices();
|
|
|
|
this.maybeAddDismissEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( prevProps.showNotices && ! this.props.showNotices ) {
|
|
|
|
this.hideNotices();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 14:49:42 +00:00
|
|
|
handleWooCommerceEmbedPage() {
|
|
|
|
if ( ! document.body.classList.contains( 'woocommerce-embed-page' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-04 14:25:41 +00:00
|
|
|
|
2018-06-26 14:49:42 +00:00
|
|
|
// Existing WooCommerce pages already have a designted area for notices, using wp-header-end
|
|
|
|
// See https://github.com/WordPress/WordPress/blob/f6a37e7d39e2534d05b9e542045174498edfe536/wp-admin/js/common.js#L737
|
|
|
|
// We want to move most notices, but keep displaying others (like success notice) where they already are
|
|
|
|
// this renames the element in-line, so we can target it later.
|
|
|
|
const headerEnds = document.getElementsByClassName( 'wp-header-end' );
|
|
|
|
for ( let i = 0; i < headerEnds.length; i++ ) {
|
|
|
|
const headerEnd = headerEnds.item( i );
|
|
|
|
if ( 'woocommerce-layout__notice-catcher' !== headerEnd.id ) {
|
|
|
|
headerEnd.className = '';
|
|
|
|
headerEnd.id = 'wp__notice-list-uncollapsed';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
const notices = document.getElementById( 'wp__notice-list' );
|
|
|
|
const noticesOpen = notices.classList.contains( 'woocommerce-layout__notice-list-show' );
|
2018-07-13 19:19:54 +00:00
|
|
|
const screenMeta = document.getElementById( 'screen-meta' );
|
2018-06-26 14:49:42 +00:00
|
|
|
const screenLinks = document.getElementById( 'screen-meta-links' );
|
|
|
|
|
|
|
|
const collapsedTargetArea = document.getElementById( 'woocommerce-layout__notice-list' );
|
|
|
|
const uncollapsedTargetArea =
|
|
|
|
document.getElementById( 'wp__notice-list-uncollapsed' ) ||
|
|
|
|
document.getElementById( 'ajax-response' ) ||
|
|
|
|
document.getElementById( 'woocommerce-layout__notice-list' );
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
for ( let i = 0; i <= notices.children.length; i++ ) {
|
|
|
|
const notice = notices.children[ i ];
|
|
|
|
if ( ! notice ) {
|
|
|
|
continue;
|
|
|
|
} else if ( ! this.shouldCollapseNotice( notice ) ) {
|
|
|
|
uncollapsedTargetArea.insertAdjacentElement( 'afterend', notice );
|
|
|
|
} else {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count = count - 1; // Remove 1 for `wp-header-end` which is a child of wp__notice-list
|
|
|
|
|
2018-07-18 15:20:00 +00:00
|
|
|
this.props.onCountUpdate( count );
|
2018-07-13 19:19:54 +00:00
|
|
|
this.setState( { count, notices, noticesOpen, screenMeta, screenLinks } );
|
2018-06-26 14:49:42 +00:00
|
|
|
|
2018-07-10 12:48:06 +00:00
|
|
|
// Move collapsed WordPress notifications into the main wc-admin body
|
2018-06-26 14:49:42 +00:00
|
|
|
collapsedTargetArea.insertAdjacentElement( 'beforeend', notices );
|
2018-06-04 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document
|
|
|
|
.getElementById( 'wpbody-content' )
|
|
|
|
.insertAdjacentElement( 'afterbegin', this.state.notices );
|
|
|
|
|
|
|
|
const dismissNotices = document.getElementsByClassName( 'notice-dismiss' );
|
|
|
|
Object.keys( dismissNotices ).forEach( function( key ) {
|
|
|
|
dismissNotices[ key ].removeEventListener( 'click', this.updateCount );
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
this.setState( { noticesOpen: false, hasEventListeners: false } );
|
|
|
|
this.hideNotices();
|
|
|
|
}
|
|
|
|
|
2018-06-26 14:49:42 +00:00
|
|
|
// Some messages should not be displayed in the toggle, like Jetpack JITM messages or update/success messages
|
|
|
|
shouldCollapseNotice( element ) {
|
|
|
|
if ( element.classList.contains( 'jetpack-jitm-message' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'woocommerce_errors' === element.id ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( element.classList.contains( 'hidden' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'message' === element.id && element.classList.contains( 'notice' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'message' === element.id && 'updated' === element.className ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-04 14:25:41 +00:00
|
|
|
updateCount() {
|
2018-06-29 15:20:08 +00:00
|
|
|
const updatedCount = this.state.count - 1;
|
|
|
|
this.setState( { count: updatedCount } );
|
2018-07-18 15:20:00 +00:00
|
|
|
this.props.onCountUpdate( updatedCount );
|
2018-06-29 15:20:08 +00:00
|
|
|
|
|
|
|
if ( updatedCount < 1 ) {
|
|
|
|
this.props.togglePanel( 'wpnotices' ); // Close the panel since all of the notices have been closed.
|
|
|
|
}
|
2018-06-04 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maybeAddDismissEvents() {
|
|
|
|
if ( this.state.hasEventListeners ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dismiss = document.getElementsByClassName( 'notice-dismiss' );
|
|
|
|
Object.keys( dismiss ).forEach( function( key ) {
|
|
|
|
dismiss[ key ].addEventListener( 'click', this.updateCount );
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
this.setState( { hasEventListeners: true } );
|
|
|
|
}
|
|
|
|
|
|
|
|
showNotices() {
|
2018-07-13 19:19:54 +00:00
|
|
|
const { notices, screenLinks, screenMeta } = this.state;
|
|
|
|
notices.classList.add( 'woocommerce-layout__notice-list-show' );
|
|
|
|
notices.classList.remove( 'woocommerce-layout__notice-list-hide' );
|
|
|
|
screenMeta && screenMeta.classList.add( 'is-hidden-by-notices' );
|
|
|
|
screenLinks && screenLinks.classList.add( 'is-hidden-by-notices' );
|
2018-06-04 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hideNotices() {
|
2018-07-13 19:19:54 +00:00
|
|
|
const { notices, screenLinks, screenMeta } = this.state;
|
|
|
|
notices.classList.add( 'woocommerce-layout__notice-list-hide' );
|
|
|
|
notices.classList.remove( 'woocommerce-layout__notice-list-show' );
|
|
|
|
screenMeta && screenMeta.classList.remove( 'is-hidden-by-notices' );
|
|
|
|
screenLinks && screenLinks.classList.remove( 'is-hidden-by-notices' );
|
2018-06-04 14:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-06-29 15:20:08 +00:00
|
|
|
const { count } = this.state;
|
|
|
|
const { showNotices, togglePanel } = this.props;
|
2018-06-04 14:25:41 +00:00
|
|
|
|
|
|
|
if ( count < 1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
const className = classnames( 'woocommerce-layout__activity-panel-tab', {
|
2018-07-06 12:40:05 +00:00
|
|
|
'woocommerce-layout__activity-panel-tab-wordpress-notices': true,
|
2018-06-29 15:20:08 +00:00
|
|
|
'is-active': showNotices,
|
|
|
|
} );
|
|
|
|
|
2018-06-04 14:25:41 +00:00
|
|
|
return (
|
|
|
|
<IconButton
|
2018-06-29 15:20:08 +00:00
|
|
|
key="wpnotices"
|
|
|
|
className={ className }
|
|
|
|
onClick={ partial( togglePanel, 'wpnotices' ) }
|
2018-06-28 15:23:26 +00:00
|
|
|
icon={ <Gridicon icon="my-sites" /> }
|
2018-07-13 20:28:01 +00:00
|
|
|
role="tab"
|
|
|
|
tabIndex={ showNotices ? null : -1 }
|
2018-06-29 15:20:08 +00:00
|
|
|
>
|
2018-07-18 15:20:00 +00:00
|
|
|
{ __( 'Notices', 'wc-admin' ) }{' '}
|
|
|
|
<span className="screen-reader-text">{ __( 'unread activity', 'wc-admin' ) }</span>
|
2018-06-29 15:20:08 +00:00
|
|
|
</IconButton>
|
2018-06-04 14:25:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:20:00 +00:00
|
|
|
WordPressNotices.propTypes = {
|
|
|
|
showNotices: PropTypes.bool,
|
|
|
|
togglePanel: PropTypes.func,
|
|
|
|
onCountUpdate: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
WordPressNotices.defaultProps = {
|
|
|
|
togglePanel: noop,
|
|
|
|
onCountUpdate: noop,
|
|
|
|
};
|
|
|
|
|
2018-06-04 14:25:41 +00:00
|
|
|
export default WordPressNotices;
|