2018-02-15 18:16:14 +00:00
|
|
|
const { __ } = wp.i18n;
|
2018-02-19 19:57:37 +00:00
|
|
|
const { Toolbar, withAPIData, Dropdown, Dashicon } = wp.components;
|
2018-02-15 18:16:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When the display mode is 'Product category' search for and select product categories to pull products from.
|
|
|
|
*/
|
|
|
|
export class ProductsCategorySelect extends React.Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
selectedCategories: props.selected_display_setting,
|
2018-02-21 13:58:20 +00:00
|
|
|
openAccordion: [],
|
2018-02-19 19:57:37 +00:00
|
|
|
filterQuery: '',
|
2018-02-21 13:58:20 +00:00
|
|
|
firstLoad: true,
|
2018-02-15 18:16:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:57:37 +00:00
|
|
|
this.checkboxChange = this.checkboxChange.bind( this );
|
|
|
|
this.accordionToggle = this.accordionToggle.bind( this );
|
|
|
|
this.filterResults = this.filterResults.bind( this );
|
2018-02-21 13:58:20 +00:00
|
|
|
this.setFirstLoad = this.setFirstLoad.bind( this );
|
2018-02-15 18:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle checkbox toggle.
|
|
|
|
*
|
2018-02-20 17:00:43 +00:00
|
|
|
* @param Checked? boolean checked
|
|
|
|
* @param Categories array categories
|
2018-02-15 18:16:14 +00:00
|
|
|
*/
|
2018-02-20 17:00:43 +00:00
|
|
|
checkboxChange( checked, categories ) {
|
2018-02-15 18:16:14 +00:00
|
|
|
let selectedCategories = this.state.selectedCategories;
|
|
|
|
|
2018-02-20 17:00:43 +00:00
|
|
|
selectedCategories = selectedCategories.filter( category => ! categories.includes( category ) );
|
|
|
|
|
|
|
|
if ( checked ) {
|
|
|
|
selectedCategories.push( ...categories );
|
2018-02-15 18:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
selectedCategories: selectedCategories
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.props.update_display_setting_callback( selectedCategories );
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:07:32 +00:00
|
|
|
/**
|
|
|
|
* Handle accordion toggle.
|
|
|
|
*
|
|
|
|
* @param Category ID category
|
|
|
|
*/
|
2018-02-19 19:57:37 +00:00
|
|
|
accordionToggle( category ) {
|
2018-02-21 13:58:20 +00:00
|
|
|
let openAccordions = this.state.openAccordion;
|
2018-02-19 19:57:37 +00:00
|
|
|
|
2018-02-21 13:58:20 +00:00
|
|
|
if ( openAccordions.includes( category ) ) {
|
|
|
|
openAccordions = openAccordions.filter( c => c !== category );
|
|
|
|
} else {
|
|
|
|
openAccordions.push( category );
|
2018-02-19 19:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState( {
|
2018-02-21 13:58:20 +00:00
|
|
|
openAccordion: openAccordions
|
2018-02-19 19:57:37 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:16:14 +00:00
|
|
|
/**
|
|
|
|
* Filter categories.
|
|
|
|
*
|
|
|
|
* @param Event object evt
|
|
|
|
*/
|
|
|
|
filterResults( evt ) {
|
|
|
|
this.setState( {
|
|
|
|
filterQuery: evt.target.value
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-02-21 13:58:20 +00:00
|
|
|
/**
|
|
|
|
* Update firstLoad state.
|
|
|
|
*
|
|
|
|
* @param Booolean loaded
|
|
|
|
*/
|
|
|
|
setFirstLoad( loaded ) {
|
|
|
|
this.setState( {
|
|
|
|
firstLoad: !! loaded
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:16:14 +00:00
|
|
|
/**
|
|
|
|
* Render the list of categories and the search input.
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
return (
|
2018-02-26 17:10:45 +00:00
|
|
|
<div className="wc-products-list-card wc-products-list-card--taxonomy wc-products-list-card--taxonomy-category">
|
2018-02-15 18:16:14 +00:00
|
|
|
<ProductCategoryFilter filterResults={ this.filterResults } />
|
2018-02-19 19:57:37 +00:00
|
|
|
<ProductCategoryList
|
|
|
|
filterQuery={ this.state.filterQuery }
|
|
|
|
selectedCategories={ this.state.selectedCategories }
|
|
|
|
checkboxChange={ this.checkboxChange }
|
|
|
|
accordionToggle={ this.accordionToggle }
|
|
|
|
openAccordion={ this.state.openAccordion }
|
2018-02-21 13:58:20 +00:00
|
|
|
firstLoad={ this.state.firstLoad }
|
|
|
|
setFirstLoad={ this.setFirstLoad }
|
2018-02-19 19:57:37 +00:00
|
|
|
/>
|
2018-02-15 18:16:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The category search input.
|
|
|
|
*/
|
|
|
|
const ProductCategoryFilter = ( { filterResults } ) => {
|
|
|
|
return (
|
|
|
|
<div>
|
2018-02-26 17:10:45 +00:00
|
|
|
<input className="wc-products-list-card__search" type="search" placeholder={ __( 'Search for categories' ) } onChange={ filterResults } />
|
2018-02-15 18:16:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and build a tree of product categories.
|
|
|
|
*/
|
|
|
|
const ProductCategoryList = withAPIData( ( props ) => {
|
|
|
|
return {
|
|
|
|
categories: '/wc/v2/products/categories'
|
|
|
|
};
|
2018-02-21 13:58:20 +00:00
|
|
|
} )( ( { categories, filterQuery, selectedCategories, checkboxChange, accordionToggle, openAccordion, firstLoad, setFirstLoad } ) => {
|
2018-02-15 18:16:14 +00:00
|
|
|
if ( ! categories.data ) {
|
|
|
|
return __( 'Loading' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 0 === categories.data.length ) {
|
|
|
|
return __( 'No categories found' );
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:00:43 +00:00
|
|
|
const handleCategoriesToCheck = ( evt, parent, categories ) => {
|
|
|
|
let ids = getCategoryChildren( parent, categories ).map( category => {
|
|
|
|
return category.id;
|
|
|
|
} );
|
|
|
|
|
|
|
|
ids.push( parent.id );
|
|
|
|
|
|
|
|
checkboxChange( evt.target.checked, ids );
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCategoryChildren = ( parent, categories ) => {
|
|
|
|
let children = [];
|
|
|
|
|
|
|
|
categories.filter( ( category ) => category.parent === parent.id ).forEach( function( category ) {
|
|
|
|
children.push( category );
|
|
|
|
children.push( ...getCategoryChildren( category, categories ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return children;
|
|
|
|
};
|
|
|
|
|
|
|
|
const categoryHasChildren = ( parent, categories ) => {
|
|
|
|
return !! getCategoryChildren( parent, categories ).length;
|
|
|
|
};
|
|
|
|
|
2018-02-21 13:58:20 +00:00
|
|
|
const isIndeterminate = ( category, categories ) => {
|
|
|
|
|
|
|
|
// Currect category selected?
|
|
|
|
if ( selectedCategories.includes( category.id ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has children?
|
|
|
|
let children = getCategoryChildren( category, categories ).map( category => {
|
|
|
|
return category.id;
|
|
|
|
} );
|
|
|
|
|
|
|
|
for ( let child of children ) {
|
|
|
|
if ( selectedCategories.includes( child ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:00:43 +00:00
|
|
|
const AccordionButton = ( { category, categories } ) => {
|
2018-02-19 19:57:37 +00:00
|
|
|
let icon = 'arrow-down-alt2';
|
|
|
|
|
2018-02-21 13:58:20 +00:00
|
|
|
if ( openAccordion.includes( category.id ) ) {
|
2018-02-19 19:57:37 +00:00
|
|
|
icon = 'arrow-up-alt2';
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:00:43 +00:00
|
|
|
let style = null;
|
|
|
|
|
|
|
|
if ( ! categoryHasChildren( category, categories ) ) {
|
|
|
|
style = {
|
|
|
|
visibility: 'hidden',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-02-19 19:57:37 +00:00
|
|
|
return (
|
2018-02-26 17:10:45 +00:00
|
|
|
<button onClick={ () => accordionToggle( category.id ) } className="wc-products-list-card__accordion-button" style={ style } type="button">
|
2018-02-19 19:57:37 +00:00
|
|
|
<Dashicon icon={ icon } />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-02-15 18:16:14 +00:00
|
|
|
const CategoryTree = ( { categories, parent } ) => {
|
|
|
|
let filteredCategories = categories.filter( ( category ) => category.parent === parent );
|
|
|
|
|
2018-02-21 13:58:20 +00:00
|
|
|
if ( firstLoad && selectedCategories.length > 0 ) {
|
|
|
|
categoriesData.filter( ( category ) => category.parent === 0 ).forEach( function( category ) {
|
|
|
|
let children = getCategoryChildren( category, categoriesData );
|
|
|
|
|
|
|
|
for ( let child of children ) {
|
|
|
|
if ( selectedCategories.includes( child.id ) && ! openAccordion.includes( category.id ) ) {
|
|
|
|
accordionToggle( category.id );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
setFirstLoad( false );
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:16:14 +00:00
|
|
|
return ( filteredCategories.length > 0 ) && (
|
|
|
|
<ul>
|
|
|
|
{ filteredCategories.map( ( category ) => (
|
2018-02-26 17:10:45 +00:00
|
|
|
<li key={ category.id } className={ ( openAccordion.includes( category.id ) ? 'wc-products-list-card__item wc-products-list-card__accordion-open' : 'wc-products-list-card__item' ) }>
|
|
|
|
<label className={ ( 0 === category.parent ) ? 'wc-products-list-card__content' : '' } htmlFor={ 'product-category-' + category.id }>
|
2018-02-15 18:16:14 +00:00
|
|
|
<input type="checkbox"
|
|
|
|
id={ 'product-category-' + category.id }
|
|
|
|
value={ category.id }
|
|
|
|
checked={ selectedCategories.includes( category.id ) }
|
2018-02-20 17:00:43 +00:00
|
|
|
onChange={ ( evt ) => handleCategoriesToCheck( evt, category, categories ) }
|
2018-02-21 13:58:20 +00:00
|
|
|
ref={ el => el && ( el.indeterminate = isIndeterminate( category, categories ) ) }
|
2018-02-15 18:16:14 +00:00
|
|
|
/> { category.name }
|
2018-02-19 19:57:37 +00:00
|
|
|
{ 0 === category.parent &&
|
2018-02-20 17:00:43 +00:00
|
|
|
<AccordionButton category={ category } categories={ categories } />
|
2018-02-19 19:57:37 +00:00
|
|
|
}
|
2018-02-26 17:10:45 +00:00
|
|
|
<span className="wc-products-list-card__taxonomy-count">{ category.count }</span>
|
2018-02-15 18:16:14 +00:00
|
|
|
</label>
|
|
|
|
<CategoryTree categories={ categories } parent={ category.id } />
|
|
|
|
</li>
|
|
|
|
) ) }
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let categoriesData = categories.data;
|
|
|
|
|
|
|
|
if ( '' !== filterQuery ) {
|
|
|
|
categoriesData = categoriesData.filter( category => category.slug.includes( filterQuery.toLowerCase() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2018-02-26 17:10:45 +00:00
|
|
|
<div className="wc-products-list-card__results">
|
2018-02-15 18:16:14 +00:00
|
|
|
<CategoryTree categories={ categoriesData } parent={ 0 } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|