| 1 |
/** |
| 2 |
* JavaScript code for the "Export Screen" component. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Export Screen |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 2.2.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* globals tp */ |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies. |
| 14 |
*/ |
| 15 |
import { useState } from 'react'; |
| 16 |
import { __, _x, sprintf } from '@wordpress/i18n'; |
| 17 |
|
| 18 |
// Number of tables. |
| 19 |
const tablesCount = Object.keys( tp.export.tables ).length; |
| 20 |
|
| 21 |
// Show at least one empty row in the select, and between 3 and 12 total rows. |
| 22 |
let exportTablesSelectSize = tablesCount + 1; |
| 23 |
const maxExportTablesSelectSize = 12; |
| 24 |
exportTablesSelectSize = Math.max( exportTablesSelectSize, 3 ); |
| 25 |
exportTablesSelectSize = Math.min( exportTablesSelectSize, maxExportTablesSelectSize ); |
| 26 |
|
| 27 |
// The <option> entries for the dropdowns do not depend on the state, so they can be created once. |
| 28 |
const tablesSelectOptions = Object.entries( tp.export.tables ).map( ( [ tableId, tableName ] ) => { |
| 29 |
if ( '' === tableName.trim() ) { |
| 30 |
tableName = __( '(no name)', 'tablepress' ); |
| 31 |
} |
| 32 |
const optionText = sprintf( __( 'ID %1$s: %2$s', 'tablepress' ), tableId, tableName ); |
| 33 |
return <option key={ tableId } value={ tableId }>{ optionText }</option>; |
| 34 |
} ); |
| 35 |
const exportFormatsSelectOptions = Object.entries( tp.export.exportFormats ).map( ( [ exportFormat, exportFormatName ] ) => |
| 36 |
<option key={ exportFormat } value={ exportFormat }>{ exportFormatName }</option> |
| 37 |
); |
| 38 |
const csvDelimitersSelectOptions = Object.entries( tp.export.csvDelimiters ).map( ( [ csvDelimiter, csvDelimiterName ] ) => |
| 39 |
<option key={ csvDelimiter } value={ csvDelimiter }>{ csvDelimiterName }</option> |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the "Export Screen" component's JSX markup. |
| 44 |
* |
| 45 |
* @return {Object} Export Screen component. |
| 46 |
*/ |
| 47 |
const ExportScreen = () => { |
| 48 |
const [ screenData, setScreenData ] = useState( { |
| 49 |
selectedTables: tp.export.selectedTables, |
| 50 |
exportFormat: tp.export.exportFormat, |
| 51 |
csvDelimiter: tp.export.csvDelimiter, |
| 52 |
createZipFile: false, |
| 53 |
reverseList: false, |
| 54 |
} ); |
| 55 |
|
| 56 |
// If more than one table is selected, force the ZIP file checkbox to checked. |
| 57 |
const zipFileRequired = screenData.selectedTables.length > 1; |
| 58 |
|
| 59 |
/** |
| 60 |
* Handles screen data state changes. |
| 61 |
* |
| 62 |
* @param {string} item Configuration item name. |
| 63 |
* @param {string|null} value Value for configuration item. |
| 64 |
*/ |
| 65 |
const updateScreenData = ( item, value ) => { |
| 66 |
const newScreenData = { |
| 67 |
...screenData, |
| 68 |
[ item ]: value, |
| 69 |
}; |
| 70 |
setScreenData( newScreenData ); |
| 71 |
}; |
| 72 |
|
| 73 |
return ( |
| 74 |
<table className="tablepress-postbox-table fixed"> |
| 75 |
<tbody> |
| 76 |
<tr> |
| 77 |
<th className="column-1 top-align" scope="row"> |
| 78 |
<label htmlFor="tables-export-list"> |
| 79 |
{ __( 'Tables to Export', 'tablepress' ) }: |
| 80 |
</label> |
| 81 |
{ tp.export.zipSupportAvailable && |
| 82 |
<> |
| 83 |
{ |
| 84 |
// Show a "Select all" checkbox to select all entries in the export tables dropdown. |
| 85 |
} |
| 86 |
<br /><br /> |
| 87 |
<label htmlFor="tables-export-select-all"> |
| 88 |
<input |
| 89 |
type="checkbox" |
| 90 |
id="tables-export-select-all" |
| 91 |
checked={ screenData.selectedTables.length === tablesCount } |
| 92 |
onChange={ () => { |
| 93 |
const selectedTables = ( screenData.selectedTables.length === tablesCount ) ? [] : Object.keys( tp.export.tables ); |
| 94 |
updateScreenData( 'selectedTables', selectedTables ); |
| 95 |
} } |
| 96 |
/> { __( 'Select all', 'tablepress' ) } |
| 97 |
</label> |
| 98 |
{ tablesCount > maxExportTablesSelectSize && |
| 99 |
<> |
| 100 |
{ |
| 101 |
// Show a "Reverse List" checkbox if more tables are shown than what the height of the export tables dropdown holds. |
| 102 |
} |
| 103 |
<br /><br /> |
| 104 |
<label htmlFor="tables-export-reverse-list"> |
| 105 |
<input |
| 106 |
id="tables-export-reverse-list" |
| 107 |
type="checkbox" |
| 108 |
checked={ screenData.reverseList } |
| 109 |
onChange={ () => { |
| 110 |
updateScreenData( 'reverseList', ! screenData.reverseList ); |
| 111 |
tablesSelectOptions.reverse(); |
| 112 |
} } |
| 113 |
/> { __( 'Reverse list', 'tablepress' ) } |
| 114 |
</label> |
| 115 |
</> |
| 116 |
} |
| 117 |
</> |
| 118 |
} |
| 119 |
</th> |
| 120 |
<td className="column-2"> |
| 121 |
<select |
| 122 |
id="tables-export-list" |
| 123 |
size={ tp.export.zipSupportAvailable ? exportTablesSelectSize : 1 } |
| 124 |
multiple={ tp.export.zipSupportAvailable } |
| 125 |
value={ screenData.selectedTables } |
| 126 |
onChange={ ( event ) => { |
| 127 |
const selectedTables = [ ...event.target.selectedOptions ].map( ( option ) => option.value ); |
| 128 |
updateScreenData( 'selectedTables', selectedTables ); |
| 129 |
} } |
| 130 |
style={ { |
| 131 |
width: '100%', |
| 132 |
} } |
| 133 |
> |
| 134 |
{ tablesSelectOptions } |
| 135 |
</select> |
| 136 |
{ tp.export.zipSupportAvailable && |
| 137 |
<> |
| 138 |
<br /> |
| 139 |
<span className="description"> |
| 140 |
{ sprintf( |
| 141 |
__( 'You can select multiple tables by holding down the “%1$s” key or the “%2$s” key for ranges.', 'tablepress' ), |
| 142 |
window?.navigator?.platform?.includes( 'Mac' ) ? _x( '⌘', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) : _x( 'Ctrl', 'keyboard key', 'tablepress' ), |
| 143 |
_x( 'Shift', 'keyboard key', 'tablepress' ) ) |
| 144 |
} |
| 145 |
</span> |
| 146 |
</> |
| 147 |
} |
| 148 |
</td> |
| 149 |
</tr> |
| 150 |
<tr> |
| 151 |
<th className="column-1" scope="row"> |
| 152 |
<label htmlFor="tables-export-format"> |
| 153 |
{ __( 'Export Format', 'tablepress' ) }: |
| 154 |
</label> |
| 155 |
</th> |
| 156 |
<td className="column-2"> |
| 157 |
<select |
| 158 |
id="tables-export-format" |
| 159 |
name="export[format]" |
| 160 |
value={ screenData.exportFormat } |
| 161 |
onChange={ ( event ) => updateScreenData( 'exportFormat', event.target.value ) } |
| 162 |
> |
| 163 |
{ exportFormatsSelectOptions } |
| 164 |
</select> |
| 165 |
</td> |
| 166 |
</tr> |
| 167 |
<tr> |
| 168 |
<th className="column-1" scope="row"> |
| 169 |
<label htmlFor="tables-export-csv-delimiter"> |
| 170 |
{ __( 'CSV Delimiter', 'tablepress' ) }: |
| 171 |
</label> |
| 172 |
</th> |
| 173 |
<td className="column-2"> |
| 174 |
<select |
| 175 |
id="tables-export-csv-delimiter" |
| 176 |
name="export[csv_delimiter]" |
| 177 |
disabled={ 'csv' !== screenData.exportFormat } |
| 178 |
value={ screenData.csvDelimiter } |
| 179 |
onChange={ ( event ) => updateScreenData( 'csvDelimiter', event.target.value ) } |
| 180 |
> |
| 181 |
{ csvDelimitersSelectOptions } |
| 182 |
</select> |
| 183 |
{ 'csv' !== screenData.exportFormat && |
| 184 |
<> |
| 185 |
{ ' ' } |
| 186 |
<span className="description"> |
| 187 |
{ __( '(Only needed for CSV export.)', 'tablepress' ) } |
| 188 |
</span> |
| 189 |
</> |
| 190 |
} |
| 191 |
</td> |
| 192 |
</tr> |
| 193 |
<tr className="bottom-border"> |
| 194 |
<th className="column-1" scope="row"> |
| 195 |
{ __( 'ZIP file', 'tablepress' ) }: |
| 196 |
</th> |
| 197 |
<td className="column-2"> |
| 198 |
{ tp.export.zipSupportAvailable && |
| 199 |
<label htmlFor="tables-export-zip-file"> |
| 200 |
<input |
| 201 |
type="checkbox" |
| 202 |
id="tables-export-zip-file" |
| 203 |
checked={ screenData.createZipFile || zipFileRequired } |
| 204 |
disabled={ zipFileRequired } |
| 205 |
onChange={ () => updateScreenData( 'createZipFile', ! screenData.createZipFile ) } |
| 206 |
/> { __( 'Create a ZIP archive.', 'tablepress' ) } |
| 207 |
{ zipFileRequired && |
| 208 |
<> |
| 209 |
{ ' ' } |
| 210 |
<span className="description"> |
| 211 |
{ __( '(Mandatory if more than one table is selected.)', 'tablepress' ) } |
| 212 |
</span> |
| 213 |
</> |
| 214 |
} |
| 215 |
</label> |
| 216 |
} |
| 217 |
{ ! tp.export.zipSupportAvailable && |
| 218 |
__( 'Note: Support for ZIP file creation seems not to be available on this server.', 'tablepress' ) |
| 219 |
} |
| 220 |
</td> |
| 221 |
</tr> |
| 222 |
<tr className="top-border"> |
| 223 |
<td className="column-1"></td> |
| 224 |
<td className="column-2"> |
| 225 |
<input |
| 226 |
type="hidden" |
| 227 |
name="export[tables_list]" |
| 228 |
value={ screenData.selectedTables.join() } |
| 229 |
/> |
| 230 |
<input |
| 231 |
type="hidden" |
| 232 |
name="export[zip_file]" |
| 233 |
value={ screenData.createZipFile || zipFileRequired } |
| 234 |
/> |
| 235 |
<input |
| 236 |
type="submit" |
| 237 |
value={ __( 'Download Export File', 'tablepress' ) } |
| 238 |
className="button button-primary button-large" |
| 239 |
disabled={ 0 === screenData.selectedTables.length } |
| 240 |
/> |
| 241 |
</td> |
| 242 |
</tr> |
| 243 |
</tbody> |
| 244 |
</table> |
| 245 |
); |
| 246 |
}; |
| 247 |
|
| 248 |
export default ExportScreen; |
| 249 |
|