AddQueryParameterButton.jsx
1 year ago
BlockSettings.jsx
1 year ago
ControlBuilder.jsx
1 year ago
DateQueryControl.jsx
1 year ago
DateTimeControl.jsx
1 year ago
ParameterControl.jsx
1 year ago
ParameterList.jsx
1 year ago
QueryInspectorControls.jsx
1 year ago
SelectQueryParameter.jsx
1 year ago
TaxonomyParameterControl.jsx
1 year ago
editor.scss
1 year ago
ControlBuilder.jsx
152 lines
| 1 | import { ToggleControl, Button, Tooltip, ComboboxControl, TextControl } from '@wordpress/components'; |
| 2 | import { sprintf, __ } from '@wordpress/i18n'; |
| 3 | |
| 4 | import { isArray, isObject } from 'lodash'; |
| 5 | import { SelectPostType, SelectPost, MultiSelect, SelectUser } from '@edge22/components'; |
| 6 | |
| 7 | import { TaxonomyParameterControl } from './TaxonomyParameterControl'; |
| 8 | import { DateQueryControl } from './DateQueryControl'; |
| 9 | |
| 10 | import getIcon from '@utils/get-icon'; |
| 11 | |
| 12 | function ControlComponent( props ) { |
| 13 | const { |
| 14 | postType, |
| 15 | queryClient, |
| 16 | ...standardProps |
| 17 | } = props; |
| 18 | |
| 19 | switch ( props?.type ) { |
| 20 | case 'text': |
| 21 | return <TextControl { ...standardProps } />; |
| 22 | case 'number': |
| 23 | return ( |
| 24 | <TextControl |
| 25 | { ...standardProps } |
| 26 | value={ standardProps.value } |
| 27 | type="number" |
| 28 | min="-1" |
| 29 | onChange={ ( newValue ) => { |
| 30 | if ( parseInt( newValue, 10 ) < -1 ) { |
| 31 | standardProps.onChange( -1 ); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | standardProps.onChange( newValue ); |
| 36 | } } |
| 37 | /> |
| 38 | ); |
| 39 | case 'postTypeSelect': |
| 40 | return <SelectPostType multiple={ true } { ...standardProps } />; |
| 41 | case 'select': |
| 42 | return <ComboboxControl { ...standardProps } />; |
| 43 | case 'multiSelect': |
| 44 | return <MultiSelect { ...standardProps } />; |
| 45 | case 'authorsSelect': |
| 46 | return ( |
| 47 | <SelectUser |
| 48 | { ...standardProps } |
| 49 | multiple={ true } |
| 50 | currentLabel={ __( 'Current author', 'generateblocks' ) } |
| 51 | queryClient={ queryClient } |
| 52 | /> |
| 53 | ); |
| 54 | case 'taxonomySelect': |
| 55 | return <TaxonomyParameterControl postType={ postType } { ...standardProps } />; |
| 56 | case 'includePosts': |
| 57 | return ( |
| 58 | <SelectPost |
| 59 | { ...standardProps } |
| 60 | includeCurrent={ false } |
| 61 | multiple={ true } |
| 62 | postType={ postType } |
| 63 | queryClient={ queryClient } |
| 64 | /> |
| 65 | ); |
| 66 | case 'excludePosts': |
| 67 | return ( |
| 68 | <SelectPost |
| 69 | { ...standardProps } |
| 70 | multiple={ true } |
| 71 | postType={ postType } |
| 72 | queryClient={ queryClient } |
| 73 | /> |
| 74 | ); |
| 75 | case 'dateQuery': |
| 76 | return <DateQueryControl { ...standardProps } />; |
| 77 | case 'toggleControl': |
| 78 | return <ToggleControl { ...standardProps } />; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export function ControlBuilder( props ) { |
| 83 | const { |
| 84 | id, |
| 85 | type, |
| 86 | label, |
| 87 | description, |
| 88 | selectOptions = [], |
| 89 | isSticky, |
| 90 | value, |
| 91 | default: defaultValue, |
| 92 | onChange, |
| 93 | onClickRemove, |
| 94 | dependencies, |
| 95 | placeholder, |
| 96 | postType, |
| 97 | queryClient, |
| 98 | } = props; |
| 99 | |
| 100 | let controlDescription = description; |
| 101 | |
| 102 | if ( 'posts_per_page' === id && ( '-1' === value || parseInt( value ) > parseInt( generateBlocksInfo.queryLoopEditorPostsCap ) ) ) { |
| 103 | controlDescription += ' ' + sprintf( |
| 104 | 'Editor only: A maximum of %s posts can be previewed in the editor.', |
| 105 | generateBlocksInfo.queryLoopEditorPostsCap |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | const defaultValuePlaceholder = !! defaultValue && ( ! isArray( defaultValue ) || ! isObject( defaultValue ) ) |
| 110 | ? defaultValue |
| 111 | : undefined; |
| 112 | |
| 113 | const controlPlaceholder = placeholder || defaultValuePlaceholder; |
| 114 | const isPostsPerPage = 'number' === type && 'posts_per_page' === id; |
| 115 | |
| 116 | const controlProps = { |
| 117 | id, |
| 118 | type, |
| 119 | label, |
| 120 | help: controlDescription, |
| 121 | options: selectOptions, |
| 122 | value, |
| 123 | placeholder: controlPlaceholder, |
| 124 | onChange, |
| 125 | min: isPostsPerPage ? -1 : undefined, |
| 126 | postType, |
| 127 | queryClient, |
| 128 | ...dependencies, |
| 129 | }; |
| 130 | |
| 131 | return ( |
| 132 | <div className={ 'gb-parameter-component' }> |
| 133 | <ControlComponent { ...controlProps } /> |
| 134 | { ! isSticky && ( |
| 135 | <Tooltip text={ __( 'Delete parameter', 'generateblocks-pro' ) }> |
| 136 | <Button |
| 137 | className="gb-remove-parameter" |
| 138 | onClick={ () => { |
| 139 | // eslint-disable-next-line |
| 140 | if ( window.confirm( __( 'This will permanently delete this parameter.', 'generateblocks' ) ) ) { |
| 141 | onClickRemove( id ); |
| 142 | } |
| 143 | } } |
| 144 | icon={ getIcon( 'trash' ) } |
| 145 | /> |
| 146 | </Tooltip> |
| 147 | ) } |
| 148 | </div> |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 |