PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.2.1
UiCore Elements – Free widgets and templates for Elementor v1.2.1
1.3.17 1.3.16 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 41 releases
uicore-elements / includes / widgets / custom-table.php

custom-table.php in UiCore Elements – Free widgets and templates for Elementor 1.2.1, at includes/widgets/custom-table.php

341 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // TODO: remove or set as false, after 3.30, when the full deprecation of widget innet wrapper is ready
45 public function has_widget_inner_wrapper(): bool {
46 return true;
47 }
48
49 protected function carousel_content_container( int $index ) {
50 return [
51 'elType' => 'container',
52 'settings' => [
53 /* translators: %s: Item index */
54 '_title' => sprintf( __( 'Cel #%s', 'uicore-elements' ), $index ),
55 'content_width' => 'full',
56 // 'flex_justify_content' => 'center',
57 // 'flex_align_items' => 'center',
58 // the settings work only ofr first default items
59 ],
60 ];
61 }
62
63 protected function get_default_children_elements() {
64 return [
65 $this->carousel_content_container( 1 ),
66 $this->carousel_content_container( 2 ),
67 $this->carousel_content_container( 3 ),
68 ];
69 }
70
71 protected function get_default_repeater_title_setting_key() {
72 return 'table_cels';
73 }
74
75 protected function get_default_children_title() {
76 /* translators: %d: Item index */
77 return esc_html__( 'Cell #%d', 'uicore-elements' );
78 }
79 protected function get_default_children_placeholder_selector() {
80 return '.ui-e-table';
81 }
82
83 protected function register_controls()
84 {
85 if( !Plugin::$instance->experiments->is_feature_active('nested-elements') ) {
86 $this->nesting_fallback('controls');
87 return;
88 }
89
90 $this->start_controls_section(
91 'section_content',
92 [
93 'label' => __('Items', 'uicore-elements'),
94 'tab' => Controls_Manager::TAB_CONTENT,
95 ]
96 );
97
98
99 //Columns
100 $columns = new Repeater();
101 $columns->add_responsive_control(
102 'col_size',
103 [
104 'label' => __('Column Size', 'uicore-elements'),
105 'type' => Controls_Manager::SLIDER,
106 'size_units' => ['px', 'fr','custom'],
107 'default' => [
108 'size' => 300,
109 'unit' => 'px',
110 ],
111 'range' => [
112 'px' => [
113 'min' => 1,
114 'max' => 1200,
115 ],
116 'fr' => [
117 'min' => 1,
118 'max' => 12,
119 ],
120 ],
121 'render_type' => 'template',
122 'selectors' => [
123 '{{WRAPPER}}' => '--ui-e-last:{{SIZE}}{{UNIT}};',
124 ],
125 ]
126 );
127
128 $this->add_control(
129 'columns',
130 [
131 'type' => Controls_Manager::REPEATER,
132 'fields' => $columns->get_controls(),
133 'title_field' => 'Column',
134 'render_type' => 'template',
135 'default' => [
136 [ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ],
137 [ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ],
138 [ 'col_size' => [ 'size' => 1, 'unit' => 'fr' ] ],
139 ],
140 ]
141 );
142
143
144
145 //Cells
146 $repeater = new Repeater();
147 $repeater->add_control(
148 'item',
149 [
150 'label' => __('Title', 'uicore-elements'),
151 'type' => Controls_Manager::TEXT,
152 ]
153 );
154
155 $this->add_control(
156 'cells',
157 [
158 'type' => Control_Nested_Repeater::CONTROL_TYPE,
159 'fields' => $repeater->get_controls(),
160 'title_field' => '{{{ item }}}',
161 'allow_empty' => false,
162 'default' => [
163 [ 'item' => __( 'Cel #1', 'uicore-elements' ) ],
164 [ 'item' => __( 'Cel #2', 'uicore-elements' ) ],
165 [ 'item' => __( 'Cel #3', 'uicore-elements' ) ],
166 ],
167 ]
168 );
169
170 $this->end_controls_section();
171
172
173 //style
174 $this->start_controls_section(
175 'section_table_style',
176 [
177 'label' => __('Table Style', 'uicore-elements'),
178 'tab' => Controls_Manager::TAB_STYLE,
179 ]
180 );
181
182 //even rows background color
183 $this->add_control(
184 'even_row_bg_color',
185 [
186 'label' => __('Even Rows Background', 'uicore-elements'),
187 'type' => Controls_Manager::COLOR,
188 'selectors' => [
189 '{{WRAPPER}} .ui-e-table .ui-e-even' => 'background-color: {{VALUE}};',
190 ],
191 ]
192 );
193
194
195 $this->end_controls_section();
196 }
197
198 protected function build_grid_css($cols)
199 {
200 // Build the grid columns css
201 if ($cols) {
202 $grid_css = '';
203 $grid_columns = [];
204 $breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints();
205
206 $fallback = [
207 'size' => 2,
208 'unit' => 'fr',
209 ];
210
211 foreach ($cols as $index => $item) {
212
213 // Set desktop column size
214 $grid_columns['desktop'][] = isset($item['col_size']['size']) && isset($item['col_size']['unit'])
215 ? $item['col_size']['size'] . $item['col_size']['unit']
216 : $fallback['size'] . $fallback['unit'];
217
218 foreach ($breakpoints as $breakpoint => $object) {
219 $size_slug = 'col_size_' . $breakpoint;
220
221 // Register the current column responsive size if exists and the
222 // value is not empty, because empty values breaks the table design
223 $grid_columns[$breakpoint][] = isset($item[$size_slug]['size']) && isset($item[$size_slug]['unit'])
224 ? $item[$size_slug]['size'] . $item[$size_slug]['unit']
225 : $grid_columns['desktop'][$index];
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 return $grid_css;
236 }
237 }
238
239 public function render()
240 {
241 if(!Plugin::$instance->experiments->is_feature_active('nested-elements')) {
242 $this->nesting_fallback();
243 return;
244 }
245
246 $cells = $this->get_settings_for_display('cells');
247 $cols = $this->get_settings_for_display('columns');
248 $grid_css = $this->build_grid_css($cols);
249
250 // Add it to the widget wrapper
251 $this->add_render_attribute('_wrapper', ['style' => $grid_css]);
252
253 ?>
254 <div class="ui-e-table">
255 <?php
256
257 $columns = count($cols);
258
259 foreach ($cells as $index => $item) {
260 $is_even = floor($index / $columns) % 2 !== 0;
261
262 // TODO: maybe there's a way of adding the class to the child wrapper instead of printing an extra element
263 if ($is_even) {
264 ?>
265 <div class="ui-e-even">
266 <?php $this->print_child($index); ?>
267 </div>
268 <?php
269 } else {
270 $this->print_child($index);
271 }
272 }
273 ?>
274 </div>
275 <?php
276 }
277
278
279 protected function get_initial_config(): array {
280 if (Plugin::$instance->experiments->is_feature_active('e_nested_atomic_repeaters')) {
281 return array_merge(parent::get_initial_config(), [
282 'support_improved_repeaters' => true,
283 'node' => '.ui-e-table'
284 ]);
285 }
286
287 return parent::get_initial_config();
288 }
289
290
291 protected function content_template() {
292 ?>
293 <#
294 const cols = settings.columns;
295 const cols_qty = '--ui-e-cols-qty:' + settings.columns.length;
296
297 if ( cols ) {
298 let gridCss = '';
299 const gridColumns = {};
300 const breakpoints = elementorFrontend.config.responsive.activeBreakpoints;
301
302 _.each( cols, function( item, index ) {
303
304 if ( ! gridColumns.desktop ) {
305 gridColumns.desktop = [];
306 }
307
308 gridColumns.desktop.push( item.col_size.size + item.col_size.unit );
309
310 _.each( breakpoints, function( object, breakpoint ) {
311 const sizeSlug = 'col_size_' + breakpoint;
312
313 if ( item[sizeSlug] && item[sizeSlug].size ) {
314 if ( ! gridColumns[breakpoint] ) {
315 gridColumns[breakpoint] = [];
316 }
317 gridColumns[breakpoint].push( item[sizeSlug].size + item[sizeSlug].unit );
318 } else {
319 if ( ! gridColumns[breakpoint] ) {
320 gridColumns[breakpoint] = [];
321 }
322 gridColumns[breakpoint].push( item.col_size.size + item.col_size.unit );
323 }
324 });
325 });
326
327 _.each( gridColumns, function( values, device ) {
328 const breakpoint = device === 'desktop' ? '' : '-' + device;
329 gridCss += '--ui-e-table-cols' + breakpoint + ':' + values.join(' ') + ';';
330 });
331
332 view.addRenderAttribute('table', 'style', [gridCss, cols_qty].join(' '));
333 }
334 #>
335 <div class="ui-e-table" {{{ view.getRenderAttributeString('table') }}}>
336
337 </div>
338 <?php
339 }
340 }
341 \Elementor\Plugin::instance()->widgets_manager->register(new CustomTable());