inspector.js
286 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import {__} from '@wordpress/i18n' |
| 5 | import { select } from '@wordpress/data'; |
| 6 | import {InspectorControls, store } from '@wordpress/block-editor'; |
| 7 | import { |
| 8 | ColorPalette, |
| 9 | FormTokenField, |
| 10 | PanelBody, |
| 11 | SelectControl, |
| 12 | TextControl, |
| 13 | ToggleControl |
| 14 | } from '@wordpress/components'; |
| 15 | |
| 16 | import ColumnSelector from '../../components/column-selector'; |
| 17 | |
| 18 | /** |
| 19 | * Internal dependencies |
| 20 | */ |
| 21 | import giveFormOptions from '../data/options'; |
| 22 | |
| 23 | /** |
| 24 | * Render Inspector Controls |
| 25 | */ |
| 26 | |
| 27 | const Inspector = ({attributes, setAttributes}) => { |
| 28 | const { |
| 29 | formsPerPage, |
| 30 | paged, |
| 31 | imageSize, |
| 32 | imageHeight, |
| 33 | formIDs, |
| 34 | excludeForms, |
| 35 | excludedFormIDs, |
| 36 | orderBy, |
| 37 | order, |
| 38 | categories, |
| 39 | tags, |
| 40 | columns, |
| 41 | showTitle, |
| 42 | showExcerpt, |
| 43 | excerptLength, |
| 44 | showGoal, |
| 45 | showProgressBar, |
| 46 | showFeaturedImage, |
| 47 | showDonateButton, |
| 48 | donateButtonBackgroundColor, |
| 49 | donateButtonTextColor, |
| 50 | displayType |
| 51 | } = attributes; |
| 52 | |
| 53 | const saveSetting = (name, value) => { |
| 54 | setAttributes({ |
| 55 | [name]: value, |
| 56 | }); |
| 57 | }; |
| 58 | |
| 59 | const getAsArray = value => { |
| 60 | if (Array.isArray(value)) { |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | // Backward compatibility |
| 65 | if (formIDs.indexOf(',')) { |
| 66 | return value.split(','); |
| 67 | } |
| 68 | |
| 69 | return [value]; |
| 70 | }; |
| 71 | |
| 72 | const getImageSizes = () => select(store).getSettings().imageSizes.map(({slug, name}) => { |
| 73 | return { |
| 74 | value: slug, |
| 75 | label: name |
| 76 | }; |
| 77 | }); |
| 78 | |
| 79 | return ( |
| 80 | <InspectorControls key="inspector"> |
| 81 | <PanelBody title={__('Display Appearance', 'give')}> |
| 82 | <ColumnSelector |
| 83 | label={__('Columns', 'give')} |
| 84 | selected={columns} |
| 85 | onClick={(value) => saveSetting('columns', value)} |
| 86 | help={__('Controls how many columns of the Form Grid appear. "Best Fit" will adjust responsively to the space available.', 'give')} |
| 87 | /> |
| 88 | |
| 89 | <ToggleControl |
| 90 | name="showTitle" |
| 91 | label={__('Show Title', 'give')} |
| 92 | checked={!!showTitle} |
| 93 | onChange={(value) => saveSetting('showTitle', value)}/> |
| 94 | |
| 95 | <ToggleControl |
| 96 | name="showExcerpt" |
| 97 | label={__('Show Excerpt', 'give')} |
| 98 | checked={!!showExcerpt} |
| 99 | onChange={(value) => saveSetting('showExcerpt', value)} |
| 100 | /> |
| 101 | |
| 102 | <TextControl |
| 103 | name="excerptLength" |
| 104 | label={__('Excerpt Length', 'give')} |
| 105 | value={excerptLength || ''} |
| 106 | className={!showExcerpt && 'hidden'} |
| 107 | onChange={(value) => saveSetting('excerptLength', Number.parseInt(value) || 0)} |
| 108 | /> |
| 109 | |
| 110 | <ToggleControl |
| 111 | name="showGoal" |
| 112 | label={__('Show Goal', 'give')} |
| 113 | checked={!!showGoal} |
| 114 | onChange={(value) => saveSetting('showGoal', value)} |
| 115 | /> |
| 116 | |
| 117 | {showGoal && ( |
| 118 | <ToggleControl |
| 119 | name="showProgressBar" |
| 120 | label={__('Show Progress Bar', 'give')} |
| 121 | checked={!!showProgressBar} |
| 122 | onChange={(value) => saveSetting('showProgressBar', value)} |
| 123 | /> |
| 124 | )} |
| 125 | |
| 126 | <ToggleControl |
| 127 | align="right" |
| 128 | name="showFeaturedImage" |
| 129 | label={__('Show Featured Image', 'give')} |
| 130 | checked={!!showFeaturedImage} |
| 131 | onChange={(value) => saveSetting('showFeaturedImage', value)} |
| 132 | /> |
| 133 | |
| 134 | {showFeaturedImage && ( |
| 135 | <> |
| 136 | <SelectControl |
| 137 | name="imageSize" |
| 138 | label={__('Image Size', 'give')} |
| 139 | value={imageSize} |
| 140 | options={getImageSizes()} |
| 141 | onChange={(value) => saveSetting('imageSize', value)} |
| 142 | help={__('Featured image size. Default "medium." Accepts WordPress image sizes, which by default are "thumbnail," "medium," "large," or "full." ', 'give')} |
| 143 | /> |
| 144 | |
| 145 | <TextControl |
| 146 | name="imageHeight" |
| 147 | label={__('Image Height', 'give')} |
| 148 | value={imageHeight} |
| 149 | onChange={(value) => saveSetting('imageHeight', value)} |
| 150 | help={__('Featured image height. Default "auto". Accepts valid heights in px, em, or rem.', 'give')} |
| 151 | /> |
| 152 | </> |
| 153 | )} |
| 154 | |
| 155 | <ToggleControl |
| 156 | name="showDonateButton" |
| 157 | label={__('Show Donate Button', 'give')} |
| 158 | checked={!!showDonateButton} |
| 159 | onChange={(value) => saveSetting('showDonateButton', value)} |
| 160 | /> |
| 161 | {showDonateButton && ( |
| 162 | <> |
| 163 | <p> |
| 164 | {__('Donate Button Background Color', 'give')} |
| 165 | </p> |
| 166 | <ColorPalette |
| 167 | clearable={false} |
| 168 | onChange={(value) => saveSetting('donateButtonBackgroundColor', value)} |
| 169 | value={donateButtonBackgroundColor} |
| 170 | /> |
| 171 | <p> |
| 172 | {__('Donate Button Text Color', 'give')} |
| 173 | </p> |
| 174 | <ColorPalette |
| 175 | clearable={false} |
| 176 | onChange={(value) => saveSetting('donateButtonTextColor', value)} |
| 177 | value={donateButtonTextColor} |
| 178 | /> |
| 179 | </> |
| 180 | )} |
| 181 | </PanelBody> |
| 182 | |
| 183 | <PanelBody title={__('Filters and Categories', 'give')}> |
| 184 | |
| 185 | <SelectControl |
| 186 | label={__('Order By', 'give')} |
| 187 | name="orderBy" |
| 188 | value={orderBy} |
| 189 | options={giveFormOptions.orderBy} |
| 190 | onChange={(value) => saveSetting('orderBy', value)} |
| 191 | help={__('The order forms are displayed in.', 'give')} |
| 192 | /> |
| 193 | |
| 194 | <SelectControl |
| 195 | label={__('Order', 'give')} |
| 196 | name="order" |
| 197 | value={order} |
| 198 | options={giveFormOptions.order} |
| 199 | onChange={(value) => saveSetting('order', value)} |
| 200 | help={__('Whether the order ascends or descends.', 'give')} |
| 201 | /> |
| 202 | |
| 203 | <FormTokenField |
| 204 | name="categories" |
| 205 | label={__('Categories', 'give')} |
| 206 | value={getAsArray(categories)} |
| 207 | onChange={(value) => saveSetting('categories', value)} |
| 208 | /> |
| 209 | |
| 210 | <p className="components-form-token-field__help"> |
| 211 | {__('Type the name of your category to add it to the list. Only forms within the categories you choose will be displayed in this grid.', 'give')} |
| 212 | </p> |
| 213 | |
| 214 | <FormTokenField |
| 215 | name="tags" |
| 216 | label={__('Tags', 'give')} |
| 217 | value={getAsArray(tags)} |
| 218 | onChange={(value) => saveSetting('tags', value)} |
| 219 | /> |
| 220 | |
| 221 | <p className="components-form-token-field__help"> |
| 222 | {__('Type the name of your tag to add it to the list. Only forms with these tags you choose will be displayed in this grid.', 'give')} |
| 223 | </p> |
| 224 | |
| 225 | <FormTokenField |
| 226 | name="formIDs" |
| 227 | label={__('Form IDs', 'give')} |
| 228 | value={getAsArray(formIDs)} |
| 229 | onChange={(value) => saveSetting('formIDs', value)} |
| 230 | /> |
| 231 | |
| 232 | <p className="components-form-token-field__help"> |
| 233 | {__('Type the ID of your form to add it to the list. Only forms with these IDs you choose will be displayed in this grid.', 'give')} |
| 234 | </p> |
| 235 | |
| 236 | <ToggleControl |
| 237 | name="excludeForms" |
| 238 | label={__('Exclude specific forms?', 'give')} |
| 239 | checked={!!excludeForms} |
| 240 | onChange={(value) => saveSetting('excludeForms', value)} |
| 241 | /> |
| 242 | |
| 243 | {excludeForms && ( |
| 244 | <> |
| 245 | <FormTokenField |
| 246 | name="excludedFormIDs" |
| 247 | label={__('Excluded Form IDs', 'give')} |
| 248 | value={getAsArray(excludedFormIDs)} |
| 249 | onChange={(value) => saveSetting('excludedFormIDs', value)} |
| 250 | /> |
| 251 | <p className="components-form-token-field__help"> |
| 252 | {__('Type the ID of your form to exclude it from the list. Forms with these IDs you choose will not be displayed in this grid.', 'give')} |
| 253 | </p> |
| 254 | </> |
| 255 | )} |
| 256 | </PanelBody> |
| 257 | |
| 258 | <PanelBody title={__('Grid Behavior', 'give')}> |
| 259 | <SelectControl |
| 260 | label={__('Display Type', 'give')} |
| 261 | name="displayType" |
| 262 | value={displayType} |
| 263 | options={giveFormOptions.displayType} |
| 264 | onChange={(value) => saveSetting('displayType', value)} |
| 265 | help={__('What should happen when a visitor clicks on a form within the grid? "Redirect" sends them to the individual form. "Modal" opens the form in a lightbox/popup on the same page.', 'give')} |
| 266 | /> |
| 267 | <TextControl |
| 268 | name="formsPerPage" |
| 269 | label={__('Forms Per Page', 'give')} |
| 270 | value={formsPerPage} |
| 271 | onChange={(value) => saveSetting('formsPerPage', value)} |
| 272 | help={__('How many forms should display on the first page load? To restrict display to only one page, disable pagination below.', 'give')} |
| 273 | /> |
| 274 | <ToggleControl |
| 275 | name="paged" |
| 276 | label={__('Show Pagination', 'give')} |
| 277 | checked={!!paged} |
| 278 | onChange={(value) => saveSetting('paged', value)} |
| 279 | /> |
| 280 | </PanelBody> |
| 281 | </InspectorControls> |
| 282 | ); |
| 283 | }; |
| 284 | |
| 285 | export default Inspector; |
| 286 |