-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionButton.php
More file actions
200 lines (172 loc) · 4.6 KB
/
Copy pathActionButton.php
File metadata and controls
200 lines (172 loc) · 4.6 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
<?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 - ActionButton
*
* @package Italix\DataSets
* @license MPL-2.0
*/
declare(strict_types=1);
namespace Italix\DataSets;
/**
* A button rendered inside an action column or toolbar.
*
* Each button has a name (used as the JS callback identifier), a label
* (displayed text), and optional styling/icon configuration.
*
* Example:
*
* $btn = new ActionButton('edit', 'Edit');
* $btn->css_class('btn btn-sm btn-primary')
* ->icon('fa fa-pencil')
* ->title('Edit this row');
*/
class ActionButton
{
/** @var string Unique action name (used as JS callback key) */
private $name;
/** @var string Display label */
private $label;
/** @var string|null CSS class for the button */
private $css_class = null;
/** @var string|null Icon class (e.g. 'fa fa-pencil') */
private $icon = null;
/** @var string|null Tooltip / title attribute */
private $title = null;
/** @var string|null Confirmation message (shown before executing) */
private $confirm = null;
/** @var array Extra attributes */
private $extra = [];
/**
* @param string $name Unique action name
* @param string $label Display label
*/
public function __construct(string $name, string $label)
{
$this->name = $name;
$this->label = $label;
}
// =========================================================================
// Fluent Setters
// =========================================================================
/**
* @param string $css_class
* @return self
*/
public function css_class(string $css_class): self
{
$this->css_class = $css_class;
return $this;
}
/**
* @param string $icon Icon class, e.g. 'fa fa-pencil'
* @return self
*/
public function icon(string $icon): self
{
$this->icon = $icon;
return $this;
}
/**
* @param string $title Tooltip text
* @return self
*/
public function title(string $title): self
{
$this->title = $title;
return $this;
}
/**
* Set a confirmation message shown before the action fires.
*
* @param string $message
* @return self
*/
public function confirm(string $message): self
{
$this->confirm = $message;
return $this;
}
/**
* Set extra options (driver-specific).
*
* @param array $options
* @return self
*/
public function extra(array $options): self
{
$this->extra = array_merge($this->extra, $options);
return $this;
}
// =========================================================================
// Getters
// =========================================================================
/** @return string */
public function get_name(): string
{
return $this->name;
}
/** @return string */
public function get_label(): string
{
return $this->label;
}
/** @return string|null */
public function get_css_class(): ?string
{
return $this->css_class;
}
/** @return string|null */
public function get_icon(): ?string
{
return $this->icon;
}
/** @return string|null */
public function get_title(): ?string
{
return $this->title;
}
/** @return string|null */
public function get_confirm(): ?string
{
return $this->confirm;
}
/** @return array */
public function get_extra(): array
{
return $this->extra;
}
// =========================================================================
// Export
// =========================================================================
/**
* @return array
*/
public function to_array(): array
{
$result = [
'name' => $this->name,
'label' => $this->label,
];
if ($this->css_class !== null) {
$result['css_class'] = $this->css_class;
}
if ($this->icon !== null) {
$result['icon'] = $this->icon;
}
if ($this->title !== null) {
$result['title'] = $this->title;
}
if ($this->confirm !== null) {
$result['confirm'] = $this->confirm;
}
if (!empty($this->extra)) {
$result['extra'] = $this->extra;
}
return $result;
}
}