| 1 |
wp.domReady(function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var __ = wp.i18n.__; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 8 |
var PanelBody = wp.components.PanelBody; |
| 9 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 10 |
var Button = wp.components.Button; |
| 11 |
var ToggleControl = wp.components.ToggleControl; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
var TextControl = wp.components.TextControl; |
| 15 |
var TextareaControl = wp.components.TextareaControl; |
| 16 |
|
| 17 |
registerBlockType('blockenberg/data-table', { |
| 18 |
title: __('Data Table', 'blockenberg'), |
| 19 |
icon: 'editor-table', |
| 20 |
category: 'blockenberg', |
| 21 |
description: __('Interactive data table with sorting, search, pagination and export.', 'blockenberg'), |
| 22 |
|
| 23 |
edit: function (props) { |
| 24 |
var attributes = props.attributes; |
| 25 |
var setAttributes = props.setAttributes; |
| 26 |
var a = attributes; |
| 27 |
|
| 28 |
// Update cell |
| 29 |
function updateCell(rowIndex, colIndex, value) { |
| 30 |
var newRows = a.rows.map(function (row, ri) { |
| 31 |
if (ri === rowIndex) { |
| 32 |
return row.map(function (cell, ci) { |
| 33 |
return ci === colIndex ? value : cell; |
| 34 |
}); |
| 35 |
} |
| 36 |
return row; |
| 37 |
}); |
| 38 |
setAttributes({ rows: newRows }); |
| 39 |
} |
| 40 |
|
| 41 |
// Update header |
| 42 |
function updateHeader(colIndex, value) { |
| 43 |
var newHeaders = a.headers.map(function (h, i) { |
| 44 |
return i === colIndex ? value : h; |
| 45 |
}); |
| 46 |
setAttributes({ headers: newHeaders }); |
| 47 |
} |
| 48 |
|
| 49 |
// Add row at end |
| 50 |
function addRow() { |
| 51 |
var newRow = new Array(a.columns).fill(''); |
| 52 |
setAttributes({ rows: a.rows.concat([newRow]) }); |
| 53 |
} |
| 54 |
|
| 55 |
// Insert row at specific position |
| 56 |
function insertRow(index, position) { |
| 57 |
var newRow = new Array(a.columns).fill(''); |
| 58 |
var newRows = a.rows.slice(); |
| 59 |
var insertAt = position === 'before' ? index : index + 1; |
| 60 |
newRows.splice(insertAt, 0, newRow); |
| 61 |
setAttributes({ rows: newRows }); |
| 62 |
} |
| 63 |
|
| 64 |
// Remove row |
| 65 |
function removeRow(index) { |
| 66 |
if (a.rows.length <= 1) return; |
| 67 |
setAttributes({ rows: a.rows.filter(function (_, i) { return i !== index; }) }); |
| 68 |
} |
| 69 |
|
| 70 |
// Add column at end |
| 71 |
function addColumn() { |
| 72 |
var newHeaders = a.headers.concat([__('Column', 'blockenberg') + ' ' + (a.columns + 1)]); |
| 73 |
var newRows = a.rows.map(function (row) { |
| 74 |
return row.concat(['']); |
| 75 |
}); |
| 76 |
setAttributes({ |
| 77 |
columns: a.columns + 1, |
| 78 |
headers: newHeaders, |
| 79 |
rows: newRows |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
// Insert column at specific position |
| 84 |
function insertColumn(index, position) { |
| 85 |
var insertAt = position === 'before' ? index : index + 1; |
| 86 |
var newHeaders = a.headers.slice(); |
| 87 |
newHeaders.splice(insertAt, 0, __('Column', 'blockenberg')); |
| 88 |
var newRows = a.rows.map(function (row) { |
| 89 |
var newRow = row.slice(); |
| 90 |
newRow.splice(insertAt, 0, ''); |
| 91 |
return newRow; |
| 92 |
}); |
| 93 |
setAttributes({ |
| 94 |
columns: a.columns + 1, |
| 95 |
headers: newHeaders, |
| 96 |
rows: newRows |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
// Remove column |
| 101 |
function removeColumn(index) { |
| 102 |
if (a.columns <= 1) return; |
| 103 |
var newHeaders = a.headers.filter(function (_, i) { return i !== index; }); |
| 104 |
var newRows = a.rows.map(function (row) { |
| 105 |
return row.filter(function (_, i) { return i !== index; }); |
| 106 |
}); |
| 107 |
setAttributes({ |
| 108 |
columns: a.columns - 1, |
| 109 |
headers: newHeaders, |
| 110 |
rows: newRows |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
// Move row |
| 115 |
function moveRow(index, direction) { |
| 116 |
var newIndex = index + direction; |
| 117 |
if (newIndex < 0 || newIndex >= a.rows.length) return; |
| 118 |
var newRows = a.rows.slice(); |
| 119 |
var temp = newRows[index]; |
| 120 |
newRows[index] = newRows[newIndex]; |
| 121 |
newRows[newIndex] = temp; |
| 122 |
setAttributes({ rows: newRows }); |
| 123 |
} |
| 124 |
|
| 125 |
// Move column |
| 126 |
function moveColumn(index, direction) { |
| 127 |
var newIndex = index + direction; |
| 128 |
if (newIndex < 0 || newIndex >= a.columns) return; |
| 129 |
var newHeaders = a.headers.slice(); |
| 130 |
var tempH = newHeaders[index]; |
| 131 |
newHeaders[index] = newHeaders[newIndex]; |
| 132 |
newHeaders[newIndex] = tempH; |
| 133 |
var newRows = a.rows.map(function (row) { |
| 134 |
var newRow = row.slice(); |
| 135 |
var temp = newRow[index]; |
| 136 |
newRow[index] = newRow[newIndex]; |
| 137 |
newRow[newIndex] = temp; |
| 138 |
return newRow; |
| 139 |
}); |
| 140 |
setAttributes({ headers: newHeaders, rows: newRows }); |
| 141 |
} |
| 142 |
|
| 143 |
// Import CSV |
| 144 |
function importCsv(csvText) { |
| 145 |
if (!csvText.trim()) return; |
| 146 |
// Normalize line endings |
| 147 |
var normalizedText = csvText.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| 148 |
var lines = normalizedText.trim().split('\n'); |
| 149 |
if (lines.length < 1) return; |
| 150 |
|
| 151 |
var parseRow = function(line) { |
| 152 |
var result = []; |
| 153 |
var current = ''; |
| 154 |
var inQuotes = false; |
| 155 |
for (var i = 0; i < line.length; i++) { |
| 156 |
var char = line[i]; |
| 157 |
if (char === '"') { |
| 158 |
if (inQuotes && line[i + 1] === '"') { |
| 159 |
// Escaped quote inside quoted field |
| 160 |
current += '"'; |
| 161 |
i++; |
| 162 |
} else { |
| 163 |
inQuotes = !inQuotes; |
| 164 |
} |
| 165 |
} else if (char === ',' && !inQuotes) { |
| 166 |
result.push(current.trim()); |
| 167 |
current = ''; |
| 168 |
} else { |
| 169 |
current += char; |
| 170 |
} |
| 171 |
} |
| 172 |
result.push(current.trim()); |
| 173 |
return result; |
| 174 |
}; |
| 175 |
|
| 176 |
var headers = parseRow(lines[0]); |
| 177 |
var rows = []; |
| 178 |
for (var i = 1; i < lines.length; i++) { |
| 179 |
if (lines[i].trim()) { |
| 180 |
rows.push(parseRow(lines[i])); |
| 181 |
} |
| 182 |
} |
| 183 |
if (rows.length === 0) { |
| 184 |
rows = [new Array(headers.length).fill('')]; |
| 185 |
} |
| 186 |
setAttributes({ |
| 187 |
columns: headers.length, |
| 188 |
headers: headers, |
| 189 |
rows: rows |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
// Style options |
| 194 |
var styleOptions = [ |
| 195 |
{ label: __('Default', 'blockenberg'), value: 'default' }, |
| 196 |
{ label: __('Bordered', 'blockenberg'), value: 'bordered' }, |
| 197 |
{ label: __('Minimal', 'blockenberg'), value: 'minimal' } |
| 198 |
]; |
| 199 |
|
| 200 |
var responsiveOptions = [ |
| 201 |
{ label: __('Horizontal Scroll', 'blockenberg'), value: 'scroll' }, |
| 202 |
{ label: __('Stack on Mobile', 'blockenberg'), value: 'stack' } |
| 203 |
]; |
| 204 |
|
| 205 |
var alignOptions = [ |
| 206 |
{ label: __('Left', 'blockenberg'), value: 'left' }, |
| 207 |
{ label: __('Center', 'blockenberg'), value: 'center' }, |
| 208 |
{ label: __('Right', 'blockenberg'), value: 'right' } |
| 209 |
]; |
| 210 |
|
| 211 |
var captionPosOptions = [ |
| 212 |
{ label: __('Top', 'blockenberg'), value: 'top' }, |
| 213 |
{ label: __('Bottom', 'blockenberg'), value: 'bottom' } |
| 214 |
]; |
| 215 |
|
| 216 |
var fontWeightOptions = [ |
| 217 |
{ label: '400', value: 400 }, |
| 218 |
{ label: '500', value: 500 }, |
| 219 |
{ label: '600', value: 600 }, |
| 220 |
{ label: '700', value: 700 } |
| 221 |
]; |
| 222 |
|
| 223 |
// State for CSV import |
| 224 |
var csvInput = wp.element.useState(''); |
| 225 |
var csvText = csvInput[0]; |
| 226 |
var setCsvText = csvInput[1]; |
| 227 |
|
| 228 |
// Inspector |
| 229 |
var inspector = el(InspectorControls, {}, |
| 230 |
// Features |
| 231 |
el(PanelBody, { title: __('Features', 'blockenberg'), initialOpen: true }, |
| 232 |
el(ToggleControl, { |
| 233 |
label: __('Enable Search', 'blockenberg'), |
| 234 |
checked: a.searchEnabled, |
| 235 |
__nextHasNoMarginBottom: true, |
| 236 |
onChange: function (v) { setAttributes({ searchEnabled: v }); } |
| 237 |
}), |
| 238 |
a.searchEnabled && el(TextControl, { |
| 239 |
label: __('Search Placeholder', 'blockenberg'), |
| 240 |
value: a.searchPlaceholder, |
| 241 |
onChange: function (v) { setAttributes({ searchPlaceholder: v }); } |
| 242 |
}), |
| 243 |
el(ToggleControl, { |
| 244 |
label: __('Enable Sorting', 'blockenberg'), |
| 245 |
checked: a.sortingEnabled, |
| 246 |
__nextHasNoMarginBottom: true, |
| 247 |
onChange: function (v) { setAttributes({ sortingEnabled: v }); } |
| 248 |
}), |
| 249 |
el(ToggleControl, { |
| 250 |
label: __('Enable Pagination', 'blockenberg'), |
| 251 |
checked: a.paginationEnabled, |
| 252 |
__nextHasNoMarginBottom: true, |
| 253 |
onChange: function (v) { setAttributes({ paginationEnabled: v }); } |
| 254 |
}), |
| 255 |
a.paginationEnabled && el(RangeControl, { |
| 256 |
label: __('Items Per Page', 'blockenberg'), |
| 257 |
value: a.itemsPerPage, |
| 258 |
min: 5, |
| 259 |
max: 100, |
| 260 |
step: 5, |
| 261 |
onChange: function (v) { setAttributes({ itemsPerPage: v }); } |
| 262 |
}), |
| 263 |
el(ToggleControl, { |
| 264 |
label: __('Enable Export', 'blockenberg'), |
| 265 |
checked: a.exportEnabled, |
| 266 |
__nextHasNoMarginBottom: true, |
| 267 |
onChange: function (v) { setAttributes({ exportEnabled: v }); } |
| 268 |
}), |
| 269 |
a.exportEnabled && el(TextControl, { |
| 270 |
label: __('Export Button Text', 'blockenberg'), |
| 271 |
value: a.exportCsvText, |
| 272 |
onChange: function (v) { setAttributes({ exportCsvText: v }); } |
| 273 |
}) |
| 274 |
), |
| 275 |
|
| 276 |
// Layout |
| 277 |
el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: false }, |
| 278 |
el(ToggleControl, { |
| 279 |
label: __('Sticky Header', 'blockenberg'), |
| 280 |
checked: a.stickyHeader, |
| 281 |
__nextHasNoMarginBottom: true, |
| 282 |
onChange: function (v) { setAttributes({ stickyHeader: v }); } |
| 283 |
}), |
| 284 |
el(ToggleControl, { |
| 285 |
label: __('Sticky First Column', 'blockenberg'), |
| 286 |
checked: a.stickyFirstColumn, |
| 287 |
__nextHasNoMarginBottom: true, |
| 288 |
onChange: function (v) { setAttributes({ stickyFirstColumn: v }); } |
| 289 |
}), |
| 290 |
el(ToggleControl, { |
| 291 |
label: __('Zebra Striping', 'blockenberg'), |
| 292 |
checked: a.zebraStriping, |
| 293 |
__nextHasNoMarginBottom: true, |
| 294 |
onChange: function (v) { setAttributes({ zebraStriping: v }); } |
| 295 |
}), |
| 296 |
el(ToggleControl, { |
| 297 |
label: __('Row Hover Highlight', 'blockenberg'), |
| 298 |
checked: a.rowHoverHighlight, |
| 299 |
__nextHasNoMarginBottom: true, |
| 300 |
onChange: function (v) { setAttributes({ rowHoverHighlight: v }); } |
| 301 |
}), |
| 302 |
el(SelectControl, { |
| 303 |
label: __('Responsive Mode', 'blockenberg'), |
| 304 |
value: a.responsiveMode, |
| 305 |
options: responsiveOptions, |
| 306 |
onChange: function (v) { setAttributes({ responsiveMode: v }); } |
| 307 |
}), |
| 308 |
el(TextControl, { |
| 309 |
label: __('Caption', 'blockenberg'), |
| 310 |
value: a.caption, |
| 311 |
onChange: function (v) { setAttributes({ caption: v }); } |
| 312 |
}), |
| 313 |
a.caption && el(SelectControl, { |
| 314 |
label: __('Caption Position', 'blockenberg'), |
| 315 |
value: a.captionPosition, |
| 316 |
options: captionPosOptions, |
| 317 |
onChange: function (v) { setAttributes({ captionPosition: v }); } |
| 318 |
}) |
| 319 |
), |
| 320 |
|
| 321 |
// Style |
| 322 |
el(PanelBody, { title: __('Style', 'blockenberg'), initialOpen: false }, |
| 323 |
el(SelectControl, { |
| 324 |
label: __('Table Style', 'blockenberg'), |
| 325 |
value: a.tableStyle, |
| 326 |
options: styleOptions, |
| 327 |
onChange: function (v) { setAttributes({ tableStyle: v }); } |
| 328 |
}), |
| 329 |
el(RangeControl, { |
| 330 |
label: __('Cell Padding', 'blockenberg'), |
| 331 |
value: a.cellPadding, |
| 332 |
min: 6, |
| 333 |
max: 24, |
| 334 |
onChange: function (v) { setAttributes({ cellPadding: v }); } |
| 335 |
}), |
| 336 |
el(RangeControl, { |
| 337 |
label: __('Border Width', 'blockenberg'), |
| 338 |
value: a.borderWidth, |
| 339 |
min: 0, |
| 340 |
max: 3, |
| 341 |
onChange: function (v) { setAttributes({ borderWidth: v }); } |
| 342 |
}), |
| 343 |
el(RangeControl, { |
| 344 |
label: __('Border Radius', 'blockenberg'), |
| 345 |
value: a.borderRadius, |
| 346 |
min: 0, |
| 347 |
max: 16, |
| 348 |
onChange: function (v) { setAttributes({ borderRadius: v }); } |
| 349 |
}) |
| 350 |
), |
| 351 |
|
| 352 |
// Typography |
| 353 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 354 |
el(RangeControl, { |
| 355 |
label: __('Header Font Size', 'blockenberg'), |
| 356 |
value: a.headerFontSize, |
| 357 |
min: 12, |
| 358 |
max: 20, |
| 359 |
onChange: function (v) { setAttributes({ headerFontSize: v }); } |
| 360 |
}), |
| 361 |
el(SelectControl, { |
| 362 |
label: __('Header Font Weight', 'blockenberg'), |
| 363 |
value: a.headerFontWeight, |
| 364 |
options: fontWeightOptions, |
| 365 |
onChange: function (v) { setAttributes({ headerFontWeight: parseInt(v, 10) }); } |
| 366 |
}), |
| 367 |
el(RangeControl, { |
| 368 |
label: __('Cell Font Size', 'blockenberg'), |
| 369 |
value: a.cellFontSize, |
| 370 |
min: 12, |
| 371 |
max: 18, |
| 372 |
onChange: function (v) { setAttributes({ cellFontSize: v }); } |
| 373 |
}), |
| 374 |
el(SelectControl, { |
| 375 |
label: __('Cell Font Weight', 'blockenberg'), |
| 376 |
value: a.cellFontWeight, |
| 377 |
options: fontWeightOptions, |
| 378 |
onChange: function (v) { setAttributes({ cellFontWeight: parseInt(v, 10) }); } |
| 379 |
}) |
| 380 |
), |
| 381 |
|
| 382 |
// Colors |
| 383 |
el(PanelColorSettings, { |
| 384 |
title: __('Header Colors', 'blockenberg'), |
| 385 |
initialOpen: false, |
| 386 |
colorSettings: [ |
| 387 |
{ value: a.headerBg, onChange: function (c) { setAttributes({ headerBg: c }); }, label: __('Background', 'blockenberg') }, |
| 388 |
{ value: a.headerColor, onChange: function (c) { setAttributes({ headerColor: c }); }, label: __('Text', 'blockenberg') }, |
| 389 |
{ value: a.headerBorderColor, onChange: function (c) { setAttributes({ headerBorderColor: c }); }, label: __('Border', 'blockenberg') } |
| 390 |
] |
| 391 |
}), |
| 392 |
|
| 393 |
el(PanelColorSettings, { |
| 394 |
title: __('Cell Colors', 'blockenberg'), |
| 395 |
initialOpen: false, |
| 396 |
colorSettings: [ |
| 397 |
{ value: a.cellBg, onChange: function (c) { setAttributes({ cellBg: c }); }, label: __('Background', 'blockenberg') }, |
| 398 |
{ value: a.cellBgAlt, onChange: function (c) { setAttributes({ cellBgAlt: c }); }, label: __('Alt Background', 'blockenberg') }, |
| 399 |
{ value: a.cellColor, onChange: function (c) { setAttributes({ cellColor: c }); }, label: __('Text', 'blockenberg') }, |
| 400 |
{ value: a.cellBorderColor, onChange: function (c) { setAttributes({ cellBorderColor: c }); }, label: __('Border', 'blockenberg') }, |
| 401 |
{ value: a.hoverBg, onChange: function (c) { setAttributes({ hoverBg: c }); }, label: __('Hover Background', 'blockenberg') }, |
| 402 |
{ value: a.accentColor, onChange: function (c) { setAttributes({ accentColor: c }); }, label: __('Accent', 'blockenberg') } |
| 403 |
] |
| 404 |
}), |
| 405 |
|
| 406 |
// Import |
| 407 |
el(PanelBody, { title: __('Import Data', 'blockenberg'), initialOpen: false }, |
| 408 |
el(TextareaControl, { |
| 409 |
label: __('Paste CSV Data', 'blockenberg'), |
| 410 |
help: __('First row will be used as headers.', 'blockenberg'), |
| 411 |
value: csvText, |
| 412 |
onChange: function (v) { setCsvText(v); }, |
| 413 |
rows: 6 |
| 414 |
}), |
| 415 |
el('div', { className: 'bkbg-dt-import-actions' }, |
| 416 |
el(Button, { |
| 417 |
variant: 'secondary', |
| 418 |
onClick: function () { importCsv(csvText); }, |
| 419 |
disabled: !csvText.trim() |
| 420 |
}, __('Import CSV', 'blockenberg')), |
| 421 |
el(Button, { |
| 422 |
variant: 'tertiary', |
| 423 |
onClick: function () { setCsvText(''); } |
| 424 |
}, __('Clear', 'blockenberg')) |
| 425 |
) |
| 426 |
) |
| 427 |
); |
| 428 |
|
| 429 |
// CSS variables |
| 430 |
var wrapStyle = { |
| 431 |
'--bkbg-dt-header-bg': a.headerBg, |
| 432 |
'--bkbg-dt-header-color': a.headerColor, |
| 433 |
'--bkbg-dt-header-border': a.headerBorderColor, |
| 434 |
'--bkbg-dt-cell-bg': a.cellBg, |
| 435 |
'--bkbg-dt-cell-bg-alt': a.cellBgAlt, |
| 436 |
'--bkbg-dt-cell-color': a.cellColor, |
| 437 |
'--bkbg-dt-cell-border': a.cellBorderColor, |
| 438 |
'--bkbg-dt-hover-bg': a.hoverBg, |
| 439 |
'--bkbg-dt-accent': a.accentColor, |
| 440 |
'--bkbg-dt-header-font-size': a.headerFontSize + 'px', |
| 441 |
'--bkbg-dt-header-font-weight': a.headerFontWeight, |
| 442 |
'--bkbg-dt-cell-font-size': a.cellFontSize + 'px', |
| 443 |
'--bkbg-dt-cell-font-weight': a.cellFontWeight, |
| 444 |
'--bkbg-dt-cell-padding': a.cellPadding + 'px', |
| 445 |
'--bkbg-dt-brd-w': a.borderWidth + 'px', |
| 446 |
'--bkbg-dt-radius': a.borderRadius + 'px' |
| 447 |
}; |
| 448 |
|
| 449 |
// Build editor table |
| 450 |
var headerCells = a.headers.map(function (header, colIndex) { |
| 451 |
return el('th', { className: 'bkbg-dt-editor-th', key: 'th-' + colIndex }, |
| 452 |
el('input', { |
| 453 |
className: 'bkbg-dt-editor-input', |
| 454 |
type: 'text', |
| 455 |
value: header, |
| 456 |
onChange: function (e) { updateHeader(colIndex, e.target.value); }, |
| 457 |
placeholder: __('Header', 'blockenberg') |
| 458 |
}), |
| 459 |
el('div', { className: 'bkbg-dt-col-actions' }, |
| 460 |
el(Button, { |
| 461 |
icon: 'table-col-before', |
| 462 |
label: __('Insert column before', 'blockenberg'), |
| 463 |
onClick: function () { insertColumn(colIndex, 'before'); }, |
| 464 |
isSmall: true |
| 465 |
}), |
| 466 |
el(Button, { |
| 467 |
icon: 'arrow-left-alt2', |
| 468 |
label: __('Move left', 'blockenberg'), |
| 469 |
onClick: function () { moveColumn(colIndex, -1); }, |
| 470 |
disabled: colIndex === 0, |
| 471 |
isSmall: true |
| 472 |
}), |
| 473 |
el(Button, { |
| 474 |
icon: 'arrow-right-alt2', |
| 475 |
label: __('Move right', 'blockenberg'), |
| 476 |
onClick: function () { moveColumn(colIndex, 1); }, |
| 477 |
disabled: colIndex === a.columns - 1, |
| 478 |
isSmall: true |
| 479 |
}), |
| 480 |
el(Button, { |
| 481 |
icon: 'table-col-after', |
| 482 |
label: __('Insert column after', 'blockenberg'), |
| 483 |
onClick: function () { insertColumn(colIndex, 'after'); }, |
| 484 |
isSmall: true |
| 485 |
}), |
| 486 |
el(Button, { |
| 487 |
icon: 'trash', |
| 488 |
label: __('Remove column', 'blockenberg'), |
| 489 |
onClick: function () { removeColumn(colIndex); }, |
| 490 |
isDestructive: true, |
| 491 |
isSmall: true, |
| 492 |
disabled: a.columns <= 1 |
| 493 |
}) |
| 494 |
) |
| 495 |
); |
| 496 |
}); |
| 497 |
|
| 498 |
var bodyRows = a.rows.map(function (row, rowIndex) { |
| 499 |
var cells = row.map(function (cell, colIndex) { |
| 500 |
return el('td', { className: 'bkbg-dt-editor-td', key: 'td-' + colIndex }, |
| 501 |
el('input', { |
| 502 |
className: 'bkbg-dt-editor-input', |
| 503 |
type: 'text', |
| 504 |
value: cell, |
| 505 |
onChange: function (e) { updateCell(rowIndex, colIndex, e.target.value); }, |
| 506 |
placeholder: '' |
| 507 |
}) |
| 508 |
); |
| 509 |
}); |
| 510 |
|
| 511 |
return el('tr', { className: 'bkbg-dt-editor-tr', key: 'tr-' + rowIndex }, |
| 512 |
cells, |
| 513 |
el('td', { className: 'bkbg-dt-row-actions' }, |
| 514 |
el(Button, { |
| 515 |
icon: 'table-row-before', |
| 516 |
label: __('Insert row before', 'blockenberg'), |
| 517 |
onClick: function () { insertRow(rowIndex, 'before'); }, |
| 518 |
isSmall: true |
| 519 |
}), |
| 520 |
el(Button, { |
| 521 |
icon: 'arrow-up-alt2', |
| 522 |
label: __('Move up', 'blockenberg'), |
| 523 |
onClick: function () { moveRow(rowIndex, -1); }, |
| 524 |
disabled: rowIndex === 0, |
| 525 |
isSmall: true |
| 526 |
}), |
| 527 |
el(Button, { |
| 528 |
icon: 'arrow-down-alt2', |
| 529 |
label: __('Move down', 'blockenberg'), |
| 530 |
onClick: function () { moveRow(rowIndex, 1); }, |
| 531 |
disabled: rowIndex === a.rows.length - 1, |
| 532 |
isSmall: true |
| 533 |
}), |
| 534 |
el(Button, { |
| 535 |
icon: 'table-row-after', |
| 536 |
label: __('Insert row after', 'blockenberg'), |
| 537 |
onClick: function () { insertRow(rowIndex, 'after'); }, |
| 538 |
isSmall: true |
| 539 |
}), |
| 540 |
el(Button, { |
| 541 |
icon: 'trash', |
| 542 |
label: __('Remove row', 'blockenberg'), |
| 543 |
onClick: function () { removeRow(rowIndex); }, |
| 544 |
isDestructive: true, |
| 545 |
isSmall: true, |
| 546 |
disabled: a.rows.length <= 1 |
| 547 |
}) |
| 548 |
) |
| 549 |
); |
| 550 |
}); |
| 551 |
|
| 552 |
var blockProps = useBlockProps({ |
| 553 |
className: 'bkbg-editor-wrap', |
| 554 |
'data-block-label': 'Data Table' |
| 555 |
}); |
| 556 |
|
| 557 |
return el('div', blockProps, |
| 558 |
inspector, |
| 559 |
el('div', { |
| 560 |
className: 'bkbg-dt-wrap', |
| 561 |
style: wrapStyle, |
| 562 |
'data-style': a.tableStyle, |
| 563 |
'data-zebra': a.zebraStriping ? '1' : '0', |
| 564 |
'data-hover': a.rowHoverHighlight ? '1' : '0' |
| 565 |
}, |
| 566 |
el('div', { className: 'bkbg-dt-container' }, |
| 567 |
el('table', { className: 'bkbg-dt-editor-table' }, |
| 568 |
el('thead', {}, |
| 569 |
el('tr', {}, headerCells) |
| 570 |
), |
| 571 |
el('tbody', {}, bodyRows) |
| 572 |
) |
| 573 |
) |
| 574 |
), |
| 575 |
el('div', { className: 'bkbg-editor-actions' }, |
| 576 |
el(Button, { variant: 'secondary', icon: 'plus-alt2', onClick: addRow }, __('Add Row', 'blockenberg')), |
| 577 |
el(Button, { variant: 'secondary', icon: 'plus-alt2', onClick: addColumn }, __('Add Column', 'blockenberg')) |
| 578 |
) |
| 579 |
); |
| 580 |
}, |
| 581 |
|
| 582 |
save: function (props) { |
| 583 |
var a = props.attributes; |
| 584 |
|
| 585 |
var wrapStyle = { |
| 586 |
'--bkbg-dt-header-bg': a.headerBg, |
| 587 |
'--bkbg-dt-header-color': a.headerColor, |
| 588 |
'--bkbg-dt-header-border': a.headerBorderColor, |
| 589 |
'--bkbg-dt-cell-bg': a.cellBg, |
| 590 |
'--bkbg-dt-cell-bg-alt': a.cellBgAlt, |
| 591 |
'--bkbg-dt-cell-color': a.cellColor, |
| 592 |
'--bkbg-dt-cell-border': a.cellBorderColor, |
| 593 |
'--bkbg-dt-hover-bg': a.hoverBg, |
| 594 |
'--bkbg-dt-accent': a.accentColor, |
| 595 |
'--bkbg-dt-header-font-size': a.headerFontSize + 'px', |
| 596 |
'--bkbg-dt-header-font-weight': a.headerFontWeight, |
| 597 |
'--bkbg-dt-cell-font-size': a.cellFontSize + 'px', |
| 598 |
'--bkbg-dt-cell-font-weight': a.cellFontWeight, |
| 599 |
'--bkbg-dt-cell-padding': a.cellPadding + 'px', |
| 600 |
'--bkbg-dt-brd-w': a.borderWidth + 'px', |
| 601 |
'--bkbg-dt-radius': a.borderRadius + 'px' |
| 602 |
}; |
| 603 |
|
| 604 |
// Search icon SVG |
| 605 |
var searchIcon = el('svg', { |
| 606 |
className: 'bkbg-dt-search-icon', |
| 607 |
viewBox: '0 0 24 24', |
| 608 |
fill: 'none', |
| 609 |
stroke: 'currentColor', |
| 610 |
strokeWidth: '2' |
| 611 |
}, |
| 612 |
el('circle', { cx: '11', cy: '11', r: '8' }), |
| 613 |
el('path', { d: 'M21 21l-4.35-4.35' }) |
| 614 |
); |
| 615 |
|
| 616 |
// Export icon |
| 617 |
var exportIcon = el('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: '2' }, |
| 618 |
el('path', { d: 'M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4' }), |
| 619 |
el('polyline', { points: '7 10 12 15 17 10' }), |
| 620 |
el('line', { x1: '12', y1: '15', x2: '12', y2: '3' }) |
| 621 |
); |
| 622 |
|
| 623 |
// Sort icon |
| 624 |
var sortIcon = el('span', { className: 'bkbg-dt-sort-icon' }, |
| 625 |
el('svg', { viewBox: '0 0 24 24', fill: 'currentColor' }, |
| 626 |
el('path', { d: 'M7 10l5 5 5-5z' }) |
| 627 |
) |
| 628 |
); |
| 629 |
|
| 630 |
// Controls bar |
| 631 |
var controls = (a.searchEnabled || a.exportEnabled) && el('div', { className: 'bkbg-dt-controls' }, |
| 632 |
a.searchEnabled && el('div', { className: 'bkbg-dt-search' }, |
| 633 |
searchIcon, |
| 634 |
el('input', { |
| 635 |
type: 'text', |
| 636 |
className: 'bkbg-dt-search-input', |
| 637 |
placeholder: a.searchPlaceholder, |
| 638 |
'data-search': '1' |
| 639 |
}) |
| 640 |
), |
| 641 |
a.exportEnabled && el('div', { className: 'bkbg-dt-export-btns' }, |
| 642 |
el('button', { className: 'bkbg-dt-btn', 'data-export': 'csv' }, |
| 643 |
exportIcon, |
| 644 |
a.exportCsvText |
| 645 |
) |
| 646 |
) |
| 647 |
); |
| 648 |
|
| 649 |
// Table header |
| 650 |
var headerCells = a.headers.map(function (header, index) { |
| 651 |
return el('th', { |
| 652 |
className: 'bkbg-dt-th', |
| 653 |
key: 'th-' + index, |
| 654 |
'data-col': index |
| 655 |
}, |
| 656 |
header, |
| 657 |
a.sortingEnabled && sortIcon |
| 658 |
); |
| 659 |
}); |
| 660 |
|
| 661 |
// Table body |
| 662 |
var bodyRows = a.rows.map(function (row, rowIndex) { |
| 663 |
var cells = row.map(function (cell, colIndex) { |
| 664 |
return el('td', { |
| 665 |
className: 'bkbg-dt-td', |
| 666 |
key: 'td-' + colIndex, |
| 667 |
'data-label': a.headers[colIndex] || '' |
| 668 |
}, cell); |
| 669 |
}); |
| 670 |
return el('tr', { className: 'bkbg-dt-tr', key: 'tr-' + rowIndex }, cells); |
| 671 |
}); |
| 672 |
|
| 673 |
// Caption |
| 674 |
var caption = a.caption && el('caption', { className: 'bkbg-dt-caption' }, a.caption); |
| 675 |
|
| 676 |
return el('div', { |
| 677 |
className: 'bkbg-dt-wrap', |
| 678 |
style: wrapStyle, |
| 679 |
'data-style': a.tableStyle, |
| 680 |
'data-sortable': a.sortingEnabled ? '1' : '0', |
| 681 |
'data-paginate': a.paginationEnabled ? '1' : '0', |
| 682 |
'data-per-page': a.itemsPerPage, |
| 683 |
'data-zebra': a.zebraStriping ? '1' : '0', |
| 684 |
'data-hover': a.rowHoverHighlight ? '1' : '0', |
| 685 |
'data-sticky-header': a.stickyHeader ? '1' : '0', |
| 686 |
'data-sticky-col': a.stickyFirstColumn ? '1' : '0', |
| 687 |
'data-responsive': a.responsiveMode, |
| 688 |
'data-caption-pos': a.captionPosition |
| 689 |
}, |
| 690 |
controls, |
| 691 |
el('div', { className: 'bkbg-dt-container' }, |
| 692 |
el('table', { className: 'bkbg-dt-table' }, |
| 693 |
caption, |
| 694 |
el('thead', { className: 'bkbg-dt-thead' }, |
| 695 |
el('tr', {}, headerCells) |
| 696 |
), |
| 697 |
el('tbody', { className: 'bkbg-dt-tbody' }, bodyRows) |
| 698 |
) |
| 699 |
) |
| 700 |
); |
| 701 |
} |
| 702 |
}); |
| 703 |
}); |
| 704 |
|
| 705 |
|
| 706 |
|