-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionColumn.php
More file actions
213 lines (186 loc) · 5.14 KB
/
Copy pathActionColumn.php
File metadata and controls
213 lines (186 loc) · 5.14 KB
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
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* Italix DataSets - ActionColumn
*
* @package Italix\DataSets
* @license MPL-2.0
*/
declare(strict_types=1);
namespace Italix\DataSets;
/**
* Defines a column of per-row action buttons (edit, delete, view, etc.).
*
* The action column is appended to the table as the last column (or first,
* if configured). Each button triggers a named JS callback with the row data.
*
* Example:
*
* $actions = new ActionColumn();
* $actions->button('edit', 'Edit')
* ->css_class('btn btn-sm btn-primary');
* $actions->button('delete', 'Delete')
* ->css_class('btn btn-sm btn-danger')
* ->confirm('Are you sure?');
* $actions->label('Actions')
* ->width('120px')
* ->position('end');
*/
class ActionColumn
{
/** @var ActionButton[] */
private $buttons = [];
/** @var string Column header label */
private $label = 'Actions';
/** @var string|null Column width */
private $width = null;
/** @var string Position: 'start' or 'end' */
private $position = 'end';
/** @var bool Whether the column is frozen (sticky) */
private $frozen = false;
/** @var string|null CSS class for the action cell */
private $css_class = null;
// =========================================================================
// Button Management
// =========================================================================
/**
* Add an action button.
*
* Returns the ActionButton so you can configure it fluently.
*
* @param string $name Unique action name (used as JS callback key)
* @param string $label Display label
* @return ActionButton
*/
public function button(string $name, string $label): ActionButton
{
$btn = new ActionButton($name, $label);
$this->buttons[] = $btn;
return $btn;
}
/**
* Get all registered buttons.
*
* @return ActionButton[]
*/
public function get_buttons(): array
{
return $this->buttons;
}
// =========================================================================
// Fluent Setters
// =========================================================================
/**
* Set the column header label.
*
* @param string $label
* @return self
*/
public function label(string $label): self
{
$this->label = $label;
return $this;
}
/**
* Set the column width.
*
* @param string $width CSS value, e.g. '120px'
* @return self
*/
public function width(string $width): self
{
$this->width = $width;
return $this;
}
/**
* Set the column position: 'start' (first column) or 'end' (last column).
*
* @param string $position
* @return self
*/
public function position(string $position): self
{
$this->position = $position;
return $this;
}
/**
* Set whether the column is frozen (sticky on scroll).
*
* @param bool $frozen
* @return self
*/
public function frozen(bool $frozen = true): self
{
$this->frozen = $frozen;
return $this;
}
/**
* Set CSS class for the action cell.
*
* @param string $css_class
* @return self
*/
public function css_class(string $css_class): self
{
$this->css_class = $css_class;
return $this;
}
// =========================================================================
// Getters
// =========================================================================
/** @return string */
public function get_label(): string
{
return $this->label;
}
/** @return string|null */
public function get_width(): ?string
{
return $this->width;
}
/** @return string */
public function get_position(): string
{
return $this->position;
}
/** @return bool */
public function is_frozen(): bool
{
return $this->frozen;
}
/** @return string|null */
public function get_css_class(): ?string
{
return $this->css_class;
}
// =========================================================================
// Export
// =========================================================================
/**
* @return array
*/
public function to_array(): array
{
$result = [
'label' => $this->label,
'position' => $this->position,
'buttons' => array_map(function (ActionButton $btn) {
return $btn->to_array();
}, $this->buttons),
];
if ($this->width !== null) {
$result['width'] = $this->width;
}
if ($this->frozen) {
$result['frozen'] = true;
}
if ($this->css_class !== null) {
$result['css_class'] = $this->css_class;
}
return $result;
}
}