| 1 |
<?php |
| 2 |
namespace UiCoreElements; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Repeater; |
| 7 |
use Elementor\Modules\NestedElements\Controls\Control_Nested_Repeater; |
| 8 |
use UiCoreElements\Utils\Animation_Trait; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit(); |
| 11 |
|
| 12 |
class CustomTable extends UiCoreNestedWidget { |
| 13 |
|
| 14 |
use Animation_Trait; |
| 15 |
|
| 16 |
public function get_name() |
| 17 |
{ |
| 18 |
return 'uicore-custom-table'; |
| 19 |
} |
| 20 |
public function get_title() |
| 21 |
{ |
| 22 |
return esc_html__('Custom Table', 'uicore-elements'); |
| 23 |
} |
| 24 |
public function get_icon() |
| 25 |
{ |
| 26 |
return 'eicon-table ui-e-widget'; |
| 27 |
} |
| 28 |
public function get_categories() |
| 29 |
{ |
| 30 |
return ['uicore']; |
| 31 |
} |
| 32 |
public function get_keywords() |
| 33 |
{ |
| 34 |
return ['slide', 'table', 'nested']; |
| 35 |
} |
| 36 |
public function get_styles() |
| 37 |
{ |
| 38 |
return ['custom-table']; |
| 39 |
} |
| 40 |
public function get_scripts() |
| 41 |
{ |
| 42 |
return []; |
| 43 |
} |
| 44 |
|
| 45 |
protected function carousel_content_container( int $index ) { |
| 46 |
return [ |
| 47 |
'elType' => 'container', |
| 48 |
'settings' => [ |
| 49 |
'_title' => sprintf( __( 'Cel #%s', 'uicore-elements' ), $index ), |
| 50 |
'content_width' => 'full', |
| 51 |
// 'flex_justify_content' => 'center', |
| 52 |
// 'flex_align_items' => 'center', |
| 53 |
// the settings work only ofr first default items |
| 54 |
], |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
protected function get_default_children_elements() { |
| 59 |
return [ |
| 60 |
$this->carousel_content_container( 1 ), |
| 61 |
$this->carousel_content_container( 2 ), |
| 62 |
$this->carousel_content_container( 3 ), |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
protected function get_default_repeater_title_setting_key() { |
| 67 |
return 'table_cels'; |
| 68 |
} |
| 69 |
|
| 70 |
protected function get_default_children_title() { |
| 71 |
return esc_html__( 'Cell #%d', 'uicore-elements' ); |
| 72 |
} |
| 73 |
protected function get_default_children_placeholder_selector() { |
| 74 |
return '.ui-e-table'; |
| 75 |
} |
| 76 |
|
| 77 |
protected function register_controls() |
| 78 |
{ |
| 79 |
if( !Plugin::$instance->experiments->is_feature_active('nested-elements') ) { |
| 80 |
$this->nesting_fallback('controls'); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$this->start_controls_section( |
| 85 |
'section_content', |
| 86 |
[ |
| 87 |
'label' => __('Items', 'uicore-elements'), |
| 88 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
|
| 93 |
//Columns |
| 94 |
$columns = new Repeater(); |
| 95 |
$columns->add_responsive_control( |
| 96 |
'col_size', |
| 97 |
[ |
| 98 |
'label' => __('Column Size', 'uicore-elements'), |
| 99 |
'type' => Controls_Manager::SLIDER, |
| 100 |
'size_units' => ['px', 'fr','custom'], |
| 101 |
'default' => [ |
| 102 |
'size' => 300, |
| 103 |
'unit' => 'px', |
| 104 |
], |
| 105 |
'range' => [ |
| 106 |
'px' => [ |
| 107 |
'min' => 1, |
| 108 |
'max' => 1200, |
| 109 |
], |
| 110 |
'fr' => [ |
| 111 |
'min' => 1, |
| 112 |
'max' => 12, |
| 113 |
], |
| 114 |
], |
| 115 |
'selectors' => [ |
| 116 |
'{{WRAPPER}}' => '--ui-e-last:{{SIZE}}{{UNIT}};', |
| 117 |
], |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
$this->add_control( |
| 122 |
'columns', |
| 123 |
[ |
| 124 |
'type' => Controls_Manager::REPEATER, |
| 125 |
'fields' => $columns->get_controls(), |
| 126 |
'title_field' => 'Column', |
| 127 |
'render_type' => 'template', |
| 128 |
'default' => [ |
| 129 |
[ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ], |
| 130 |
[ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ], |
| 131 |
[ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ], |
| 132 |
], |
| 133 |
] |
| 134 |
); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
//Cells |
| 139 |
$repeater = new Repeater(); |
| 140 |
$repeater->add_control( |
| 141 |
'item', |
| 142 |
[ |
| 143 |
'label' => __('Title', 'uicore-elements'), |
| 144 |
'type' => Controls_Manager::TEXT, |
| 145 |
] |
| 146 |
); |
| 147 |
|
| 148 |
$this->add_control( |
| 149 |
'cells', |
| 150 |
[ |
| 151 |
'type' => Control_Nested_Repeater::CONTROL_TYPE, |
| 152 |
'fields' => $repeater->get_controls(), |
| 153 |
'allow_empty' => false, |
| 154 |
'default' => [ |
| 155 |
[ 'item' => __( 'Cel #1', 'uicore-elements' ) ], |
| 156 |
[ 'item' => __( 'Cel #2', 'uicore-elements' ) ], |
| 157 |
[ 'item' => __( 'Cel #3', 'uicore-elements' ) ], |
| 158 |
], |
| 159 |
] |
| 160 |
); |
| 161 |
|
| 162 |
$this->end_controls_section(); |
| 163 |
|
| 164 |
|
| 165 |
//style |
| 166 |
$this->start_controls_section( |
| 167 |
'section_table_style', |
| 168 |
[ |
| 169 |
'label' => __('Table Style', 'uicore-elements'), |
| 170 |
'tab' => Controls_Manager::TAB_STYLE, |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
//even rows background color |
| 175 |
$this->add_control( |
| 176 |
'even_row_bg_color', |
| 177 |
[ |
| 178 |
'label' => __('Even Rows Background', 'uicore-elements'), |
| 179 |
'type' => Controls_Manager::COLOR, |
| 180 |
'selectors' => [ |
| 181 |
'{{WRAPPER}} .ui-e-table .ui-e-even' => 'background-color: {{VALUE}};', |
| 182 |
], |
| 183 |
] |
| 184 |
); |
| 185 |
|
| 186 |
|
| 187 |
$this->end_controls_section(); |
| 188 |
} |
| 189 |
|
| 190 |
public function render() |
| 191 |
{ |
| 192 |
if(!Plugin::$instance->experiments->is_feature_active('nested-elements')) { |
| 193 |
$this->nesting_fallback(); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$cells = $this->get_settings_for_display('cells'); |
| 198 |
$cols = $this->get_settings_for_display('columns'); |
| 199 |
|
| 200 |
// Build the grid columns css |
| 201 |
if ($cols) { |
| 202 |
|
| 203 |
$grid_css = ''; |
| 204 |
$grid_columns = []; |
| 205 |
$breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints(); |
| 206 |
|
| 207 |
// Columns loop |
| 208 |
foreach ($cols as $index => $item) { |
| 209 |
|
| 210 |
// Register default desktop size |
| 211 |
$grid_columns['desktop'][] = $item['col_size']['size'] . $item['col_size']['unit']; |
| 212 |
|
| 213 |
// Check for the current column responsive sizes |
| 214 |
foreach ($breakpoints as $breakpoint => $object) { |
| 215 |
$size_slug = 'col_size_' . $breakpoint; |
| 216 |
|
| 217 |
// Register the current column responsive size if it exists and |
| 218 |
// the value is not empty, because empty values will break the table design |
| 219 |
if ( isset($item[$size_slug]) && !empty($item[$size_slug]['size']) ) { |
| 220 |
$grid_columns[$breakpoint][] = $item[$size_slug]['size'] . $item[$size_slug]['unit']; |
| 221 |
|
| 222 |
// Use the default size from desktop otherwhise |
| 223 |
} else { |
| 224 |
$grid_columns[$breakpoint][] = $item['col_size']['size'] . $item['col_size']['unit']; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Generate the css variables |
| 230 |
foreach ($grid_columns as $device => $values) { |
| 231 |
$breakpoint = $device === 'desktop' ? '' : '-' . $device; |
| 232 |
$grid_css .= '--ui-e-table-cols' . $breakpoint . ':' . implode(' ', $values) . ';'; |
| 233 |
} |
| 234 |
|
| 235 |
// Add it to the widget wrapper |
| 236 |
$this->add_render_attribute('_wrapper', [ |
| 237 |
'style' => $grid_css, |
| 238 |
]); |
| 239 |
} |
| 240 |
|
| 241 |
?> |
| 242 |
<div class="ui-e-table"> |
| 243 |
<?php |
| 244 |
|
| 245 |
$columns = count($cols); |
| 246 |
|
| 247 |
foreach ($cells as $index => $item) { |
| 248 |
$is_even = floor($index / $columns) % 2 !== 0; |
| 249 |
|
| 250 |
// TODO: maybe there's a way of adding the class to the child wrapper instead of printing an extra element |
| 251 |
if ($is_even) { |
| 252 |
?> |
| 253 |
<div class="ui-e-even"> |
| 254 |
<?php $this->print_child($index); ?> |
| 255 |
</div> |
| 256 |
<?php |
| 257 |
} else { |
| 258 |
$this->print_child($index); |
| 259 |
} |
| 260 |
} |
| 261 |
?> |
| 262 |
</div> |
| 263 |
<?php |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
protected function get_initial_config(): array { |
| 268 |
if (Plugin::$instance->experiments->is_feature_active('e_nested_atomic_repeaters')) { |
| 269 |
return array_merge(parent::get_initial_config(), [ |
| 270 |
'support_improved_repeaters' => true, |
| 271 |
'node' => '.ui-e-table' |
| 272 |
]); |
| 273 |
} |
| 274 |
|
| 275 |
return parent::get_initial_config(); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
protected function content_template() { |
| 280 |
?> |
| 281 |
<# |
| 282 |
const cols = settings.columns; |
| 283 |
const cols_qty = '--ui-e-cols-qty:' + settings.columns.length; |
| 284 |
|
| 285 |
if ( cols ) { |
| 286 |
let gridCss = ''; |
| 287 |
const gridColumns = {}; |
| 288 |
const breakpoints = elementorFrontend.config.breakpoints.activeBreakpoints; |
| 289 |
|
| 290 |
_.each( cols, function( item, index ) { |
| 291 |
|
| 292 |
if ( ! gridColumns.desktop ) { |
| 293 |
gridColumns.desktop = []; |
| 294 |
} |
| 295 |
|
| 296 |
gridColumns.desktop.push( item.col_size.size + item.col_size.unit ); |
| 297 |
|
| 298 |
_.each( breakpoints, function( object, breakpoint ) { |
| 299 |
const sizeSlug = 'col_size_' + breakpoint; |
| 300 |
|
| 301 |
if ( item[sizeSlug] && item[sizeSlug].size ) { |
| 302 |
if ( ! gridColumns[breakpoint] ) { |
| 303 |
gridColumns[breakpoint] = []; |
| 304 |
} |
| 305 |
gridColumns[breakpoint].push( item[sizeSlug].size + item[sizeSlug].unit ); |
| 306 |
} else { |
| 307 |
if ( ! gridColumns[breakpoint] ) { |
| 308 |
gridColumns[breakpoint] = []; |
| 309 |
} |
| 310 |
gridColumns[breakpoint].push( item.col_size.size + item.col_size.unit ); |
| 311 |
} |
| 312 |
}); |
| 313 |
}); |
| 314 |
|
| 315 |
_.each( gridColumns, function( values, device ) { |
| 316 |
const breakpoint = device === 'desktop' ? '' : '-' + device; |
| 317 |
gridCss += '--ui-e-table-cols' + breakpoint + ':' + values.join(' ') + ';'; |
| 318 |
}); |
| 319 |
|
| 320 |
view.addRenderAttribute('table', 'style', [gridCss, cols_qty].join(' ')); |
| 321 |
} |
| 322 |
#> |
| 323 |
<div class="ui-e-table" {{{ view.getRenderAttributeString('table') }}}> |
| 324 |
|
| 325 |
</div> |
| 326 |
<?php |
| 327 |
} |
| 328 |
} |
| 329 |
\Elementor\Plugin::instance()->widgets_manager->register(new CustomTable()); |