Completed
Push — master ( 0d3c07...26769c )
by Song
04:07
created

Checkbox::hasCheckAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
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 $hasCheckAll = 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
     * @return $this
45
     */
46
    public function hasCheckAll()
47
    {
48
        $this->hasCheckAll = true;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Set checked.
55
     *
56
     * @param array|callable|string $checked
57
     *
58
     * @return $this
59
     */
60 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...
61
    {
62
        if ($checked instanceof Arrayable) {
63
            $checked = $checked->toArray();
64
        }
65
66
        $this->checked = (array)$checked;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Draw inline checkboxes.
73
     *
74
     * @return $this
75
     */
76
    public function inline()
77
    {
78
        $this->inline = true;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Draw stacked checkboxes.
85
     *
86
     * @return $this
87
     */
88
    public function stacked()
89
    {
90
        $this->inline = false;
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function render()
99
    {
100
        $this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});";
101
102
        $this->addVariables([
103
            'checked'     => $this->checked,
104
            'inline'      => $this->inline,
105
            'hasCheckAll' => $this->hasCheckAll,
106
        ]);
107
108
        if ($this->hasCheckAll) {
109
110
            $checkAllClass = uniqid('check-all-');
111
112
            $this->script .= <<<SCRIPT
113
$('.{$checkAllClass}').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
114
    if (this.checked) {
115
        $('{$this->getElementClassSelector()}').iCheck('check');
116
    } else {
117
        $('{$this->getElementClassSelector()}').iCheck('uncheck');
118
    }
119
})
120
SCRIPT;
121
            $this->addVariables(['checkAllClass' => $checkAllClass]);
122
        }
123
124
        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 124 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
125
    }
126
}
127