-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Checkbox.php
149 lines (122 loc) · 2.98 KB
/
Checkbox.php
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
<?php
namespace Encore\Admin\Form\Field;
use Illuminate\Contracts\Support\Arrayable;
class Checkbox extends MultipleSelect
{
protected $inline = true;
protected $canCheckAll = false;
protected $groups = null;
protected static $css = [
'/vendor/laravel-admin/AdminLTE/plugins/iCheck/all.css',
];
protected static $js = [
'/vendor/laravel-admin/AdminLTE/plugins/iCheck/icheck.min.js',
];
/**
* @var string
*/
protected $cascadeEvent = 'ifChanged';
/**
* Set options.
*
* @param array|callable|string $options
*
* @return $this|mixed
*/
public function options($options = [])
{
if ($options instanceof Arrayable) {
$options = $options->toArray();
}
if (is_callable($options)) {
$this->options = $options;
} else {
$this->options = (array) $options;
}
return $this;
}
/**
* Add a checkbox above this component, so you can select all checkboxes by click on it.
*
* @return $this
*/
public function canCheckAll()
{
$this->canCheckAll = true;
return $this;
}
/**
* Set chekbox groups.
*
* @param array
*
* @return $this
*/
public function groups(array $groups = [])
{
$this->groups = $groups;
return $this;
}
/**
* Set checked.
*
* @param array|callable|string $checked
*
* @return $this
*/
public function checked($checked = [])
{
if ($checked instanceof Arrayable) {
$checked = $checked->toArray();
}
$this->checked = (array) $checked;
return $this;
}
/**
* Draw inline checkboxes.
*
* @return $this
*/
public function inline()
{
$this->inline = true;
return $this;
}
/**
* Draw stacked checkboxes.
*
* @return $this
*/
public function stacked()
{
$this->inline = false;
return $this;
}
/**
* {@inheritdoc}
*/
public function render()
{
$this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});";
$this->addVariables([
'checked' => $this->checked,
'inline' => $this->inline,
'canCheckAll' => $this->canCheckAll,
'groups' => $this->groups,
]);
if ($this->canCheckAll) {
$checkAllClass = uniqid('check-all-');
$this->script .= <<<SCRIPT
$('.{$checkAllClass}').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
if (this.checked) {
$('{$this->getElementClassSelector()}').iCheck('check');
} else {
$('{$this->getElementClassSelector()}').iCheck('uncheck');
}
});
SCRIPT;
$this->addVariables(['checkAllClass' => $checkAllClass]);
}
return parent::render();
}
}