Completed
Push — master ( 5cbacf...be3f6f )
by Song
05:42 queued 02:37
created

Listbox::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
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
     * {@inheritdoc}
31
     */
32
    protected function loadRemoteOptions($url, $parameters = [], $options = [])
33
    {
34
        $ajaxOptions = json_encode(array_merge([
35
            'url' => $url.'?'.http_build_query($parameters),
36
        ], $options));
37
38
        $this->script = <<<EOT
39
        
40
$.ajax($ajaxOptions).done(function(data) {
41
42
  var listbox = $("{$this->getElementClassSelector()}");
43
44
    var value = listbox.data('value') + '';
45
    
46
    if (value) {
47
      value = value.split(',');
48
    }
49
    
50
    for (var key in data) {
51
        var selected =  ($.inArray(key, value) >= 0) ? 'selected' : '';
52
        listbox.append('<option value="'+key+'" '+selected+'>'+data[key]+'</option>');
53
    }
54
    
55
    listbox.bootstrapDualListbox('refresh', true);
56
});
57
EOT;
58
59
        return $this;
60
    }
61
62
    public function render()
63
    {
64
        $settings = array_merge($this->settings, [
65
            'infoText'          => trans('admin.listbox.text_total'),
66
            'infoTextEmpty'     => trans('admin.listbox.text_empty'),
67
            'infoTextFiltered'  => trans('admin.listbox.filtered'),
68
            'filterTextClear'   => trans('admin.listbox.filter_clear'),
69
            'filterPlaceHolder' => trans('admin.listbox.filter_placeholder'),
70
        ]);
71
72
        $settings = json_encode($settings);
73
74
        $this->script .= <<<SCRIPT
75
76
$("{$this->getElementClassSelector()}").bootstrapDualListbox($settings);
77
78
SCRIPT;
79
80
        $this->attribute('data-value', implode(',', (array) $this->value()));
81
82
        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 82 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
83
    }
84
}
85