PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / admin / js / edit / datatables-features.js

datatables-features.js in TablePress – Tables in WordPress made easy 3.4, at admin/js/edit/datatables-features.js

270 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 { useState } from 'react';
14 import {
15 CheckboxControl,
16 __experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis
17 __experimentalNumberControl as NumberControl, // eslint-disable-line @wordpress/no-unsafe-wp-apis
18 TextareaControl,
19 __experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis
20 } from '@wordpress/components';
21 import { createInterpolateElement } from '@wordpress/element';
22 import { __, sprintf } from '@wordpress/i18n';
23
24 /**
25 * Internal dependencies.
26 */
27 import { initializeReactComponentInPortal } from '../common/react-loader';
28 import { Alert } from '../common/alert';
29
30 // Wrap the "Custom Commands" in a separate component, to keep internal states at a lower level.
31 const CustomCommandsControl = ( { tableOptionCustomCommands, dataTablesEnabled, updateTableOptions } ) => {
32 const [ customCommands, setCustomCommands ] = useState( tableOptionCustomCommands );
33 const [ customCommandsExpanded, setCustomCommandsExpanded ] = useState( false );
34
35 /*
36 // Updating the state variable when the table option changes is not necessary, as it's never modified externally.
37 useEffect( () => {
38 setCustomCommands( tableOptionCustomCommands );
39 }, [ tableOptionCustomCommands ] );
40 */
41
42 return (
43 <TextareaControl
44 __nextHasNoMarginBottom
45 id="option-datatables_custom_commands"
46 rows={
47 customCommandsExpanded && '' !== customCommands
48 ? Math.min( 15, customCommands.split( '\n' ).length )
49 : 1
50 }
51 className="code"
52 value={ customCommands }
53 disabled={ ! dataTablesEnabled }
54 onChange={ ( newCustomCommands ) => setCustomCommands( newCustomCommands ) }
55 onBlur={ () => {
56 if ( customCommands !== tableOptionCustomCommands ) {
57 updateTableOptions( { datatables_custom_commands: customCommands } );
58 }
59 } }
60 onFocus={ () => setCustomCommandsExpanded( true ) }
61 help={
62 createInterpolateElement(
63 __( 'Additional parameters from the <a>DataTables documentation</a> to be added to the JS call.', 'tablepress' ) + ' ' + __( 'For advanced use only.', 'tablepress' ),
64 {
65 a: <a href="https://datatables.net/" />, // eslint-disable-line jsx-a11y/anchor-has-content
66 },
67 )
68 }
69 />
70 );
71 };
72
73 const Section = ( { tableOptions, updateTableOptions } ) => {
74 const [ alertEnableDataTablesMergedCellsIsShown, setAlertEnableDataTablesMergedCellsIsShown ] = useState( false );
75
76 const tableHeadEnabled = ( tableOptions.table_head > 0 );
77 const dataTablesEnabled = ( tableHeadEnabled && tableOptions.use_datatables );
78
79 return (
80 <VStack
81 spacing="16px"
82 style={ {
83 paddingTop: '6px',
84 } }
85 >
86 {
87 ( ! tableHeadEnabled ) && (
88 <span>
89 <em>
90 {
91 sprintf(
92 __( 'These features and options are only available when the “%1$s” setting in the “%2$s” section is used.', 'tablepress' ),
93 __( 'Table Header', 'tablepress' ),
94 __( 'Table Options', 'tablepress' ),
95 )
96 }
97 </em>
98 </span>
99 )
100 }
101 <table className="tablepress-postbox-table fixed">
102 <tbody>
103 <tr className="bottom-border">
104 <th className="column-1" scope="row">{ __( 'Enable Visitor Features', 'tablepress' ) }:</th>
105 <td className="column-2">
106 <CheckboxControl
107 __nextHasNoMarginBottom
108 label={ __( 'Offer the following functions for site visitors with this table:', 'tablepress' ) }
109 checked={ tableOptions.use_datatables }
110 disabled={ ! tableHeadEnabled }
111 onChange={ ( use_datatables ) => {
112 // Don't turn on "Enable Visitor Features" if the table has merged cells.
113 if ( use_datatables ) {
114 tp.helpers.visibility.update(); // Update information about hidden rows and columns.
115 if ( tp.helpers.editor.has_merged_body_cells() ) {
116 setAlertEnableDataTablesMergedCellsIsShown( true );
117 return;
118 }
119 }
120 updateTableOptions( { use_datatables } );
121 } }
122 />
123 { alertEnableDataTablesMergedCellsIsShown && (
124 <Alert
125 title={ __( 'Enable Visitor Features', 'tablepress' ) }
126 text={ __( 'You can not enable the Table Features for Site Visitors, because your table’s body rows contain combined/merged cells.', 'tablepress' ) }
127 onConfirm={ () => setAlertEnableDataTablesMergedCellsIsShown( false ) }
128 modalProps={ {
129 size: 'medium',
130 } }
131 />
132 ) }
133 </td>
134 </tr>
135 <tr className="top-border">
136 <th className="column-1" scope="row">{ __( 'Sorting', 'tablepress' ) }:</th>
137 <td className="column-2">
138 <CheckboxControl
139 __nextHasNoMarginBottom
140 label={ __( 'Enable sorting of the table by the visitor.', 'tablepress' ) }
141 checked={ tableOptions.datatables_sort }
142 disabled={ ! dataTablesEnabled }
143 onChange={ ( datatables_sort ) => updateTableOptions( { datatables_sort } ) }
144 />
145 </td>
146 </tr>
147 <tr>
148 <th className="column-1" scope="row">{ __( 'Search/Filtering', 'tablepress' ) }:</th>
149 <td className="column-2">
150 <CheckboxControl
151 __nextHasNoMarginBottom
152 label={ __( 'Enable the visitor to filter or search the table. Only rows with the search word in them are shown.', 'tablepress' ) }
153 checked={ tableOptions.datatables_filter }
154 disabled={ ! dataTablesEnabled }
155 onChange={ ( datatables_filter ) => updateTableOptions( { datatables_filter } ) }
156 />
157 </td>
158 </tr>
159 <tr>
160 <th className="column-1" scope="row">{ __( 'Pagination', 'tablepress' ) }:</th>
161 <td className="column-2">
162 <CheckboxControl
163 __nextHasNoMarginBottom
164 label={ __( 'Enable pagination of the table (viewing only a certain number of rows at a time) by the visitor.', 'tablepress' ) }
165 checked={ tableOptions.datatables_paginate }
166 disabled={ ! dataTablesEnabled }
167 onChange={ ( datatables_paginate ) => updateTableOptions( { datatables_paginate } ) }
168 />
169 </td>
170 </tr>
171 <tr>
172 <th className="column-1" scope="row"></th>
173 <td className="column-2">
174 <label htmlFor="option-datatables_paginate_entries">
175 <HStack
176 alignment="left"
177 style={ {
178 paddingLeft: '24px',
179 } }
180 >
181 {
182 createInterpolateElement(
183 __( 'Show <input /> rows per page.', 'tablepress' ),
184 {
185 input: (
186 <NumberControl
187 size="compact"
188 id="option-datatables_paginate_entries"
189 title={ __( 'This field must contain a non-negative number.', 'tablepress' ) }
190 isDragEnabled={ false }
191 value={ tableOptions.datatables_paginate_entries }
192 disabled={ ! dataTablesEnabled || ! tableOptions.datatables_paginate }
193 onChange={ ( datatables_paginate_entries ) => {
194 datatables_paginate_entries = '' !== datatables_paginate_entries ? parseInt( datatables_paginate_entries, 10 ) : 1;
195 updateTableOptions( { datatables_paginate_entries } );
196 } }
197 min={ 1 }
198 required={ true }
199 style={ {
200 width: '65px',
201 } }
202 />
203 ),
204 },
205 )
206 }
207 </HStack>
208 </label>
209 </td>
210 </tr>
211 <tr>
212 <th className="column-1" scope="row">{ __( 'Pagination Length Change', 'tablepress' ) }:</th>
213 <td className="column-2">
214 <CheckboxControl
215 __nextHasNoMarginBottom
216 label={ __( 'Allow the visitor to change the number of rows shown when using pagination.', 'tablepress' ) }
217 checked={ tableOptions.datatables_lengthchange }
218 disabled={ ! dataTablesEnabled || ! tableOptions.datatables_paginate }
219 onChange={ ( datatables_lengthchange ) => updateTableOptions( { datatables_lengthchange } ) }
220 />
221 </td>
222 </tr>
223 <tr>
224 <th className="column-1" scope="row">{ __( 'Info', 'tablepress' ) }:</th>
225 <td className="column-2">
226 <CheckboxControl
227 __nextHasNoMarginBottom
228 label={ __( 'Enable the table information display, with information about the currently visible data, like the number of rows.', 'tablepress' ) }
229 checked={ tableOptions.datatables_info }
230 disabled={ ! dataTablesEnabled }
231 onChange={ ( datatables_info ) => updateTableOptions( { datatables_info } ) }
232 />
233 </td>
234 </tr>
235 <tr className={ tp.screenOptions.showCustomCommands ? 'bottom-border' : undefined }>
236 <th className="column-1" scope="row">{ __( 'Horizontal Scrolling', 'tablepress' ) }:</th>
237 <td className="column-2">
238 <CheckboxControl
239 __nextHasNoMarginBottom
240 label={ __( 'Enable horizontal scrolling, to make viewing tables with many columns easier.', 'tablepress' ) }
241 checked={ tableOptions.datatables_scrollx }
242 disabled={ ! dataTablesEnabled }
243 onChange={ ( datatables_scrollx ) => updateTableOptions( { datatables_scrollx } ) }
244 />
245 </td>
246 </tr>
247 { ( tp.screenOptions.showCustomCommands ) && (
248 <tr className="top-border">
249 <th className="column-1 top-align" scope="row"><label htmlFor="option-datatables_custom_commands">{ __( 'Custom Commands', 'tablepress' ) }:</label></th>
250 <td className="column-2">
251 <CustomCommandsControl
252 tableOptionCustomCommands={ tableOptions.datatables_custom_commands }
253 updateTableOptions={ updateTableOptions }
254 dataTablesEnabled={ dataTablesEnabled }
255 />
256 </td>
257 </tr>
258 ) }
259 </tbody>
260 </table>
261 </VStack>
262 );
263 };
264
265 initializeReactComponentInPortal(
266 'datatables-features',
267 'edit',
268 Section,
269 );
270