fields-map.js
161 lines
| 1 | |
| 2 | /** |
| 3 | * Internal dependencies |
| 4 | */ |
| 5 | const { |
| 6 | SelectControl, |
| 7 | TextControl |
| 8 | } = wp.components; |
| 9 | |
| 10 | |
| 11 | class JetFieldsMapControl extends wp.element.Component { |
| 12 | |
| 13 | constructor( props ) { |
| 14 | super( props ); |
| 15 | |
| 16 | this.fieldTypes = this.props.fieldTypes; |
| 17 | this.taxonomiesList = this.props.taxonomiesList; |
| 18 | this.className = this.props.className; |
| 19 | this.metaProp = this.props.metaProp ? this.props.metaProp : 'post_meta'; |
| 20 | this.termsProp = this.props.termsProp ? this.props.termsProp : 'post_terms'; |
| 21 | this.index = this.props.index; |
| 22 | |
| 23 | this.init(); |
| 24 | this.bindFunctions(); |
| 25 | |
| 26 | this.state = { |
| 27 | type: this.getFieldType( this.props.fieldValue ), |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | bindFunctions() { |
| 32 | this.onChangeType = this.onChangeType.bind( this ); |
| 33 | this.onChangeValue = this.onChangeValue.bind( this ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | init() { |
| 38 | this.id = `inspector-select-control-${ this.index }`; |
| 39 | this.preparedTaxes = []; |
| 40 | this.taxPrefix = 'jet_tax__'; |
| 41 | |
| 42 | if ( ! this.taxonomiesList ) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | for ( var i = 0; i < this.taxonomiesList.length; i ++ ) { |
| 47 | this.preparedTaxes.push( { |
| 48 | value: this.taxPrefix + this.taxonomiesList[ i ].value, |
| 49 | label: this.taxonomiesList[ i ].label, |
| 50 | } ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | getFieldName( value ) { |
| 55 | |
| 56 | if ( ! value ) { |
| 57 | return ''; |
| 58 | } |
| 59 | |
| 60 | const fieldType = this.getFieldType( value ); |
| 61 | |
| 62 | if ( this.termsProp === fieldType || this.metaProp === fieldType ) { |
| 63 | return value; |
| 64 | } |
| 65 | else { |
| 66 | return ''; |
| 67 | } |
| 68 | |
| 69 | }; |
| 70 | |
| 71 | isTermOrMeta( value ) { |
| 72 | return ( this.termsProp === value || this.metaProp === value ); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | getFieldType( value ) { |
| 77 | |
| 78 | if ( ! value ) { |
| 79 | return ''; |
| 80 | } |
| 81 | |
| 82 | for ( var i = 0; i < this.fieldTypes.length; i ++ ) { |
| 83 | if ( value === this.fieldTypes[ i ].value ) { |
| 84 | return value; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if ( value.includes( this.taxPrefix ) ) { |
| 89 | return this.termsProp; |
| 90 | } |
| 91 | else { |
| 92 | return this.metaProp; |
| 93 | } |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | onChangeValue( newValue ) { |
| 99 | this.props.onChange( { |
| 100 | ...this.props.fieldsMap, |
| 101 | [ this.props.fieldName ]: newValue |
| 102 | } ); |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | onChangeType( newValue ) { |
| 107 | let val = this.getFieldType( newValue ); |
| 108 | |
| 109 | this.setState( { |
| 110 | type: val |
| 111 | } ); |
| 112 | |
| 113 | if ( this.isTermOrMeta( val ) ) { |
| 114 | val = ''; |
| 115 | } |
| 116 | |
| 117 | this.onChangeValue( val ); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // Disable reason: A select with an onchange throws a warning |
| 122 | |
| 123 | /* eslint-disable jsx-a11y/no-onchange */ |
| 124 | render() { |
| 125 | return <div |
| 126 | className="jet-fields-map__row" |
| 127 | > |
| 128 | <div className="jet-post-field-control"> |
| 129 | <SelectControl |
| 130 | key={ 'field_type_' + this.props.fieldName + this.index } |
| 131 | label={ this.props.fieldName } |
| 132 | labelPosition="side" |
| 133 | value={ this.state.type } |
| 134 | onChange={ this.onChangeType } |
| 135 | options={ this.fieldTypes } |
| 136 | style={ { |
| 137 | width: '160px' |
| 138 | } } |
| 139 | /> |
| 140 | { ( this.metaProp === this.state.type ) && <TextControl |
| 141 | key={ 'field_name_' + this.props.fieldName + this.index } |
| 142 | value={ this.props.fieldValue } |
| 143 | onChange={ this.onChangeValue } |
| 144 | style={ { width: '200px' } } |
| 145 | /> } |
| 146 | { ( this.termsProp === this.state.type ) && <SelectControl |
| 147 | className='jet-control-without-label' |
| 148 | key={ 'field_tax_' + this.props.fieldName + this.index } |
| 149 | value={ this.props.fieldValue } |
| 150 | onChange={ this.onChangeValue } |
| 151 | options={ this.preparedTaxes } |
| 152 | style={ { width: '200px' } } |
| 153 | /> } |
| 154 | </div> |
| 155 | </div>; |
| 156 | } |
| 157 | |
| 158 | /* eslint-enable jsx-a11y/no-onchange */ |
| 159 | } |
| 160 | |
| 161 | export default JetFieldsMapControl; |