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

src/Form/Field/Listbox.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
/**
6
 * Class ListBox.
7
 *
8
 * @see https://github.com/istvan-ujjmeszaros/bootstrap-duallistbox
9
 */
10
class Listbox extends MultipleSelect
11
{
12
    protected $settings = [];
13
14
    protected static $css = [
15
        '/vendor/laravel-admin/bootstrap-duallistbox/dist/bootstrap-duallistbox.min.css',
16
    ];
17
18
    protected static $js = [
19
        '/vendor/laravel-admin/bootstrap-duallistbox/dist/jquery.bootstrap-duallistbox.min.js',
20
    ];
21
22
    public function settings(array $settings)
23
    {
24
        $this->settings = $settings;
25
26
        return $this;
27
    }
28
29
    /**
30
     * Set listbox height.
31
     *
32
     * @param int $height
33
     *
34
     * @return Listbox
35
     */
36
    public function height($height = 200)
37
    {
38
        return $this->settings(['selectorMinimalHeight' => $height]);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function loadRemoteOptions($url, $parameters = [], $options = [])
45
    {
46
        $ajaxOptions = json_encode(array_merge([
47
            'url' => $url.'?'.http_build_query($parameters),
48
        ], $options));
49
50
        $this->script = <<<EOT
51
        
52
$.ajax($ajaxOptions).done(function(data) {
53
54
  var listbox = $("{$this->getElementClassSelector()}");
55
56
    var value = listbox.data('value') + '';
57
    
58
    if (value) {
59
      value = value.split(',');
60
    }
61
    
62
    for (var key in data) {
63
        var selected =  ($.inArray(key, value) >= 0) ? 'selected' : '';
64
        listbox.append('<option value="'+key+'" '+selected+'>'+data[key]+'</option>');
65
    }
66
    
67
    listbox.bootstrapDualListbox('refresh', true);
68
});
69
EOT;
70
71
        return $this;
72
    }
73
74
    public function render()
75
    {
76
        $settings = array_merge([
77
            'infoText'              => trans('admin.listbox.text_total'),
78
            'infoTextEmpty'         => trans('admin.listbox.text_empty'),
79
            'infoTextFiltered'      => trans('admin.listbox.filtered'),
80
            'filterTextClear'       => trans('admin.listbox.filter_clear'),
81
            'filterPlaceHolder'     => trans('admin.listbox.filter_placeholder'),
82
            'selectorMinimalHeight' => 200,
83
        ], $this->settings);
84
85
        $settings = json_encode($settings);
86
87
        $this->script .= <<<SCRIPT
88
89
$("{$this->getElementClassSelector()}").bootstrapDualListbox($settings);
90
91
SCRIPT;
92
93
        $this->attribute('data-value', implode(',', (array) $this->value()));
94
95
        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 95 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
96
    }
97
}
98