| 1 |
/** |
| 2 |
* Field Visibility inspector. Wire separator is ", " (legacy WCK / PHP explode). |
| 3 |
* 'all' is exclusive with other items; empty list falls back to 'all'. |
| 4 |
*/ |
| 5 |
import { PanelBody, SelectControl, CheckboxControl } from '@wordpress/components'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
|
| 8 |
const SEP = ', '; |
| 9 |
|
| 10 |
function csvList( csv ) { |
| 11 |
if ( ! csv ) return []; |
| 12 |
return csv.split( ',' ).map( ( v ) => v.trim() ).filter( Boolean ); |
| 13 |
} |
| 14 |
|
| 15 |
function csvHas( csv, value ) { |
| 16 |
return csvList( csv ).includes( value ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Toggle a single item in the comma-separated value. |
| 21 |
* - Clicking 'all' always resets to 'all' (mutually exclusive with everything). |
| 22 |
* - Clicking any other item drops 'all' from the list. |
| 23 |
* - Empty list falls back to 'all'. |
| 24 |
*/ |
| 25 |
function toggleItem( current, key, checked ) { |
| 26 |
if ( key === 'all' ) { |
| 27 |
return 'all'; |
| 28 |
} |
| 29 |
let list = csvList( current ).filter( ( v ) => v !== 'all' ); |
| 30 |
if ( checked && ! list.includes( key ) ) list.push( key ); |
| 31 |
if ( ! checked ) list = list.filter( ( v ) => v !== key ); |
| 32 |
if ( list.length === 0 ) return 'all'; |
| 33 |
return list.join( SEP ); |
| 34 |
} |
| 35 |
|
| 36 |
export default function FieldVisibilityPanel( { attributes, setAttributes } ) { |
| 37 |
const cfg = ( typeof window !== 'undefined' && window.wppbFb && window.wppbFb.fieldVisibility ) || null; |
| 38 |
if ( ! cfg ) return null; |
| 39 |
|
| 40 |
const visibility = attributes[ 'visibility' ] || 'all'; |
| 41 |
const userRoleVisibility = attributes[ 'user-role-visibility' ] || 'all'; |
| 42 |
const locationVisibility = attributes[ 'location-visibility' ] || 'all'; |
| 43 |
|
| 44 |
const userRoles = cfg.userRoles || {}; |
| 45 |
const locations = cfg.locations || {}; |
| 46 |
const visibilityOptions = cfg.visibilityOptions || {}; |
| 47 |
|
| 48 |
const visOptions = Object.keys( visibilityOptions ).map( ( value ) => ( { |
| 49 |
value, |
| 50 |
label: visibilityOptions[ value ], |
| 51 |
} ) ); |
| 52 |
|
| 53 |
return ( |
| 54 |
<PanelBody title={ __( 'Field Visibility', 'profile-builder' ) } initialOpen={ false }> |
| 55 |
<SelectControl |
| 56 |
label={ __( 'Visibility', 'profile-builder' ) } |
| 57 |
value={ visibility } |
| 58 |
options={ visOptions } |
| 59 |
onChange={ ( val ) => setAttributes( { 'visibility': val } ) } |
| 60 |
help={ __( 'Admin Only: visible only for administrators. User Locked: visible for everyone, but only administrators can edit.', 'profile-builder' ) } |
| 61 |
/> |
| 62 |
|
| 63 |
<p style={ { marginTop: '1em', marginBottom: '0.25em', fontWeight: 600 } }> |
| 64 |
{ __( 'User Role Visibility', 'profile-builder' ) } |
| 65 |
</p> |
| 66 |
<CheckboxControl |
| 67 |
label={ __( 'All', 'profile-builder' ) } |
| 68 |
checked={ csvHas( userRoleVisibility, 'all' ) } |
| 69 |
onChange={ ( c ) => setAttributes( { 'user-role-visibility': toggleItem( userRoleVisibility, 'all', c ) } ) } |
| 70 |
/> |
| 71 |
{ Object.keys( userRoles ).map( ( slug ) => ( |
| 72 |
<CheckboxControl |
| 73 |
key={ slug } |
| 74 |
label={ userRoles[ slug ] } |
| 75 |
checked={ csvHas( userRoleVisibility, slug ) } |
| 76 |
onChange={ ( c ) => setAttributes( { 'user-role-visibility': toggleItem( userRoleVisibility, slug, c ) } ) } |
| 77 |
/> |
| 78 |
) ) } |
| 79 |
<p style={ { color: '#757575', fontSize: '12px', marginTop: '0.25em' } }> |
| 80 |
{ __( 'Select which user roles see this field.', 'profile-builder' ) } |
| 81 |
</p> |
| 82 |
|
| 83 |
<p style={ { marginTop: '1em', marginBottom: '0.25em', fontWeight: 600 } }> |
| 84 |
{ __( 'Location Visibility', 'profile-builder' ) } |
| 85 |
</p> |
| 86 |
<CheckboxControl |
| 87 |
label={ __( 'All', 'profile-builder' ) } |
| 88 |
checked={ csvHas( locationVisibility, 'all' ) } |
| 89 |
onChange={ ( c ) => setAttributes( { 'location-visibility': toggleItem( locationVisibility, 'all', c ) } ) } |
| 90 |
/> |
| 91 |
{ Object.keys( locations ).map( ( slug ) => ( |
| 92 |
<CheckboxControl |
| 93 |
key={ slug } |
| 94 |
label={ locations[ slug ] } |
| 95 |
checked={ csvHas( locationVisibility, slug ) } |
| 96 |
onChange={ ( c ) => setAttributes( { 'location-visibility': toggleItem( locationVisibility, slug, c ) } ) } |
| 97 |
/> |
| 98 |
) ) } |
| 99 |
<p style={ { color: '#757575', fontSize: '12px', marginTop: '0.25em' } }> |
| 100 |
{ __( 'Select the locations you wish the field to appear.', 'profile-builder' ) } |
| 101 |
</p> |
| 102 |
</PanelBody> |
| 103 |
); |
| 104 |
} |
| 105 |
|