-
Notifications
You must be signed in to change notification settings - Fork 0
/
zukit-table.php
278 lines (243 loc) · 7.5 KB
/
zukit-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
// Plugin Table Class ---------------------------------------------------------]
class zukit_Table {
private $headers = [];
private $align = ['left', 'center', 'right'];
private $colors = [
'red' => '#ff3358',
'green' => '#5b9a68',
'blue' => '#00aced',
'violet' => '#bc2a8d',
'orange' => '#fb8f3d',
];
private $config = ['align' => [], 'style' => [], 'className' => []];
private $cells = [];
private $row = [];
private $rows = [];
public function __construct($cells = []) {
foreach($cells as $cell) {
$this->cells[$cell] = $this->generate_cell(ucwords($cell));
}
$count = count($this->cells);
$this->config['template'] = "repeat(${columns}, 1fr)";
$this->config['align'] = array_fill(0, $count, null);
$this->config['style'] = array_fill(0, $count, null);
$this->config['className'] = array_fill(0, $count, '');
foreach($this->cells as $cell => $content) {
$this->config($cell, 'className', "cell__$cell");
}
}
private function cell_index($cell) {
return isset($this->cells[$cell]) ? array_search($this->cells[$cell], array_values($this->cells)) : false;
}
private function has($cell) {
return isset($this->cells[$cell]);
}
private function align_cell($align = 'left') {
return in_array($align, $this->align) ? $align : 'left';
}
private function params_for_cell($params) {
$params = is_array($params) ? $params : [$params];
$validated = [];
foreach($params as $key => $value) {
if($value === 'markdown') $validated[$value] = true;
else if($key === 'link' && is_array($value) && zu_snippets()->validate_url($value[0])) {
$validated['link'] = [
'href' => $value[0],
'title' => $value[1] ?? ''
];
} else {
$validated[$key] = $value;
}
}
return empty($validated) ? null : $validated;
}
private function color_cell(&$style) {
if(isset($style['color'])) {
$color = $style['color'];
$style['color'] = in_array($color, array_keys($this->colors)) ? $this->colors[$color] : $color;
}
}
private function style_cell($style, &$cell, $config_index = false) {
if(is_array($style)) {
$this->color_cell($style);
if($config_index !== false) $this->config['style'][$config_index] = $style;
else $cell['style'] = $style;
}
}
private function generate_cell($content, $style = null, $align = null, $params = null) {
$cell = ['content' => $content];
if(!empty($align)) $cell['align'] = $this->align_cell($align);
if(!empty($params)) $cell['params'] = $this->params_for_cell($params);
$this->style_cell($style, $cell);
return $cell;
}
private function empty_cell() {
return $this->generate_cell('');
}
private function config($cells, $key, $value) {
if(is_string($cells)) $cells = [$cells];
foreach($cells as $cell) {
$index = $this->cell_index($cell);
if($index !== false) {
if($key === 'style') $this->style_cell($value, $cell, $index);
else if($key === 'className') $this->config[$key][$index] .= ' '.$value;
else $this->config[$key][$index] = $value;
}
}
}
public function align($cells, $align = 'left') {
$this->config($cells, 'align', $this->align_cell($align));
}
public function classes($cells, $className = null) {
$this->config($cells, 'className', $className);
}
public function strong($cells) {
$this->config($cells, 'className', '__zu_strong');
}
public function as_icon($cells) {
$this->config($cells, 'className', '__zu_icon');
}
public function fit_content($cells) {
$this->config($cells, 'className', '__zu_fitcontent');
}
public function minmax($cells, $min, $max = '1fr') {
$this->config($cells, 'className', "__zu_minmax($min,$max)");
}
public function fix_width($cells, $styles = null) {
$this->config($cells, 'className', '__zu_fixwidth');
if(is_string($cells)) $cells = [$cells];
if(is_string($styles)) $styles = [$styles];
foreach($cells as $key => $cell) {
$index = $this->cell_index($cell);
$width = $styles[$key] ?? null;
if($index !== false && $width) {
$this->style_cell(['width' => $width], $cell, $index);
}
}
}
public function style($cells, $style = []) {
$this->config($cells, 'style', $style);
}
public function cell($name, $content, $style = null, $align = null, $params = null) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$content,
$style,
$align,
$params
);
}
}
public function cell_with_params($name, $content, $params) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$content,
null,
null,
$params
);
}
}
public function icon_cell($name, $dashicon, $svg = null, $tooltip = null, $style = null) {
if($this->has($name)) {
$icon = [];
if(is_array($dashicon)) {
$style = $dashicon['style'] ?? null;
$tooltip = $dashicon['tooltip'] ?? null;
$svg = $dashicon['svg'] ?? null;
$dashicon = $dashicon['dashicon'] ?? null;
}
if($tooltip) $icon['tooltip'] = $tooltip;
if($dashicon) $icon['dashicon'] = $dashicon;
if($svg) $icon['svg'] = $svg;
$this->row[$name] = $this->generate_cell(
$icon,
$style
);
}
}
public function markdown_cell($name, $content, $align = null, $style = null) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$content,
$style,
$align,
'markdown'
);
}
}
public function link_cell($name, $href, $title = '', $align = null, $style = null) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$title,
$style,
$align,
['link' => [$href, $title]]
);
}
}
public function dynamic_cell($name, $params = [], $defaultContent = null, $className = null) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$defaultContent,
null,
null,
[
'dynamic' => array_merge($params, array_key_exists('id', $params ?? []) ? [] : ['id' => $name]),
'className' => $className,
]
);
}
}
public function cell_with_class($name, $content, $className) {
if($this->has($name)) {
$this->row[$name] = $this->generate_cell(
$content,
null,
null,
['className' => $className]
);
}
}
public function next_row() {
$nextrow = [];
foreach($this->cells as $cell => $val) {
$nextrow[] = isset($this->row[$cell]) ? $this->row[$cell] : $this->empty_cell();
}
$this->rows[] = $nextrow;
$this->row = [];
}
public function get($with_headers = true) {
$this->update_grid_template();
return array_filter([
'config' => $this->config,
'headers' => $with_headers ? array_values($this->cells) : null,
'rows' => $this->rows,
]);
}
private function update_grid_template() {
$grid = [];
foreach($this->config['className'] as $index => $className) {
$column = strpos($className, '__zu_icon') !== false ? 'minmax(auto, 80px)' : '1fr';
if(strpos($className, '__zu_fixwidth') !== false) {
$column = $this->config['style'][$index]['width'] ?? '1fr';
unset($this->config['style'][$index]['width']);
}
if(strpos($className, '__zu_fitcontent') !== false) {
$column = 'max-content';
$className = str_replace('__zu_fitcontent', '', $className);
$this->config['className'][$index] = $className;
}
if(strpos($className, '__zu_minmax') !== false) {
$regex = '/__zu_(minmax[^)]+\))/';
$column = preg_match($regex, $className, $matches) ? $matches[1] : '1fr';
$className = trim(preg_replace($regex, '', $className));
$this->config['className'][$index] = $className;
}
$this->config['className'][$index] = preg_replace('/\s+/', ' ', $className);
$grid[] = $column;
}
$this->config['template'] = implode(' ', $grid);
}
}