1
|
|
|
<?php namespace Arcanesoft\Core\Helpers\UI; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class AbstractClickable |
8
|
|
|
* |
9
|
|
|
* @package Arcanesoft\Core\Helpers\UI |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractClickable implements Htmlable |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $action; |
20
|
|
|
|
21
|
|
|
/** @var \Illuminate\Support\Collection */ |
22
|
|
|
protected $attributes; |
23
|
|
|
|
24
|
|
|
/** @var bool */ |
25
|
|
|
protected $disabled = false; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $size = 'md'; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $withTitle = true; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
protected $withIcon = true; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
protected $withTooltip = false; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
protected $extraClass = []; |
41
|
|
|
|
42
|
|
|
/* ----------------------------------------------------------------- |
43
|
|
|
| Getters & Setters |
44
|
|
|
| ----------------------------------------------------------------- |
45
|
|
|
*/ |
46
|
|
|
/** |
47
|
|
|
* Set the action. |
48
|
|
|
* |
49
|
|
|
* @param string $action |
50
|
|
|
* |
51
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
52
|
|
|
*/ |
53
|
108 |
|
public function setAction($action) |
54
|
|
|
{ |
55
|
108 |
|
$this->action = $action; |
56
|
|
|
|
57
|
108 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the attributes. |
62
|
|
|
* |
63
|
|
|
* @param array $attributes |
64
|
|
|
* |
65
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
66
|
|
|
*/ |
67
|
108 |
|
public function setAttributes(array $attributes) |
68
|
|
|
{ |
69
|
108 |
|
$this->attributes = collect($attributes); |
70
|
|
|
|
71
|
108 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set an attribute. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
* |
80
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
81
|
|
|
*/ |
82
|
12 |
|
public function setAttribute($key, $value) |
83
|
|
|
{ |
84
|
12 |
|
$this->attributes->put($key, $value); |
85
|
|
|
|
86
|
12 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Append a style class. |
91
|
|
|
* |
92
|
|
|
* @param string $class |
93
|
|
|
* |
94
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
95
|
|
|
*/ |
96
|
12 |
|
public function appendClass($class) |
97
|
|
|
{ |
98
|
12 |
|
$this->extraClass = array_merge($this->extraClass, explode(' ', $class)); |
99
|
|
|
|
100
|
12 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set disabled state. |
105
|
|
|
* |
106
|
|
|
* @param bool $disabled |
107
|
|
|
* |
108
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
109
|
|
|
*/ |
110
|
108 |
|
public function setDisabled($disabled) |
111
|
|
|
{ |
112
|
108 |
|
$this->disabled = (bool) $disabled; |
113
|
|
|
|
114
|
108 |
|
if ($this->disabled) { |
115
|
6 |
|
$this->attributes = $this->attributes->reject(function ($value, $key) { |
116
|
|
|
return Str::startsWith($key, ['data-']); |
117
|
6 |
|
}); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
108 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Forget attribute(s). |
125
|
|
|
* |
126
|
|
|
* @param string|array $attributes |
127
|
|
|
* |
128
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
129
|
|
|
*/ |
130
|
12 |
|
public function forgetAttribute($attributes) |
131
|
|
|
{ |
132
|
12 |
|
$this->attributes->forget($attributes); |
133
|
|
|
|
134
|
12 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the size. |
139
|
|
|
* |
140
|
|
|
* @param string $size |
141
|
|
|
* |
142
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
143
|
|
|
*/ |
144
|
12 |
|
public function size($size) |
145
|
|
|
{ |
146
|
12 |
|
$this->size = $size; |
147
|
|
|
|
148
|
12 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Show/Hide the title. |
153
|
|
|
* |
154
|
|
|
* @param bool $withTitle |
155
|
|
|
* |
156
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
157
|
|
|
*/ |
158
|
6 |
|
public function withTitle($withTitle) |
159
|
|
|
{ |
160
|
6 |
|
$this->withTitle = $withTitle; |
161
|
|
|
|
162
|
6 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Show/Hide the icon. |
167
|
|
|
* |
168
|
|
|
* @var bool $withIcon |
169
|
|
|
* |
170
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
171
|
|
|
*/ |
172
|
18 |
|
public function withIcon($withIcon) |
173
|
|
|
{ |
174
|
18 |
|
$this->withIcon = (bool) $withIcon; |
175
|
|
|
|
176
|
18 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Enable/Disable the tooltip. |
181
|
|
|
* |
182
|
|
|
* @param bool $withTooltip |
183
|
|
|
* |
184
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
185
|
|
|
*/ |
186
|
12 |
|
public function withTooltip($withTooltip) |
187
|
|
|
{ |
188
|
12 |
|
$this->withTooltip = (bool) $withTooltip; |
189
|
|
|
|
190
|
12 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Show only the icon and the title as tooltip. |
195
|
|
|
* |
196
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
197
|
|
|
*/ |
198
|
6 |
|
public function onlyIcon() |
199
|
|
|
{ |
200
|
6 |
|
return $this->withIcon(true)->withTooltip(true); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Add loading text attribute. |
205
|
|
|
* |
206
|
|
|
* @return \Arcanesoft\Core\Helpers\UI\AbstractClickable |
207
|
|
|
*/ |
208
|
6 |
|
public function withLoadingText() |
209
|
|
|
{ |
210
|
6 |
|
return $this->setAttribute('data-loading-text', Str::ucfirst(trans('core::generals.loading'))); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/* ----------------------------------------------------------------- |
214
|
|
|
| Main Methods |
215
|
|
|
| ----------------------------------------------------------------- |
216
|
|
|
*/ |
217
|
|
|
/** |
218
|
|
|
* Get the string content for the link instance. |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
6 |
|
public function __toString() |
223
|
|
|
{ |
224
|
6 |
|
return $this->toHtml(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/* ----------------------------------------------------------------- |
228
|
|
|
| Other Methods |
229
|
|
|
| ----------------------------------------------------------------- |
230
|
|
|
*/ |
231
|
|
|
/** |
232
|
|
|
* Render the value. |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
108 |
|
protected function renderValue() |
237
|
|
|
{ |
238
|
108 |
|
if ($this->withTooltip || ! $this->withTitle) |
239
|
48 |
|
return $this->renderIcon(); |
240
|
|
|
|
241
|
90 |
|
return $this->withIcon |
242
|
82 |
|
? $this->renderIcon().' '.$this->getTitle() |
243
|
90 |
|
: $this->getTitle(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Render the icon. |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
96 |
|
protected function renderIcon() |
252
|
|
|
{ |
253
|
96 |
|
return '<i class="'.$this->getIcon().'"></i>'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get the action title. |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
102 |
|
protected function getTitle() |
262
|
|
|
{ |
263
|
102 |
|
return Str::ucfirst(trans("core::actions.{$this->action}")); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get the icon. |
268
|
|
|
* |
269
|
|
|
* @return string|null |
270
|
|
|
*/ |
271
|
96 |
|
protected function getIcon() |
272
|
|
|
{ |
273
|
96 |
|
return $this->getConfig("icons.{$this->action}"); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get the button class. |
278
|
|
|
* |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
108 |
|
protected function getStyleClass() |
282
|
|
|
{ |
283
|
108 |
|
$classes = array_merge([ |
284
|
108 |
|
$this->getBaseStyleClass(), |
285
|
108 |
|
$this->getSize(), |
286
|
108 |
|
$this->getColor(), |
287
|
108 |
|
], $this->extraClass); |
288
|
|
|
|
289
|
108 |
|
return implode(' ', array_filter(array_unique($classes, SORT_STRING))); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get the base style class. |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
108 |
|
protected function getBaseStyleClass() |
298
|
|
|
{ |
299
|
108 |
|
return 'btn'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get the link size. |
304
|
|
|
* |
305
|
|
|
* @return string|null |
306
|
|
|
*/ |
307
|
108 |
|
protected function getSize() |
308
|
|
|
{ |
309
|
108 |
|
return $this->getConfig("sizes.{$this->size}"); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get the link color. |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
108 |
|
protected function getColor() |
318
|
|
|
{ |
319
|
108 |
|
$state = $this->disabled ? 'default' : $this->action; |
320
|
|
|
|
321
|
108 |
|
return $this->getConfig("colors.{$state}"); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the value from config. |
326
|
|
|
* |
327
|
|
|
* @param string $key |
328
|
|
|
* |
329
|
|
|
* @return mixed |
330
|
|
|
*/ |
331
|
|
|
abstract protected function getConfig($key); |
332
|
|
|
} |
333
|
|
|
|