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