Completed
Push — master ( 7104c9...665657 )
by Arjay
06:12
created

Select2::ajaxUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html\Editor\Fields;
4
5
/**
6
 * @see https://editor.datatables.net/plug-ins/field-type/editor.select2
7
 */
8
class Select2 extends Select
9
{
10
    protected $type = 'select2';
11
12
    /**
13
     * @param bool $state
14
     * @return $this
15
     */
16
    public function allowClear($state = true)
17
    {
18
        $this->attributes['opts']['allowClear'] = $state;
19
20
        return $this;
21
    }
22
23
    /**
24
     * @param string $text
25
     * @param string|null $id
26
     * @return $this
27
     */
28
    public function placeholder($text = '', $id = null)
29
    {
30
        $this->attributes['opts']['placeholder'] = [
31
            'id'   => $id,
32
            'text' => $text,
33
        ];
34
35
        return $this;
36
    }
37
38
    /**
39
     * Set select2 ajax option.
40
     *
41
     * @param mixed $value
42
     * @return $this
43
     */
44 View Code Duplication
    public function ajax($value)
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...
45
    {
46
        if (is_array($value)) {
47
            $this->attributes['opts']['ajax'] = $value;
48
        } else {
49
            $this->attributes['opts']['ajax']['url'] = $value;
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set select2 ajax url option.
57
     *
58
     * @param mixed $value
59
     * @return $this
60
     */
61
    public function ajaxUrl($value)
62
    {
63
        $this->attributes['opts']['ajax']['url'] = $value;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Set select2 ajaxDelay option.
70
     *
71
     * @param mixed $value
72
     * @return $this
73
     */
74
    public function ajaxDelay($value = 250)
75
    {
76
        $this->attributes['opts']['ajax']['delay'] = $value;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Set select2 ajax data option.
83
     *
84
     * @param mixed $data
85
     * @return $this
86
     */
87
    public function ajaxData($data)
88
    {
89
        if (is_array($data)) {
90
            $script = 'function(params) {';
91
            foreach ($data as $key => $value) {
92
                $value  = json_encode($value);
93
                $script .= " params.{$key} = {$value}; ";
94
            }
95
            $script .= 'return params; }';
96
97
            $data = $script;
98
        }
99
100
        $this->attributes['opts']['ajax']['data'] = $data;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set select2 ajax processResults option to process a paginated results.
107
     *
108
     * @param string $display
109
     * @param string $id
110
     * @return $this
111
     */
112
    public function processPaginatedResults($display = 'text', $id = 'id')
113
    {
114
        $script = 'function(data, params) { ';
115
        $script .= 'params.page = params.page || 1; ';
116
        $script .= "data.data.map(function(e) { e.text = e.{$display}; e.id = e.{$id}; return e; }); ";
117
        $script .= 'return { results: data.data, pagination: { more: data.current_page < data.last_page } };';
118
        $script .= '}';
119
120
        return $this->processResults($script);
121
    }
122
123
    /**
124
     * Set select2 ajax processResults option.
125
     *
126
     * @param string $value
127
     * @return $this
128
     */
129
    public function processResults($value)
130
    {
131
        $this->attributes['opts']['ajax']['processResults'] = $value;
132
133
        return $this;
134
    }
135
}
136