| 1 |
/** |
| 2 |
* JavaScript code for the "Edit" section integration of the "Table Features for Site Visitors". |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies. |
| 12 |
*/ |
| 13 |
import { |
| 14 |
CheckboxControl, |
| 15 |
__experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 16 |
__experimentalNumberControl as NumberControl, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 17 |
TextareaControl, |
| 18 |
__experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 19 |
} from '@wordpress/components'; |
| 20 |
import { createInterpolateElement } from '@wordpress/element'; |
| 21 |
import { __, sprintf } from '@wordpress/i18n'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Internal dependencies. |
| 25 |
*/ |
| 26 |
import { initializeReactComponentInPortal } from '../common/react-loader'; |
| 27 |
|
| 28 |
const Section = ( { tableOptions, updateTableOptions } ) => { |
| 29 |
const tableHeadEnabled = ( tableOptions.table_head > 0 ); |
| 30 |
const dataTablesEnabled = ( tableHeadEnabled && tableOptions.use_datatables ); |
| 31 |
|
| 32 |
return ( |
| 33 |
<VStack |
| 34 |
spacing="16px" |
| 35 |
style={ { |
| 36 |
paddingTop: '6px', |
| 37 |
} } |
| 38 |
> |
| 39 |
{ |
| 40 |
( ! tableHeadEnabled ) && ( |
| 41 |
<span> |
| 42 |
<em> |
| 43 |
{ |
| 44 |
sprintf( |
| 45 |
__( 'These features and options are only available when the “%1$s” setting in the “%2$s” section is used.', 'tablepress' ), |
| 46 |
__( 'Table Header', 'tablepress' ), |
| 47 |
__( 'Table Options', 'tablepress' ), |
| 48 |
) |
| 49 |
} |
| 50 |
</em> |
| 51 |
</span> |
| 52 |
) |
| 53 |
} |
| 54 |
<table className="tablepress-postbox-table fixed"> |
| 55 |
<tbody> |
| 56 |
<tr className="bottom-border"> |
| 57 |
<th className="column-1" scope="row">{ __( 'Enable Visitor Features', 'tablepress' ) }:</th> |
| 58 |
<td className="column-2"> |
| 59 |
<CheckboxControl |
| 60 |
__nextHasNoMarginBottom |
| 61 |
label={ __( 'Offer the following functions for site visitors with this table:', 'tablepress' ) } |
| 62 |
checked={ tableOptions.use_datatables } |
| 63 |
disabled={ ! tableHeadEnabled } |
| 64 |
onChange={ ( use_datatables ) => { |
| 65 |
// Turn off "Enable Visitor Features" if the table has merged cells. |
| 66 |
if ( use_datatables ) { |
| 67 |
tp.helpers.visibility.update(); // Update information about hidden rows and columns. |
| 68 |
if ( tp.helpers.editor.has_merged_body_cells() ) { |
| 69 |
use_datatables = false; |
| 70 |
window.alert( __( 'You can not enable the Table Features for Site Visitors, because your table’s body rows contain combined/merged cells.', 'tablepress' ) ); |
| 71 |
} |
| 72 |
} |
| 73 |
updateTableOptions( { use_datatables } ); |
| 74 |
} } |
| 75 |
/> |
| 76 |
</td> |
| 77 |
</tr> |
| 78 |
<tr className="top-border"> |
| 79 |
<th className="column-1" scope="row">{ __( 'Sorting', 'tablepress' ) }:</th> |
| 80 |
<td className="column-2"> |
| 81 |
<CheckboxControl |
| 82 |
__nextHasNoMarginBottom |
| 83 |
label={ __( 'Enable sorting of the table by the visitor.', 'tablepress' ) } |
| 84 |
checked={ tableOptions.datatables_sort } |
| 85 |
disabled={ ! dataTablesEnabled } |
| 86 |
onChange={ ( datatables_sort ) => updateTableOptions( { datatables_sort } ) } |
| 87 |
/> |
| 88 |
</td> |
| 89 |
</tr> |
| 90 |
<tr> |
| 91 |
<th className="column-1" scope="row">{ __( 'Search/Filtering', 'tablepress' ) }:</th> |
| 92 |
<td className="column-2"> |
| 93 |
<CheckboxControl |
| 94 |
__nextHasNoMarginBottom |
| 95 |
label={ __( 'Enable the visitor to filter or search the table. Only rows with the search word in them are shown.', 'tablepress' ) } |
| 96 |
checked={ tableOptions.datatables_filter } |
| 97 |
disabled={ ! dataTablesEnabled } |
| 98 |
onChange={ ( datatables_filter ) => updateTableOptions( { datatables_filter } ) } |
| 99 |
/> |
| 100 |
</td> |
| 101 |
</tr> |
| 102 |
<tr> |
| 103 |
<th className="column-1" scope="row">{ __( 'Pagination', 'tablepress' ) }:</th> |
| 104 |
<td className="column-2"> |
| 105 |
<CheckboxControl |
| 106 |
__nextHasNoMarginBottom |
| 107 |
label={ __( 'Enable pagination of the table (viewing only a certain number of rows at a time) by the visitor.', 'tablepress' ) } |
| 108 |
checked={ tableOptions.datatables_paginate } |
| 109 |
disabled={ ! dataTablesEnabled } |
| 110 |
onChange={ ( datatables_paginate ) => updateTableOptions( { datatables_paginate } ) } |
| 111 |
/> |
| 112 |
</td> |
| 113 |
</tr> |
| 114 |
<tr> |
| 115 |
<th className="column-1" scope="row"></th> |
| 116 |
<td className="column-2"> |
| 117 |
<label htmlFor="option-datatables_paginate_entries"> |
| 118 |
<HStack |
| 119 |
alignment="left" |
| 120 |
style={ { |
| 121 |
paddingLeft: '24px', |
| 122 |
} } |
| 123 |
> |
| 124 |
{ |
| 125 |
createInterpolateElement( |
| 126 |
__( 'Show <input /> rows per page.', 'tablepress' ), |
| 127 |
{ |
| 128 |
input: ( |
| 129 |
<NumberControl |
| 130 |
id="option-datatables_paginate_entries" |
| 131 |
title={ __( 'This field must contain a non-negative number.', 'tablepress' ) } |
| 132 |
isDragEnabled={ false } |
| 133 |
value={ tableOptions.datatables_paginate_entries } |
| 134 |
disabled={ ! dataTablesEnabled || ! tableOptions.datatables_paginate } |
| 135 |
onChange={ ( datatables_paginate_entries ) => { |
| 136 |
datatables_paginate_entries = '' !== datatables_paginate_entries ? parseInt( datatables_paginate_entries, 10 ) : 1; |
| 137 |
updateTableOptions( { datatables_paginate_entries } ); |
| 138 |
} } |
| 139 |
min={ 1 } |
| 140 |
required={ true } |
| 141 |
style={ { |
| 142 |
width: '65px', |
| 143 |
} } |
| 144 |
/> |
| 145 |
), |
| 146 |
}, |
| 147 |
) |
| 148 |
} |
| 149 |
</HStack> |
| 150 |
</label> |
| 151 |
</td> |
| 152 |
</tr> |
| 153 |
<tr> |
| 154 |
<th className="column-1" scope="row">{ __( 'Pagination Length Change', 'tablepress' ) }:</th> |
| 155 |
<td className="column-2"> |
| 156 |
<CheckboxControl |
| 157 |
__nextHasNoMarginBottom |
| 158 |
label={ __( 'Allow the visitor to change the number of rows shown when using pagination.', 'tablepress' ) } |
| 159 |
checked={ tableOptions.datatables_lengthchange } |
| 160 |
disabled={ ! dataTablesEnabled || ! tableOptions.datatables_paginate } |
| 161 |
onChange={ ( datatables_lengthchange ) => updateTableOptions( { datatables_lengthchange } ) } |
| 162 |
/> |
| 163 |
</td> |
| 164 |
</tr> |
| 165 |
<tr> |
| 166 |
<th className="column-1" scope="row">{ __( 'Info', 'tablepress' ) }:</th> |
| 167 |
<td className="column-2"> |
| 168 |
<CheckboxControl |
| 169 |
__nextHasNoMarginBottom |
| 170 |
label={ __( 'Enable the table information display, with information about the currently visible data, like the number of rows.', 'tablepress' ) } |
| 171 |
checked={ tableOptions.datatables_info } |
| 172 |
disabled={ ! dataTablesEnabled } |
| 173 |
onChange={ ( datatables_info ) => updateTableOptions( { datatables_info } ) } |
| 174 |
/> |
| 175 |
</td> |
| 176 |
</tr> |
| 177 |
<tr className={ tp.screen_options.showCustomCommands ? 'bottom-border' : undefined }> |
| 178 |
<th className="column-1" scope="row">{ __( 'Horizontal Scrolling', 'tablepress' ) }:</th> |
| 179 |
<td className="column-2"> |
| 180 |
<CheckboxControl |
| 181 |
__nextHasNoMarginBottom |
| 182 |
label={ __( 'Enable horizontal scrolling, to make viewing tables with many columns easier.', 'tablepress' ) } |
| 183 |
checked={ tableOptions.datatables_scrollx } |
| 184 |
disabled={ ! dataTablesEnabled } |
| 185 |
onChange={ ( datatables_scrollx ) => updateTableOptions( { datatables_scrollx } ) } |
| 186 |
/> |
| 187 |
</td> |
| 188 |
</tr> |
| 189 |
{ ( tp.screen_options.showCustomCommands ) && ( |
| 190 |
<tr className="top-border"> |
| 191 |
<th className="column-1 top-align" scope="row"><label htmlFor="option-datatables_custom_commands">{ __( 'Custom Commands', 'tablepress' ) }:</label></th> |
| 192 |
<td className="column-2"> |
| 193 |
<TextareaControl |
| 194 |
__nextHasNoMarginBottom |
| 195 |
id="option-datatables_custom_commands" |
| 196 |
rows="1" |
| 197 |
className="code" |
| 198 |
value={ tableOptions.datatables_custom_commands } |
| 199 |
disabled={ ! dataTablesEnabled } |
| 200 |
onChange={ ( datatables_custom_commands ) => updateTableOptions( { datatables_custom_commands } ) } |
| 201 |
help={ |
| 202 |
createInterpolateElement( |
| 203 |
__( 'Additional parameters from the <a>DataTables documentation</a> to be added to the JS call.', 'tablepress' ) + ' ' + __( 'For advanced use only.', 'tablepress' ), |
| 204 |
{ |
| 205 |
a: <a href="https://datatables.net/" />, // eslint-disable-line jsx-a11y/anchor-has-content |
| 206 |
}, |
| 207 |
) |
| 208 |
} |
| 209 |
/> |
| 210 |
</td> |
| 211 |
</tr> |
| 212 |
) } |
| 213 |
</tbody> |
| 214 |
</table> |
| 215 |
</VStack> |
| 216 |
); |
| 217 |
}; |
| 218 |
|
| 219 |
initializeReactComponentInPortal( |
| 220 |
'datatables-features', |
| 221 |
'edit', |
| 222 |
Section, |
| 223 |
); |
| 224 |
|