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 Encore\Admin\Form\Field; |
||
6 | use Illuminate\Support\Arr; |
||
7 | |||
8 | class SwitchField extends Field |
||
9 | { |
||
10 | protected static $css = [ |
||
11 | '/vendor/laravel-admin/bootstrap-switch/dist/css/bootstrap3/bootstrap-switch.min.css', |
||
12 | ]; |
||
13 | |||
14 | protected static $js = [ |
||
15 | '/vendor/laravel-admin/bootstrap-switch/dist/js/bootstrap-switch.min.js', |
||
16 | ]; |
||
17 | |||
18 | protected $states = [ |
||
19 | 'on' => ['value' => 1, 'text' => 'ON', 'color' => 'primary'], |
||
20 | 'off' => ['value' => 0, 'text' => 'OFF', 'color' => 'default'], |
||
21 | ]; |
||
22 | |||
23 | protected $size = 'small'; |
||
24 | |||
25 | public function setSize($size) |
||
26 | { |
||
27 | $this->size = $size; |
||
28 | |||
29 | return $this; |
||
30 | } |
||
31 | |||
32 | public function states($states = []) |
||
33 | { |
||
34 | foreach (Arr::dot($states) as $key => $state) { |
||
35 | Arr::set($this->states, $key, $state); |
||
36 | } |
||
37 | |||
38 | return $this; |
||
39 | } |
||
40 | |||
41 | public function prepare($value) |
||
42 | { |
||
43 | if (isset($this->states[$value])) { |
||
44 | return $this->states[$value]['value']; |
||
45 | } |
||
46 | |||
47 | return $value; |
||
48 | } |
||
49 | |||
50 | public function render() |
||
51 | { |
||
52 | if (!$this->shouldRender()) { |
||
53 | return ''; |
||
54 | } |
||
55 | |||
56 | foreach ($this->states as $state => $option) { |
||
57 | if ($this->value() == $option['value']) { |
||
58 | $this->value = $state; |
||
59 | break; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | $this->script = <<<EOT |
||
64 | |||
65 | $('{$this->getElementClassSelector()}.la_checkbox').bootstrapSwitch({ |
||
66 | size:'{$this->size}', |
||
67 | onText: '{$this->states['on']['text']}', |
||
68 | offText: '{$this->states['off']['text']}', |
||
69 | onColor: '{$this->states['on']['color']}', |
||
70 | offColor: '{$this->states['off']['color']}', |
||
71 | onSwitchChange: function(event, state) { |
||
72 | $(event.target).closest('.bootstrap-switch').next().val(state ? 'on' : 'off').change(); |
||
73 | } |
||
74 | }); |
||
75 | |||
76 | EOT; |
||
77 | |||
78 | return parent::render(); |
||
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
79 | } |
||
80 | } |
||
81 |