const { __ } = wp.i18n; const { Toolbar, withAPIData, Dropdown, Dashicon } = wp.components; /** * Get the identifier for an attribute. The identifier can be used to determine * the slug or the ID of the attribute. * * @param string slug The attribute slug. * @param int|numeric string id The attribute ID. */ export function getAttributeIdentifier( slug, id ) { return slug + ',' + id; } /** * Get the attribute slug from an identifier. * * @param string identifier The attribute identifier. * @return string */ export function getAttributeSlug( identifier ) { return identifier.split( ',' )[0]; } /** * Get the attribute ID from an identifier. * * @param string identifier The attribute identifier. * @return numeric string */ export function getAttributeID( identifier ) { return identifier.split( ',' )[1]; } /** * When the display mode is 'Attribute' search for and select product attributes to pull products from. */ export class ProductsAttributeSelect extends React.Component { /** * Constructor. */ constructor( props ) { super( props ); /** * The first item in props.selected_display_setting is the attribute slug and id separated by a comma. * This is to work around limitations in the API which sometimes requires a slug and sometimes an id. * The rest of the elements in selected_display_setting are the term ids for any selected terms. */ this.state = { selectedAttribute: props.selected_display_setting.length ? props.selected_display_setting[0] : '', selectedTerms: props.selected_display_setting.length > 1 ? props.selected_display_setting.slice( 1 ) : [], filterQuery: '', } this.setSelectedAttribute = this.setSelectedAttribute.bind( this ); this.addTerm = this.addTerm.bind( this ); this.removeTerm = this.removeTerm.bind( this ); } /** * Set the selected attribute. * * @param identifier string Attribute slug and id separated by a comma. */ setSelectedAttribute( identifier ) { this.setState( { selectedAttribute: identifier, selectedTerms: [], } ); this.props.update_display_setting_callback( [ identifier ] ); } /** * Add a term to the selected attribute's terms. * * @param id int Term id. */ addTerm( id ) { let terms = this.state.selectedTerms; terms.push( id ); this.setState( { selectedTerms: terms, } ); let displaySetting = [ this.state.selectedAttribute ]; displaySetting = displaySetting.concat( terms ); this.props.update_display_setting_callback( displaySetting ); } /** * Remove a term from the selected attribute's terms. * * @param id int Term id. */ removeTerm( id ) { let newTerms = []; for ( let termId of this.state.selectedTerms ) { if ( termId !== id ) { newTerms.push( termId ); } } this.setState( { selectedTerms: newTerms, } ); let displaySetting = [ this.state.selectedAttribute ]; displaySetting = displaySetting.concat( newTerms ); this.props.update_display_setting_callback( displaySetting ); } /** * Update the search results when typing in the attributes box. * * @param evt Event object */ updateFilter( evt ) { this.setState( { filterQuery: evt.target.value, } ); } /** * Render the whole section. */ render() { return (