|
1
|
|
|
<?php namespace Arcanesoft\Core\Helpers\UI; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Button |
|
5
|
|
|
* |
|
6
|
|
|
* @package Arcanesoft\Core\Helpers\UI |
|
7
|
|
|
* @author ARCANEDEV <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class Button extends AbstractClickable |
|
10
|
|
|
{ |
|
11
|
|
|
/* ----------------------------------------------------------------- |
|
12
|
|
|
| Properties |
|
13
|
|
|
| ----------------------------------------------------------------- |
|
14
|
|
|
*/ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $type; |
|
17
|
|
|
|
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Constructor |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
/** |
|
23
|
|
|
* Button constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $action |
|
26
|
|
|
* @param string $type |
|
27
|
|
|
* @param array $attributes |
|
28
|
|
|
* @param bool $disabled |
|
29
|
|
|
*/ |
|
30
|
54 |
|
public function __construct($action, $type = 'button', array $attributes = [], $disabled) |
|
31
|
|
|
{ |
|
32
|
54 |
|
$this->setAction($action); |
|
33
|
54 |
|
$this->setType($type); |
|
34
|
54 |
|
$this->setAttributes($attributes); |
|
35
|
54 |
|
$this->setDisabled($disabled); |
|
36
|
54 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/* ----------------------------------------------------------------- |
|
39
|
|
|
| Getters & Setters |
|
40
|
|
|
| ----------------------------------------------------------------- |
|
41
|
|
|
*/ |
|
42
|
|
|
/** |
|
43
|
|
|
* Set the button type. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $type |
|
46
|
|
|
* |
|
47
|
|
|
* @return self |
|
48
|
|
|
*/ |
|
49
|
54 |
|
public function setType($type) |
|
50
|
|
|
{ |
|
51
|
54 |
|
$this->type = in_array($type = strtolower($type), ['button', 'submit', 'reset']) ? $type : 'button'; |
|
52
|
|
|
|
|
53
|
54 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
|
57
|
|
|
| Main Methods |
|
58
|
|
|
| ----------------------------------------------------------------- |
|
59
|
|
|
*/ |
|
60
|
|
|
/** |
|
61
|
|
|
* Make a button instance. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $action |
|
64
|
|
|
* @param string $type |
|
65
|
|
|
* @param array $attributes |
|
66
|
|
|
* @param bool $disabled |
|
67
|
|
|
* |
|
68
|
|
|
* @return self |
|
69
|
|
|
*/ |
|
70
|
54 |
|
public static function make($action, $type = 'button', array $attributes = [], $disabled = false) |
|
71
|
|
|
{ |
|
72
|
54 |
|
return new static($action, $type, $attributes, $disabled); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get content as a string of HTML. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
54 |
|
public function toHtml() |
|
81
|
|
|
{ |
|
82
|
54 |
|
return '<button'.$this->renderAttributes().'>'.$this->renderValue().'</button>'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/* ----------------------------------------------------------------- |
|
86
|
|
|
| Other Methods |
|
87
|
|
|
| ----------------------------------------------------------------- |
|
88
|
|
|
*/ |
|
89
|
|
|
/** |
|
90
|
|
|
* Render the attributes. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
54 |
|
protected function renderAttributes() |
|
95
|
|
|
{ |
|
96
|
54 |
|
$attributes = collect(); |
|
97
|
54 |
|
$attributes->put('type', $this->type); |
|
98
|
54 |
|
$attributes->put('class', $this->getStyleClass()); |
|
99
|
|
|
|
|
100
|
54 |
|
if ($this->withTooltip) { |
|
101
|
|
|
// This is for Bootstrap |
|
102
|
6 |
|
$attributes->put('data-toggle', 'tooltip'); |
|
103
|
6 |
|
$attributes->put('data-original-title', $this->getTitle()); |
|
104
|
2 |
|
} |
|
105
|
|
|
|
|
106
|
54 |
|
if ($this->disabled) { |
|
107
|
3 |
|
$attributes->put('type', 'button'); |
|
108
|
3 |
|
$attributes->put('disabled', 'disabled'); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
54 |
|
return html()->attributes($attributes->merge($this->attributes)->toArray()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the value from config. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
54 |
|
protected function getConfig($key) |
|
122
|
|
|
{ |
|
123
|
54 |
|
return config("arcanesoft.core.ui.buttons.{$key}"); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|