Completed
Push — master ( 82be35...1537c0 )
by Song
02:28
created

src/Form/Field/Checkbox.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class Checkbox extends MultipleSelect
8
{
9
    protected $inline = true;
10
11
    protected $canCheckAll = false;
12
13
    protected static $css = [
14
        '/vendor/laravel-admin/AdminLTE/plugins/iCheck/all.css',
15
    ];
16
17
    protected static $js = [
18
        '/vendor/laravel-admin/AdminLTE/plugins/iCheck/icheck.min.js',
19
    ];
20
21
    /**
22
     * @var string
23
     */
24
    protected $cascadeEvent = 'ifChanged';
25
26
    /**
27
     * Set options.
28
     *
29
     * @param array|callable|string $options
30
     *
31
     * @return $this|mixed
32
     */
33
    public function options($options = [])
34
    {
35
        if ($options instanceof Arrayable) {
36
            $options = $options->toArray();
37
        }
38
39
        if (is_callable($options)) {
40
            $this->options = $options;
41
        } else {
42
            $this->options = (array) $options;
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Add a checkbox above this component, so you can select all checkboxes by click on it.
50
     *
51
     * @return $this
52
     */
53
    public function canCheckAll()
54
    {
55
        $this->canCheckAll = true;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Set checked.
62
     *
63
     * @param array|callable|string $checked
64
     *
65
     * @return $this
66
     */
67 View Code Duplication
    public function checked($checked = [])
68
    {
69
        if ($checked instanceof Arrayable) {
70
            $checked = $checked->toArray();
71
        }
72
73
        $this->checked = (array) $checked;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Draw inline checkboxes.
80
     *
81
     * @return $this
82
     */
83
    public function inline()
84
    {
85
        $this->inline = true;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Draw stacked checkboxes.
92
     *
93
     * @return $this
94
     */
95
    public function stacked()
96
    {
97
        $this->inline = false;
98
99
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function render()
106
    {
107
        $this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});";
108
109
        $this->addVariables([
110
            'checked'     => $this->checked,
111
            'inline'      => $this->inline,
112
            'canCheckAll' => $this->canCheckAll,
113
        ]);
114
115
        if ($this->canCheckAll) {
116
            $checkAllClass = uniqid('check-all-');
117
118
            $this->script .= <<<SCRIPT
119
$('.{$checkAllClass}').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
120
    if (this.checked) {
121
        $('{$this->getElementClassSelector()}').iCheck('check');
122
    } else {
123
        $('{$this->getElementClassSelector()}').iCheck('uncheck');
124
    }
125
})
126
SCRIPT;
127
            $this->addVariables(['checkAllClass' => $checkAllClass]);
128
        }
129
130
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type string|Illuminate\View\V...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 130 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
131
    }
132
}
133