Completed
Push — master ( 26769c...3cc478 )
by Song
02:21
created

Checkbox::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
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
     * Set options.
23
     *
24
     * @param array|callable|string $options
25
     *
26
     * @return $this|mixed
27
     */
28
    public function options($options = [])
29
    {
30
        if ($options instanceof Arrayable) {
31
            $options = $options->toArray();
32
        }
33
34
        if (is_callable($options)) {
35
            $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type callable is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        } else {
37
            $this->options = (array)$options;
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * Add a checkbox above this component, so you can select all checkboxes by click on it.
45
     *
46
     * @return $this
47
     */
48
    public function canCheckAll()
49
    {
50
        $this->canCheckAll = true;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set checked.
57
     *
58
     * @param array|callable|string $checked
59
     *
60
     * @return $this
61
     */
62 View Code Duplication
    public function checked($checked = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        if ($checked instanceof Arrayable) {
65
            $checked = $checked->toArray();
66
        }
67
68
        $this->checked = (array)$checked;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Draw inline checkboxes.
75
     *
76
     * @return $this
77
     */
78
    public function inline()
79
    {
80
        $this->inline = true;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Draw stacked checkboxes.
87
     *
88
     * @return $this
89
     */
90
    public function stacked()
91
    {
92
        $this->inline = false;
93
94
        return $this;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function render()
101
    {
102
        $this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});";
103
104
        $this->addVariables([
105
            'checked'     => $this->checked,
106
            'inline'      => $this->inline,
107
            'canCheckAll' => $this->canCheckAll,
108
        ]);
109
110
        if ($this->canCheckAll) {
111
112
            $checkAllClass = uniqid('check-all-');
113
114
            $this->script .= <<<SCRIPT
115
$('.{$checkAllClass}').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
116
    if (this.checked) {
117
        $('{$this->getElementClassSelector()}').iCheck('check');
118
    } else {
119
        $('{$this->getElementClassSelector()}').iCheck('uncheck');
120
    }
121
})
122
SCRIPT;
123
            $this->addVariables(['checkAllClass' => $checkAllClass]);
124
        }
125
126
        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 126 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
127
    }
128
}
129